OSDN Git Service

34520be873af571d1a9fe71d4d0d487ccf46b342
[bytom/vapor.git] / protocol / bc / entry_test.go
1 package bc
2
3 import (
4         "reflect"
5         "testing"
6 )
7
8 func BenchmarkEntryID(b *testing.B) {
9         m := NewMux([]*ValueSource{{Position: 1}}, &Program{Code: []byte{1}, VmVersion: 1})
10
11         entries := []Entry{
12                 m,
13                 NewTxHeader(1, 1, 0, nil),
14                 NewIntraChainOutput(&ValueSource{}, &Program{Code: []byte{1}, VmVersion: 1}, 0),
15                 NewRetirement(&ValueSource{}, 1),
16                 NewSpend(&Hash{}, 0),
17         }
18
19         for _, e := range entries {
20                 name := reflect.TypeOf(e).Elem().Name()
21                 b.Run(name, func(b *testing.B) {
22                         for i := 0; i < b.N; i++ {
23                                 EntryID(e)
24                         }
25                 })
26         }
27 }
28
29 func TestEntryID(t *testing.T) {
30         cases := []struct {
31                 entry         Entry
32                 expectEntryID string
33         }{
34                 {
35                         entry: NewMux(
36                                 []*ValueSource{
37                                         {
38                                                 Ref:      &Hash{V0: 0, V1: 1, V2: 2, V3: 3},
39                                                 Value:    &AssetAmount{&AssetID{V0: 1, V1: 2, V2: 3, V3: 4}, 100},
40                                                 Position: 1,
41                                         },
42                                 },
43                                 &Program{VmVersion: 1, Code: []byte{1, 2, 3, 4}},
44                         ),
45                         expectEntryID: "16c4265a8a90916434c2a904a90132c198c7ebf8512aa1ba4485455b0beff388",
46                 },
47                 {
48                         entry: NewIntraChainOutput(
49                                 &ValueSource{
50                                         Ref:      &Hash{V0: 4, V1: 5, V2: 6, V3: 7},
51                                         Value:    &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10},
52                                         Position: 10,
53                                 },
54                                 &Program{VmVersion: 1, Code: []byte{5, 5, 5, 5}},
55                                 1,
56                         ),
57                         expectEntryID: "c60faad6ae44b15d54a57b5bd021f6cec0e5f7d2c55f53b90d6231ce5c561e9c",
58                 },
59                 {
60                         entry: NewRetirement(
61                                 &ValueSource{
62                                         Ref:      &Hash{V0: 4, V1: 5, V2: 6, V3: 7},
63                                         Value:    &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10},
64                                         Position: 10,
65                                 },
66                                 1,
67                         ),
68                         expectEntryID: "538c367f7b6e1e9bf205ed0a29def84a1467c477b19812a6934e831c78c4da62",
69                 },
70                 {
71                         entry: NewCrossChainInput(
72                                 &Hash{V0: 0, V1: 1, V2: 2, V3: 3},
73                                 &Program{VmVersion: 1, Code: []byte{5, 5, 5, 5}},
74                                 1,
75                                 &AssetDefinition{
76                                         IssuanceProgram: &Program{VmVersion: 1, Code: []byte{1, 2, 3, 4}},
77                                         Data:            &Hash{V0: 0, V1: 1, V2: 2, V3: 3},
78                                 },
79                         ),
80                         expectEntryID: "14bb3f6e68f37d037b1f1539a21ab41e182b8d59d703a1af6c426d52cfc775d9",
81                 },
82                 {
83                         entry: NewCrossChainOutput(
84                                 &ValueSource{
85                                         Ref:      &Hash{V0: 4, V1: 5, V2: 6, V3: 7},
86                                         Value:    &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10},
87                                         Position: 10,
88                                 },
89                                 &Program{VmVersion: 1, Code: []byte{5, 5, 5, 5}},
90                                 1,
91                         ),
92                         expectEntryID: "8e212555174bb8b725d7023cbe1864408c6a586389875ea0143257c2402b3be9",
93                 },
94                 {
95                         entry: NewVoteOutput(
96                                 &ValueSource{
97                                         Ref:      &Hash{V0: 4, V1: 5, V2: 6, V3: 7},
98                                         Value:    &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10},
99                                         Position: 10,
100                                 },
101                                 &Program{VmVersion: 1, Code: []byte{5, 5, 5, 5}},
102                                 1,
103                                 []byte("vote"),
104                         ),
105                         expectEntryID: "67e722b339e58604e46b5a08b9684ab8b6dcb3e6218954db133c05eb2b76f0e8",
106                 },
107                 {
108                         entry:         NewVetoInput(&Hash{V0: 0, V1: 1, V2: 2, V3: 3}, 1),
109                         expectEntryID: "a4f4909f947977b50bdd978fcd320161b66a266833546b6399f4709b8dd6ad59",
110                 },
111                 {
112                         entry:         NewSpend(&Hash{V0: 0, V1: 1, V2: 2, V3: 3}, 1),
113                         expectEntryID: "2761dbb13967af8944620c134e0f336bbbb26f61eb4ecd154bc034ad6155b9e8",
114                 },
115                 {
116                         entry:         NewTxHeader(1, 100, 1000, []*Hash{&Hash{V0: 4, V1: 5, V2: 6, V3: 7}}),
117                         expectEntryID: "ba592aa0841bd4649d9a04309e2e8497ac6f295a847cadd9de6b6f9c2d806663",
118                 },
119         }
120
121         for _, c := range cases {
122                 entryID := EntryID(c.entry)
123                 if entryID.String() != c.expectEntryID {
124                         t.Errorf("the got extry id:%s is not equals to expect entry id:%s", entryID.String(), c.expectEntryID)
125                 }
126         }
127 }