forked from ava-labs/coreth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tx.go
152 lines (126 loc) · 4.12 KB
/
test_tx.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// (c) 2020-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"math/big"
"math/rand"
"github.com/haowang0402/avalanchego/utils"
"github.com/haowang0402/avalanchego/chains/atomic"
"github.com/haowang0402/avalanchego/codec"
"github.com/haowang0402/avalanchego/codec/linearcodec"
"github.com/haowang0402/avalanchego/ids"
"github.com/haowang0402/avalanchego/snow"
"github.com/haowang0402/avalanchego/utils/wrappers"
"github.com/haowang0402/coreth/core/state"
"github.com/haowang0402/coreth/params"
)
type TestTx struct {
GasUsedV uint64 `serialize:"true"`
AcceptRequestsBlockchainIDV ids.ID `serialize:"true"`
AcceptRequestsV *atomic.Requests `serialize:"true"`
VerifyV error
IDV ids.ID `serialize:"true" json:"id"`
BurnedV uint64 `serialize:"true"`
UnsignedBytesV []byte
BytesV []byte
InputUTXOsV ids.Set
SemanticVerifyV error
EVMStateTransferV error
}
var _ UnsignedAtomicTx = &TestTx{}
// GasUsed implements the UnsignedAtomicTx interface
func (t *TestTx) GasUsed(fixedFee bool) (uint64, error) { return t.GasUsedV, nil }
// Verify implements the UnsignedAtomicTx interface
func (t *TestTx) Verify(ctx *snow.Context, rules params.Rules) error { return t.VerifyV }
// AtomicOps implements the UnsignedAtomicTx interface
func (t *TestTx) AtomicOps() (ids.ID, *atomic.Requests, error) {
return t.AcceptRequestsBlockchainIDV, t.AcceptRequestsV, nil
}
// Initialize implements the UnsignedAtomicTx interface
func (t *TestTx) Initialize(unsignedBytes, signedBytes []byte) {}
// ID implements the UnsignedAtomicTx interface
func (t *TestTx) ID() ids.ID { return t.IDV }
// Burned implements the UnsignedAtomicTx interface
func (t *TestTx) Burned(assetID ids.ID) (uint64, error) { return t.BurnedV, nil }
// UnsignedBytes implements the UnsignedAtomicTx interface
func (t *TestTx) UnsignedBytes() []byte { return t.UnsignedBytesV }
// Bytes implements the UnsignedAtomicTx interface
func (t *TestTx) Bytes() []byte { return t.BytesV }
// InputUTXOs implements the UnsignedAtomicTx interface
func (t *TestTx) InputUTXOs() ids.Set { return t.InputUTXOsV }
// SemanticVerify implements the UnsignedAtomicTx interface
func (t *TestTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules params.Rules) error {
return t.SemanticVerifyV
}
// EVMStateTransfer implements the UnsignedAtomicTx interface
func (t *TestTx) EVMStateTransfer(ctx *snow.Context, state *state.StateDB) error {
return t.EVMStateTransferV
}
func testTxCodec() codec.Manager {
codec := codec.NewDefaultManager()
c := linearcodec.NewDefault()
errs := wrappers.Errs{}
errs.Add(
c.RegisterType(&TestTx{}),
c.RegisterType(&atomic.Element{}),
c.RegisterType(&atomic.Requests{}),
codec.RegisterCodec(codecVersion, c),
)
if errs.Errored() {
panic(errs.Err)
}
return codec
}
var blockChainID = ids.GenerateTestID()
func testDataImportTx() *Tx {
return &Tx{
UnsignedAtomicTx: &TestTx{
IDV: ids.GenerateTestID(),
AcceptRequestsBlockchainIDV: blockChainID,
AcceptRequestsV: &atomic.Requests{
RemoveRequests: [][]byte{
utils.RandomBytes(32),
utils.RandomBytes(32),
},
},
},
}
}
func testDataExportTx() *Tx {
return &Tx{
UnsignedAtomicTx: &TestTx{
IDV: ids.GenerateTestID(),
AcceptRequestsBlockchainIDV: blockChainID,
AcceptRequestsV: &atomic.Requests{
PutRequests: []*atomic.Element{
{
Key: utils.RandomBytes(16),
Value: utils.RandomBytes(24),
Traits: [][]byte{
utils.RandomBytes(32),
utils.RandomBytes(32),
},
},
},
},
},
}
}
func newTestTx() *Tx {
txType := rand.Intn(2)
switch txType {
case 0:
return testDataImportTx()
case 1:
return testDataExportTx()
default:
panic("rng generated unexpected value for tx type")
}
}
func newTestTxs(numTxs int) []*Tx {
txs := make([]*Tx, 0, numTxs)
for i := 0; i < numTxs; i++ {
txs = append(txs, newTestTx())
}
return txs
}