OSDN Git Service

Optimize p2p transfer business layer logic (#501)
[bytom/bytom.git] / protocol / tx_test.go
1 package protocol
2
3 import (
4         "fmt"
5         "testing"
6
7         "golang.org/x/crypto/sha3"
8
9         "github.com/bytom/crypto/ed25519"
10         "github.com/bytom/protocol/bc"
11         "github.com/bytom/protocol/bc/types"
12         "github.com/bytom/protocol/vm"
13         "github.com/bytom/protocol/vm/vmutil"
14         "github.com/bytom/testutil"
15 )
16
17 /*func TestBadMaxIssuanceWindow(t *testing.T) {
18         ctx := context.Background()
19         c, b1 := newTestChain(t, time.Now())
20         c.MaxIssuanceWindow = time.Second
21
22         issueTx, _, _ := issue(t, nil, nil, 1)
23
24         got, _, err := c.GenerateBlock(ctx, b1, state.Empty(), time.Now(), []*types.Tx{issueTx})
25         if err != nil {
26                 t.Fatal(err)
27         }
28         if len(got.Transactions) != 0 {
29                 t.Error("expected issuance past max issuance window to be rejected")
30         }
31 }*/
32
33 type testDest struct {
34         privKey ed25519.PrivateKey
35 }
36
37 func newDest(t testing.TB) *testDest {
38         _, priv, err := ed25519.GenerateKey(nil)
39         if err != nil {
40                 testutil.FatalErr(t, err)
41         }
42         return &testDest{
43                 privKey: priv,
44         }
45 }
46
47 func (d *testDest) sign(t testing.TB, tx *types.Tx, index uint32) {
48         txsighash := tx.SigHash(index)
49         prog, _ := vm.Assemble(fmt.Sprintf("0x%x TXSIGHASH EQUAL", txsighash.Bytes()))
50         h := sha3.Sum256(prog)
51         sig := ed25519.Sign(d.privKey, h[:])
52         tx.Inputs[index].SetArguments([][]byte{vm.Int64Bytes(0), sig, prog})
53 }
54
55 func (d testDest) controlProgram() ([]byte, error) {
56         pub := d.privKey.Public().(ed25519.PublicKey)
57         return vmutil.P2SPMultiSigProgram([]ed25519.PublicKey{pub}, 1)
58 }
59
60 type testAsset struct {
61         bc.AssetID
62         testDest
63 }
64
65 func newAsset(t testing.TB) *testAsset {
66         dest := newDest(t)
67         cp, _ := dest.controlProgram()
68         assetID := bc.ComputeAssetID(cp, 1, &bc.EmptyStringHash)
69
70         return &testAsset{
71                 AssetID:  assetID,
72                 testDest: *dest,
73         }
74 }
75
76 func issue(t testing.TB, asset *testAsset, dest *testDest, amount uint64) (*types.Tx, *testAsset, *testDest) {
77         if asset == nil {
78                 asset = newAsset(t)
79         }
80         if dest == nil {
81                 dest = newDest(t)
82         }
83         assetCP, _ := asset.controlProgram()
84         destCP, _ := dest.controlProgram()
85         tx := types.NewTx(types.TxData{
86                 Version: 1,
87                 Inputs: []*types.TxInput{
88                         types.NewIssuanceInput([]byte{1}, amount, assetCP, nil, nil),
89                 },
90                 Outputs: []*types.TxOutput{
91                         types.NewTxOutput(asset.AssetID, amount, destCP),
92                 },
93         })
94         asset.sign(t, tx, 0)
95
96         return tx, asset, dest
97 }