|
| 1 | +package coreutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "go.sia.tech/core/types" |
| 8 | + "go.sia.tech/coreutils/chain" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMiner(t *testing.T) { |
| 12 | + n, genesisBlock := chain.TestnetZen() |
| 13 | + n.InitialTarget = types.BlockID{0xFF} |
| 14 | + |
| 15 | + sk := types.GeneratePrivateKey() |
| 16 | + genesisBlock.Transactions = []types.Transaction{{ |
| 17 | + SiacoinOutputs: []types.SiacoinOutput{ |
| 18 | + { |
| 19 | + Address: types.StandardUnlockHash(sk.PublicKey()), |
| 20 | + Value: types.Siacoins(10), |
| 21 | + }, |
| 22 | + }, |
| 23 | + }} |
| 24 | + |
| 25 | + store, tipState, err := chain.NewDBStore(chain.NewMemDB(), n, genesisBlock) |
| 26 | + if err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + defer store.Close() |
| 30 | + cm := chain.NewManager(store, tipState) |
| 31 | + |
| 32 | + // create a transaction |
| 33 | + txn := types.Transaction{ |
| 34 | + SiacoinInputs: []types.SiacoinInput{{ |
| 35 | + ParentID: genesisBlock.Transactions[0].SiacoinOutputID(0), |
| 36 | + UnlockConditions: types.StandardUnlockConditions(sk.PublicKey()), |
| 37 | + }}, |
| 38 | + SiacoinOutputs: []types.SiacoinOutput{{ |
| 39 | + Address: types.StandardUnlockHash(sk.PublicKey()), |
| 40 | + Value: types.Siacoins(9), |
| 41 | + }}, |
| 42 | + MinerFees: []types.Currency{types.Siacoins(1)}, |
| 43 | + } |
| 44 | + |
| 45 | + // sign the inputs |
| 46 | + for _, sci := range txn.SiacoinInputs { |
| 47 | + sig := sk.SignHash(cm.TipState().WholeSigHash(txn, types.Hash256(sci.ParentID), 0, 0, nil)) |
| 48 | + txn.Signatures = append(txn.Signatures, types.TransactionSignature{ |
| 49 | + ParentID: types.Hash256(sci.ParentID), |
| 50 | + CoveredFields: types.CoveredFields{WholeTransaction: true}, |
| 51 | + PublicKeyIndex: 0, |
| 52 | + Signature: sig[:], |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + // add the transaction to the pool |
| 57 | + _, err = cm.AddPoolTransactions([]types.Transaction{txn}) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + |
| 62 | + // assert the minerpayout includes the txn fee |
| 63 | + b, found := MineBlock(cm, types.VoidAddress, time.Second) |
| 64 | + if !found { |
| 65 | + t.Fatal("PoW failed") |
| 66 | + } else if len(b.MinerPayouts) != 1 { |
| 67 | + t.Fatal("expected one miner payout") |
| 68 | + } else if b.MinerPayouts[0].Value.Cmp(types.Siacoins(1).Add(cm.TipState().BlockReward())) != 0 { |
| 69 | + t.Fatal("unexpected miner payout", b.MinerPayouts[0].Value.ExactString()) |
| 70 | + } |
| 71 | +} |
0 commit comments