github.com/iotexproject/iotex-core@v1.14.1-rc1/action/sealedenvelope_test.go (about) 1 package action 2 3 import ( 4 "encoding/hex" 5 "math/big" 6 "testing" 7 8 "github.com/iotexproject/go-pkgs/crypto" 9 "github.com/iotexproject/go-pkgs/hash" 10 "github.com/iotexproject/iotex-proto/golang/iotextypes" 11 "github.com/stretchr/testify/require" 12 13 "github.com/iotexproject/iotex-core/pkg/unit" 14 "github.com/iotexproject/iotex-core/state" 15 "github.com/iotexproject/iotex-core/test/identityset" 16 ) 17 18 const ( 19 _publicKey = "04403d3c0dbd3270ddfc248c3df1f9aafd60f1d8e7456961c9ef262" + 20 "92262cc68f0ea9690263bef9e197a38f06026814fc70912c2b98d2e90a68f8ddc5328180a01" 21 _evmNetworkID uint32 = 4689 22 ) 23 24 var ( 25 _signByte = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9} 26 _validSig, _ = hex.DecodeString("15e73ad521ec9e06600c59e49b127c9dee114ad64fb2fcbe5e0d9f4c8d2b766e73d708cca1dc050dd27b20f2ee607f30428bf035f45d4da8ec2fb04a90c2c30901") 27 ) 28 29 func TestSealedEnvelope_Basic(t *testing.T) { 30 req := require.New(t) 31 for _, v := range []struct { 32 id uint32 33 hash string 34 }{ 35 {0, "322884fb04663019be6fb461d9453827487eafdd57b4de3bd89a7d77c9bf8395"}, 36 {1, "80af7840d73772d3022d8bdc46278fb755352e5e9d5f2a1f12ee7ec4f1ea98e9"}, 37 } { 38 se, err := createSealedEnvelope(v.id) 39 req.NoError(err) 40 rHash, err := se.Hash() 41 req.NoError(err) 42 req.Equal(v.id, se.ChainID()) 43 req.Equal(v.hash, hex.EncodeToString(rHash[:])) 44 req.Equal(_publicKey, se.SrcPubkey().HexString()) 45 req.Equal(_signByte, se.Signature()) 46 req.Zero(se.Encoding()) 47 48 var se1 SealedEnvelope 49 se.signature = _validSig 50 req.NoError(se1.loadProto(se.Proto(), _evmNetworkID)) 51 req.Equal(se.Envelope, se1.Envelope) 52 } 53 } 54 55 func TestSealedEnvelope_InvalidType(t *testing.T) { 56 require := require.New(t) 57 candidates := state.CandidateList{} 58 r := NewPutPollResult(1, 10001, candidates) 59 60 bd := &EnvelopeBuilder{} 61 elp := bd.SetNonce(1). 62 SetAction(r). 63 SetGasLimit(100000).Build() 64 selp := FakeSeal(elp, identityset.PrivateKey(27).PublicKey()) 65 selp.encoding = iotextypes.Encoding_ETHEREUM_EIP155 66 hash1, err := selp.envelopeHash() 67 require.Equal(hash1, hash.ZeroHash256) 68 require.Contains(err.Error(), "invalid action type") 69 } 70 71 func TestSealedEnvelope_Actions(t *testing.T) { 72 require := require.New(t) 73 74 createStake, err := NewCreateStake(uint64(10), _addr2, "100", uint32(10000), true, _payload, _gasLimit, _gasPrice) 75 require.NoError(err) 76 77 depositToStake, err := NewDepositToStake(1, 2, big.NewInt(10).String(), _payload, _gasLimit, _gasPrice) 78 require.NoError(err) 79 80 changeCandidate, err := NewChangeCandidate(1, _candidate1Name, 2, _payload, _gasLimit, _gasPrice) 81 require.NoError(err) 82 83 unstake, err := NewUnstake(_nonce, 2, _payload, _gasLimit, _gasPrice) 84 require.NoError(err) 85 86 withdrawStake, err := NewWithdrawStake(_nonce, 2, _payload, _gasLimit, _gasPrice) 87 require.NoError(err) 88 89 restake, err := NewRestake(_nonce, _index, _duration, _autoStake, _payload, _gasLimit, _gasPrice) 90 require.NoError(err) 91 92 transferStake, err := NewTransferStake(_nonce, _cand1Addr, 2, _payload, _gasLimit, _gasPrice) 93 require.NoError(err) 94 95 candidateRegister, err := NewCandidateRegister(_nonce, _candidate1Name, _cand1Addr, _cand1Addr, _cand1Addr, big.NewInt(10).String(), 91, true, _payload, _gasLimit, _gasPrice) 96 require.NoError(err) 97 98 candidateUpdate, err := NewCandidateUpdate(_nonce, _candidate1Name, _cand1Addr, _cand1Addr, _gasLimit, _gasPrice) 99 require.NoError(err) 100 101 tests := []actionPayload{ 102 createStake, 103 depositToStake, 104 changeCandidate, 105 unstake, 106 withdrawStake, 107 restake, 108 transferStake, 109 candidateRegister, 110 candidateUpdate, 111 } 112 113 for _, test := range tests { 114 bd := &EnvelopeBuilder{} 115 elp := bd.SetNonce(1). 116 SetAction(test). 117 SetGasLimit(100000).Build() 118 selp := FakeSeal(elp, identityset.PrivateKey(27).PublicKey()) 119 act, ok := selp.Action().(EthCompatibleAction) 120 require.True(ok) 121 rlp, err := act.ToEthTx(0) 122 require.NoError(err) 123 124 require.Equal(elp.Nonce(), rlp.Nonce()) 125 require.Equal(elp.GasPrice(), rlp.GasPrice()) 126 require.Equal(elp.GasLimit(), rlp.Gas()) 127 } 128 } 129 130 func TestSealedEnvelope_Proto(t *testing.T) { 131 req := require.New(t) 132 se, err := createSealedEnvelope(0) 133 req.NoError(err) 134 tsf, ok := se.Envelope.Action().(*Transfer) 135 req.True(ok) 136 proto := se.Proto() 137 ac := &iotextypes.Action{ 138 Core: se.Envelope.Proto(), 139 SenderPubKey: se.srcPubkey.Bytes(), 140 Signature: se.signature, 141 } 142 req.Equal(ac, proto) 143 144 se2 := &SealedEnvelope{} 145 for _, v := range []struct { 146 encoding iotextypes.Encoding 147 sig []byte 148 err string 149 }{ 150 {0, _signByte, "invalid signature length ="}, 151 {iotextypes.Encoding_ETHEREUM_UNPROTECTED + 1, _validSig, "unknown encoding type"}, 152 } { 153 se.encoding = v.encoding 154 se.signature = v.sig 155 req.Contains(se2.loadProto(se.Proto(), _evmNetworkID).Error(), v.err) 156 } 157 158 for _, v := range []struct { 159 enc iotextypes.Encoding 160 hash string 161 }{ 162 {0, "0562e100b057804ee3cb4fa906a897852aa8075013a02ef1e229360f1e5ee339"}, 163 {1, "d5dc789026c12cc69f1ea7997fbe0aa1bcc02e85176848c7b2ecf4da6b4560d0"}, 164 } { 165 se, err = createSealedEnvelope(0) 166 se.signature = _validSig 167 se.encoding = v.enc 168 req.NoError(se2.loadProto(se.Proto(), _evmNetworkID)) 169 if v.enc > 0 { 170 se.evmNetworkID = _evmNetworkID 171 } 172 h, _ := se.Hash() 173 req.Equal(v.hash, hex.EncodeToString(h[:])) 174 se.SenderAddress() 175 _, _ = se2.Hash() 176 se2.SenderAddress() 177 req.Equal(se, se2) 178 tsf2, ok := se2.Envelope.Action().(*Transfer) 179 req.True(ok) 180 req.Equal(tsf, tsf2) 181 } 182 } 183 184 func createSealedEnvelope(chainID uint32) (*SealedEnvelope, error) { 185 tsf, _ := NewTransfer( 186 uint64(10), 187 unit.ConvertIotxToRau(1000+int64(10)), 188 identityset.Address(10%identityset.Size()).String(), 189 nil, 190 20000+uint64(10), 191 unit.ConvertIotxToRau(1+int64(10)), 192 ) 193 eb := EnvelopeBuilder{} 194 evlp := eb. 195 SetAction(tsf). 196 SetGasLimit(tsf.GasLimit()). 197 SetGasPrice(tsf.GasPrice()). 198 SetNonce(tsf.Nonce()). 199 SetVersion(1). 200 SetChainID(chainID).Build() 201 202 cPubKey, err := crypto.HexStringToPublicKey(_publicKey) 203 se := &SealedEnvelope{} 204 se.Envelope = evlp 205 se.srcPubkey = cPubKey 206 se.signature = _signByte 207 return se, err 208 }