Skip to content

Commit

Permalink
Copy ERC20 module implementation from Canto repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBorst committed Oct 7, 2024
1 parent 963820c commit 3c59b03
Show file tree
Hide file tree
Showing 76 changed files with 13,969 additions and 199 deletions.
2 changes: 1 addition & 1 deletion app/ante/charge_gasfree_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,6 @@ func (suite *AnteTestSuite) TestChargeGasfreeFeesDecorator() {
// nolint: exhaustruct
suite.app.GasfreeKeeper.SetGasFreeMessageTypes(bothGasfreeCtx, []string{sdk.MsgTypeURL(&microtxtypes.MsgMicrotx{}), sdk.MsgTypeURL(&banktypes.MsgSend{})})

// Expect the error from the mempool fee decorator to contain something like "insufficient fees; got: x required: provided fee < minimum global feey"
// Expect the error from the mempool fee decorator to contain something like "insufficient fees; got: x required: provided fee < minimum global fee y"
suite.Require().NoError(runGasfreeTests(suite, gasfreeMicrotxCtx, gasfreeSendCtx, noGasfreeCtx, bothGasfreeCtx, msgMicrotxTx, msgSendTx, bothTx, addr, testDenom))
}
4 changes: 2 additions & 2 deletions app/ante/cosmos_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (suite *AnteTestSuite) TestCosmosAnteHandlerMempoolFeeBypass() {
// nolint: exhaustruct
suite.app.GasfreeKeeper.SetGasFreeMessageTypes(bothGasfreeCtx, []string{sdk.MsgTypeURL(&microtxtypes.MsgMicrotx{}), sdk.MsgTypeURL(&banktypes.MsgSend{})})

// Expect the error from the mempool fee decorator to contain something like "insufficient fees; got: x required: provided fee < minimum global feey"
suite.Require().NoError(runBypassTest(suite, "insufficient fees; got:", gasfreeMicrotxCtx, gasfreeSendCtx, noGasfreeCtx, bothGasfreeCtx, msgMicrotxTx, msgSendTx, bothTx))
// Expect the error from the mempool fee decorator to contain something like "insufficient fees; got: x required: provided fee < minimum global fee"
suite.Require().NoError(runBypassTest(suite, "provided fee < minimum global fee", gasfreeMicrotxCtx, gasfreeSendCtx, noGasfreeCtx, bothGasfreeCtx, msgMicrotxTx, msgSendTx, bothTx))
}

// Checks that the MinGasPrices antedecorator is bypassed for applicable txs
Expand Down
13 changes: 0 additions & 13 deletions app/ante/eth_set_pubkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,6 @@ func (suite *AnteTestSuite) TestEthSetPubkeyHandler() {
suite.Require().Error(err)
}
suite.Require().True(tc.correctPubKey(pubKey), "PubKey type incorrect after AnteHandler")

// Now check the old antehandler against the same Tx, see if it would create a pubkey
ctx, _ = suite.ctx.CacheContext()
suite.testCaseSetup(ctx, addr, tc.startBaseAcc)
// nolint: errcheck
_, _ = suite.oldAnteHandler(ctx, tx, false)
acc = suite.app.AccountKeeper.GetAccount(ctx, addr)
pubKey = acc.GetPubKey()
if tc.expOldPubKey {
suite.Require().True(pubKey != nil && tc.correctPubKey(pubKey), "Old ante handler should have set a pubkey")
} else {
suite.Require().True(pubKey == nil, "Old ante handler should have set a pubkey")
}
})
}
}
20 changes: 2 additions & 18 deletions app/ante/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import (
evmtypes "github.com/evmos/ethermint/x/evm/types"
feemarkettypes "github.com/evmos/ethermint/x/feemarket/types"

cantoante "github.com/Canto-Network/Canto/v6/app/ante"

althea "github.com/AltheaFoundation/althea-L1/app"
ante "github.com/AltheaFoundation/althea-L1/app/ante"
altheaconfig "github.com/AltheaFoundation/althea-L1/config"
Expand All @@ -55,7 +53,6 @@ type AnteTestSuite struct {
app *althea.AltheaApp
clientCtx client.Context
anteHandler sdk.AnteHandler
oldAnteHandler sdk.AnteHandler
ethSigner ethtypes.Signer
enableFeemarket bool
evmParamsOption func(*evmtypes.Params)
Expand All @@ -73,12 +70,13 @@ func (suite *AnteTestSuite) SetupTest() {
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount("althea", "altheapub")

suite.app = althea.Setup(checkTx, func(app *althea.AltheaApp, genesis althea.GenesisState) althea.GenesisState {
suite.app = althea.NewSetup(checkTx, func(app *althea.AltheaApp, genesis simapp.GenesisState) simapp.GenesisState {
if suite.enableFeemarket {
// setup feemarketGenesis params
feemarketGenesis := feemarkettypes.DefaultGenesisState()
feemarketGenesis.Params.EnableHeight = 1
feemarketGenesis.Params.NoBaseFee = false
feemarketGenesis.BlockGas = 30000000
// Verify feeMarket genesis
err := feemarketGenesis.Validate()
suite.Require().NoError(err)
Expand Down Expand Up @@ -117,20 +115,6 @@ func (suite *AnteTestSuite) SetupTest() {

suite.anteHandler = anteHandler

// Also make a copy of the old Canto antehandler we were using to ensure that our changes fix the problem
// nolint: exhaustruct
oldAnteHandler := cantoante.NewAnteHandler(cantoante.HandlerOptions{
AccountKeeper: *suite.app.AccountKeeper,
BankKeeper: suite.app.BankKeeper,
EvmKeeper: suite.app.EvmKeeper,
FeegrantKeeper: nil,
IBCKeeper: suite.app.IbcKeeper,
FeeMarketKeeper: *suite.app.FeemarketKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: althea.SigVerificationGasConsumer,
})
suite.oldAnteHandler = oldAnteHandler

// Defines the siging method (e.g. homestead, london, etc)
suite.ethSigner = ethtypes.LatestSignerForChainID(suite.app.EvmKeeper.ChainID())
}
Expand Down
31 changes: 15 additions & 16 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ import (

// EVM + ERC20

"github.com/Canto-Network/Canto/v6/x/erc20"
erc20client "github.com/Canto-Network/Canto/v6/x/erc20/client"
erc20keeper "github.com/Canto-Network/Canto/v6/x/erc20/keeper"
erc20types "github.com/Canto-Network/Canto/v6/x/erc20/types"

ethante "github.com/evmos/ethermint/app/ante"
"github.com/evmos/ethermint/ethereum/eip712"
ethermintsrvflags "github.com/evmos/ethermint/server/flags"
Expand All @@ -137,6 +132,10 @@ import (
"github.com/AltheaFoundation/althea-L1/app/upgrades"
"github.com/AltheaFoundation/althea-L1/app/upgrades/neutrino"
altheacfg "github.com/AltheaFoundation/althea-L1/config"
"github.com/AltheaFoundation/althea-L1/x/erc20"
erc20client "github.com/AltheaFoundation/althea-L1/x/erc20/client"
erc20keeper "github.com/AltheaFoundation/althea-L1/x/erc20/keeper"
erc20types "github.com/AltheaFoundation/althea-L1/x/erc20/types"
"github.com/AltheaFoundation/althea-L1/x/gasfree"
gasfreekeeper "github.com/AltheaFoundation/althea-L1/x/gasfree/keeper"
gasfreetypes "github.com/AltheaFoundation/althea-L1/x/gasfree/types"
Expand Down Expand Up @@ -578,6 +577,16 @@ func NewAltheaApp(
)
app.SlashingKeeper = &slashingKeeper

// Connect the inter-module staking hooks together, these are the only modules allowed to interact with how staking
// works, including inflationary staking rewards and punishing bad actors (excluding genutil which works at genesis to
// seed the set of validators from the genesis txs set)
stakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
distrKeeper.Hooks(),
slashingKeeper.Hooks(),
),
)

upgradeKeeper := upgradekeeper.NewKeeper(
skipUpgradeHeights,
keys[upgradetypes.StoreKey],
Expand Down Expand Up @@ -682,16 +691,6 @@ func NewAltheaApp(
ibcRouter.AddRoute(icahosttypes.SubModuleName, icaHostStack)
ibcKeeper.SetRouter(ibcRouter)

// Connect the inter-module staking hooks together, these are the only modules allowed to interact with how staking
// works, including inflationary staking rewards and punishing bad actors (excluding genutil which works at genesis to
// seed the set of validators from the genesis txs set)
stakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
distrKeeper.Hooks(),
slashingKeeper.Hooks(),
),
)

mintKeeper := mintkeeper.NewKeeper(
appCodec,
keys[minttypes.StoreKey],
Expand Down Expand Up @@ -1065,7 +1064,7 @@ func (app *AltheaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci
// InitChainer deserializes the given chain genesis state, registers in-place upgrade migrations, and delegates
// the ABCI InitGenesis execution to the ModuleManager
func (app *AltheaApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
var genesisState simapp.GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
Expand Down
14 changes: 2 additions & 12 deletions app/genesis.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package althea

import (
"encoding/json"
"github.com/cosmos/cosmos-sdk/simapp"
)

// The genesis state of the blockchain is represented here as a map of raw json
// messages key'd by a identifier string.
// The identifier is used to determine which module genesis information belongs
// to so it may be appropriately routed during init chain.
// Within this application default genesis information is retrieved from
// the ModuleBasicManager which populates json from each BasicModule
// object provided to it during init.
type GenesisState map[string]json.RawMessage

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState() GenesisState {
func NewDefaultGenesisState() simapp.GenesisState {
encCfg := MakeEncodingConfig()
return ModuleBasics.DefaultGenesis(encCfg.Codec)
}
80 changes: 77 additions & 3 deletions app/sigverify.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package althea

import (
"fmt"

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

canto "github.com/Canto-Network/Canto/v6/app"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
)

const (
Expand Down Expand Up @@ -38,6 +45,73 @@ func SigVerificationGasConsumer(
return nil

default:
return canto.SigVerificationGasConsumer(meter, sig, params)
return CantoSigVerificationGasConsumer(meter, sig, params)
}
}

var _ authante.SignatureVerificationGasConsumer = CantoSigVerificationGasConsumer

// SigVerificationGasConsumer is the canto implementation of SignatureVerificationGasConsumer. It consumes gas
// for signature verification based upon the public key type. The cost is fetched from the given params and is matched
// by the concrete type.
// The types of keys supported are:
//
// - ethsecp256k1 (Ethereum keys)
//
// - ed25519 (Validators)
//
// - multisig (Cosmos SDK multisigs)
func CantoSigVerificationGasConsumer(
meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params,
) error {
pubkey := sig.PubKey
switch pubkey := pubkey.(type) {

case *ethsecp256k1.PubKey:
// Ethereum keys
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
return nil
case *ed25519.PubKey:
// Validator keys
meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519")
return errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "ED25519 public keys are unsupported")

case multisig.PubKey:
// Multisig keys
multisignature, ok := sig.Data.(*signing.MultiSignatureData)
if !ok {
return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data)
}
return ConsumeMultisignatureVerificationGas(meter, multisignature, pubkey, params, sig.Sequence)

default:
return errorsmod.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized/unsupported public key type: %T", pubkey)
}
}

// ConsumeMultisignatureVerificationGas consumes gas from a GasMeter for verifying a multisig pubkey signature
func ConsumeMultisignatureVerificationGas(
meter sdk.GasMeter, sig *signing.MultiSignatureData, pubkey multisig.PubKey,
params authtypes.Params, accSeq uint64,
) error {
size := sig.BitArray.Count()
sigIndex := 0

for i := 0; i < size; i++ {
if !sig.BitArray.GetIndex(i) {
continue
}
sigV2 := signing.SignatureV2{
PubKey: pubkey.GetPubKeys()[i],
Data: sig.Signatures[sigIndex],
Sequence: accSeq,
}
err := SigVerificationGasConsumer(meter, sigV2, params)
if err != nil {
return err
}
sigIndex++
}

return nil
}
Loading

0 comments on commit 3c59b03

Please sign in to comment.