-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathtx_test.go
More file actions
103 lines (88 loc) · 2.32 KB
/
Copy pathtx_test.go
File metadata and controls
103 lines (88 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package protocol
import (
"context"
"fmt"
"testing"
"time"
"golang.org/x/crypto/sha3"
"chain/crypto/ed25519"
"chain/protocol/bc"
"chain/protocol/bc/legacy"
"chain/protocol/state"
"chain/protocol/vm"
"chain/protocol/vm/vmutil"
"chain/testutil"
)
func TestBadMaxIssuanceWindow(t *testing.T) {
ctx := context.Background()
c, b1 := newTestChain(t, time.Now())
c.MaxIssuanceWindow = time.Second
issueTx, _, _ := issue(t, nil, nil, 1)
got, _, err := c.GenerateBlock(ctx, b1, state.Empty(), time.Now(), []*legacy.Tx{issueTx})
if err != nil {
t.Fatal(err)
}
if len(got.Transactions) != 0 {
t.Error("expected issuance past max issuance window to be rejected")
}
}
type testDest struct {
privKey ed25519.PrivateKey
}
func newDest(t testing.TB) *testDest {
_, priv, err := ed25519.GenerateKey(nil)
if err != nil {
testutil.FatalErr(t, err)
}
return &testDest{
privKey: priv,
}
}
func (d *testDest) sign(t testing.TB, tx *legacy.Tx, index uint32) {
txsighash := tx.SigHash(index)
prog, _ := vm.Assemble(fmt.Sprintf("0x%x TXSIGHASH EQUAL", txsighash.Bytes()))
h := sha3.Sum256(prog)
sig := ed25519.Sign(d.privKey, h[:])
tx.Inputs[index].SetArguments([][]byte{vm.Int64Bytes(0), sig, prog})
}
func (d testDest) controlProgram() ([]byte, error) {
pub := d.privKey.Public().(ed25519.PublicKey)
return vmutil.P2SPMultiSigProgram([]ed25519.PublicKey{pub}, 1)
}
type testAsset struct {
bc.AssetID
testDest
}
func newAsset(t testing.TB) *testAsset {
dest := newDest(t)
cp, _ := dest.controlProgram()
var initialBlockID bc.Hash
assetID := bc.ComputeAssetID(cp, &initialBlockID, 1, &bc.EmptyStringHash)
return &testAsset{
AssetID: assetID,
testDest: *dest,
}
}
func issue(t testing.TB, asset *testAsset, dest *testDest, amount uint64) (*legacy.Tx, *testAsset, *testDest) {
if asset == nil {
asset = newAsset(t)
}
if dest == nil {
dest = newDest(t)
}
assetCP, _ := asset.controlProgram()
destCP, _ := dest.controlProgram()
tx := legacy.NewTx(legacy.TxData{
Version: 1,
Inputs: []*legacy.TxInput{
legacy.NewIssuanceInput([]byte{1}, amount, nil, bc.Hash{}, assetCP, nil, nil),
},
Outputs: []*legacy.TxOutput{
legacy.NewTxOutput(asset.AssetID, amount, destCP, nil),
},
MinTime: bc.Millis(time.Now()),
MaxTime: bc.Millis(time.Now().Add(time.Hour)),
})
asset.sign(t, tx, 0)
return tx, asset, dest
}