(t *testing.T)
| 72 | } |
| 73 | |
| 74 | func TestGenerateBlock(t *testing.T) { |
| 75 | ctx := context.Background() |
| 76 | now := time.Unix(233400000, 0) |
| 77 | c, b1 := newTestChain(t, now) |
| 78 | |
| 79 | txs := []*bc.Tx{ |
| 80 | {ID: bc.NewHash([32]byte{1}), Contracts: []bc.Contract{{Type: bc.OutputType, ID: bc.NewHash([32]byte{2})}}, Finalized: true}, |
| 81 | {ID: bc.NewHash([32]byte{3}), Contracts: []bc.Contract{{Type: bc.OutputType, ID: bc.NewHash([32]byte{4})}}, Finalized: true}, |
| 82 | } |
| 83 | |
| 84 | st := state.Empty() |
| 85 | err := st.ApplyBlock(b1.UnsignedBlock) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | got, _, err := c.GenerateBlock(ctx, bc.Millis(now)+1, bctest.WithCommitments(txs)) |
| 90 | if err != nil { |
| 91 | t.Fatalf("err got = %v want nil", err) |
| 92 | } |
| 93 | |
| 94 | // TODO(bobg): verify these hashes are correct |
| 95 | wantTxRoot := mustDecodeHash("e437b69d1dd70254e165163415e69830b8cbf2eded94b79aa5de911e0691a89f") |
| 96 | wantContractsRoot := mustDecodeHash("5ff56d780f78809e63fb7be7fdbd7bf825704914311b4d0819c50d411f3b662d") |
| 97 | wantNoncesRoot := bc.NewHash(new(patricia.Tree).RootHash()) |
| 98 | |
| 99 | b1ID := b1.Hash() |
| 100 | want := &bc.UnsignedBlock{ |
| 101 | BlockHeader: &bc.BlockHeader{ |
| 102 | Version: 3, |
| 103 | Height: 2, |
| 104 | RefsCount: 1, |
| 105 | PreviousBlockId: &b1ID, |
| 106 | TimestampMs: bc.Millis(now) + 1, |
| 107 | TransactionsRoot: &wantTxRoot, |
| 108 | ContractsRoot: &wantContractsRoot, |
| 109 | NoncesRoot: &wantNoncesRoot, |
| 110 | NextPredicate: b1.NextPredicate, |
| 111 | }, |
| 112 | Transactions: txs, |
| 113 | } |
| 114 | |
| 115 | if !testutil.DeepEqual(got, want) { |
| 116 | t.Errorf("generated block:\ngot: %+v\nwant: %+v", got, want) |
| 117 | } |
| 118 | |
| 119 | _, _, err = c.GenerateBlock(ctx, bc.Millis(now), nil) |
| 120 | if err == nil { |
| 121 | t.Error("expected error for bad generate timestamp") |
| 122 | } |
| 123 | |
| 124 | for i := 0; i < maxBlockTxs+1; i++ { |
| 125 | txs = append(txs, bctest.EmptyTx(t, b1.Hash(), now.Add(time.Minute))) |
| 126 | } |
| 127 | |
| 128 | got, _, err = c.GenerateBlock(ctx, bc.Millis(now)+1, bctest.WithCommitments(txs)) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
nothing calls this directly
no test coverage detected