Skip to content

Commit

Permalink
[state] convert clean address to zero-nonce type (#3991)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Jan 29, 2024
1 parent 6b429b4 commit 593dba7
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 12 deletions.
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type (
FixContractStakingWeightedVotes bool
SharedGasWithDapp bool
ExecutionSizeLimit32KB bool
UseZeroNonceForFreshAccount bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -255,6 +256,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
FixContractStakingWeightedVotes: g.IsRedsea(height),
SharedGasWithDapp: g.IsToBeEnabled(height),
ExecutionSizeLimit32KB: !g.IsToBeEnabled(height),
UseZeroNonceForFreshAccount: g.IsToBeEnabled(height),
},
)
}
Expand Down
12 changes: 10 additions & 2 deletions action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@ func (v *GenericValidator) Validate(ctx context.Context, selp action.SealedEnvel
return action.ErrSystemActionNonce
}
} else {
featureCtx, ok := GetFeatureCtx(ctx)
var (
nonce uint64
featureCtx, ok = GetFeatureCtx(ctx)
)
if ok && featureCtx.FixGasAndNonceUpdate || selp.Nonce() != 0 {
confirmedState, err := v.accountState(ctx, v.sr, caller)
if err != nil {
return errors.Wrapf(err, "invalid state of account %s", caller.String())
}
if confirmedState.PendingNonce() > selp.Nonce() {
if featureCtx.UseZeroNonceForFreshAccount {
nonce = confirmedState.PendingNonceConsideringFreshAccount()
} else {
nonce = confirmedState.PendingNonce()
}
if nonce > selp.Nonce() {
return action.ErrNonceTooLow
}
}
Expand Down
11 changes: 9 additions & 2 deletions actpool/actpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ func (ap *actPool) GetPendingNonce(addrStr string) (uint64, error) {
if err != nil {
return 0, err
}
return confirmedState.PendingNonce(), err
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
return confirmedState.PendingNonceConsideringFreshAccount(), nil
}
return confirmedState.PendingNonce(), nil
}

// GetUnconfirmedActs returns unconfirmed actions in pool given an account address
Expand Down Expand Up @@ -424,7 +427,11 @@ func (ap *actPool) removeInvalidActs(acts []action.SealedEnvelope) {
}

func (ap *actPool) context(ctx context.Context) context.Context {
return genesis.WithGenesisContext(ctx, ap.g)
height, _ := ap.sf.Height()
return protocol.WithFeatureCtx(protocol.WithBlockCtx(
genesis.WithGenesisContext(ctx, ap.g), protocol.BlockCtx{
BlockHeight: height + 1,
}))
}

func (ap *actPool) enqueue(ctx context.Context, act action.SealedEnvelope, replace bool) error {
Expand Down
6 changes: 5 additions & 1 deletion actpool/actpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ func TestActPool_GetSize(t *testing.T) {
func TestActPool_AddActionNotEnoughGasPrice(t *testing.T) {
ctrl := gomock.NewController(t)
sf := mock_chainmanager.NewMockStateReader(ctrl)
sf.EXPECT().Height().Return(uint64(0), nil).Times(2)

apConfig := DefaultConfig
ap, err := NewActPool(genesis.Default, sf, apConfig)
Expand All @@ -1056,7 +1057,10 @@ func TestActPool_AddActionNotEnoughGasPrice(t *testing.T) {
require.NoError(t, err)

ctx := protocol.WithBlockchainCtx(context.Background(), protocol.BlockchainCtx{})
ctx = genesis.WithGenesisContext(ctx, genesis.Default)
ctx = protocol.WithFeatureCtx(protocol.WithBlockCtx(
genesis.WithGenesisContext(ctx, genesis.Default), protocol.BlockCtx{
BlockHeight: 1,
}))
require.Error(t, ap.Add(ctx, tsf))
}

Expand Down
8 changes: 7 additions & 1 deletion actpool/actqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/iotexproject/iotex-address/address"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/pkg/log"
)
Expand Down Expand Up @@ -283,10 +284,15 @@ func (q *actQueue) PendingActs(ctx context.Context) []action.SealedEnvelope {
}

var (
nonce = confirmedState.PendingNonce()
nonce uint64
balance = new(big.Int).Set(confirmedState.Balance)
acts = make([]action.SealedEnvelope, 0, len(q.items))
)
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
nonce = confirmedState.PendingNonceConsideringFreshAccount()
} else {
nonce = confirmedState.PendingNonce()
}
q.mu.RLock()
defer q.mu.RUnlock()
for ; ; nonce++ {
Expand Down
5 changes: 4 additions & 1 deletion actpool/actqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ func TestActQueuePendingActs(t *testing.T) {
accountState.Balance = big.NewInt(maxBalance)
}).Return(uint64(0), nil).Times(1)
sf.EXPECT().Height().Return(uint64(1), nil).AnyTimes()
ctx := genesis.WithGenesisContext(context.Background(), genesis.Default)
ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(
genesis.WithGenesisContext(context.Background(), genesis.Default), protocol.BlockCtx{
BlockHeight: 1,
}))
ap, err := NewActPool(genesis.Default, sf, DefaultConfig)
require.NoError(err)
q := NewActQueue(ap.(*actPool), identityset.Address(0).String(), 1, big.NewInt(maxBalance)).(*actQueue)
Expand Down
9 changes: 8 additions & 1 deletion actpool/queueworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/tracer"
Expand Down Expand Up @@ -144,7 +145,13 @@ func (worker *queueWorker) getConfirmedState(ctx context.Context, sender address
if err != nil {
return 0, nil, err
}
return confirmedState.PendingNonce(), confirmedState.Balance, nil
var nonce uint64
if protocol.MustGetFeatureCtx(ctx).UseZeroNonceForFreshAccount {
nonce = confirmedState.PendingNonceConsideringFreshAccount()
} else {
nonce = confirmedState.PendingNonce()
}
return nonce, confirmedState.Balance, nil
}
nonce, balance := queue.AccountState()
return nonce, balance, nil
Expand Down
2 changes: 1 addition & 1 deletion api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (svr *web3Handler) sendRawTransaction(in *gjson.Result) (interface{}, error
pubkey crypto.PublicKey
err error
)
if g := cs.Genesis(); g.IsSumatra(cs.TipHeight()) {
if g := cs.Genesis(); g.IsToBeEnabled(cs.TipHeight()) {
tx, err = action.DecodeEtherTx(dataStr.String())
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 593dba7

Please sign in to comment.