OSDN Git Service

Added blockchain struct.
[bytom/bytom.git] / protocol / bc / legacy / tx_test.go
1 package legacy
2
3 import (
4         "testing"
5
6         "chain/protocol/bc"
7
8         "github.com/davecgh/go-spew/spew"
9 )
10
11 func TestTxHashes(t *testing.T) {
12         cases := []struct {
13                 txdata *TxData
14                 hash   bc.Hash
15         }{
16                 {
17                         txdata: &TxData{},
18                         hash:   mustDecodeHash("e367a95b0f1dafdedd86f633456c81ef6bd4f2623f0890d56417f73a18a67297"),
19                 },
20                 {
21                         txdata: sampleTx(),
22                         hash:   mustDecodeHash("9fad4f5024412d99d17508ef3cc66f81f1e09914a71b2641683acca87081c098"), // todo: verify this value,
23                 },
24         }
25
26         for i, c := range cases {
27                 txEntries := MapTx(c.txdata)
28                 if len(txEntries.InputIDs) != len(c.txdata.Inputs) {
29                         t.Errorf("case %d: len(txEntries.InputIDs) = %d, want %d", i, len(txEntries.InputIDs), len(c.txdata.Inputs))
30                 }
31                 if c.hash != txEntries.ID {
32                         t.Errorf("case %d: got txid %x, want %x. txEntries is:\n%s", i, txEntries.ID.Bytes(), c.hash.Bytes(), spew.Sdump(txEntries))
33                 }
34         }
35 }
36
37 func BenchmarkHashEmptyTx(b *testing.B) {
38         tx := &TxData{}
39         for i := 0; i < b.N; i++ {
40                 _ = MapTx(tx)
41         }
42 }
43
44 func BenchmarkHashNonemptyTx(b *testing.B) {
45         tx := sampleTx()
46         for i := 0; i < b.N; i++ {
47                 _ = MapTx(tx)
48         }
49 }
50
51 func sampleTx() *TxData {
52         initialBlockHash := mustDecodeHash("03deff1d4319d67baa10a6d26c1fea9c3e8d30e33474efee1a610a9bb49d758d")
53         assetID := bc.ComputeAssetID([]byte{1}, &initialBlockHash, 1, &bc.EmptyStringHash)
54         return &TxData{
55                 Version: 1,
56                 Inputs: []*TxInput{
57                         NewSpendInput(nil, mustDecodeHash("dd385f6fe25d91d8c1bd0fa58951ad56b0c5229dcc01f61d9f9e8b9eb92d3292"), assetID, 1000000000000, 1, []byte{1}, bc.Hash{}, []byte("input")),
58                         NewSpendInput(nil, bc.NewHash([32]byte{0x11}), assetID, 1, 1, []byte{2}, bc.Hash{}, []byte("input2")),
59                 },
60                 Outputs: []*TxOutput{
61                         NewTxOutput(assetID, 600000000000, []byte{1}, nil),
62                         NewTxOutput(assetID, 400000000000, []byte{2}, nil),
63                 },
64                 MinTime:       1492590000,
65                 MaxTime:       1492590591,
66                 ReferenceData: []byte("distribution"),
67         }
68 }