OSDN Git Service

feat: add processIssuing (#152)
[bytom/vapor.git] / vendor / github.com / bytom / protocol / bc / asset_test.go
1 package bc
2
3 import (
4         "testing"
5         "encoding/hex"
6
7         "golang.org/x/crypto/sha3"
8 )
9
10 func TestComputeAssetID(t *testing.T) {
11         issuanceScript := []byte{1}
12         assetID := ComputeAssetID(issuanceScript, 1, &EmptyStringHash)
13
14         unhashed := append([]byte{})
15         unhashed = append(unhashed, []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}...) // vmVersion
16         unhashed = append(unhashed, 0x01)                                                      // length of issuanceScript
17         unhashed = append(unhashed, issuanceScript...)
18         unhashed = append(unhashed, EmptyStringHash.Bytes()...)
19
20         if want := NewAssetID(sha3.Sum256(unhashed)); assetID != want {
21                 t.Errorf("asset id = %x want %x", assetID.Bytes(), want.Bytes())
22         }
23 }
24
25 func TestComputeAssetIDReally(t *testing.T) {
26         cases := []struct {
27                 program       string
28                 rawDefinition string
29                 wantAssetID   string
30         }{
31                 {
32                         program: "ae2039294f652632eee970765550c245f0b0314256b4b93aadc86279fdb45db3b70e5151ad",
33                         rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d",
34                         wantAssetID: "07c7ced3f37f48ea39da6971c89f90e9cff3202d54b0a911f12ace8501f3834e",
35                 },
36                 {
37                         program: "ae20620b1755451738b04f42822f4b37186563f824c9c30d485987298918f96395fe5151ad",
38                         rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d6f626f6c223a2022220a7d",
39                         wantAssetID: "0dafd0f0e42f06f3bf9a8cf5787519d3860650f27a2b3393d34e1fe06e89b469",
40                 },
41                 {
42                         program: "ae20db11f9dfa39c9e66421c530fe027218edd3d5b1cd98f24c826f4d9c0cd131a475151ad",
43                         rawDefinition: "7b0a202022646563696d616c73223a20382c0a2020226465736372697074696f6e223a207b7d2c0a2020226e616d65223a2022222c0a20202273796d626f6c223a2022220a7d",
44                         wantAssetID: "a5bc30d8d0ad051e6e352ebc21d79ba798cd8c436e89f4149969c2c562371791",
45                 },
46         }
47
48         for _, c := range cases {
49                 progBytes, err := hex.DecodeString(c.program)
50                 if err != nil {
51                         t.Fatal(err)
52                 }
53
54                 defBytes, err := hex.DecodeString(c.rawDefinition)
55                 if err != nil {
56                         t.Fatal(err)
57                 }
58
59                 defHash := NewHash(sha3.Sum256(defBytes))
60                 assetID := ComputeAssetID(progBytes, 1, &defHash)
61                 if assetID.String() != c.wantAssetID {
62                         t.Errorf("got asset id:%s, want asset id:%s", assetID.String(), c.wantAssetID)
63                 }
64         }
65 }
66
67 var assetIDSink AssetID
68
69 func BenchmarkComputeAssetID(b *testing.B) {
70         var (
71                 issuanceScript = []byte{5}
72         )
73
74         for i := 0; i < b.N; i++ {
75                 assetIDSink = ComputeAssetID(issuanceScript, 1, &EmptyStringHash)
76         }
77 }