Skip to content

Commit

Permalink
upgrade v0.50 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikluke authored Sep 25, 2024
1 parent 1620ea0 commit e6b77d8
Show file tree
Hide file tree
Showing 90 changed files with 2,004 additions and 2,498 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ issues:
- errcheck
path: testutil

- linters:
- errcheck
path: x/scorum/client/cli
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DOCKER := $(shell which docker)
LINTER_NAME := golangci-lint
LINTER_VERSION := v1.59.0

CONTAINER_PROTO_VERSION=0.13.0
CONTAINER_PROTO_VERSION=0.14.0
CONTAINER_PROTO_IMAGE=ghcr.io/cosmos/proto-builder:$(CONTAINER_PROTO_VERSION)

GOSRC := $(shell go env GOPATH)/src
Expand Down
5 changes: 4 additions & 1 deletion app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ante

import (
errorsmod "cosmossdk.io/errors"
circuitante "cosmossdk.io/x/circuit/ante"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
Expand All @@ -13,6 +14,7 @@ type HandlerOptions struct {
ScorumKeeper ScorumKeeper
AccountKeeper AccountKeeper
BankKeeper BankKeeper
CircuitKeeper circuitante.CircuitBreaker
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
Expand All @@ -34,7 +36,8 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
NewZeroGasTxDecorator(options.ScorumKeeper),
circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper),
NewZeroGasTxDecorator(options.AccountKeeper, options.ScorumKeeper),
NewTrackGasConsumedDecorator(options.AccountKeeper, options.BankKeeper, options.ScorumKeeper),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
Expand Down
2 changes: 1 addition & 1 deletion app/ante/check_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d CheckAddressesDecorator) AnteHandle(
for _, msg := range tx.GetMsgs() {
for _, addr := range extractAddresses(msg) {
if !d.ak.HasAccount(ctx, addr) {
if err := d.sk.Mint(ctx, addr, sdk.NewCoin(scorumtypes.GasDenom, d.sk.GetParams(ctx).GasLimit.Int)); err != nil {
if err := d.sk.Mint(ctx, addr, sdk.NewCoin(scorumtypes.GasDenom, d.sk.GetParams(ctx).GasLimit)); err != nil {
return sdk.Context{}, errorsmod.Wrap(sdkerrors.ErrPanic, fmt.Sprintf("failed to mint gas to new account: %s", err.Error()))
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/ante/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ante

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -19,11 +21,11 @@ type ScorumKeeper interface {
type AccountKeeper interface {
ante.AccountKeeper

HasAccount(ctx sdk.Context, addr sdk.AccAddress) bool
HasAccount(ctx context.Context, addr sdk.AccAddress) bool
}

type BankKeeper interface {
authtypes.BankKeeper

GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
}
70 changes: 45 additions & 25 deletions app/ante/sig_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package ante
import (
"fmt"

errorsmod "cosmossdk.io/errors"
txsigning "cosmossdk.io/x/tx/signing"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"google.golang.org/protobuf/types/known/anypb"
)

// SigVerificationDecorator is a copy of cosmos original SigVerificationDecorator
Expand All @@ -16,20 +20,20 @@ type SigVerificationDecorator struct {
ak AccountKeeper
sk ScorumKeeper

signModeHandler authsigning.SignModeHandler
signModeHandler *txsigning.HandlerMap
}

func NewSigVerificationDecorator(ak AccountKeeper, sk ScorumKeeper, signModeHandler authsigning.SignModeHandler) SigVerificationDecorator {
func NewSigVerificationDecorator(ak AccountKeeper, sk ScorumKeeper, signModeHandler *txsigning.HandlerMap) SigVerificationDecorator {
return SigVerificationDecorator{
ak: ak,
sk: sk,
signModeHandler: signModeHandler,
}
}
func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
sigTx, ok := tx.(authsigning.SigVerifiableTx)
sigTx, ok := tx.(authsigning.Tx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}

// stdSigs contains the sequence number, account number, and signatures.
Expand All @@ -39,32 +43,38 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
return ctx, err
}

signerAddrs := sigTx.GetSigners()
signers, err := sigTx.GetSigners()
if err != nil {
return ctx, err
}

// check that signer length and signature length are the same
if len(sigs) != len(signerAddrs) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signer; expected: %d, got %d", len(signerAddrs), len(sigs))
if len(sigs) != len(signers) {
return ctx, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "invalid number of signer; expected: %d, got %d", len(signers), len(sigs))
}

for i, sig := range sigs {
acc, err := ante.GetSignerAcc(ctx, svd.ak, signerAddrs[i])
acc, err := ante.GetSignerAcc(ctx, svd.ak, signers[i])
if err != nil {
return ctx, err
}

// retrieve pubkey
pubKey := acc.GetPubKey()
if !simulate && pubKey == nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "pubkey on account is not set")
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "pubkey on account is not set")
}

// Check account sequence number.
// Applicable only for not supervisors
if !svd.sk.IsSupervisor(ctx, acc.GetAddress().String()) && sig.Sequence != acc.GetSequence() {
return ctx, sdkerrors.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
)
if sig.Sequence != acc.GetSequence() {
// Check account sequence number.
// Applicable only for not supervisors
if !svd.sk.IsSupervisor(ctx, acc.GetAddress().String()) && sig.Sequence != acc.GetSequence() {
return ctx, errorsmod.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
)
}
}

// retrieve signer data
Expand All @@ -74,27 +84,37 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
if !genesis {
accNum = acc.GetAccountNumber()
}
signerData := authsigning.SignerData{
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: sig.Sequence, // acc.GetSequence() was originally here, but supervisors allowed to put any value
PubKey: pubKey,
}

// no need to verify signatures on recheck tx
if !simulate && !ctx.IsReCheckTx() {
err := authsigning.VerifySignature(pubKey, signerData, sig.Data, svd.signModeHandler, tx)
anyPk, _ := codectypes.NewAnyWithValue(pubKey)

signerData := txsigning.SignerData{
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: acc.GetSequence(),
PubKey: &anypb.Any{
TypeUrl: anyPk.TypeUrl,
Value: anyPk.Value,
},
}
adaptableTx, ok := tx.(authsigning.V2AdaptableTx)
if !ok {
return ctx, fmt.Errorf("expected tx to implement V2AdaptableTx, got %T", tx)
}
txData := adaptableTx.GetSigningTxData()
err = authsigning.VerifySignature(ctx, pubKey, signerData, sig.Data, svd.signModeHandler, txData)
if err != nil {
var errMsg string
if ante.OnlyLegacyAminoSigners(sig.Data) {
// If all signers are using SIGN_MODE_LEGACY_AMINO, we rely on VerifySignature to check account sequence number,
// and therefore communicate sequence number as a potential cause of error.
errMsg = fmt.Sprintf("signature verification failed; please verify account number (%d), sequence (%d) and chain-id (%s)", accNum, acc.GetSequence(), chainID)
} else {
errMsg = fmt.Sprintf("signature verification failed; please verify account number (%d) and chain-id (%s)", accNum, chainID)
errMsg = fmt.Sprintf("signature verification failed; please verify account number (%d) and chain-id (%s): (%s)", accNum, chainID, err.Error())
}
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, errMsg)
return ctx, errorsmod.Wrap(sdkerrors.ErrUnauthorized, errMsg)

}
}
Expand Down
27 changes: 21 additions & 6 deletions app/ante/track_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)

type TrackGasConsumedDecorator struct {
Expand All @@ -26,14 +28,27 @@ func (d TrackGasConsumedDecorator) AnteHandle(
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
for _, v := range tx.GetMsgs() {
for _, addr := range v.GetSigners() {
if !d.ak.HasAccount(ctx, addr) {
return sdk.Context{}, errorsmod.Wrap(sdkerrors.ErrUnknownAddress, "address is not registered")
}
sigTx, ok := tx.(authsigning.Tx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}

signers, err := sigTx.GetSigners()
if !ok {
return ctx, err
}

d.sk.SetAddressToRestoreGas(ctx, addr)
for _, signer := range signers {
addr, err := ante.GetSignerAcc(ctx, d.ak, signer)
if err != nil {
return ctx, err
}

if !d.ak.HasAccount(ctx, addr.GetAddress()) {
return sdk.Context{}, errorsmod.Wrap(sdkerrors.ErrUnknownAddress, "address is not registered")
}

d.sk.SetAddressToRestoreGas(ctx, addr.GetAddress())
}

return next(ctx, tx, simulate)
Expand Down
65 changes: 44 additions & 21 deletions app/ante/zero-gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import (
"fmt"
"math"

"github.com/cosmos/cosmos-sdk/x/auth/ante"

errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"

storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type ZeroGasTxDecorator struct {
sk ScorumKeeper
ak AccountKeeper
}

func NewZeroGasTxDecorator(sk ScorumKeeper) ZeroGasTxDecorator {
func NewZeroGasTxDecorator(ak AccountKeeper, sk ScorumKeeper) ZeroGasTxDecorator {
return ZeroGasTxDecorator{
ak: ak,
sk: sk,
}
}
Expand All @@ -23,59 +33,72 @@ func (d ZeroGasTxDecorator) AnteHandle(
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
for _, msg := range tx.GetMsgs() {
for _, v := range msg.GetSigners() {
if d.sk.IsSupervisor(ctx.WithGasMeter(NewFixedGasMeter(0, ctx.GasMeter().Limit())), v.String()) {
return next(
ctx.
WithGasMeter(NewFixedGasMeter(0, ctx.GasMeter().Limit())).
WithMinGasPrices(sdk.NewDecCoins()),
tx,
simulate,
)
}
sigTx, ok := tx.(authsigning.Tx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type")
}

signers, err := sigTx.GetSigners()
if !ok {
return ctx, err
}

for _, signer := range signers {
v, err := ante.GetSignerAcc(ctx, d.ak, signer)
if err != nil {
return ctx, err
}

if d.sk.IsSupervisor(ctx.WithGasMeter(NewFixedGasMeter(0, ctx.GasMeter().Limit())), v.GetAddress().String()) {
return next(
ctx.
WithGasMeter(NewFixedGasMeter(0, ctx.GasMeter().Limit())).
WithMinGasPrices(sdk.NewDecCoins()),
tx,
simulate,
)
}
}

return next(ctx, tx, simulate)
}

type fixedGasMeter struct {
limit sdk.Gas
consumed sdk.Gas
limit storetypes.Gas
consumed storetypes.Gas
}

// NewFixedGasMeter returns a reference to a new basicGasMeter.
func NewFixedGasMeter(consumed, limit sdk.Gas) sdk.GasMeter {
func NewFixedGasMeter(consumed, limit storetypes.Gas) storetypes.GasMeter {
return &fixedGasMeter{
limit: limit,
consumed: consumed,
}
}

func (g *fixedGasMeter) GasConsumed() sdk.Gas {
func (g *fixedGasMeter) GasConsumed() storetypes.Gas {
return g.consumed
}

func (g *fixedGasMeter) Limit() sdk.Gas {
func (g *fixedGasMeter) Limit() storetypes.Gas {
return g.limit
}

func (g *fixedGasMeter) GasRemaining() sdk.Gas {
func (g *fixedGasMeter) GasRemaining() storetypes.Gas {
return math.MaxUint64
}

func (g *fixedGasMeter) GasConsumedToLimit() sdk.Gas {
func (g *fixedGasMeter) GasConsumedToLimit() storetypes.Gas {
if g.IsPastLimit() {
return g.limit
}
return g.consumed
}

func (g *fixedGasMeter) ConsumeGas(_ sdk.Gas, _ string) {
func (g *fixedGasMeter) ConsumeGas(_ storetypes.Gas, _ string) {
}

func (g *fixedGasMeter) RefundGas(_ sdk.Gas, _ string) {
func (g *fixedGasMeter) RefundGas(_ storetypes.Gas, _ string) {
}

func (g *fixedGasMeter) IsPastLimit() bool {
Expand Down
Loading

0 comments on commit e6b77d8

Please sign in to comment.