Skip to content

Commit

Permalink
use context.Context in workingset run actions (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yutong Pei committed Nov 7, 2018
1 parent 7f8f108 commit a4c1d6d
Show file tree
Hide file tree
Showing 15 changed files with 278 additions and 106 deletions.
9 changes: 7 additions & 2 deletions action/protocols/multichain/mainchain/createdeposit_test.go
Expand Up @@ -64,8 +64,13 @@ func TestValidateDeposit(t *testing.T) {
)
require.NoError(t, err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateCtx)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(t, err)
require.NoError(t, sf.Commit(ws))

Expand Down
9 changes: 7 additions & 2 deletions action/protocols/multichain/mainchain/putblock_test.go
Expand Up @@ -49,8 +49,13 @@ func TestHandlePutBlock(t *testing.T) {
)
require.NoError(t, err)
gasLimit := testutil.TestGasLimit
stateContext := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateContext)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(t, err)
require.NoError(t, sf.Commit(ws))

Expand Down
9 changes: 7 additions & 2 deletions action/protocols/multichain/mainchain/startsubchain_test.go
Expand Up @@ -236,8 +236,13 @@ func TestHandleStartSubChain(t *testing.T) {
)
require.NoError(t, err)
gasLimit := testutil.TestGasLimit
stateContext := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateContext)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(t, err)
require.NoError(t, sf.Commit(ws))

Expand Down
56 changes: 44 additions & 12 deletions actpool/actpool_test.go
Expand Up @@ -95,8 +95,13 @@ func TestActPool_validateGenericAction(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, []action.Action{prevTsf}, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, []action.Action{prevTsf})
require.NoError(err)
require.Nil(sf.Commit(ws))
ap.Reset()
Expand Down Expand Up @@ -384,8 +389,13 @@ func TestActPool_removeConfirmedActs(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, []action.Action{tsf1, tsf2, tsf3, vote4}, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, []action.Action{tsf1, tsf2, tsf3, vote4})
require.NoError(err)
require.Nil(sf.Commit(ws))
ap.removeConfirmedActs()
Expand Down Expand Up @@ -542,8 +552,13 @@ func TestActPool_Reset(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, pickedActs, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, pickedActs)
require.NoError(err)
require.Nil(sf.Commit(ws))
//Reset
Expand Down Expand Up @@ -654,8 +669,13 @@ func TestActPool_Reset(t *testing.T) {
// ap2 commits update of accounts to trie
ws, err = sf.NewWorkingSet()
require.NoError(err)
ctx = state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, pickedActs, ctx)
ctx = state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, pickedActs)
require.NoError(err)
require.Nil(sf.Commit(ws))
//Reset
Expand Down Expand Up @@ -747,8 +767,14 @@ func TestActPool_Reset(t *testing.T) {
// ap1 commits update of accounts to trie
ws, err = sf.NewWorkingSet()
require.NoError(err)
ctx = state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, pickedActs, ctx)

ctx = state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, pickedActs)
require.NoError(err)
require.Nil(sf.Commit(ws))
//Reset
Expand Down Expand Up @@ -977,8 +1003,14 @@ func TestActPool_GetSize(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, []action.Action{tsf1, tsf2, tsf3, vote4}, ctx)

ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, []action.Action{tsf1, tsf2, tsf3, vote4})
require.NoError(err)
require.Nil(sf.Commit(ws))
ap.removeConfirmedActs()
Expand Down
18 changes: 14 additions & 4 deletions blockchain/block_test.go
Expand Up @@ -265,8 +265,13 @@ func TestWrongNonce(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(1, []action.Action{tsf1}, stateCtx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 1, []action.Action{tsf1})
require.NoError(err)
require.Nil(sf.Commit(ws))

Expand Down Expand Up @@ -623,8 +628,13 @@ func addCreatorToFactory(sf state.Factory) error {
return err
}
gasLimit := testutil.TestGasLimit
ctx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
if _, _, err = ws.RunActions(0, nil, ctx); err != nil {
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
if _, _, err = ws.RunActions(ctx, 0, nil); err != nil {
return err
}
if err = sf.Commit(ws); err != nil {
Expand Down
36 changes: 28 additions & 8 deletions blockchain/blockchain.go
Expand Up @@ -293,6 +293,7 @@ func (bc *blockchain) Start(ctx context.Context) (err error) {
}

func (bc *blockchain) startEmptyBlockchain() error {
ctx := context.Background()
genesis := NewGenesisBlock(bc.config)
if genesis == nil {
return errors.New("cannot create genesis block")
Expand All @@ -313,8 +314,12 @@ func (bc *blockchain) startEmptyBlockchain() error {
return errors.Wrap(err, "failed to create Creator into StateFactory")
}
gasLimit := GasLimit
ctx := state.Context{genesis.ProducerAddress(), &gasLimit, bc.config.Chain.EnableGasCharge}
if _, _, err := ws.RunActions(0, nil, ctx); err != nil {
ctx = state.WithRunActionsCtx(ctx, state.RunActionsCtx{
ProducerAddr: genesis.ProducerAddress(),
GasLimit: &gasLimit,
EnableGasCharge: bc.config.Chain.EnableGasCharge,
})
if _, _, err := ws.RunActions(ctx, 0, nil); err != nil {
return errors.Wrap(err, "failed to create Creator into StateFactory")
}
if err := bc.sf.Commit(ws); err != nil {
Expand Down Expand Up @@ -360,8 +365,13 @@ func (bc *blockchain) startExistingBlockchain(recoveryHeight uint64) error {
return err
}
gasLimit := GasLimit
ctx := state.Context{genesisBlk.ProducerAddress(), &gasLimit, bc.config.Chain.EnableGasCharge}
if _, _, err := ws.RunActions(0, nil, ctx); err != nil {
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: genesisBlk.ProducerAddress(),
GasLimit: &gasLimit,
EnableGasCharge: bc.config.Chain.EnableGasCharge,
})
if _, _, err := ws.RunActions(ctx, 0, nil); err != nil {
return errors.Wrap(err, "failed to create Creator into StateFactory")
}
if err := bc.sf.Commit(ws); err != nil {
Expand Down Expand Up @@ -429,8 +439,13 @@ func (bc *blockchain) CreateState(addr string, init *big.Int) (*state.Account, e
return nil, errors.Wrap(err, "failed to get genesis block")
}
gasLimit := GasLimit
ctx := state.Context{genesisBlk.ProducerAddress(), &gasLimit, bc.config.Chain.EnableGasCharge}
if _, _, err = ws.RunActions(0, nil, ctx); err != nil {
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: genesisBlk.ProducerAddress(),
GasLimit: &gasLimit,
EnableGasCharge: bc.config.Chain.EnableGasCharge,
})
if _, _, err = ws.RunActions(ctx, 0, nil); err != nil {
return nil, errors.Wrap(err, "failed to run the account creation")
}
if err = bc.sf.Commit(ws); err != nil {
Expand Down Expand Up @@ -865,8 +880,13 @@ func (bc *blockchain) runActions(blk *Block, ws state.WorkingSet, verify bool) (
ExecuteContracts(blk, ws, bc, &gasLimit, bc.config.Chain.EnableGasCharge)
}
// update state factory
ctx := state.Context{blk.ProducerAddress(), &gasLimit, bc.config.Chain.EnableGasCharge}
root, receipts, err := ws.RunActions(blk.Height(), blk.Actions, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: blk.ProducerAddress(),
GasLimit: &gasLimit,
EnableGasCharge: bc.config.Chain.EnableGasCharge,
})
root, receipts, err := ws.RunActions(ctx, blk.Height(), blk.Actions)
if err != nil {
return root, err
}
Expand Down
18 changes: 14 additions & 4 deletions blockchain/blockchain_test.go
Expand Up @@ -781,8 +781,13 @@ func TestBlocks(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(c.RawAddress, big.NewInt(100000))
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(err)
require.NoError(sf.Commit(ws))

Expand Down Expand Up @@ -829,8 +834,13 @@ func TestActions(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(c.RawAddress, big.NewInt(100000))
require.NoError(err)
gasLimit := testutil.TestGasLimit
ctx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, ctx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(err)
require.NoError(sf.Commit(ws))

Expand Down
27 changes: 21 additions & 6 deletions blockchain/evm_test.go
Expand Up @@ -53,8 +53,13 @@ func TestEVM(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(ta.Addrinfo["producer"].RawAddress, Gen.TotalSupply)
require.NoError(err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateCtx)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(err)
require.NoError(sf.Commit(ws))

Expand Down Expand Up @@ -207,8 +212,13 @@ func TestRollDice(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(ta.Addrinfo["bravo"].RawAddress, big.NewInt(12000000))
require.NoError(err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateCtx)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(err)
require.NoError(sf.Commit(ws))

Expand Down Expand Up @@ -316,8 +326,13 @@ func TestERC20(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(ta.Addrinfo["bravo"].RawAddress, big.NewInt(0))
require.NoError(err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateCtx)
ctx = state.WithRunActionsCtx(ctx,
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(err)
require.NoError(sf.Commit(ws))

Expand Down
9 changes: 7 additions & 2 deletions consensus/scheme/rolldpos/rolldpos_test.go
Expand Up @@ -734,8 +734,13 @@ func TestRollDPoSConsensus(t *testing.T) {
_, err = ws.LoadOrCreateAccountState(chainRawAddrs[j], big.NewInt(0))
require.NoError(t, err)
gasLimit := testutil.TestGasLimit
stateCtx := state.Context{testaddress.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
_, _, err = ws.RunActions(0, nil, stateCtx)
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: testaddress.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
_, _, err = ws.RunActions(ctx, 0, nil)
require.NoError(t, err)
require.NoError(t, sf.Commit(ws))
}
Expand Down
9 changes: 7 additions & 2 deletions explorer/explorer_test.go
Expand Up @@ -1037,8 +1037,13 @@ func addCreatorToFactory(sf state.Factory) error {
return err
}
gasLimit := testutil.TestGasLimit
ctx := state.Context{ta.Addrinfo["producer"].RawAddress, &gasLimit, testutil.EnableGasCharge}
if _, _, err = ws.RunActions(0, nil, ctx); err != nil {
ctx := state.WithRunActionsCtx(context.Background(),
state.RunActionsCtx{
ProducerAddr: ta.Addrinfo["producer"].RawAddress,
GasLimit: &gasLimit,
EnableGasCharge: testutil.EnableGasCharge,
})
if _, _, err = ws.RunActions(ctx, 0, nil); err != nil {
return err
}
if err = sf.Commit(ws); err != nil {
Expand Down
33 changes: 33 additions & 0 deletions state/context.go
@@ -0,0 +1,33 @@
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package state

import "context"

type runActionsCtxKey struct{}

// RunActionsCtx provides the runactions with auxiliary information.
type RunActionsCtx struct {
// producer who compose those actions
ProducerAddr string

// gas Limit for perform those actions
GasLimit *uint64

// whether disable gas charge
EnableGasCharge bool
}

// WithRunActionsCtx add RunActionsCtx into context.
func WithRunActionsCtx(ctx context.Context, ra RunActionsCtx) context.Context {
return context.WithValue(ctx, runActionsCtxKey{}, ra)
}

func getRunActionsCtx(ctx context.Context) (RunActionsCtx, bool) {
ra, ok := ctx.Value(runActionsCtxKey{}).(RunActionsCtx)
return ra, ok
}

0 comments on commit a4c1d6d

Please sign in to comment.