From 064dbd1043d3781af281d9edd11ba6f8ab997b70 Mon Sep 17 00:00:00 2001 From: envestcc Date: Fri, 31 Mar 2023 21:27:23 +0800 Subject: [PATCH 1/7] validate system action layout --- state/factory/workingset.go | 39 +++++++++++- state/factory/workingset_test.go | 105 ++++++++++++++++++++++++++++--- 2 files changed, 135 insertions(+), 9 deletions(-) diff --git a/state/factory/workingset.go b/state/factory/workingset.go index b47f3cee0d..0ee5c79e89 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -37,6 +37,7 @@ var ( ) errUnsupportWeb3Rewarding = errors.New("unsupported web3 rewarding") + errSystemActionLayout = errors.New("system action layout is incorrect") ) func init() { @@ -394,7 +395,6 @@ func (ws *workingSet) process(ctx context.Context, actions []action.SealedEnvelo } } } - // TODO: verify whether the post system actions are appended tail receipts, err := ws.runActions(ctx, actions) if err != nil { @@ -404,6 +404,40 @@ func (ws *workingSet) process(ctx context.Context, actions []action.SealedEnvelo return ws.finalize() } +func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions []action.SealedEnvelope) error { + reg := protocol.MustGetRegistry(ctx) + postSystemActions := []action.Envelope{} + for _, p := range reg.All() { + if psc, ok := p.(protocol.PostSystemActionsCreator); ok { + elps, err := psc.CreatePostSystemActions(ctx, ws) + if err != nil { + return err + } + postSystemActions = append(postSystemActions, elps...) + } + } + receivedSystemActions := []action.Envelope{} + startIdx := len(actions) + for i := range actions { + if action.IsSystemAction(actions[i]) { + if startIdx == len(actions) { + startIdx = i + } + receivedSystemActions = append(receivedSystemActions, actions[i].Envelope) + } + } + if len(receivedSystemActions) != len(postSystemActions) || + startIdx+len(postSystemActions) != len(actions) { + return errors.Wrapf(errSystemActionLayout, "systen actions start at index %d with length %d, expected length is %d", startIdx, len(receivedSystemActions), len(postSystemActions)) + } + for i := range receivedSystemActions { + if receivedSystemActions[i].Proto().String() != postSystemActions[i].Proto().String() { + return errors.Wrapf(errSystemActionLayout, "the system action of index %d is incorrect", i) + } + } + return nil +} + func (ws *workingSet) pickAndRunActions( ctx context.Context, ap actpool.ActPool, @@ -520,6 +554,9 @@ func (ws *workingSet) ValidateBlock(ctx context.Context, blk *block.Block) error if err := ws.validateNonce(ctx, blk); err != nil { return errors.Wrap(err, "failed to validate nonce") } + if err := ws.validateSystemActionLayout(ctx, blk.RunnableActions().Actions()); err != nil { + return err + } if err := ws.process(ctx, blk.RunnableActions().Actions()); err != nil { log.L().Error("Failed to update state.", zap.Uint64("height", ws.height), zap.Error(err)) return err diff --git a/state/factory/workingset_test.go b/state/factory/workingset_test.go index 56b8d5ae33..92da6dc8f2 100644 --- a/state/factory/workingset_test.go +++ b/state/factory/workingset_test.go @@ -208,19 +208,19 @@ func TestWorkingSet_ValidateBlock(t *testing.T) { err error }{ { - makeBlock(t, 1, hash.ZeroHash256, receiptRoot, digestHash), + makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, makeTransferAction(t, 1)), nil, }, { - makeBlock(t, 3, hash.ZeroHash256, receiptRoot, digestHash), + makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, makeTransferAction(t, 3)), action.ErrNonceTooHigh, }, { - makeBlock(t, 1, hash.ZeroHash256, hash.Hash256b([]byte("test")), digestHash), + makeBlock(t, hash.ZeroHash256, hash.Hash256b([]byte("test")), digestHash, makeTransferAction(t, 1)), block.ErrReceiptRootMismatch, }, { - makeBlock(t, 1, hash.ZeroHash256, receiptRoot, hash.Hash256b([]byte("test"))), + makeBlock(t, hash.ZeroHash256, receiptRoot, hash.Hash256b([]byte("test")), makeTransferAction(t, 1)), block.ErrDeltaStateMismatch, }, } @@ -254,8 +254,80 @@ func TestWorkingSet_ValidateBlock(t *testing.T) { } } -func makeBlock(t *testing.T, nonce uint64, prevHash hash.Hash256, receiptRoot hash.Hash256, digest hash.Hash256) *block.Block { - var sevlps []action.SealedEnvelope +func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { + require := require.New(t) + cfg := Config{ + Chain: blockchain.DefaultConfig, + Genesis: genesis.TestDefault(), + } + cfg.Genesis.InitBalanceMap[identityset.Address(28).String()] = "100000000" + registry := protocol.NewRegistry() + require.NoError(account.NewProtocol(rewarding.DepositGas).Register(registry)) + require.NoError(rewarding.NewProtocol(cfg.Genesis.Rewarding).Register(registry)) + var ( + f1, _ = NewFactory(cfg, db.NewMemKVStore(), RegistryOption(registry)) + f2, _ = NewStateDB(cfg, db.NewMemKVStore(), RegistryStateDBOption(registry)) + factories = []Factory{f1, f2} + ) + + ctx := protocol.WithBlockCtx( + genesis.WithGenesisContext(context.Background(), cfg.Genesis), + protocol.BlockCtx{}, + ) + require.NoError(f1.Start(ctx)) + require.NoError(f2.Start(ctx)) + defer func() { + require.NoError(f1.Stop(ctx)) + require.NoError(f2.Stop(ctx)) + }() + + zctx := protocol.WithBlockCtx(context.Background(), + protocol.BlockCtx{ + BlockHeight: uint64(1), + Producer: identityset.Address(27), + GasLimit: testutil.TestGasLimit * 100000, + }) + zctx = genesis.WithGenesisContext(zctx, cfg.Genesis) + zctx = protocol.WithFeatureCtx(protocol.WithBlockchainCtx(zctx, protocol.BlockchainCtx{ + ChainID: 1, + })) + + t.Run("missing system action", func(t *testing.T) { + digestHash, err := hash.HexStringToHash256("8f9b7694c325a4f4b0065cd382f8af0a4e913113a4ce7ef1ac899f96158c74f4") + require.NoError(err) + receiptRoot, err := hash.HexStringToHash256("f04673451e31386a8fddfcf7750665bfcf33f239f6c4919430bb11a144e1aa95") + require.NoError(err) + actions := []action.SealedEnvelope{makeTransferAction(t, 1)} + for _, f := range factories { + block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) + require.Equal(errSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + } + }) + t.Run("correct system action", func(t *testing.T) { + digestHash, err := hash.HexStringToHash256("ade24a5c647b5af34c4e74fe0d8f1fa410f6fb115f8fc2d39e45ca2f895de9ca") + require.NoError(err) + receiptRoot, err := hash.HexStringToHash256("0aa4c3109e75d3da8c6b1cb51720a769d7a3a8c735247063cede8e3ecf90ed62") + require.NoError(err) + actions := []action.SealedEnvelope{makeTransferAction(t, 1), makeRewardAction(t)} + for _, f := range factories { + block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) + require.Equal(nil, errors.Cause(f.Validate(zctx, block))) + } + }) + t.Run("postiche system action", func(t *testing.T) { + digestHash, err := hash.HexStringToHash256("ade24a5c647b5af34c4e74fe0d8f1fa410f6fb115f8fc2d39e45ca2f895de9ca") + require.NoError(err) + receiptRoot, err := hash.HexStringToHash256("0aa4c3109e75d3da8c6b1cb51720a769d7a3a8c735247063cede8e3ecf90ed62") + require.NoError(err) + actions := []action.SealedEnvelope{makeTransferAction(t, 1), makeRewardAction(t), makeRewardAction(t)} + for _, f := range factories { + block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) + require.Equal(errSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + } + }) +} + +func makeTransferAction(t *testing.T, nonce uint64) action.SealedEnvelope { tsf, err := action.NewTransfer( uint64(nonce), big.NewInt(1), @@ -275,9 +347,26 @@ func makeBlock(t *testing.T, nonce uint64, prevHash hash.Hash256, receiptRoot ha Build() sevlp, err := action.Sign(evlp, identityset.PrivateKey(28)) require.NoError(t, err) - sevlps = append(sevlps, sevlp) + return sevlp +} + +func makeRewardAction(t *testing.T) action.SealedEnvelope { + gb := action.GrantRewardBuilder{} + grant := gb.SetRewardType(action.BlockReward).SetHeight(1).Build() + eb2 := action.EnvelopeBuilder{} + evlp := eb2.SetNonce(0). + SetGasPrice(big.NewInt(0)). + SetGasLimit(grant.GasLimit()). + SetAction(&grant). + Build() + sevlp, err := action.Sign(evlp, identityset.PrivateKey(28)) + require.NoError(t, err) + return sevlp +} + +func makeBlock(t *testing.T, prevHash hash.Hash256, receiptRoot hash.Hash256, digest hash.Hash256, actions ...action.SealedEnvelope) *block.Block { rap := block.RunnableActionsBuilder{} - ra := rap.AddActions(sevlps...).Build() + ra := rap.AddActions(actions...).Build() blk, err := block.NewBuilder(ra). SetHeight(1). SetTimestamp(time.Now()). From c5980a2e1ebe22bac3e22eef9c21d3b271170e8b Mon Sep 17 00:00:00 2001 From: envestcc Date: Fri, 31 Mar 2023 21:45:01 +0800 Subject: [PATCH 2/7] rename --- state/factory/workingset.go | 8 ++++---- state/factory/workingset_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/state/factory/workingset.go b/state/factory/workingset.go index 0ee5c79e89..41a9d3bdb4 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -36,8 +36,8 @@ var ( []string{"type"}, ) - errUnsupportWeb3Rewarding = errors.New("unsupported web3 rewarding") - errSystemActionLayout = errors.New("system action layout is incorrect") + errUnsupportWeb3Rewarding = errors.New("unsupported web3 rewarding") + errInvalidSystemActionLayout = errors.New("system action layout is invalid") ) func init() { @@ -428,11 +428,11 @@ func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions [] } if len(receivedSystemActions) != len(postSystemActions) || startIdx+len(postSystemActions) != len(actions) { - return errors.Wrapf(errSystemActionLayout, "systen actions start at index %d with length %d, expected length is %d", startIdx, len(receivedSystemActions), len(postSystemActions)) + return errors.Wrapf(errInvalidSystemActionLayout, "systen actions start at index %d with length %d, expected length is %d", startIdx, len(receivedSystemActions), len(postSystemActions)) } for i := range receivedSystemActions { if receivedSystemActions[i].Proto().String() != postSystemActions[i].Proto().String() { - return errors.Wrapf(errSystemActionLayout, "the system action of index %d is incorrect", i) + return errors.Wrapf(errInvalidSystemActionLayout, "the system action of index %d is incorrect", i) } } return nil diff --git a/state/factory/workingset_test.go b/state/factory/workingset_test.go index 92da6dc8f2..c433336adc 100644 --- a/state/factory/workingset_test.go +++ b/state/factory/workingset_test.go @@ -300,7 +300,7 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { actions := []action.SealedEnvelope{makeTransferAction(t, 1)} for _, f := range factories { block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) - require.Equal(errSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + require.Equal(errInvalidSystemActionLayout, errors.Cause(f.Validate(zctx, block))) } }) t.Run("correct system action", func(t *testing.T) { @@ -322,7 +322,7 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { actions := []action.SealedEnvelope{makeTransferAction(t, 1), makeRewardAction(t), makeRewardAction(t)} for _, f := range factories { block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) - require.Equal(errSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + require.Equal(errInvalidSystemActionLayout, errors.Cause(f.Validate(zctx, block))) } }) } From d80d340e291319d9738e0d1e5855e57a2184098d Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 4 Apr 2023 12:30:42 +0800 Subject: [PATCH 3/7] add comments and refactor generate system actions --- state/factory/workingset.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/state/factory/workingset.go b/state/factory/workingset.go index 41a9d3bdb4..bad670dd87 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -404,18 +404,27 @@ func (ws *workingSet) process(ctx context.Context, actions []action.SealedEnvelo return ws.finalize() } -func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions []action.SealedEnvelope) error { +func (ws *workingSet) generateSystemActions(ctx context.Context) ([]action.Envelope, error) { reg := protocol.MustGetRegistry(ctx) postSystemActions := []action.Envelope{} for _, p := range reg.All() { if psc, ok := p.(protocol.PostSystemActionsCreator); ok { elps, err := psc.CreatePostSystemActions(ctx, ws) if err != nil { - return err + return nil, err } postSystemActions = append(postSystemActions, elps...) } } + return postSystemActions, nil +} + +// validateSystemActionLayout verify whether the post system actions are appended tail +func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions []action.SealedEnvelope) error { + postSystemActions, err := ws.generateSystemActions(ctx) + if err != nil { + return err + } receivedSystemActions := []action.Envelope{} startIdx := len(actions) for i := range actions { @@ -426,10 +435,12 @@ func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions [] receivedSystemActions = append(receivedSystemActions, actions[i].Envelope) } } + // system actions should be at the end of the action list, and they should be continuous if len(receivedSystemActions) != len(postSystemActions) || startIdx+len(postSystemActions) != len(actions) { return errors.Wrapf(errInvalidSystemActionLayout, "systen actions start at index %d with length %d, expected length is %d", startIdx, len(receivedSystemActions), len(postSystemActions)) } + // verify each system action for i := range receivedSystemActions { if receivedSystemActions[i].Proto().String() != postSystemActions[i].Proto().String() { return errors.Wrapf(errInvalidSystemActionLayout, "the system action of index %d is incorrect", i) From c03d885347bee81c3b7e964d6cf818645c5383c5 Mon Sep 17 00:00:00 2001 From: envestcc Date: Tue, 4 Apr 2023 15:09:21 +0800 Subject: [PATCH 4/7] refactor validate system action and add tests for it --- state/factory/workingset.go | 26 +++++++++++--------------- state/factory/workingset_test.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/state/factory/workingset.go b/state/factory/workingset.go index bad670dd87..ef6dec534a 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -425,26 +425,22 @@ func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions [] if err != nil { return err } - receivedSystemActions := []action.Envelope{} - startIdx := len(actions) + // system actions should be at the end of the action list, and they should be continuous + expectedStartIdx := len(actions) - len(postSystemActions) + offset := 0 for i := range actions { if action.IsSystemAction(actions[i]) { - if startIdx == len(actions) { - startIdx = i + if i != expectedStartIdx+offset { + return errors.Wrapf(errInvalidSystemActionLayout, "the action of idx %d should not be a system action", i) + } + if actions[i].Envelope.Proto().String() != postSystemActions[offset].Proto().String() { + return errors.Wrapf(errInvalidSystemActionLayout, "the action of idx %d is not the expected system action", i) } - receivedSystemActions = append(receivedSystemActions, actions[i].Envelope) + offset++ } } - // system actions should be at the end of the action list, and they should be continuous - if len(receivedSystemActions) != len(postSystemActions) || - startIdx+len(postSystemActions) != len(actions) { - return errors.Wrapf(errInvalidSystemActionLayout, "systen actions start at index %d with length %d, expected length is %d", startIdx, len(receivedSystemActions), len(postSystemActions)) - } - // verify each system action - for i := range receivedSystemActions { - if receivedSystemActions[i].Proto().String() != postSystemActions[i].Proto().String() { - return errors.Wrapf(errInvalidSystemActionLayout, "the system action of index %d is incorrect", i) - } + if offset != len(postSystemActions) { + return errors.Wrapf(errInvalidSystemActionLayout, "the number of system actions is incorrect, expected %d", len(postSystemActions)) } return nil } diff --git a/state/factory/workingset_test.go b/state/factory/workingset_test.go index c433336adc..101033a332 100644 --- a/state/factory/workingset_test.go +++ b/state/factory/workingset_test.go @@ -300,7 +300,18 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { actions := []action.SealedEnvelope{makeTransferAction(t, 1)} for _, f := range factories { block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) - require.Equal(errInvalidSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + require.ErrorIs(f.Validate(zctx, block), errInvalidSystemActionLayout) + } + }) + t.Run("system action not on tail", func(t *testing.T) { + digestHash, err := hash.HexStringToHash256("8f9b7694c325a4f4b0065cd382f8af0a4e913113a4ce7ef1ac899f96158c74f4") + require.NoError(err) + receiptRoot, err := hash.HexStringToHash256("f04673451e31386a8fddfcf7750665bfcf33f239f6c4919430bb11a144e1aa95") + require.NoError(err) + actions := []action.SealedEnvelope{makeRewardAction(t), makeTransferAction(t, 1)} + for _, f := range factories { + block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) + require.ErrorIs(f.Validate(zctx, block), errInvalidSystemActionLayout) } }) t.Run("correct system action", func(t *testing.T) { @@ -311,7 +322,7 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { actions := []action.SealedEnvelope{makeTransferAction(t, 1), makeRewardAction(t)} for _, f := range factories { block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) - require.Equal(nil, errors.Cause(f.Validate(zctx, block))) + require.ErrorIs(f.Validate(zctx, block), nil) } }) t.Run("postiche system action", func(t *testing.T) { @@ -322,7 +333,20 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { actions := []action.SealedEnvelope{makeTransferAction(t, 1), makeRewardAction(t), makeRewardAction(t)} for _, f := range factories { block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) - require.Equal(errInvalidSystemActionLayout, errors.Cause(f.Validate(zctx, block))) + require.ErrorIs(f.Validate(zctx, block), errInvalidSystemActionLayout) + } + }) + t.Run("inconsistent system action", func(t *testing.T) { + digestHash, err := hash.HexStringToHash256("8f9b7694c325a4f4b0065cd382f8af0a4e913113a4ce7ef1ac899f96158c74f4") + require.NoError(err) + receiptRoot, err := hash.HexStringToHash256("f04673451e31386a8fddfcf7750665bfcf33f239f6c4919430bb11a144e1aa95") + require.NoError(err) + rewardAct := makeRewardAction(t) + rewardAct.SetNonce(2) + actions := []action.SealedEnvelope{makeTransferAction(t, 1), rewardAct} + for _, f := range factories { + block := makeBlock(t, hash.ZeroHash256, receiptRoot, digestHash, actions...) + require.ErrorIs(f.Validate(zctx, block), errInvalidSystemActionLayout) } }) } From c7bf980927dd12896fb890e02a5730c2a666bf67 Mon Sep 17 00:00:00 2001 From: envestcc Date: Thu, 6 Apr 2023 15:45:31 +0800 Subject: [PATCH 5/7] reduce code redundancy --- state/factory/factory.go | 22 +++++++++------------- state/factory/statedb.go | 22 +++++++++------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/state/factory/factory.go b/state/factory/factory.go index a48f30e10e..982cf4cd5f 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -359,20 +359,16 @@ func (sf *factory) NewBlockBuilder( return nil, errors.Wrap(err, "Failed to obtain working set from state factory") } postSystemActions := make([]action.SealedEnvelope, 0) - for _, p := range sf.registry.All() { - if psac, ok := p.(protocol.PostSystemActionsCreator); ok { - elps, err := psac.CreatePostSystemActions(ctx, ws) - if err != nil { - return nil, err - } - for _, elp := range elps { - se, err := sign(elp) - if err != nil { - return nil, err - } - postSystemActions = append(postSystemActions, se) - } + unsignedSystemActions, err := ws.generateSystemActions(ctx) + if err != nil { + return nil, err + } + for _, elp := range unsignedSystemActions { + se, err := sign(elp) + if err != nil { + return nil, err } + postSystemActions = append(postSystemActions, se) } blkBuilder, err := ws.CreateBuilder(ctx, ap, postSystemActions, sf.cfg.Chain.AllowedBlockGasResidue) if err != nil { diff --git a/state/factory/statedb.go b/state/factory/statedb.go index 6f676c75de..46dea6893d 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -239,20 +239,16 @@ func (sdb *stateDB) NewBlockBuilder( return nil, err } postSystemActions := make([]action.SealedEnvelope, 0) - for _, p := range sdb.registry.All() { - if psac, ok := p.(protocol.PostSystemActionsCreator); ok { - elps, err := psac.CreatePostSystemActions(ctx, ws) - if err != nil { - return nil, err - } - for _, elp := range elps { - se, err := sign(elp) - if err != nil { - return nil, err - } - postSystemActions = append(postSystemActions, se) - } + unsignedSystemActions, err := ws.generateSystemActions(ctx) + if err != nil { + return nil, err + } + for _, elp := range unsignedSystemActions { + se, err := sign(elp) + if err != nil { + return nil, err } + postSystemActions = append(postSystemActions, se) } blkBuilder, err := ws.CreateBuilder(ctx, ap, postSystemActions, sdb.cfg.Chain.AllowedBlockGasResidue) if err != nil { From 60b15886eaa497dcc3ce1d91c9a886b178acacb5 Mon Sep 17 00:00:00 2001 From: envestcc Date: Thu, 6 Apr 2023 16:13:50 +0800 Subject: [PATCH 6/7] add hard-fork --- action/protocol/context.go | 2 ++ state/factory/workingset.go | 7 +++++-- state/factory/workingset_test.go | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/action/protocol/context.go b/action/protocol/context.go index 4d724cce9a..df6d91ef27 100644 --- a/action/protocol/context.go +++ b/action/protocol/context.go @@ -111,6 +111,7 @@ type ( FixRewardErroCheckPosition bool EnableWeb3Rewarding bool EnableNodeInfo bool + ValidateSystemAction bool } // FeatureWithHeightCtx provides feature check functions. @@ -248,6 +249,7 @@ func WithFeatureCtx(ctx context.Context) context.Context { FixRewardErroCheckPosition: g.IsOkhotsk(height), EnableWeb3Rewarding: g.IsPalau(height), EnableNodeInfo: g.IsPalau(height), + ValidateSystemAction: g.IsToBeEnabled(height), }, ) } diff --git a/state/factory/workingset.go b/state/factory/workingset.go index ef6dec534a..a0e2278ee1 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -561,9 +561,12 @@ func (ws *workingSet) ValidateBlock(ctx context.Context, blk *block.Block) error if err := ws.validateNonce(ctx, blk); err != nil { return errors.Wrap(err, "failed to validate nonce") } - if err := ws.validateSystemActionLayout(ctx, blk.RunnableActions().Actions()); err != nil { - return err + if protocol.MustGetFeatureCtx(ctx).ValidateSystemAction { + if err := ws.validateSystemActionLayout(ctx, blk.RunnableActions().Actions()); err != nil { + return err + } } + if err := ws.process(ctx, blk.RunnableActions().Actions()); err != nil { log.L().Error("Failed to update state.", zap.Uint64("height", ws.height), zap.Error(err)) return err diff --git a/state/factory/workingset_test.go b/state/factory/workingset_test.go index 101033a332..05b599be45 100644 --- a/state/factory/workingset_test.go +++ b/state/factory/workingset_test.go @@ -260,6 +260,7 @@ func TestWorkingSet_ValidateBlock_SystemAction(t *testing.T) { Chain: blockchain.DefaultConfig, Genesis: genesis.TestDefault(), } + cfg.Genesis.ToBeEnabledBlockHeight = 1 // enable validate system action cfg.Genesis.InitBalanceMap[identityset.Address(28).String()] = "100000000" registry := protocol.NewRegistry() require.NoError(account.NewProtocol(rewarding.DepositGas).Register(registry)) From fd6e6bf73a38af7394c99e45ea7fb1bef6ad5d73 Mon Sep 17 00:00:00 2001 From: envestcc Date: Wed, 12 Apr 2023 21:21:48 +0800 Subject: [PATCH 7/7] prettify --- state/factory/workingset.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/state/factory/workingset.go b/state/factory/workingset.go index a0e2278ee1..c9644d84d0 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -427,20 +427,20 @@ func (ws *workingSet) validateSystemActionLayout(ctx context.Context, actions [] } // system actions should be at the end of the action list, and they should be continuous expectedStartIdx := len(actions) - len(postSystemActions) - offset := 0 + sysActCnt := 0 for i := range actions { if action.IsSystemAction(actions[i]) { - if i != expectedStartIdx+offset { - return errors.Wrapf(errInvalidSystemActionLayout, "the action of idx %d should not be a system action", i) + if i != expectedStartIdx+sysActCnt { + return errors.Wrapf(errInvalidSystemActionLayout, "the %d-th action should not be a system action", i) } - if actions[i].Envelope.Proto().String() != postSystemActions[offset].Proto().String() { - return errors.Wrapf(errInvalidSystemActionLayout, "the action of idx %d is not the expected system action", i) + if actions[i].Envelope.Proto().String() != postSystemActions[sysActCnt].Proto().String() { + return errors.Wrapf(errInvalidSystemActionLayout, "the %d-th action is not the expected system action", i) } - offset++ + sysActCnt++ } } - if offset != len(postSystemActions) { - return errors.Wrapf(errInvalidSystemActionLayout, "the number of system actions is incorrect, expected %d", len(postSystemActions)) + if sysActCnt != len(postSystemActions) { + return errors.Wrapf(errInvalidSystemActionLayout, "the number of system actions is incorrect, expected %d, got %d", len(postSystemActions), sysActCnt) } return nil }