Skip to content

Commit

Permalink
skip nonce and gas
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi committed May 15, 2022
1 parent f5b7022 commit d63b9a1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
5 changes: 1 addition & 4 deletions action/protocol/account/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ func SetNonce(state *state.Account, nonce uint64) error {
state.SetNonce(nonce)
return nil
}
if nonce != 0 { // for system actions
return errors.Errorf("invalid nonce %d, expect %d", nonce, state.PendingNonce())
}

return nil
return errors.Errorf("invalid nonce %d, expect %d", nonce, state.PendingNonce())
}

// LoadOrCreateAccount either loads an account state or creates an account state
Expand Down
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type (
EnableWeb3Staking bool
ValidateRewardProtocol bool
CreateZeroNonceAccount bool
SkipUpdateForSystemAction bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -236,6 +237,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
EnableWeb3Staking: g.IsNewfoundland(height),
ValidateRewardProtocol: g.IsNewfoundland(height),
CreateZeroNonceAccount: g.IsToBeEnabled(height),
SkipUpdateForSystemAction: g.IsToBeEnabled(height),
},
)
}
Expand Down
73 changes: 50 additions & 23 deletions action/protocol/rewarding/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func createGrantRewardAction(rewardType int, height uint64) action.Envelope {
Build()
}

// Validate validates a reward action
// Validate validates a reward action
func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error {
if !protocol.MustGetFeatureCtx(ctx).ValidateRewardProtocol {
return nil
Expand Down Expand Up @@ -214,38 +214,38 @@ func (p *Protocol) Handle(
rlog, err := p.Deposit(ctx, sm, act.Amount(), iotextypes.TransactionLogType_DEPOSIT_TO_REWARDING_FUND)
if err != nil {
log.L().Debug("Error when handling rewarding action", zap.Error(err))
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
return p.settleUserAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
}
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
return p.settleUserAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
case *action.ClaimFromRewardingFund:
si := sm.Snapshot()
rlog, err := p.Claim(ctx, sm, act.Amount())
if err != nil {
log.L().Debug("Error when handling rewarding action", zap.Error(err))
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
return p.settleUserAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
}
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
return p.settleUserAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil, rlog)
case *action.GrantReward:
switch act.RewardType() {
case action.BlockReward:
si := sm.Snapshot()
rewardLog, err := p.GrantBlockReward(ctx, sm)
if err != nil {
log.L().Debug("Error when handling rewarding action", zap.Error(err))
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
return p.settleSystemAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
}
if rewardLog == nil {
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil)
return p.settleSystemAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, nil)
}
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, []*action.Log{rewardLog})
return p.settleSystemAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, []*action.Log{rewardLog})
case action.EpochReward:
si := sm.Snapshot()
rewardLogs, err := p.GrantEpochReward(ctx, sm)
if err != nil {
log.L().Debug("Error when handling rewarding action", zap.Error(err))
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
return p.settleSystemAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Failure), si, nil)
}
return p.settleAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rewardLogs)
return p.settleSystemAction(ctx, sm, uint64(iotextypes.ReceiptStatus_Success), si, rewardLogs)
}
}
return nil, nil
Expand Down Expand Up @@ -381,11 +381,34 @@ func (p *Protocol) deleteStateV2(sm protocol.StateManager, key []byte) error {
return err
}

func (p *Protocol) settleSystemAction(
ctx context.Context,
sm protocol.StateManager,
status uint64,
si int,
logs []*action.Log,
tLogs ...*action.TransactionLog,
) (*action.Receipt, error) {
return p.settleAction(ctx, sm, status, si, true, logs, tLogs...)
}

func (p *Protocol) settleUserAction(
ctx context.Context,
sm protocol.StateManager,
status uint64,
si int,
logs []*action.Log,
tLogs ...*action.TransactionLog,
) (*action.Receipt, error) {
return p.settleAction(ctx, sm, status, si, false, logs, tLogs...)
}

func (p *Protocol) settleAction(
ctx context.Context,
sm protocol.StateManager,
status uint64,
si int,
isSystemAction bool,
logs []*action.Log,
tLogs ...*action.TransactionLog,
) (*action.Receipt, error) {
Expand All @@ -396,21 +419,23 @@ func (p *Protocol) settleAction(
return nil, err
}
}
gasFee := big.NewInt(0).Mul(actionCtx.GasPrice, big.NewInt(0).SetUint64(actionCtx.IntrinsicGas))
depositLog, err := DepositGas(ctx, sm, gasFee)
if err != nil {
return nil, err
}
if depositLog != nil {
tLogs = append(tLogs, depositLog)
}
if err := p.increaseNonce(ctx, sm, actionCtx.Caller, actionCtx.Nonce); err != nil {
return nil, err
if !isSystemAction || !protocol.MustGetFeatureCtx(ctx).SkipUpdateForSystemAction {
gasFee := big.NewInt(0).Mul(actionCtx.GasPrice, big.NewInt(0).SetUint64(actionCtx.IntrinsicGas))
depositLog, err := DepositGas(ctx, sm, gasFee)
if err != nil {
return nil, err
}
if depositLog != nil {
tLogs = append(tLogs, depositLog)
}
if err := p.increaseNonce(ctx, sm, actionCtx.Caller, actionCtx.Nonce, isSystemAction); err != nil {
return nil, err
}
}
return p.createReceipt(status, blkCtx.BlockHeight, actionCtx.ActionHash, actionCtx.IntrinsicGas, logs, tLogs...), nil
}

func (p *Protocol) increaseNonce(ctx context.Context, sm protocol.StateManager, addr address.Address, nonce uint64) error {
func (p *Protocol) increaseNonce(ctx context.Context, sm protocol.StateManager, addr address.Address, nonce uint64, isSystemAction bool) error {
accountCreationOpts := []accountutil.AccountCreationOption{}
if protocol.MustGetFeatureCtx(ctx).CreateZeroNonceAccount {
accountCreationOpts = append(accountCreationOpts, accountutil.ZeroNonceAccountTypeOption())
Expand All @@ -419,8 +444,10 @@ func (p *Protocol) increaseNonce(ctx context.Context, sm protocol.StateManager,
if err != nil {
return err
}
if err := accountutil.SetNonce(acc, nonce); err != nil {
return errors.Wrapf(err, "invalid nonce %d", nonce)
if !isSystemAction || nonce != 0 {
if err := accountutil.SetNonce(acc, nonce); err != nil {
return errors.Wrapf(err, "invalid nonce %d", nonce)
}
}
return accountutil.StoreAccount(sm, addr, acc)
}
Expand Down
6 changes: 3 additions & 3 deletions state/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func testState(sf Factory, t *testing.T) {
tsf, err := action.NewTransfer(1, big.NewInt(10), identityset.Address(31).String(), nil, uint64(20000), big.NewInt(0))
require.NoError(t, err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetAction(tsf).SetGasLimit(20000).Build()
elp := bd.SetAction(tsf).SetGasLimit(20000).SetNonce(1).Build()
selp, err := action.Sign(elp, priKeyA)
require.NoError(t, err)
ctx = protocol.WithBlockCtx(
Expand Down Expand Up @@ -511,7 +511,7 @@ func testHistoryState(sf Factory, t *testing.T, statetx, archive bool) {
tsf, err := action.NewTransfer(1, big.NewInt(10), b.String(), nil, uint64(20000), big.NewInt(0))
require.NoError(t, err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetAction(tsf).SetGasLimit(20000).Build()
elp := bd.SetAction(tsf).SetGasLimit(20000).SetNonce(1).Build()
selp, err := action.Sign(elp, priKeyA)
require.NoError(t, err)
ctx = protocol.WithBlockCtx(
Expand Down Expand Up @@ -595,7 +595,7 @@ func testFactoryStates(sf Factory, t *testing.T) {
tsf, err := action.NewTransfer(1, big.NewInt(10), b, nil, uint64(20000), big.NewInt(0))
require.NoError(t, err)
bd := &action.EnvelopeBuilder{}
elp := bd.SetAction(tsf).SetGasLimit(20000).Build()
elp := bd.SetAction(tsf).SetGasLimit(20000).SetNonce(1).Build()
selp, err := action.Sign(elp, priKeyA)
require.NoError(t, err)
ctx = protocol.WithBlockCtx(
Expand Down

0 comments on commit d63b9a1

Please sign in to comment.