-
Notifications
You must be signed in to change notification settings - Fork 328
/
sealedenvelope_test.go
180 lines (157 loc) · 5.01 KB
/
sealedenvelope_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package action
import (
"encoding/hex"
"math/big"
"testing"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"
"github.com/iotexproject/iotex-core/v2/state"
"github.com/iotexproject/iotex-core/v2/test/identityset"
)
const (
_publicKey = "04403d3c0dbd3270ddfc248c3df1f9aafd60f1d8e7456961c9ef262" +
"92262cc68f0ea9690263bef9e197a38f06026814fc70912c2b98d2e90a68f8ddc5328180a01"
_evmNetworkID uint32 = 4689
)
var (
_signByte = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
_validSig, _ = hex.DecodeString("15e73ad521ec9e06600c59e49b127c9dee114ad64fb2fcbe5e0d9f4c8d2b766e73d708cca1dc050dd27b20f2ee607f30428bf035f45d4da8ec2fb04a90c2c30901")
)
func TestSealedEnvelope_Basic(t *testing.T) {
req := require.New(t)
for _, v := range []struct {
id uint32
hash string
}{
{0, "322884fb04663019be6fb461d9453827487eafdd57b4de3bd89a7d77c9bf8395"},
{1, "80af7840d73772d3022d8bdc46278fb755352e5e9d5f2a1f12ee7ec4f1ea98e9"},
} {
se, err := createSealedEnvelope(v.id)
req.NoError(err)
rHash, err := se.Hash()
req.NoError(err)
req.Equal(v.id, se.ChainID())
req.Equal(v.hash, hex.EncodeToString(rHash[:]))
req.Equal(_publicKey, se.SrcPubkey().HexString())
req.Equal(_signByte, se.Signature())
req.Zero(se.Encoding())
var se1 SealedEnvelope
se.signature = _validSig
req.NoError(se1.loadProto(se.Proto(), _evmNetworkID))
req.Equal(se.Envelope, se1.Envelope)
}
}
func TestSealedEnvelope_InvalidType(t *testing.T) {
require := require.New(t)
candidates := state.CandidateList{}
r := NewPutPollResult(10001, candidates)
elp := (&EnvelopeBuilder{}).SetNonce(1).SetAction(r).
SetGasLimit(100000).Build()
selp := FakeSeal(elp, identityset.PrivateKey(27).PublicKey())
selp.encoding = iotextypes.Encoding_ETHEREUM_EIP155
hash1, err := selp.envelopeHash()
require.Equal(hash1, hash.ZeroHash256)
require.Contains(err.Error(), "invalid action type")
}
func TestSealedEnvelope_Actions(t *testing.T) {
require := require.New(t)
createStake, err := NewCreateStake(_addr2, "100", uint32(10000), true, _payload)
require.NoError(err)
depositToStake, err := NewDepositToStake(2, big.NewInt(10).String(), _payload)
require.NoError(err)
changeCandidate := NewChangeCandidate(_candidate1Name, 2, _payload)
unstake := NewUnstake(2, _payload)
withdrawStake := NewWithdrawStake(2, _payload)
restake := NewRestake(_index, _duration, _autoStake, _payload)
transferStake, err := NewTransferStake(_cand1Addr, 2, _payload)
require.NoError(err)
candidateRegister, err := NewCandidateRegister(_candidate1Name, _cand1Addr, _cand1Addr, _cand1Addr, big.NewInt(10).String(), 91, true, _payload)
require.NoError(err)
candidateUpdate, err := NewCandidateUpdate(_candidate1Name, _cand1Addr, _cand1Addr)
require.NoError(err)
tests := []actionPayload{
createStake,
depositToStake,
changeCandidate,
unstake,
withdrawStake,
restake,
transferStake,
candidateRegister,
candidateUpdate,
}
for _, test := range tests {
bd := &EnvelopeBuilder{}
elp := bd.SetNonce(1).
SetAction(test).
SetGasLimit(100000).Build()
selp := FakeSeal(elp, identityset.PrivateKey(27).PublicKey())
rlp, err := selp.ToEthTx()
require.NoError(err)
require.Equal(elp.Nonce(), rlp.Nonce())
require.Equal(elp.GasPrice(), rlp.GasPrice())
require.Equal(elp.Gas(), rlp.Gas())
}
}
func TestSealedEnvelope_Proto(t *testing.T) {
req := require.New(t)
se, err := createSealedEnvelope(0)
req.NoError(err)
tsf, ok := se.Envelope.Action().(*Transfer)
req.True(ok)
proto := se.Proto()
ac := &iotextypes.Action{
Core: se.Envelope.Proto(),
SenderPubKey: se.srcPubkey.Bytes(),
Signature: se.signature,
}
req.Equal(ac, proto)
se2 := &SealedEnvelope{}
for _, v := range []struct {
encoding iotextypes.Encoding
sig []byte
err string
}{
{0, _signByte, "invalid signature length ="},
{iotextypes.Encoding_ETHEREUM_UNPROTECTED + 1, _validSig, "unknown encoding type"},
} {
se.encoding = v.encoding
se.signature = v.sig
req.Contains(se2.loadProto(se.Proto(), _evmNetworkID).Error(), v.err)
}
for _, v := range []struct {
enc iotextypes.Encoding
hash string
}{
{0, "0562e100b057804ee3cb4fa906a897852aa8075013a02ef1e229360f1e5ee339"},
{1, "d5dc789026c12cc69f1ea7997fbe0aa1bcc02e85176848c7b2ecf4da6b4560d0"},
} {
se, err = createSealedEnvelope(0)
se.signature = _validSig
se.encoding = v.enc
req.NoError(se2.loadProto(se.Proto(), _evmNetworkID))
if v.enc > 0 {
se.evmNetworkID = _evmNetworkID
}
h, _ := se.Hash()
req.Equal(v.hash, hex.EncodeToString(h[:]))
se.SenderAddress()
_, _ = se2.Hash()
se2.SenderAddress()
req.Equal(se, se2)
tsf2, ok := se2.Envelope.Action().(*Transfer)
req.True(ok)
req.Equal(tsf, tsf2)
}
}
func createSealedEnvelope(chainID uint32) (*SealedEnvelope, error) {
evlp, _ := createEnvelope(chainID)
cPubKey, err := crypto.HexStringToPublicKey(_publicKey)
se := &SealedEnvelope{}
se.Envelope = evlp
se.srcPubkey = cPubKey
se.signature = _signByte
return se, err
}