-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use AccountNumberDecorator to avoid account number collision (#220)
- Loading branch information
Showing
4 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ante | ||
|
||
import ( | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
cosmosante "github.com/cosmos/cosmos-sdk/x/auth/ante" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
) | ||
|
||
// AccountNumberDecorator is a custom ante handler that increments the account number depending on | ||
// the execution mode (Simulate, CheckTx, Finalize). | ||
// | ||
// This is to avoid account number conflicts when running concurrent Simulate, CheckTx, and Finalize. | ||
type AccountNumberDecorator struct { | ||
ak cosmosante.AccountKeeper | ||
} | ||
|
||
// NewAccountNumberDecorator creates a new instance of AccountNumberDecorator. | ||
func NewAccountNumberDecorator(ak cosmosante.AccountKeeper) AccountNumberDecorator { | ||
return AccountNumberDecorator{ak} | ||
} | ||
|
||
// AnteHandle is the AnteHandler implementation for AccountNumberDecorator. | ||
func (and AccountNumberDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { | ||
if !ctx.IsCheckTx() && !ctx.IsReCheckTx() && !simulate { | ||
return next(ctx, tx, simulate) | ||
} | ||
|
||
ak := and.ak.(*authkeeper.AccountKeeper) | ||
|
||
gasFreeCtx := ctx.WithGasMeter(storetypes.NewInfiniteGasMeter()) | ||
num, err := ak.AccountNumber.Peek(gasFreeCtx) | ||
if err != nil { | ||
return ctx, err | ||
} | ||
|
||
accountNumAddition := uint64(1_000_000) | ||
if simulate { | ||
accountNumAddition += 1_000_000 | ||
} | ||
|
||
if err := ak.AccountNumber.Set(gasFreeCtx, num+accountNumAddition); err != nil { | ||
return ctx, err | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters