refactor: precompile

This commit is contained in:
MiniFrenchBread 2024-12-31 23:21:08 +08:00
parent 32601927cf
commit 691cc06686
7 changed files with 32 additions and 19 deletions

4
go.mod
View File

@ -242,7 +242,7 @@ replace (
github.com/cometbft/cometbft-db => github.com/kava-labs/cometbft-db v0.9.1-kava.2 github.com/cometbft/cometbft-db => github.com/kava-labs/cometbft-db v0.9.1-kava.2
// Use cosmos-sdk fork with backported fix for unsafe-reset-all, staking transfer events, and custom tally handler support // Use cosmos-sdk fork with backported fix for unsafe-reset-all, staking transfer events, and custom tally handler support
// github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-kava.3 // github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.46.11-kava.3
github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.47.10-0glabs.7 github.com/cosmos/cosmos-sdk => github.com/0glabs/cosmos-sdk v0.0.0-20241226164042-2543aa53eee1
github.com/cosmos/iavl => github.com/kava-labs/iavl v1.2.0-kava.1 github.com/cosmos/iavl => github.com/kava-labs/iavl v1.2.0-kava.1
// See https://github.com/cosmos/cosmos-sdk/pull/13093 // See https://github.com/cosmos/cosmos-sdk/pull/13093
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
@ -250,7 +250,7 @@ replace (
// TODO: Tag before release // TODO: Tag before release
github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26-evmos-rc2 github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26-evmos-rc2
// Use ethermint fork that respects min-gas-price with NoBaseFee true and london enabled, and includes eip712 support // Use ethermint fork that respects min-gas-price with NoBaseFee true and london enabled, and includes eip712 support
github.com/evmos/ethermint => github.com/0glabs/ethermint v0.21.0-0g.v3.1.7 github.com/evmos/ethermint => github.com/0glabs/ethermint v0.0.0-20241231151651-f7d1b1499e5a
// See https://github.com/cosmos/cosmos-sdk/pull/10401, https://github.com/cosmos/cosmos-sdk/commit/0592ba6158cd0bf49d894be1cef4faeec59e8320 // See https://github.com/cosmos/cosmos-sdk/pull/10401, https://github.com/cosmos/cosmos-sdk/commit/0592ba6158cd0bf49d894be1cef4faeec59e8320
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0 github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
// Downgraded to avoid bugs in following commits which causes "version does not exist" errors // Downgraded to avoid bugs in following commits which causes "version does not exist" errors

8
go.sum
View File

@ -211,10 +211,10 @@ git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3/go.mod h1:wMEGFFFN
git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA= git.sr.ht/~sircmpwn/go-bare v0.0.0-20210406120253-ab86bc2846d9/go.mod h1:BVJwbDfVjCjoFiKrhkei6NdGcZYpkDkdyCdg1ukytRA=
github.com/0glabs/cometbft v0.37.9-0glabs.1 h1:KQJG17Y21suKP3QNICLto4b5Ak73XbSmKxeLbg0ZM68= github.com/0glabs/cometbft v0.37.9-0glabs.1 h1:KQJG17Y21suKP3QNICLto4b5Ak73XbSmKxeLbg0ZM68=
github.com/0glabs/cometbft v0.37.9-0glabs.1/go.mod h1:j0Q3RqrCd+cztWCugs3obbzC4NyHGBPZZjtm/fWV00I= github.com/0glabs/cometbft v0.37.9-0glabs.1/go.mod h1:j0Q3RqrCd+cztWCugs3obbzC4NyHGBPZZjtm/fWV00I=
github.com/0glabs/cosmos-sdk v0.47.10-0glabs.7 h1:6+JquK9BaZZdIA3gx1AXhPBAdYCG+FQ94Y7FN35CvB4= github.com/0glabs/cosmos-sdk v0.0.0-20241226164042-2543aa53eee1 h1:5bREC0rOIpQDay71Weorv26WivJGRYnEk8kRH+jMPZg=
github.com/0glabs/cosmos-sdk v0.47.10-0glabs.7/go.mod h1:KskIVnhXTFqrw7CDccMvx7To5KzUsOomIsQV7sPGOog= github.com/0glabs/cosmos-sdk v0.0.0-20241226164042-2543aa53eee1/go.mod h1:KskIVnhXTFqrw7CDccMvx7To5KzUsOomIsQV7sPGOog=
github.com/0glabs/ethermint v0.21.0-0g.v3.1.7 h1:wzr6z/LTsbjoAaBf0JkMtIDl/+B5KLd0GrU5brSZCY0= github.com/0glabs/ethermint v0.0.0-20241231151651-f7d1b1499e5a h1:C7edknSFFmB0PdXApWryR+3NjK8Lr/gHiYQ156rUahw=
github.com/0glabs/ethermint v0.21.0-0g.v3.1.7/go.mod h1:S1Ahmqpzo1XUsfmmpGT7ok0hu5Fekz/pD6EDtXaBg9Q= github.com/0glabs/ethermint v0.0.0-20241231151651-f7d1b1499e5a/go.mod h1:WD4qMi5eOSa34aMeNS7aYMdI5Yby+PK57sWmNqdKqJM=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM= github.com/Azure/azure-sdk-for-go/sdk/azcore v0.21.1/go.mod h1:fBF9PQNqB8scdgpZ3ufzaLntG0AG7C1WjPMsiFOmfHM=

View File

@ -1,7 +1,7 @@
package common package common
import ( import (
"fmt" "errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types" storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -13,6 +13,7 @@ import (
type PrecompileCommon interface { type PrecompileCommon interface {
Abi() *abi.ABI Abi() *abi.ABI
IsTx(string) bool IsTx(string) bool
KVGasConfig() storetypes.GasConfig
} }
func InitializePrecompileCall( func InitializePrecompileCall(
@ -42,12 +43,12 @@ func InitializePrecompileCall(
} }
// readonly check // readonly check
if readonly && p.IsTx(method.Name) { if readonly && p.IsTx(method.Name) {
return sdk.Context{}, nil, nil, uint64(0), nil, fmt.Errorf(ErrWriteOnReadOnly) return sdk.Context{}, nil, nil, uint64(0), nil, errors.New(ErrWriteOnReadOnly)
} }
// get state db and context // get state db and context
stateDB, ok := evm.StateDB.(*statedb.StateDB) stateDB, ok := evm.StateDB.(*statedb.StateDB)
if !ok { if !ok {
return sdk.Context{}, nil, nil, uint64(0), nil, fmt.Errorf(ErrGetStateDB) return sdk.Context{}, nil, nil, uint64(0), nil, errors.New(ErrGetStateDB)
} }
ctx, err = stateDB.GetCachedContextForPrecompile() ctx, err = stateDB.GetCachedContextForPrecompile()
if err != nil { if err != nil {
@ -55,6 +56,7 @@ func InitializePrecompileCall(
} }
// initial gas // initial gas
initialGas = ctx.GasMeter().GasConsumed() initialGas = ctx.GasMeter().GasConsumed()
ctx = ctx.WithKVGasConfig(p.KVGasConfig())
return ctx, stateDB, method, initialGas, args, nil return ctx, stateDB, method, initialGas, args, nil
} }

View File

@ -5,6 +5,7 @@ import (
precompiles_common "github.com/0glabs/0g-chain/precompiles/common" precompiles_common "github.com/0glabs/0g-chain/precompiles/common"
dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper" dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/core/vm"
@ -95,6 +96,10 @@ func (d *DASignersPrecompile) Abi() *abi.ABI {
return &d.abi return &d.abi
} }
func (d *DASignersPrecompile) KVGasConfig() storetypes.GasConfig {
return storetypes.KVGasConfig()
}
// Run implements vm.PrecompiledContract. // Run implements vm.PrecompiledContract.
func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) { func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
ctx, stateDB, method, initialGas, args, err := precompiles_common.InitializePrecompileCall(d, evm, contract, readonly) ctx, stateDB, method, initialGas, args, err := precompiles_common.InitializePrecompileCall(d, evm, contract, readonly)

View File

@ -1,6 +1,7 @@
package dasigners package dasigners
import ( import (
"errors"
"fmt" "fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -29,7 +30,7 @@ func (d *DASignersPrecompile) RegisterSigner(
return nil, fmt.Errorf(ErrInvalidSender, sender, msg.Signer.Account) return nil, fmt.Errorf(ErrInvalidSender, sender, msg.Signer.Account)
} }
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
// execute // execute
_, err = d.dasignersKeeper.RegisterSigner(sdk.WrapSDKContext(ctx), msg) _, err = d.dasignersKeeper.RegisterSigner(sdk.WrapSDKContext(ctx), msg)
@ -58,7 +59,7 @@ func (d *DASignersPrecompile) RegisterNextEpoch(
} }
// validation // validation
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
// execute // execute
_, err = d.dasignersKeeper.RegisterNextEpoch(sdk.WrapSDKContext(ctx), msg) _, err = d.dasignersKeeper.RegisterNextEpoch(sdk.WrapSDKContext(ctx), msg)
@ -82,7 +83,7 @@ func (d *DASignersPrecompile) UpdateSocket(
} }
// validation // validation
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
// execute // execute
_, err = d.dasignersKeeper.UpdateSocket(sdk.WrapSDKContext(ctx), msg) _, err = d.dasignersKeeper.UpdateSocket(sdk.WrapSDKContext(ctx), msg)

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
precompiles_common "github.com/0glabs/0g-chain/precompiles/common" precompiles_common "github.com/0glabs/0g-chain/precompiles/common"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -83,6 +84,10 @@ func (s *StakingPrecompile) IsTx(method string) bool {
} }
} }
func (s *StakingPrecompile) KVGasConfig() storetypes.GasConfig {
return storetypes.KVGasConfig()
}
// Run implements vm.PrecompiledContract. // Run implements vm.PrecompiledContract.
func (s *StakingPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) { func (s *StakingPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
ctx, stateDB, method, initialGas, args, err := precompiles_common.InitializePrecompileCall(s, evm, contract, readonly) ctx, stateDB, method, initialGas, args, err := precompiles_common.InitializePrecompileCall(s, evm, contract, readonly)

View File

@ -1,7 +1,7 @@
package staking package staking
import ( import (
"fmt" "errors"
"math/big" "math/big"
precompiles_common "github.com/0glabs/0g-chain/precompiles/common" precompiles_common "github.com/0glabs/0g-chain/precompiles/common"
@ -26,7 +26,7 @@ func (s *StakingPrecompile) CreateValidator(
} }
// validation // validation
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
// execute // execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).CreateValidator(ctx, msg) _, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).CreateValidator(ctx, msg)
@ -51,7 +51,7 @@ func (s *StakingPrecompile) EditValidator(
} }
// validation // validation
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
// execute // execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).EditValidator(ctx, msg) _, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).EditValidator(ctx, msg)
@ -77,7 +77,7 @@ func (s *StakingPrecompile) Delegate(
// validation // validation
/* /*
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
*/ */
// execute // execute
@ -104,7 +104,7 @@ func (s *StakingPrecompile) BeginRedelegate(
// validation // validation
/* /*
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
*/ */
// execute // execute
@ -131,7 +131,7 @@ func (s *StakingPrecompile) Undelegate(
// validation // validation
/* /*
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
*/ */
// execute // execute
@ -158,7 +158,7 @@ func (s *StakingPrecompile) CancelUnbondingDelegation(
// validation // validation
/* /*
if contract.CallerAddress != evm.Origin { if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin) return nil, errors.New(precompiles_common.ErrSenderNotOrigin)
} }
*/ */
// execute // execute