github.com/iotexproject/iotex-core@v1.14.1-rc1/actpool/actpool_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 actpool 7 8 import ( 9 "bytes" 10 "context" 11 "math/big" 12 "testing" 13 "time" 14 15 "github.com/golang/mock/gomock" 16 "github.com/iotexproject/iotex-address/address" 17 "github.com/pkg/errors" 18 "github.com/stretchr/testify/require" 19 20 "github.com/iotexproject/iotex-core/action" 21 "github.com/iotexproject/iotex-core/action/protocol" 22 "github.com/iotexproject/iotex-core/action/protocol/account" 23 accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" 24 "github.com/iotexproject/iotex-core/action/protocol/rewarding" 25 "github.com/iotexproject/iotex-core/actpool/actioniterator" 26 "github.com/iotexproject/iotex-core/blockchain" 27 "github.com/iotexproject/iotex-core/blockchain/genesis" 28 "github.com/iotexproject/iotex-core/state" 29 "github.com/iotexproject/iotex-core/test/identityset" 30 "github.com/iotexproject/iotex-core/test/mock/mock_chainmanager" 31 "github.com/iotexproject/iotex-core/test/mock/mock_sealed_envelope_validator" 32 ) 33 34 const ( 35 _maxNumActsPerPool = 8192 36 _maxGasLimitPerPool = 81920000 37 _maxNumActsPerAcct = 256 38 ) 39 40 var ( 41 _addr1 = identityset.Address(28).String() 42 _priKey1 = identityset.PrivateKey(28) 43 _addr2 = identityset.Address(29).String() 44 _priKey2 = identityset.PrivateKey(29) 45 _addr3 = identityset.Address(30).String() 46 _priKey3 = identityset.PrivateKey(30) 47 _addr4 = identityset.Address(31).String() 48 _priKey4 = identityset.PrivateKey(31) 49 _addr5 = identityset.Address(32).String() 50 _priKey5 = identityset.PrivateKey(32) 51 _addr6 = identityset.Address(33).String() 52 _priKey6 = identityset.PrivateKey(33) 53 ) 54 55 func TestActPool_NewActPool(t *testing.T) { 56 ctrl := gomock.NewController(t) 57 require := require.New(t) 58 59 //error caused by nil blockchain 60 _, err := NewActPool(genesis.Default, nil, DefaultConfig, nil) 61 require.Error(err) 62 63 // all good 64 require.Panics(func() { blockchain.NewBlockchain(blockchain.DefaultConfig, genesis.Default, nil, nil, nil) }, "option is nil") 65 sf := mock_chainmanager.NewMockStateReader(ctrl) 66 act, err := NewActPool(genesis.Default, sf, DefaultConfig) 67 require.NoError(err) 68 require.NotNil(act) 69 70 // panic caused by option is nil 71 require.Panics(func() { NewActPool(genesis.Default, sf, DefaultConfig, nil) }, "option is nil") 72 73 // error caused by option 74 opt2 := func(pool *actPool) error { 75 return errors.New("test error") 76 } 77 _, err = NewActPool(genesis.Default, sf, DefaultConfig, opt2) 78 require.Error(err) 79 80 // test AddAction nil 81 require.NotPanics(func() { act.AddActionEnvelopeValidators(nil) }, "option is nil") 82 } 83 84 func TestValidate(t *testing.T) { 85 require := require.New(t) 86 ctrl := gomock.NewController(t) 87 88 g := genesis.Default 89 g.InitBalanceMap[_addr1] = "100" 90 re := protocol.NewRegistry() 91 acc := account.NewProtocol(rewarding.DepositGas) 92 require.NoError(acc.Register(re)) 93 ctx := genesis.WithGenesisContext( 94 protocol.WithRegistry(context.Background(), re), 95 g, 96 ) 97 sf := mock_chainmanager.NewMockStateReader(ctrl) 98 sev := mock_sealed_envelope_validator.NewMockSealedEnvelopeValidator(ctrl) 99 mockError := errors.New("mock error") 100 sev.EXPECT().Validate(gomock.Any(), gomock.Any()).Return(mockError).Times(1) 101 apConfig := getActPoolCfg() 102 Ap, err := NewActPool(g, sf, apConfig) 103 require.NoError(err) 104 ap, ok := Ap.(*actPool) 105 require.True(ok) 106 ap.AddActionEnvelopeValidators(sev) 107 // Case 0: Blacklist 108 tsfFromBL, err := action.SignedTransfer(_addr6, _priKey6, 1, big.NewInt(1), nil, 0, big.NewInt(0)) 109 require.NoError(err) 110 require.Equal(action.ErrAddress, errors.Cause(ap.Validate(ctx, tsfFromBL))) 111 // Case I: failed by sealed envelope validator 112 tsf, err := action.SignedTransfer(_addr1, _priKey1, 1, big.NewInt(1), nil, 0, big.NewInt(0)) 113 require.NoError(err) 114 require.Equal(mockError, errors.Cause(ap.Validate(ctx, tsf))) 115 } 116 117 func TestActPool_AddActs(t *testing.T) { 118 ctrl := gomock.NewController(t) 119 120 require := require.New(t) 121 sf := mock_chainmanager.NewMockStateReader(ctrl) 122 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 123 acct, ok := account.(*state.Account) 124 require.True(ok) 125 cfg := &protocol.StateConfig{} 126 for _, opt := range opts { 127 opt(cfg) 128 } 129 if bytes.Equal(cfg.Key, identityset.Address(28).Bytes()) { 130 require.NoError(acct.AddBalance(big.NewInt(100))) 131 } else { 132 require.NoError(acct.AddBalance(big.NewInt(10))) 133 } 134 return 0, nil 135 }).AnyTimes() 136 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 137 // Create actpool 138 apConfig := getActPoolCfg() 139 Ap, err := NewActPool(genesis.Default, sf, apConfig) 140 require.NoError(err) 141 ap, ok := Ap.(*actPool) 142 require.True(ok) 143 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 144 // Test actpool status after adding a sequence of Tsfs/votes: need to check confirmed nonce, pending nonce, and pending balance 145 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 146 require.NoError(err) 147 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(20), []byte{}, uint64(100000), big.NewInt(0)) 148 require.NoError(err) 149 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 150 require.NoError(err) 151 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 152 require.NoError(err) 153 tsf5, err := action.SignedTransfer(_addr1, _priKey1, uint64(5), big.NewInt(50), []byte{}, uint64(100000), big.NewInt(0)) 154 require.NoError(err) 155 tsf6, err := action.SignedTransfer(_addr2, _priKey2, uint64(1), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(0)) 156 require.NoError(err) 157 tsf7, err := action.SignedTransfer(_addr2, _priKey2, uint64(3), big.NewInt(1), []byte{}, uint64(100000), big.NewInt(0)) 158 require.NoError(err) 159 tsf8, err := action.SignedTransfer(_addr2, _priKey2, uint64(4), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(0)) 160 require.NoError(err) 161 162 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 163 require.NoError(ap.Add(ctx, tsf1)) 164 require.NoError(ap.Add(ctx, tsf2)) 165 require.NoError(ap.Add(ctx, tsf3)) 166 require.NoError(ap.Add(ctx, tsf4)) 167 require.Equal(action.ErrInsufficientFunds, errors.Cause(ap.Add(ctx, tsf5))) 168 require.NoError(ap.Add(ctx, tsf6)) 169 require.NoError(ap.Add(ctx, tsf7)) 170 require.NoError(ap.Add(ctx, tsf8)) 171 172 pBalance1, _ := getPendingBalance(ap, _addr1) 173 require.Equal(uint64(10), pBalance1.Uint64()) 174 pNonce1, _ := ap.GetPendingNonce(_addr1) 175 require.Equal(uint64(5), pNonce1) 176 177 pBalance2, _ := getPendingBalance(ap, _addr2) 178 require.Equal(uint64(5), pBalance2.Uint64()) 179 pNonce2, _ := ap.GetPendingNonce(_addr2) 180 require.Equal(uint64(2), pNonce2) 181 182 tsf9, err := action.SignedTransfer(_addr2, _priKey2, uint64(2), big.NewInt(3), []byte{}, uint64(100000), big.NewInt(0)) 183 require.NoError(err) 184 require.NoError(ap.Add(ctx, tsf9)) 185 pBalance2, _ = getPendingBalance(ap, _addr2) 186 require.Equal(uint64(1), pBalance2.Uint64()) 187 pNonce2, _ = ap.GetPendingNonce(_addr2) 188 require.Equal(uint64(4), pNonce2) 189 // Error Case Handling 190 // Case I: Action source address is blacklisted 191 bannedTsf, err := action.SignedTransfer(_addr6, _priKey6, uint64(1), big.NewInt(0), []byte{}, uint64(100000), big.NewInt(0)) 192 require.NoError(err) 193 err = ap.Add(ctx, bannedTsf) 194 require.Contains(err.Error(), "action source address is blacklisted") 195 // Case II: Action already exists in pool 196 require.Error(ap.Add(ctx, tsf1)) 197 require.Error(ap.Add(ctx, tsf4)) 198 // Case III: Pool space/gas space is full 199 Ap2, err := NewActPool(genesis.Default, sf, apConfig) 200 require.NoError(err) 201 ap2, ok := Ap2.(*actPool) 202 require.True(ok) 203 for i := uint64(0); i < ap2.cfg.MaxNumActsPerPool; i++ { 204 nTsf, err := action.SignedTransfer(_addr2, _priKey2, i, big.NewInt(50), nil, uint64(0), big.NewInt(0)) 205 require.NoError(err) 206 nTsfHash, err := nTsf.Hash() 207 require.NoError(err) 208 ap2.allActions.Set(nTsfHash, nTsf) 209 } 210 require.Equal(uint64(ap2.allActions.Count()), apConfig.MaxNumActsPerPool) 211 // Tx Pool is full, but replacement happens 212 require.Error(action.ErrTxPoolOverflow, ap2.Add(ctx, tsf1)) 213 require.Equal(uint64(ap2.allActions.Count()), apConfig.MaxNumActsPerPool) 214 require.NoError(ap2.Add(ctx, tsf4)) 215 require.Equal(uint64(ap2.allActions.Count()), apConfig.MaxNumActsPerPool) 216 217 Ap3, err := NewActPool(genesis.Default, sf, apConfig) 218 require.NoError(err) 219 ap3, ok := Ap3.(*actPool) 220 require.True(ok) 221 for i := uint64(1); i < apConfig.MaxGasLimitPerPool/10000; i++ { 222 nTsf, err := action.SignedTransfer(_addr2, _priKey2, i, big.NewInt(50), nil, uint64(10000), big.NewInt(0)) 223 require.NoError(err) 224 nTsfHash, err := nTsf.Hash() 225 require.NoError(err) 226 ap3.allActions.Set(nTsfHash, nTsf) 227 intrinsicGas, err := nTsf.IntrinsicGas() 228 require.NoError(err) 229 ap3.gasInPool += intrinsicGas 230 } 231 tsf10, err := action.SignedTransfer(_addr2, _priKey2, uint64(apConfig.MaxGasLimitPerPool/10000), big.NewInt(50), []byte{1, 2, 3}, uint64(20000), big.NewInt(0)) 232 require.NoError(err) 233 require.Equal(uint64(ap2.allActions.Count()), apConfig.MaxNumActsPerPool) 234 require.Error(action.ErrNonceTooHigh, ap3.Add(ctx, tsf10)) 235 require.Equal(uint64(ap2.allActions.Count()), apConfig.MaxNumActsPerPool) 236 237 // Case IV: Nonce already exists 238 replaceTsf, err := action.SignedTransfer(_addr2, _priKey1, uint64(1), big.NewInt(1), []byte{}, uint64(100000), big.NewInt(0)) 239 require.NoError(err) 240 err = ap.Add(ctx, replaceTsf) 241 require.Equal(action.ErrReplaceUnderpriced, errors.Cause(err)) 242 replaceTransfer, err := action.NewTransfer(uint64(4), big.NewInt(1), _addr2, []byte{}, uint64(100000), big.NewInt(0)) 243 require.NoError(err) 244 245 bd := &action.EnvelopeBuilder{} 246 elp := bd.SetNonce(4). 247 SetAction(replaceTransfer). 248 SetGasLimit(100000).Build() 249 selp, err := action.Sign(elp, _priKey1) 250 251 require.NoError(err) 252 253 err = ap.Add(ctx, selp) 254 require.Equal(action.ErrReplaceUnderpriced, errors.Cause(err)) 255 // Case V: Nonce is too large 256 outOfBoundsTsf, err := action.SignedTransfer(_addr1, _priKey1, ap.cfg.MaxNumActsPerAcct+1, big.NewInt(1), []byte{}, uint64(100000), big.NewInt(0)) 257 require.NoError(err) 258 err = ap.Add(ctx, outOfBoundsTsf) 259 require.Equal(action.ErrNonceTooHigh, errors.Cause(err)) 260 // Case VI: Insufficient balance 261 overBalTsf, err := action.SignedTransfer(_addr2, _priKey2, uint64(4), big.NewInt(20), []byte{}, uint64(100000), big.NewInt(0)) 262 require.NoError(err) 263 err = ap.Add(ctx, overBalTsf) 264 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 265 // Case VII: insufficient gas 266 tmpData := [1234]byte{} 267 creationExecution, err := action.NewExecution( 268 action.EmptyAddress, 269 uint64(5), 270 big.NewInt(int64(0)), 271 10, 272 big.NewInt(10), 273 tmpData[:], 274 ) 275 require.NoError(err) 276 277 bd = &action.EnvelopeBuilder{} 278 elp = bd.SetNonce(5). 279 SetGasPrice(big.NewInt(10)). 280 SetGasLimit(10). 281 SetAction(creationExecution).Build() 282 selp, err = action.Sign(elp, _priKey1) 283 require.NoError(err) 284 285 err = ap.Add(ctx, selp) 286 require.Equal(action.ErrIntrinsicGas, errors.Cause(err)) 287 288 // test reject system action 289 elp = bd.SetAction(&action.GrantReward{}).Build() 290 err = ap.Add(ctx, action.FakeSeal(elp, _priKey1.PublicKey())) 291 require.Equal(action.ErrInvalidAct, errors.Cause(err)) 292 elp = bd.SetAction(&action.PutPollResult{}).Build() 293 err = ap.Add(ctx, action.FakeSeal(elp, _priKey1.PublicKey())) 294 require.Equal(action.ErrInvalidAct, errors.Cause(err)) 295 } 296 297 func TestActPool_PickActs(t *testing.T) { 298 ctrl := gomock.NewController(t) 299 require := require.New(t) 300 sf := mock_chainmanager.NewMockStateReader(ctrl) 301 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 302 createActPool := func(cfg Config) (*actPool, []*action.SealedEnvelope, []*action.SealedEnvelope, []*action.SealedEnvelope) { 303 // Create actpool 304 Ap, err := NewActPool(genesis.Default, sf, cfg) 305 require.NoError(err) 306 ap, ok := Ap.(*actPool) 307 require.True(ok) 308 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 309 310 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 311 require.NoError(err) 312 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(20), []byte{}, uint64(100000), big.NewInt(0)) 313 require.NoError(err) 314 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 315 require.NoError(err) 316 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(40), []byte{}, uint64(100000), big.NewInt(0)) 317 require.NoError(err) 318 tsf5, err := action.SignedTransfer(_addr1, _priKey1, uint64(5), big.NewInt(50), []byte{}, uint64(100000), big.NewInt(0)) 319 require.NoError(err) 320 tsf6, err := action.SignedTransfer(_addr1, _priKey1, uint64(7), big.NewInt(50), []byte{}, uint64(100000), big.NewInt(0)) 321 require.NoError(err) 322 tsf7, err := action.SignedTransfer(_addr2, _priKey2, uint64(1), big.NewInt(50), []byte{}, uint64(100000), big.NewInt(0)) 323 require.NoError(err) 324 tsf8, err := action.SignedTransfer(_addr2, _priKey2, uint64(3), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(0)) 325 require.NoError(err) 326 tsf9, err := action.SignedTransfer(_addr2, _priKey2, uint64(4), big.NewInt(1), []byte{}, uint64(100000), big.NewInt(0)) 327 require.NoError(err) 328 tsf10, err := action.SignedTransfer(_addr2, _priKey2, uint64(5), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(0)) 329 require.NoError(err) 330 331 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 332 acct, ok := account.(*state.Account) 333 require.True(ok) 334 cfg := &protocol.StateConfig{} 335 for _, opt := range opts { 336 opt(cfg) 337 } 338 if bytes.Equal(cfg.Key, identityset.Address(28).Bytes()) { 339 require.NoError(acct.AddBalance(big.NewInt(100))) 340 } else { 341 require.NoError(acct.AddBalance(big.NewInt(10))) 342 } 343 return 0, nil 344 }).AnyTimes() 345 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 346 require.NoError(ap.Add(ctx, tsf1)) 347 require.NoError(ap.Add(ctx, tsf2)) 348 require.NoError(ap.Add(ctx, tsf3)) 349 require.NoError(ap.Add(ctx, tsf4)) 350 require.Equal(action.ErrInsufficientFunds, errors.Cause(ap.Add(ctx, tsf5))) 351 require.Error(ap.Add(ctx, tsf6)) 352 353 require.Error(ap.Add(ctx, tsf7)) 354 require.NoError(ap.Add(ctx, tsf8)) 355 require.NoError(ap.Add(ctx, tsf9)) 356 require.NoError(ap.Add(ctx, tsf10)) 357 358 return ap, []*action.SealedEnvelope{tsf1, tsf2, tsf3, tsf4}, []*action.SealedEnvelope{}, []*action.SealedEnvelope{} 359 } 360 361 t.Run("no-expiry", func(t *testing.T) { 362 apConfig := getActPoolCfg() 363 ap, transfers, _, executions := createActPool(apConfig) 364 pickedActs := ap.PendingActionMap() 365 require.Equal(len(transfers)+len(executions), lenPendingActionMap(pickedActs)) 366 time.Sleep(2 * time.Second) 367 pickedActs = ap.PendingActionMap() 368 require.Equal(len(transfers)+len(executions), lenPendingActionMap(pickedActs)) 369 }) 370 } 371 372 func TestActPool_removeConfirmedActs(t *testing.T) { 373 ctrl := gomock.NewController(t) 374 require := require.New(t) 375 sf := mock_chainmanager.NewMockStateReader(ctrl) 376 // Create actpool 377 apConfig := getActPoolCfg() 378 Ap, err := NewActPool(genesis.Default, sf, apConfig) 379 require.NoError(err) 380 ap, ok := Ap.(*actPool) 381 require.True(ok) 382 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 383 384 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 385 require.NoError(err) 386 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(20), []byte{}, uint64(100000), big.NewInt(0)) 387 require.NoError(err) 388 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 389 require.NoError(err) 390 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 391 require.NoError(err) 392 393 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 394 acct, ok := account.(*state.Account) 395 require.True(ok) 396 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 397 398 return 0, nil 399 }).Times(5) 400 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 401 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 402 require.NoError(ap.Add(ctx, tsf1)) 403 require.NoError(ap.Add(ctx, tsf2)) 404 require.NoError(ap.Add(ctx, tsf3)) 405 require.NoError(ap.Add(ctx, tsf4)) 406 407 require.Equal(4, ap.allActions.Count()) 408 addr, err := address.FromString(_addr1) 409 require.NoError(err) 410 require.NotNil(ap.worker[ap.allocatedWorker(addr)].accountActs.Account(addr.String())) 411 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 412 acct, ok := account.(*state.Account) 413 require.True(ok) 414 for i := uint64(1); i <= 4; i++ { 415 require.NoError(acct.SetPendingNonce(i + 1)) 416 } 417 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 418 419 return 0, nil 420 }).Times(1) 421 ap.Reset() 422 require.Equal(0, ap.allActions.Count()) 423 require.True(ap.worker[ap.allocatedWorker(addr)].accountActs.Account(addr.String()).Empty()) 424 } 425 426 func TestActPool_Reset(t *testing.T) { 427 ctrl := gomock.NewController(t) 428 require := require.New(t) 429 sf := mock_chainmanager.NewMockStateReader(ctrl) 430 431 balances := []*big.Int{ 432 big.NewInt(100), 433 big.NewInt(200), 434 big.NewInt(300), 435 big.NewInt(10), 436 big.NewInt(20), 437 } 438 nonces := []uint64{0, 0, 0, 0, 0} 439 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 440 acct, ok := account.(*state.Account) 441 require.True(ok) 442 cfg := &protocol.StateConfig{} 443 for _, opt := range opts { 444 opt(cfg) 445 } 446 switch { 447 case bytes.Equal(cfg.Key, identityset.Address(28).Bytes()): 448 require.NoError(acct.AddBalance(new(big.Int).Set(balances[0]))) 449 for i := uint64(1); i <= nonces[0]; i++ { 450 require.NoError(acct.SetPendingNonce(i + 1)) 451 } 452 case bytes.Equal(cfg.Key, identityset.Address(29).Bytes()): 453 require.NoError(acct.AddBalance(new(big.Int).Set(balances[1]))) 454 for i := uint64(1); i <= nonces[1]; i++ { 455 require.NoError(acct.SetPendingNonce(i + 1)) 456 } 457 case bytes.Equal(cfg.Key, identityset.Address(30).Bytes()): 458 require.NoError(acct.AddBalance(new(big.Int).Set(balances[2]))) 459 for i := uint64(1); i <= nonces[2]; i++ { 460 require.NoError(acct.SetPendingNonce(i + 1)) 461 } 462 case bytes.Equal(cfg.Key, identityset.Address(31).Bytes()): 463 require.NoError(acct.AddBalance(new(big.Int).Set(balances[3]))) 464 for i := uint64(1); i <= nonces[3]; i++ { 465 require.NoError(acct.SetPendingNonce(i + 1)) 466 } 467 case bytes.Equal(cfg.Key, identityset.Address(32).Bytes()): 468 require.NoError(acct.AddBalance(new(big.Int).Set(balances[4]))) 469 for i := uint64(1); i <= nonces[4]; i++ { 470 require.NoError(acct.SetPendingNonce(i + 1)) 471 } 472 } 473 return 0, nil 474 }).AnyTimes() 475 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 476 477 apConfig := getActPoolCfg() 478 Ap1, err := NewActPool(genesis.Default, sf, apConfig) 479 require.NoError(err) 480 ap1, ok := Ap1.(*actPool) 481 require.True(ok) 482 ap1.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 483 Ap2, err := NewActPool(genesis.Default, sf, apConfig) 484 require.NoError(err) 485 ap2, ok := Ap2.(*actPool) 486 require.True(ok) 487 ap2.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 488 489 // Tsfs to be added to ap1 490 tsf1, err := action.SignedTransfer(_addr2, _priKey1, uint64(1), big.NewInt(50), []byte{}, uint64(20000), big.NewInt(0)) 491 require.NoError(err) 492 tsf2, err := action.SignedTransfer(_addr3, _priKey1, uint64(2), big.NewInt(30), []byte{}, uint64(20000), big.NewInt(0)) 493 require.NoError(err) 494 tsf3, err := action.SignedTransfer(_addr2, _priKey1, uint64(3), big.NewInt(60), []byte{}, uint64(20000), big.NewInt(0)) 495 require.NoError(err) 496 tsf4, err := action.SignedTransfer(_addr1, _priKey2, uint64(1), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0)) 497 require.NoError(err) 498 tsf5, err := action.SignedTransfer(_addr3, _priKey2, uint64(2), big.NewInt(50), []byte{}, uint64(20000), big.NewInt(0)) 499 require.NoError(err) 500 tsf6, err := action.SignedTransfer(_addr1, _priKey2, uint64(3), big.NewInt(60), []byte{}, uint64(20000), big.NewInt(0)) 501 require.NoError(err) 502 tsf7, err := action.SignedTransfer(_addr1, _priKey3, uint64(1), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0)) 503 require.NoError(err) 504 tsf8, err := action.SignedTransfer(_addr2, _priKey3, uint64(2), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0)) 505 require.NoError(err) 506 tsf9, err := action.SignedTransfer(_addr1, _priKey3, uint64(4), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0)) 507 require.NoError(err) 508 509 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 510 require.NoError(ap1.Add(ctx, tsf1)) 511 require.NoError(ap1.Add(ctx, tsf2)) 512 err = ap1.Add(ctx, tsf3) 513 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 514 require.NoError(ap1.Add(ctx, tsf4)) 515 require.NoError(ap1.Add(ctx, tsf5)) 516 err = ap1.Add(ctx, tsf6) 517 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 518 require.NoError(ap1.Add(ctx, tsf7)) 519 require.NoError(ap1.Add(ctx, tsf8)) 520 require.NoError(ap1.Add(ctx, tsf9)) 521 // Tsfs to be added to ap2 only 522 tsf10, err := action.SignedTransfer(_addr2, _priKey1, uint64(3), big.NewInt(20), []byte{}, uint64(20000), big.NewInt(0)) 523 require.NoError(err) 524 tsf11, err := action.SignedTransfer(_addr3, _priKey1, uint64(4), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 525 require.NoError(err) 526 tsf12, err := action.SignedTransfer(_addr3, _priKey2, uint64(2), big.NewInt(70), []byte{}, uint64(20000), big.NewInt(0)) 527 require.NoError(err) 528 tsf13, err := action.SignedTransfer(_addr1, _priKey3, uint64(1), big.NewInt(200), []byte{}, uint64(20000), big.NewInt(0)) 529 require.NoError(err) 530 tsf14, err := action.SignedTransfer(_addr2, _priKey3, uint64(2), big.NewInt(50), []byte{}, uint64(20000), big.NewInt(0)) 531 require.NoError(err) 532 533 require.NoError(ap2.Add(ctx, tsf1)) 534 require.NoError(ap2.Add(ctx, tsf2)) 535 require.NoError(ap2.Add(ctx, tsf10)) 536 err = ap2.Add(ctx, tsf11) 537 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 538 require.NoError(ap2.Add(ctx, tsf4)) 539 require.NoError(ap2.Add(ctx, tsf12)) 540 require.NoError(ap2.Add(ctx, tsf13)) 541 require.NoError(ap2.Add(ctx, tsf14)) 542 err = ap2.Add(ctx, tsf9) 543 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 544 // Check confirmed nonce, pending nonce, and pending balance after adding Tsfs above for each account 545 // ap1 546 // Addr1 547 ap1PNonce1, _ := ap1.GetPendingNonce(_addr1) 548 require.Equal(uint64(3), ap1PNonce1) 549 ap1PBalance1, _ := getPendingBalance(ap1, _addr1) 550 require.Equal(big.NewInt(20).Uint64(), ap1PBalance1.Uint64()) 551 // Addr2 552 ap1PNonce2, _ := ap1.GetPendingNonce(_addr2) 553 require.Equal(uint64(3), ap1PNonce2) 554 ap1PBalance2, _ := getPendingBalance(ap1, _addr2) 555 require.Equal(big.NewInt(50).Uint64(), ap1PBalance2.Uint64()) 556 // Addr3 557 ap1PNonce3, _ := ap1.GetPendingNonce(_addr3) 558 require.Equal(uint64(3), ap1PNonce3) 559 ap1PBalance3, _ := getPendingBalance(ap1, _addr3) 560 require.Equal(big.NewInt(100).Uint64(), ap1PBalance3.Uint64()) 561 // ap2 562 // Addr1 563 ap2PNonce1, _ := ap2.GetPendingNonce(_addr1) 564 require.Equal(uint64(4), ap2PNonce1) 565 ap2PBalance1, _ := getPendingBalance(ap2, _addr1) 566 require.Equal(big.NewInt(0).Uint64(), ap2PBalance1.Uint64()) 567 // Addr2 568 ap2PNonce2, _ := ap2.GetPendingNonce(_addr2) 569 require.Equal(uint64(3), ap2PNonce2) 570 ap2PBalance2, _ := getPendingBalance(ap2, _addr2) 571 require.Equal(big.NewInt(30).Uint64(), ap2PBalance2.Uint64()) 572 // Addr3 573 ap2PNonce3, _ := ap2.GetPendingNonce(_addr3) 574 require.Equal(uint64(3), ap2PNonce3) 575 ap2PBalance3, _ := getPendingBalance(ap2, _addr3) 576 require.Equal(big.NewInt(50).Uint64(), ap2PBalance3.Uint64()) 577 // Let ap1 be BP's actpool 578 balances[0] = big.NewInt(220) 579 nonces[0] = 2 580 balances[1] = big.NewInt(200) 581 nonces[1] = 2 582 balances[2] = big.NewInt(180) 583 nonces[2] = 2 584 //Reset 585 ap1.Reset() 586 ap2.Reset() 587 // Check confirmed nonce, pending nonce, and pending balance after resetting actpool for each account 588 // ap1 589 // Addr1 590 ap1PNonce1, _ = ap1.GetPendingNonce(_addr1) 591 require.Equal(uint64(3), ap1PNonce1) 592 ap1PBalance1, _ = getPendingBalance(ap1, _addr1) 593 require.Equal(big.NewInt(220).Uint64(), ap1PBalance1.Uint64()) 594 // Addr2 595 ap1PNonce2, _ = ap1.GetPendingNonce(_addr2) 596 require.Equal(uint64(3), ap1PNonce2) 597 ap1PBalance2, _ = getPendingBalance(ap1, _addr2) 598 require.Equal(big.NewInt(200).Uint64(), ap1PBalance2.Uint64()) 599 // Addr3 600 ap1PNonce3, _ = ap1.GetPendingNonce(_addr3) 601 require.Equal(uint64(3), ap1PNonce3) 602 ap1PBalance3, _ = getPendingBalance(ap1, _addr3) 603 require.Equal(big.NewInt(180).Uint64(), ap1PBalance3.Uint64()) 604 // ap2 605 // Addr1 606 ap2PNonce1, _ = ap2.GetPendingNonce(_addr1) 607 require.Equal(uint64(4), ap2PNonce1) 608 ap2PBalance1, _ = getPendingBalance(ap2, _addr1) 609 require.Equal(big.NewInt(200).Uint64(), ap2PBalance1.Uint64()) 610 // Addr2 611 ap2PNonce2, _ = ap2.GetPendingNonce(_addr2) 612 require.Equal(uint64(3), ap2PNonce2) 613 ap2PBalance2, _ = getPendingBalance(ap2, _addr2) 614 require.Equal(big.NewInt(200).Uint64(), ap2PBalance2.Uint64()) 615 // Addr3 616 ap2PNonce3, _ = ap2.GetPendingNonce(_addr3) 617 require.Equal(uint64(3), ap2PNonce3) 618 ap2PBalance3, _ = getPendingBalance(ap2, _addr3) 619 require.Equal(big.NewInt(180).Uint64(), ap2PBalance3.Uint64()) 620 // Add more Tsfs after resetting 621 // Tsfs To be added to ap1 only 622 tsf15, err := action.SignedTransfer(_addr2, _priKey3, uint64(3), big.NewInt(80), []byte{}, uint64(20000), big.NewInt(0)) 623 require.NoError(err) 624 // Tsfs To be added to ap2 only 625 tsf16, err := action.SignedTransfer(_addr2, _priKey1, uint64(4), big.NewInt(150), []byte{}, uint64(20000), big.NewInt(0)) 626 require.NoError(err) 627 tsf17, err := action.SignedTransfer(_addr1, _priKey2, uint64(3), big.NewInt(90), []byte{}, uint64(20000), big.NewInt(0)) 628 require.NoError(err) 629 tsf18, err := action.SignedTransfer(_addr3, _priKey2, uint64(4), big.NewInt(100), []byte{}, uint64(20000), big.NewInt(0)) 630 require.NoError(err) 631 tsf19, err := action.SignedTransfer(_addr1, _priKey2, uint64(5), big.NewInt(50), []byte{}, uint64(20000), big.NewInt(0)) 632 require.NoError(err) 633 tsf20, err := action.SignedTransfer(_addr2, _priKey3, uint64(3), big.NewInt(200), []byte{}, uint64(20000), big.NewInt(0)) 634 require.NoError(err) 635 636 require.NoError(ap1.Add(ctx, tsf15)) 637 require.NoError(ap2.Add(ctx, tsf16)) 638 require.NoError(ap2.Add(ctx, tsf17)) 639 require.NoError(ap2.Add(ctx, tsf18)) 640 err = ap2.Add(ctx, tsf19) 641 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 642 err = ap2.Add(ctx, tsf20) 643 require.Equal(action.ErrInsufficientFunds, errors.Cause(err)) 644 // Check confirmed nonce, pending nonce, and pending balance after adding Tsfs above for each account 645 // ap1 646 // Addr1 647 ap1PNonce1, _ = ap1.GetPendingNonce(_addr1) 648 require.Equal(uint64(3), ap1PNonce1) 649 ap1PBalance1, _ = getPendingBalance(ap1, _addr1) 650 require.Equal(big.NewInt(220).Uint64(), ap1PBalance1.Uint64()) 651 // Addr2 652 ap1PNonce2, _ = ap1.GetPendingNonce(_addr2) 653 require.Equal(uint64(3), ap1PNonce2) 654 ap1PBalance2, _ = getPendingBalance(ap1, _addr2) 655 require.Equal(big.NewInt(200).Uint64(), ap1PBalance2.Uint64()) 656 // Addr3 657 ap1PNonce3, _ = ap1.GetPendingNonce(_addr3) 658 require.Equal(uint64(5), ap1PNonce3) 659 ap1PBalance3, _ = getPendingBalance(ap1, _addr3) 660 require.Equal(big.NewInt(0).Uint64(), ap1PBalance3.Uint64()) 661 // ap2 662 // Addr1 663 ap2PNonce1, _ = ap2.GetPendingNonce(_addr1) 664 require.Equal(uint64(5), ap2PNonce1) 665 ap2PBalance2, _ = getPendingBalance(ap2, _addr2) 666 require.Equal(big.NewInt(10).Uint64(), ap2PBalance2.Uint64()) 667 // Addr2 668 ap2PNonce2, _ = ap2.GetPendingNonce(_addr2) 669 require.Equal(uint64(5), ap2PNonce2) 670 ap2PBalance2, _ = getPendingBalance(ap2, _addr2) 671 require.Equal(big.NewInt(10).Uint64(), ap2PBalance2.Uint64()) 672 // Addr3 673 ap2PNonce3, _ = ap2.GetPendingNonce(_addr3) 674 require.Equal(uint64(3), ap2PNonce3) 675 ap2PBalance3, _ = getPendingBalance(ap2, _addr3) 676 require.Equal(big.NewInt(180).Uint64(), ap2PBalance3.Uint64()) 677 // Let ap2 be BP's actpool 678 balances[0] = big.NewInt(140) 679 nonces[0] = 4 680 balances[1] = big.NewInt(180) 681 nonces[1] = 4 682 balances[2] = big.NewInt(280) 683 nonces[2] = 2 684 685 //Reset 686 ap1.Reset() 687 ap2.Reset() 688 // Check confirmed nonce, pending nonce, and pending balance after resetting actpool for each account 689 // ap1 690 // Addr1 691 ap1PNonce1, _ = ap1.GetPendingNonce(_addr1) 692 require.Equal(uint64(5), ap1PNonce1) 693 ap1PBalance1, _ = getPendingBalance(ap1, _addr1) 694 require.Equal(big.NewInt(140).Uint64(), ap1PBalance1.Uint64()) 695 // Addr2 696 ap1PNonce2, _ = ap1.GetPendingNonce(_addr2) 697 require.Equal(uint64(5), ap1PNonce2) 698 ap1PBalance2, _ = getPendingBalance(ap1, _addr2) 699 require.Equal(big.NewInt(180).Uint64(), ap1PBalance2.Uint64()) 700 // Addr3 701 ap1PNonce3, _ = ap1.GetPendingNonce(_addr3) 702 require.Equal(uint64(5), ap1PNonce3) 703 ap1PBalance3, _ = getPendingBalance(ap1, _addr3) 704 require.Equal(big.NewInt(100).Uint64(), ap1PBalance3.Uint64()) 705 // ap2 706 // Addr1 707 ap2PNonce1, _ = ap2.GetPendingNonce(_addr1) 708 require.Equal(uint64(5), ap2PNonce1) 709 ap2PBalance1, _ = getPendingBalance(ap2, _addr1) 710 require.Equal(big.NewInt(140).Uint64(), ap2PBalance1.Uint64()) 711 // Addr2 712 ap2PNonce2, _ = ap2.GetPendingNonce(_addr2) 713 require.Equal(uint64(5), ap2PNonce2) 714 ap2PBalance2, _ = getPendingBalance(ap2, _addr2) 715 require.Equal(big.NewInt(180).Uint64(), ap2PBalance2.Uint64()) 716 // Addr3 717 ap2PNonce3, _ = ap2.GetPendingNonce(_addr3) 718 require.Equal(uint64(3), ap2PNonce3) 719 ap2PBalance3, _ = getPendingBalance(ap2, _addr3) 720 require.Equal(big.NewInt(280).Uint64(), ap2PBalance3.Uint64()) 721 722 // Add two more players 723 tsf21, err := action.SignedTransfer(_addr5, _priKey4, uint64(1), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 724 require.NoError(err) 725 tsf22, err := action.SignedTransfer(_addr5, _priKey4, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 726 require.NoError(err) 727 tsf23, err := action.NewTransfer(uint64(3), big.NewInt(1), "", []byte{}, uint64(100000), big.NewInt(0)) 728 require.NoError(err) 729 730 bd := &action.EnvelopeBuilder{} 731 elp := bd.SetNonce(3). 732 SetGasLimit(20000). 733 SetAction(tsf23).Build() 734 selp23, err := action.Sign(elp, _priKey4) 735 require.NoError(err) 736 737 tsf24, err := action.SignedTransfer(_addr5, _priKey5, uint64(1), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 738 require.NoError(err) 739 tsf25, err := action.SignedTransfer(_addr4, _priKey5, uint64(2), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 740 require.NoError(err) 741 tsf26, err := action.NewTransfer(uint64(3), big.NewInt(1), _addr4, []byte{}, uint64(20000), big.NewInt(0)) 742 require.NoError(err) 743 744 bd = &action.EnvelopeBuilder{} 745 elp = bd.SetNonce(3). 746 SetGasLimit(20000). 747 SetAction(tsf26).Build() 748 selp26, err := action.Sign(elp, _priKey5) 749 require.NoError(err) 750 751 require.NoError(ap1.Add(ctx, tsf21)) 752 require.Error(ap1.Add(ctx, tsf22)) 753 require.Error(ap1.Add(ctx, selp23)) 754 require.NoError(ap1.Add(ctx, tsf24)) 755 require.NoError(ap1.Add(ctx, tsf25)) 756 require.Error(ap1.Add(ctx, selp26)) 757 // Check confirmed nonce, pending nonce, and pending balance after adding actions above for account4 and account5 758 // ap1 759 // Addr4 760 ap1PNonce4, _ := ap1.GetPendingNonce(_addr4) 761 require.Equal(uint64(2), ap1PNonce4) 762 ap1PBalance4, _ := getPendingBalance(ap1, _addr4) 763 require.Equal(big.NewInt(0).Uint64(), ap1PBalance4.Uint64()) 764 // Addr5 765 ap1PNonce5, _ := ap1.GetPendingNonce(_addr5) 766 require.Equal(uint64(3), ap1PNonce5) 767 ap1PBalance5, _ := getPendingBalance(ap1, _addr5) 768 require.Equal(big.NewInt(0).Uint64(), ap1PBalance5.Uint64()) 769 // Let ap1 be BP's actpool 770 balances[3] = big.NewInt(10) 771 nonces[3] = 1 772 balances[4] = big.NewInt(20) 773 nonces[4] = 2 774 //Reset 775 ap1.Reset() 776 // Check confirmed nonce, pending nonce, and pending balance after resetting actpool for each account 777 // ap1 778 // Addr4 779 ap1PNonce4, _ = ap1.GetPendingNonce(_addr4) 780 require.Equal(uint64(2), ap1PNonce4) 781 ap1PBalance4, _ = getPendingBalance(ap1, _addr4) 782 require.Equal(big.NewInt(10).Uint64(), ap1PBalance4.Uint64()) 783 // Addr5 784 ap1PNonce5, _ = ap1.GetPendingNonce(_addr5) 785 require.Equal(uint64(3), ap1PNonce5) 786 ap1PBalance5, _ = getPendingBalance(ap1, _addr5) 787 require.Equal(big.NewInt(20).Uint64(), ap1PBalance5.Uint64()) 788 } 789 790 func TestActPool_removeInvalidActs(t *testing.T) { 791 ctrl := gomock.NewController(t) 792 require := require.New(t) 793 sf := mock_chainmanager.NewMockStateReader(ctrl) 794 // Create actpool 795 apConfig := getActPoolCfg() 796 Ap, err := NewActPool(genesis.Default, sf, apConfig) 797 require.NoError(err) 798 ap, ok := Ap.(*actPool) 799 require.True(ok) 800 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 801 802 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 803 require.NoError(err) 804 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(20), []byte{}, uint64(100000), big.NewInt(0)) 805 require.NoError(err) 806 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 807 require.NoError(err) 808 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 809 require.NoError(err) 810 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 811 acct, ok := account.(*state.Account) 812 require.True(ok) 813 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 814 815 return 0, nil 816 }).Times(5) 817 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 818 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 819 require.NoError(ap.Add(ctx, tsf1)) 820 require.NoError(ap.Add(ctx, tsf2)) 821 require.NoError(ap.Add(ctx, tsf3)) 822 require.NoError(ap.Add(ctx, tsf4)) 823 824 hash1, err := tsf1.Hash() 825 require.NoError(err) 826 hash2, err := tsf4.Hash() 827 require.NoError(err) 828 acts := []*action.SealedEnvelope{tsf1, tsf4} 829 _, exist1 := ap.allActions.Get(hash1) 830 require.True(exist1) 831 _, exist2 := ap.allActions.Get(hash2) 832 require.True(exist2) 833 ap.removeInvalidActs(acts) 834 _, exist1 = ap.allActions.Get(hash1) 835 require.False(exist1) 836 _, exist2 = ap.allActions.Get(hash2) 837 require.False(exist2) 838 } 839 840 func TestActPool_GetPendingNonce(t *testing.T) { 841 ctrl := gomock.NewController(t) 842 require := require.New(t) 843 sf := mock_chainmanager.NewMockStateReader(ctrl) 844 // Create actpool 845 apConfig := getActPoolCfg() 846 Ap, err := NewActPool(genesis.Default, sf, apConfig) 847 require.NoError(err) 848 ap, ok := Ap.(*actPool) 849 require.True(ok) 850 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 851 852 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 853 require.NoError(err) 854 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 855 require.NoError(err) 856 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 857 require.NoError(err) 858 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 859 require.NoError(err) 860 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 861 acct, ok := account.(*state.Account) 862 require.True(ok) 863 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 864 865 return 0, nil 866 }).Times(6) 867 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 868 869 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 870 require.NoError(ap.Add(ctx, tsf1)) 871 require.NoError(ap.Add(ctx, tsf3)) 872 require.NoError(ap.Add(ctx, tsf4)) 873 874 nonce, err := ap.GetPendingNonce(_addr2) 875 require.NoError(err) 876 require.Equal(uint64(1), nonce) 877 878 nonce, err = ap.GetPendingNonce(_addr1) 879 require.NoError(err) 880 require.Equal(uint64(2), nonce) 881 882 require.NoError(ap.Add(ctx, tsf2)) 883 nonce, err = ap.GetPendingNonce(_addr1) 884 require.NoError(err) 885 require.Equal(uint64(5), nonce) 886 } 887 888 func TestActPool_GetUnconfirmedActs(t *testing.T) { 889 ctrl := gomock.NewController(t) 890 require := require.New(t) 891 sf := mock_chainmanager.NewMockStateReader(ctrl) 892 // Create actpool 893 apConfig := getActPoolCfg() 894 Ap, err := NewActPool(genesis.Default, sf, apConfig) 895 require.NoError(err) 896 ap, ok := Ap.(*actPool) 897 require.True(ok) 898 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 899 900 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 901 require.NoError(err) 902 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 903 require.NoError(err) 904 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 905 require.NoError(err) 906 tsf5, err := action.SignedTransfer(_addr1, _priKey2, uint64(1), big.NewInt(30), []byte{}, uint64(100000), big.NewInt(0)) 907 require.NoError(err) 908 909 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 910 acct, ok := account.(*state.Account) 911 require.True(ok) 912 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 913 914 return 0, nil 915 }).Times(6) 916 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 917 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 918 require.NoError(ap.Add(ctx, tsf1)) 919 require.NoError(ap.Add(ctx, tsf3)) 920 require.NoError(ap.Add(ctx, tsf4)) 921 require.NoError(ap.Add(ctx, tsf5)) 922 923 acts := ap.GetUnconfirmedActs(_addr3) 924 require.Equal([]*action.SealedEnvelope(nil), acts) 925 926 acts = ap.GetUnconfirmedActs(_addr1) 927 validated := []*action.SealedEnvelope{tsf1, tsf3, tsf4, tsf5} 928 require.Equal(len(acts), len(validated)) 929 for i := 0; i < len(acts); i++ { 930 hashVal1, hashErr1 := validated[i].Hash() 931 require.NoError(hashErr1) 932 hashVal2, hashErr2 := acts[i].Hash() 933 require.NoError(hashErr2) 934 require.Equal(hashVal1, hashVal2) 935 } 936 } 937 938 func TestActPool_GetActionByHash(t *testing.T) { 939 ctrl := gomock.NewController(t) 940 require := require.New(t) 941 942 sf := mock_chainmanager.NewMockStateReader(ctrl) 943 // Create actpool 944 apConfig := getActPoolCfg() 945 Ap, err := NewActPool(genesis.Default, sf, apConfig) 946 require.NoError(err) 947 ap, ok := Ap.(*actPool) 948 require.True(ok) 949 950 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 951 require.NoError(err) 952 hash1, err := tsf1.Hash() 953 require.NoError(err) 954 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 955 require.NoError(err) 956 hash2, err := tsf2.Hash() 957 require.NoError(err) 958 959 ap.allActions.Set(hash1, tsf1) 960 act, err := ap.GetActionByHash(hash1) 961 require.NoError(err) 962 require.Equal(tsf1, act) 963 act, err = ap.GetActionByHash(hash2) 964 require.Equal(action.ErrNotFound, errors.Cause(err)) 965 require.Nil(act) 966 967 ap.allActions.Set(hash2, tsf2) 968 act, err = ap.GetActionByHash(hash2) 969 require.NoError(err) 970 require.Equal(tsf2, act) 971 } 972 973 func TestActPool_GetCapacity(t *testing.T) { 974 ctrl := gomock.NewController(t) 975 require := require.New(t) 976 sf := mock_chainmanager.NewMockStateReader(ctrl) 977 // Create actpool 978 apConfig := getActPoolCfg() 979 Ap, err := NewActPool(genesis.Default, sf, apConfig) 980 require.NoError(err) 981 ap, ok := Ap.(*actPool) 982 require.True(ok) 983 require.Equal(uint64(_maxNumActsPerPool), ap.GetCapacity()) 984 require.Equal(uint64(_maxGasLimitPerPool), ap.GetGasCapacity()) 985 } 986 987 func TestActPool_GetSize(t *testing.T) { 988 ctrl := gomock.NewController(t) 989 require := require.New(t) 990 sf := mock_chainmanager.NewMockStateReader(ctrl) 991 // Create actpool 992 apConfig := getActPoolCfg() 993 Ap, err := NewActPool(genesis.Default, sf, apConfig) 994 require.NoError(err) 995 ap, ok := Ap.(*actPool) 996 require.True(ok) 997 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 998 require.Zero(ap.GetSize()) 999 require.Zero(ap.GetGasSize()) 1000 1001 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(20000), big.NewInt(0)) 1002 require.NoError(err) 1003 tsf2, err := action.SignedTransfer(_addr1, _priKey1, uint64(2), big.NewInt(20), []byte{}, uint64(20000), big.NewInt(0)) 1004 require.NoError(err) 1005 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(3), big.NewInt(30), []byte{}, uint64(20000), big.NewInt(0)) 1006 require.NoError(err) 1007 tsf4, err := action.SignedTransfer(_addr1, _priKey1, uint64(4), big.NewInt(30), []byte{}, uint64(20000), big.NewInt(0)) 1008 require.NoError(err) 1009 1010 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 1011 acct, ok := account.(*state.Account) 1012 require.True(ok) 1013 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 1014 1015 return 0, nil 1016 }).Times(5) 1017 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 1018 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 1019 require.NoError(ap.Add(ctx, tsf1)) 1020 require.NoError(ap.Add(ctx, tsf2)) 1021 require.NoError(ap.Add(ctx, tsf3)) 1022 require.NoError(ap.Add(ctx, tsf4)) 1023 require.Equal(uint64(4), ap.GetSize()) 1024 require.Equal(uint64(40000), ap.GetGasSize()) 1025 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 1026 acct, ok := account.(*state.Account) 1027 require.True(ok) 1028 for i := uint64(1); i <= 4; i++ { 1029 require.NoError(acct.SetPendingNonce(i + 1)) 1030 } 1031 require.NoError(acct.AddBalance(big.NewInt(100000000000000000))) 1032 1033 return 0, nil 1034 }).Times(1) 1035 ap.Reset() 1036 require.Equal(uint64(0), ap.GetSize()) 1037 require.Equal(uint64(0), ap.GetGasSize()) 1038 } 1039 1040 func TestActPool_AddActionNotEnoughGasPrice(t *testing.T) { 1041 ctrl := gomock.NewController(t) 1042 sf := mock_chainmanager.NewMockStateReader(ctrl) 1043 sf.EXPECT().Height().Return(uint64(0), nil).Times(2) 1044 1045 apConfig := DefaultConfig 1046 ap, err := NewActPool(genesis.Default, sf, apConfig) 1047 require.NoError(t, err) 1048 tsf, err := action.SignedTransfer( 1049 identityset.Address(0).String(), 1050 identityset.PrivateKey(1), 1051 uint64(1), 1052 big.NewInt(10), 1053 []byte{}, 1054 uint64(20000), 1055 big.NewInt(0), 1056 ) 1057 require.NoError(t, err) 1058 1059 ctx := protocol.WithBlockchainCtx(context.Background(), protocol.BlockchainCtx{}) 1060 ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx( 1061 genesis.WithGenesisContext(ctx, genesis.Default), protocol.BlockCtx{ 1062 BlockHeight: 1, 1063 })) 1064 require.Error(t, ap.Add(ctx, tsf)) 1065 } 1066 1067 func TestActPool_SpeedUpAction(t *testing.T) { 1068 ctrl := gomock.NewController(t) 1069 require := require.New(t) 1070 sf := mock_chainmanager.NewMockStateReader(ctrl) 1071 sf.EXPECT().State(gomock.Any(), gomock.Any()).DoAndReturn(func(account interface{}, opts ...protocol.StateOption) (uint64, error) { 1072 acct, ok := account.(*state.Account) 1073 require.True(ok) 1074 cfg := &protocol.StateConfig{} 1075 for _, opt := range opts { 1076 opt(cfg) 1077 } 1078 require.NoError(acct.AddBalance(big.NewInt(10000000))) 1079 return 0, nil 1080 }).AnyTimes() 1081 sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes() 1082 1083 // Create actpool 1084 apConfig := getActPoolCfg() 1085 Ap, err := NewActPool(genesis.Default, sf, apConfig) 1086 require.NoError(err) 1087 ap, ok := Ap.(*actPool) 1088 require.True(ok) 1089 ap.AddActionEnvelopeValidators(protocol.NewGenericValidator(sf, accountutil.AccountState)) 1090 1091 tsf1, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(0)) 1092 require.NoError(err) 1093 tsf2, err := action.SignedTransfer(_addr2, _priKey2, uint64(1), big.NewInt(5), []byte{}, uint64(100000), big.NewInt(1)) 1094 require.NoError(err) 1095 tsf3, err := action.SignedTransfer(_addr1, _priKey1, uint64(1), big.NewInt(10), []byte{}, uint64(100000), big.NewInt(3)) 1096 require.NoError(err) 1097 1098 // A send action tsf1 with nonce 1, B send action tsf2 with nonce 1 1099 ctx := genesis.WithGenesisContext(context.Background(), genesis.Default) 1100 require.NoError(ap.Add(ctx, tsf1)) 1101 require.NoError(ap.Add(ctx, tsf2)) 1102 1103 // check account and actpool status 1104 pBalance1, _ := getPendingBalance(ap, _addr1) 1105 require.Equal(uint64(10000000-10), pBalance1.Uint64()) 1106 pNonce1, _ := ap.GetPendingNonce(_addr1) 1107 require.Equal(uint64(2), pNonce1) 1108 pBalance2, _ := getPendingBalance(ap, _addr2) 1109 require.Equal(uint64(10000000-5-10000), pBalance2.Uint64()) 1110 pNonce2, _ := ap.GetPendingNonce(_addr2) 1111 require.Equal(uint64(2), pNonce2) 1112 1113 // A send action tsf3 with nonce 1 and higher gas price 1114 require.NoError(ap.Add(ctx, tsf3)) 1115 1116 // check account and actpool status again after new action is inserted 1117 pNonce3, _ := ap.GetPendingNonce(_addr1) 1118 require.Equal(uint64(2), pNonce3) 1119 1120 ai := actioniterator.NewActionIterator(ap.PendingActionMap()) 1121 appliedActionList := make([]*action.SealedEnvelope, 0) 1122 for { 1123 bestAction, ok := ai.Next() 1124 if !ok { 1125 break 1126 } 1127 appliedActionList = append(appliedActionList, bestAction) 1128 } 1129 // tsf1 is replaced by tsf3 with higher gas price 1130 validated := []*action.SealedEnvelope{tsf3, tsf2} 1131 require.Equal(len(appliedActionList), len(validated)) 1132 for i := 0; i < len(appliedActionList); i++ { 1133 hashVal1, hashErr1 := validated[i].Hash() 1134 require.NoError(hashErr1) 1135 hashVal2, hashErr2 := appliedActionList[i].Hash() 1136 require.NoError(hashErr2) 1137 require.Equal(hashVal1, hashVal2) 1138 } 1139 } 1140 1141 // Helper function to return the correct pending balance just in case of empty queue 1142 func getPendingBalance(ap *actPool, addrStr string) (*big.Int, error) { 1143 addr, err := address.FromString(addrStr) 1144 if err != nil { 1145 return nil, err 1146 } 1147 if queue := ap.worker[ap.allocatedWorker(addr)].accountActs.Account(addr.String()); queue != nil { 1148 q := queue.(*actQueue) 1149 return q.getPendingBalanceAtNonce(q.pendingNonce), nil 1150 } 1151 state, err := accountutil.AccountState(genesis.WithGenesisContext(context.Background(), genesis.Default), ap.sf, addr) 1152 if err != nil { 1153 return nil, err 1154 } 1155 return state.Balance, nil 1156 } 1157 1158 func getActPoolCfg() Config { 1159 return Config{ 1160 MaxNumActsPerPool: _maxNumActsPerPool, 1161 MaxGasLimitPerPool: _maxGasLimitPerPool, 1162 MaxNumActsPerAcct: _maxNumActsPerAcct, 1163 MinGasPriceStr: "0", 1164 BlackList: []string{_addr6}, 1165 } 1166 } 1167 1168 func lenPendingActionMap(acts map[string][]*action.SealedEnvelope) int { 1169 l := 0 1170 for _, part := range acts { 1171 l += len(part) 1172 } 1173 return l 1174 } 1175 1176 func TestValidateMinGasPrice(t *testing.T) { 1177 ap := Config{MinGasPriceStr: DefaultConfig.MinGasPriceStr} 1178 mgp := ap.MinGasPrice() 1179 require.IsType(t, &big.Int{}, mgp) 1180 }