MCPcopy Create free account
hub / github.com/chain/txvm / TestBlock

Function TestBlock

protocol/validation/validation_test.go:13–71  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestBlock(t *testing.T) {
14 cases := []struct {
15 f func(*bc.UnsignedBlock, *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader)
16 wantErr bool
17 }{{
18 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
19 return b1, nil
20 },
21 wantErr: false,
22 }, {
23 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
24 transactionsRoot := bc.NewHash([32]byte{1})
25 b1.TransactionsRoot = &transactionsRoot // make b1 be invalid
26 return b1, nil
27 },
28 wantErr: true,
29 }, {
30 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
31 return b2, b1.BlockHeader
32 },
33 wantErr: false,
34 }, {
35 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
36 transactionsRoot := bc.NewHash([32]byte{1})
37 b2.TransactionsRoot = &transactionsRoot // make b2 be invalid
38 return b2, b1.BlockHeader
39 },
40 wantErr: true,
41 }, {
42 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
43 b2.Version = 2
44 return b2, b1.BlockHeader
45 },
46 wantErr: true,
47 }, {
48 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
49 return b2, nil
50 },
51 wantErr: true,
52 }, {
53 f: func(b1, b2 *bc.UnsignedBlock) (*bc.UnsignedBlock, *bc.BlockHeader) {
54 b2.ExtraFields = append(b2.ExtraFields, &bc.DataItem{Type: bc.DataType_INT})
55 return b2, b1.BlockHeader
56 },
57 wantErr: true,
58 }}
59
60 for _, c := range cases {
61 b1 := newInitialBlock(t)
62 b2 := generate(t, b1)
63 b, bh := c.f(b1, b2)
64 err := Block(b, bh)
65 if (err == nil) && c.wantErr {
66 t.Errorf("Block(%v, %v) = nil, want error", b, bh)
67 } else if (err != nil) && !c.wantErr {
68 t.Errorf("Block(%v, %v) = %v, want nil", b, bh, err)
69 }
70 }

Callers

nothing calls this directly

Calls 4

NewHashFunction · 0.92
newInitialBlockFunction · 0.85
generateFunction · 0.85
BlockFunction · 0.70

Tested by

no test coverage detected