github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/entry_test.go (about) 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 NewIssuance(nil, &AssetAmount{}, 0), 13 m, 14 NewTxHeader(1, 1, 0, nil), 15 NewOriginalOutput(&ValueSource{}, &Program{Code: []byte{1}, VmVersion: 1}, [][]byte{{1}}, 0), 16 NewRetirement(&ValueSource{}, 1), 17 NewSpend(&Hash{}, 0), 18 } 19 20 for _, e := range entries { 21 name := reflect.TypeOf(e).Elem().Name() 22 b.Run(name, func(b *testing.B) { 23 for i := 0; i < b.N; i++ { 24 EntryID(e) 25 } 26 }) 27 } 28 } 29 30 func TestEntryID(t *testing.T) { 31 cases := []struct { 32 entry Entry 33 expectEntryID string 34 }{ 35 { 36 entry: NewIssuance(&Hash{V0: 0, V1: 1, V2: 2, V3: 3}, &AssetAmount{&AssetID{V0: 1, V1: 2, V2: 3, V3: 4}, 100}, 1), 37 expectEntryID: "3012b9b6da3962bb2388cdf5db7f3b93a2b696fcc70e79bc5da1238a6d66ae73", 38 }, 39 { 40 entry: NewMux( 41 []*ValueSource{ 42 { 43 Ref: &Hash{V0: 0, V1: 1, V2: 2, V3: 3}, 44 Value: &AssetAmount{&AssetID{V0: 1, V1: 2, V2: 3, V3: 4}, 100}, 45 Position: 1, 46 }, 47 }, 48 &Program{VmVersion: 1, Code: []byte{1, 2, 3, 4}}, 49 ), 50 expectEntryID: "16c4265a8a90916434c2a904a90132c198c7ebf8512aa1ba4485455b0beff388", 51 }, 52 { 53 entry: NewOriginalOutput( 54 &ValueSource{ 55 Ref: &Hash{V0: 4, V1: 5, V2: 6, V3: 7}, 56 Value: &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10}, 57 Position: 10, 58 }, 59 &Program{VmVersion: 1, Code: []byte{5, 5, 5, 5}}, 60 [][]byte{{3, 4}}, 61 1, 62 ), 63 expectEntryID: "63fbfda2cf0acc573f2a514ddff8ee64c33e713aebe4c85670507545c38841b2", 64 }, 65 { 66 entry: NewRetirement( 67 &ValueSource{ 68 Ref: &Hash{V0: 4, V1: 5, V2: 6, V3: 7}, 69 Value: &AssetAmount{&AssetID{V0: 1, V1: 1, V2: 1, V3: 1}, 10}, 70 Position: 10, 71 }, 72 1, 73 ), 74 expectEntryID: "538c367f7b6e1e9bf205ed0a29def84a1467c477b19812a6934e831c78c4da62", 75 }, 76 { 77 entry: NewSpend(&Hash{V0: 0, V1: 1, V2: 2, V3: 3}, 1), 78 expectEntryID: "2761dbb13967af8944620c134e0f336bbbb26f61eb4ecd154bc034ad6155b9e8", 79 }, 80 { 81 entry: NewTxHeader(1, 100, 1000, []*Hash{&Hash{V0: 4, V1: 5, V2: 6, V3: 7}}), 82 expectEntryID: "ba592aa0841bd4649d9a04309e2e8497ac6f295a847cadd9de6b6f9c2d806663", 83 }, 84 } 85 86 for _, c := range cases { 87 entryID := EntryID(c.entry) 88 if entryID.String() != c.expectEntryID { 89 t.Errorf("the got extry id:%s is not equals to expect entry id:%s", entryID.String(), c.expectEntryID) 90 } 91 } 92 }