Skip to content

Commit 424ecfa

Browse files
coderbradleeraullenchai
authored andcommitted
Review all negative cases of big.Int iotexproject#625 (iotexproject#1035)
* Add tests for negative cases of big.Int iotexproject#625
1 parent a6a1b17 commit 424ecfa

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

e2etest/bigint_test.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright (c) 2019 IoTeX
2+
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
3+
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
4+
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
5+
// License 2.0 that can be found in the LICENSE file.
6+
7+
package e2etest
8+
9+
import (
10+
"context"
11+
"math/big"
12+
"testing"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
"github.com/iotexproject/iotex-core/action"
17+
"github.com/iotexproject/iotex-core/action/protocol"
18+
"github.com/iotexproject/iotex-core/action/protocol/account"
19+
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
20+
"github.com/iotexproject/iotex-core/action/protocol/execution"
21+
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
22+
"github.com/iotexproject/iotex-core/action/protocol/vote"
23+
"github.com/iotexproject/iotex-core/blockchain"
24+
"github.com/iotexproject/iotex-core/blockchain/block"
25+
"github.com/iotexproject/iotex-core/blockchain/genesis"
26+
"github.com/iotexproject/iotex-core/config"
27+
"github.com/iotexproject/iotex-core/pkg/keypair"
28+
"github.com/iotexproject/iotex-core/test/testaddress"
29+
"github.com/iotexproject/iotex-core/testutil"
30+
)
31+
32+
const (
33+
executor = "io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms"
34+
recipient = "io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6"
35+
executorPriKey = "cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1"
36+
)
37+
38+
func TestTransfer_Negative(t *testing.T) {
39+
r := require.New(t)
40+
ctx := context.Background()
41+
bc := prepareBlockchain(ctx, executor, r)
42+
defer r.NoError(bc.Stop(ctx))
43+
balanceBeforeTransfer, err := bc.Balance(executor)
44+
r.NoError(err)
45+
blk, err := prepareTransfer(bc, r)
46+
r.NoError(err)
47+
err = bc.ValidateBlock(blk)
48+
r.Error(err)
49+
err = bc.CommitBlock(blk)
50+
r.NoError(err)
51+
balance, err := bc.Balance(executor)
52+
r.NoError(err)
53+
r.Equal(0, balance.Cmp(balanceBeforeTransfer))
54+
}
55+
func TestAction_Negative(t *testing.T) {
56+
r := require.New(t)
57+
ctx := context.Background()
58+
bc := prepareBlockchain(ctx, executor, r)
59+
defer r.NoError(bc.Stop(ctx))
60+
balanceBeforeTransfer, err := bc.Balance(executor)
61+
r.NoError(err)
62+
blk, err := prepareAction(bc, r)
63+
r.NoError(err)
64+
r.NotNil(blk)
65+
err = bc.ValidateBlock(blk)
66+
r.Error(err)
67+
err = bc.CommitBlock(blk)
68+
r.NoError(err)
69+
balance, err := bc.Balance(executor)
70+
r.NoError(err)
71+
r.Equal(-1, balance.Cmp(balanceBeforeTransfer))
72+
}
73+
74+
func prepareBlockchain(
75+
ctx context.Context, executor string, r *require.Assertions) blockchain.Blockchain {
76+
cfg := config.Default
77+
cfg.Plugins[config.GatewayPlugin] = true
78+
cfg.Chain.EnableAsyncIndexWrite = false
79+
registry := protocol.Registry{}
80+
acc := account.NewProtocol()
81+
registry.Register(account.ProtocolID, acc)
82+
rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
83+
registry.Register(rolldpos.ProtocolID, rp)
84+
bc := blockchain.NewBlockchain(
85+
cfg,
86+
blockchain.InMemDaoOption(),
87+
blockchain.InMemStateFactoryOption(),
88+
blockchain.RegistryOption(&registry),
89+
)
90+
r.NotNil(bc)
91+
registry.Register(vote.ProtocolID, vote.NewProtocol(bc))
92+
bc.Validator().AddActionEnvelopeValidators(protocol.NewGenericValidator(bc, genesis.Default.ActionGasLimit))
93+
bc.Validator().AddActionValidators(account.NewProtocol(), execution.NewProtocol(bc))
94+
sf := bc.GetFactory()
95+
r.NotNil(sf)
96+
sf.AddActionHandlers(execution.NewProtocol(bc))
97+
r.NoError(bc.Start(ctx))
98+
ws, err := sf.NewWorkingSet()
99+
r.NoError(err)
100+
balance, ok := new(big.Int).SetString("1000000000000000000000000000", 10)
101+
r.True(ok)
102+
_, err = accountutil.LoadOrCreateAccount(ws, executor, balance)
103+
r.NoError(err)
104+
105+
ctx = protocol.WithRunActionsCtx(ctx,
106+
protocol.RunActionsCtx{
107+
Producer: testaddress.Addrinfo["producer"],
108+
GasLimit: uint64(10000000),
109+
})
110+
_, err = ws.RunActions(ctx, 0, nil)
111+
r.NoError(err)
112+
r.NoError(sf.Commit(ws))
113+
return bc
114+
}
115+
func prepareTransfer(bc blockchain.Blockchain, r *require.Assertions) (*block.Block, error) {
116+
exec, err := action.NewTransfer(1, big.NewInt(-10000), recipient, nil, uint64(1000000), big.NewInt(9000000000000))
117+
r.NoError(err)
118+
builder := &action.EnvelopeBuilder{}
119+
elp := builder.SetAction(exec).
120+
SetNonce(exec.Nonce()).
121+
SetGasLimit(exec.GasLimit()).
122+
SetGasPrice(exec.GasPrice()).
123+
Build()
124+
return prepare(bc, elp, r)
125+
}
126+
func prepareAction(bc blockchain.Blockchain, r *require.Assertions) (*block.Block, error) {
127+
exec, err := action.NewExecution(action.EmptyAddress, 1, big.NewInt(-100), uint64(1000000), big.NewInt(9000000000000), []byte{})
128+
r.NoError(err)
129+
builder := &action.EnvelopeBuilder{}
130+
elp := builder.SetAction(exec).
131+
SetNonce(exec.Nonce()).
132+
SetGasLimit(exec.GasLimit()).
133+
SetGasPrice(exec.GasPrice()).
134+
Build()
135+
return prepare(bc, elp, r)
136+
}
137+
func prepare(bc blockchain.Blockchain, elp action.Envelope, r *require.Assertions) (*block.Block, error) {
138+
priKey, err := keypair.HexStringToPrivateKey(executorPriKey)
139+
selp, err := action.Sign(elp, priKey)
140+
r.NoError(err)
141+
actionMap := make(map[string][]action.SealedEnvelope)
142+
actionMap[executor] = []action.SealedEnvelope{selp}
143+
blk, err := bc.MintNewBlock(
144+
actionMap,
145+
testutil.TimestampNow(),
146+
)
147+
r.NoError(err)
148+
return blk, nil
149+
}

0 commit comments

Comments
 (0)