github.com/iotexproject/iotex-core@v1.14.1-rc1/actpool/actioniterator/actioniterator_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package actioniterator 7 8 import ( 9 "fmt" 10 "math/big" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/go-pkgs/crypto" 16 17 "github.com/iotexproject/iotex-core/action" 18 "github.com/iotexproject/iotex-core/test/identityset" 19 ) 20 21 func TestActionIterator(t *testing.T) { 22 require := require.New(t) 23 24 a := identityset.Address(28) 25 priKeyA := identityset.PrivateKey(28) 26 b := identityset.Address(29) 27 priKeyB := identityset.PrivateKey(29) 28 c := identityset.Address(30) 29 priKeyC := identityset.PrivateKey(30) 30 accMap := make(map[string][]*action.SealedEnvelope) 31 tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), b.String(), nil, uint64(0), big.NewInt(13)) 32 require.NoError(err) 33 bd := &action.EnvelopeBuilder{} 34 elp := bd.SetNonce(1). 35 SetGasPrice(big.NewInt(13)). 36 SetAction(tsf1).Build() 37 selp1, err := action.Sign(elp, priKeyA) 38 require.NoError(err) 39 40 tsf2, err := action.NewTransfer(uint64(2), big.NewInt(100), "2", nil, uint64(0), big.NewInt(30)) 41 require.NoError(err) 42 bd = &action.EnvelopeBuilder{} 43 elp = bd.SetNonce(2). 44 SetGasPrice(big.NewInt(30)). 45 SetAction(tsf2).Build() 46 selp2, err := action.Sign(elp, priKeyA) 47 require.NoError(err) 48 49 accMap[a.String()] = []*action.SealedEnvelope{selp1, selp2} 50 51 tsf3, err := action.NewTransfer(uint64(1), big.NewInt(100), c.String(), nil, uint64(0), big.NewInt(15)) 52 require.NoError(err) 53 bd = &action.EnvelopeBuilder{} 54 elp = bd.SetNonce(1). 55 SetGasPrice(big.NewInt(15)). 56 SetAction(tsf3).Build() 57 selp3, err := action.Sign(elp, priKeyB) 58 require.NoError(err) 59 60 tsf4, err := action.NewTransfer(uint64(2), big.NewInt(100), "3", nil, uint64(0), big.NewInt(10)) 61 require.NoError(err) 62 bd = &action.EnvelopeBuilder{} 63 elp = bd.SetNonce(2). 64 SetGasPrice(big.NewInt(10)). 65 SetAction(tsf4).Build() 66 selp4, err := action.Sign(elp, priKeyB) 67 require.NoError(err) 68 69 tsf5, err := action.NewTransfer(uint64(3), big.NewInt(100), a.String(), nil, uint64(0), big.NewInt(2)) 70 require.NoError(err) 71 bd = &action.EnvelopeBuilder{} 72 elp = bd.SetNonce(3). 73 SetGasPrice(big.NewInt(20)). 74 SetAction(tsf5).Build() 75 selp5, err := action.Sign(elp, priKeyB) 76 require.NoError(err) 77 78 accMap[b.String()] = []*action.SealedEnvelope{selp3, selp4, selp5} 79 80 tsf6, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(0), big.NewInt(5)) 81 require.NoError(err) 82 bd = &action.EnvelopeBuilder{} 83 elp = bd.SetNonce(1). 84 SetGasPrice(big.NewInt(5)). 85 SetAction(tsf6).Build() 86 selp6, err := action.Sign(elp, priKeyC) 87 require.NoError(err) 88 89 accMap[c.String()] = []*action.SealedEnvelope{selp6} 90 91 ai := NewActionIterator(accMap) 92 appliedActionList := make([]*action.SealedEnvelope, 0) 93 for { 94 bestAction, ok := ai.Next() 95 if !ok { 96 break 97 } 98 appliedActionList = append(appliedActionList, bestAction) 99 } 100 require.Equal(appliedActionList, []*action.SealedEnvelope{selp3, selp1, selp2, selp4, selp5, selp6}) 101 } 102 103 func TestActionByPrice(t *testing.T) { 104 require := require.New(t) 105 106 s := &actionByPrice{} 107 require.Equal(0, s.Len()) 108 109 tsf1, err := action.NewTransfer(uint64(1), big.NewInt(100), "100", nil, uint64(0), big.NewInt(13)) 110 require.NoError(err) 111 bd := &action.EnvelopeBuilder{} 112 elp := bd.SetNonce(1). 113 SetGasPrice(big.NewInt(5)). 114 SetAction(tsf1).Build() 115 selp, err := action.Sign(elp, identityset.PrivateKey(28)) 116 require.NoError(err) 117 118 s.Push(selp) 119 require.Equal(1, s.Len()) 120 121 se := s.Pop() 122 require.Equal(0, s.Len()) 123 require.Equal(selp, se) 124 } 125 126 func BenchmarkLooping(b *testing.B) { 127 accMap := make(map[string][]*action.SealedEnvelope) 128 for i := 0; i < b.N; i++ { 129 priKey, err := crypto.HexStringToPrivateKey(fmt.Sprintf("1%063x", i)) 130 require.NoError(b, err) 131 addr := priKey.PublicKey().Address() 132 require.NotNil(b, addr) 133 tsf, err := action.NewTransfer(uint64(1), big.NewInt(100), "1", nil, uint64(100000), big.NewInt(int64(i))) 134 require.NoError(b, err) 135 bd := &action.EnvelopeBuilder{} 136 elp := bd.SetNonce(1). 137 SetGasPrice(big.NewInt(5)). 138 SetAction(tsf).Build() 139 selp, err := action.Sign(elp, priKey) 140 require.NoError(b, err) 141 accMap[addr.String()] = []*action.SealedEnvelope{selp} 142 } 143 ai := NewActionIterator(accMap) 144 b.ResetTimer() 145 for { 146 act, ok := ai.Next() 147 if !ok { 148 break 149 } 150 if act.GasLimit() < 30 { 151 ai.PopAccount() 152 } 153 } 154 b.StopTimer() 155 }