2024-05-09 18:54:47 +00:00
|
|
|
package dasigners
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
precopmiles_common "github.com/0glabs/0g-chain/precompiles/common"
|
|
|
|
dasignerskeeper "github.com/0glabs/0g-chain/x/dasigners/v1/keeper"
|
2024-05-10 18:41:14 +00:00
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
2024-05-09 18:54:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-05-10 18:41:14 +00:00
|
|
|
PrecompileAddress = "0x0000000000000000000000000000000000001000"
|
|
|
|
|
|
|
|
RequiredGasMax uint64 = 1000_000_000
|
2024-05-09 18:54:47 +00:00
|
|
|
|
2024-09-24 01:39:16 +00:00
|
|
|
DASignersFunctionParams = "params"
|
2024-05-09 18:54:47 +00:00
|
|
|
DASignersFunctionEpochNumber = "epochNumber"
|
2024-05-16 14:49:29 +00:00
|
|
|
DASignersFunctionQuorumCount = "quorumCount"
|
2024-05-09 18:54:47 +00:00
|
|
|
DASignersFunctionGetSigner = "getSigner"
|
2024-05-16 14:49:29 +00:00
|
|
|
DASignersFunctionGetQuorum = "getQuorum"
|
2024-06-12 05:27:48 +00:00
|
|
|
DASignersFunctionGetQuorumRow = "getQuorumRow"
|
2024-05-16 14:49:29 +00:00
|
|
|
DASignersFunctionRegisterSigner = "registerSigner"
|
2024-05-09 18:54:47 +00:00
|
|
|
DASignersFunctionUpdateSocket = "updateSocket"
|
|
|
|
DASignersFunctionRegisterNextEpoch = "registerNextEpoch"
|
|
|
|
DASignersFunctionGetAggPkG1 = "getAggPkG1"
|
2024-05-18 15:01:28 +00:00
|
|
|
DASignersFunctionIsSigner = "isSigner"
|
|
|
|
DASignersFunctionRegisteredEpoch = "registeredEpoch"
|
2024-05-09 18:54:47 +00:00
|
|
|
)
|
|
|
|
|
2024-05-10 18:41:14 +00:00
|
|
|
var RequiredGasBasic = map[string]uint64{
|
2024-09-24 01:39:16 +00:00
|
|
|
DASignersFunctionParams: 1000,
|
2024-05-16 15:25:43 +00:00
|
|
|
DASignersFunctionEpochNumber: 1000,
|
|
|
|
DASignersFunctionQuorumCount: 1000,
|
|
|
|
DASignersFunctionGetSigner: 100000,
|
|
|
|
DASignersFunctionGetQuorum: 100000,
|
2024-06-12 05:27:48 +00:00
|
|
|
DASignersFunctionGetQuorumRow: 10000,
|
2024-05-16 15:25:43 +00:00
|
|
|
DASignersFunctionRegisterSigner: 100000,
|
|
|
|
DASignersFunctionUpdateSocket: 50000,
|
|
|
|
DASignersFunctionRegisterNextEpoch: 100000,
|
|
|
|
DASignersFunctionGetAggPkG1: 1000000,
|
2024-05-18 15:01:28 +00:00
|
|
|
DASignersFunctionIsSigner: 10000,
|
|
|
|
DASignersFunctionRegisteredEpoch: 10000,
|
2024-05-10 18:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var KVGasConfig storetypes.GasConfig = storetypes.GasConfig{
|
|
|
|
HasCost: 0,
|
|
|
|
DeleteCost: 0,
|
|
|
|
ReadCostFlat: 0,
|
|
|
|
ReadCostPerByte: 0,
|
|
|
|
WriteCostFlat: 0,
|
|
|
|
WriteCostPerByte: 0,
|
|
|
|
IterNextCostFlat: 0,
|
|
|
|
}
|
|
|
|
|
2024-05-09 18:54:47 +00:00
|
|
|
var _ vm.PrecompiledContract = &DASignersPrecompile{}
|
|
|
|
|
|
|
|
type DASignersPrecompile struct {
|
|
|
|
abi abi.ABI
|
|
|
|
dasignersKeeper dasignerskeeper.Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDASignersPrecompile(dasignersKeeper dasignerskeeper.Keeper) (*DASignersPrecompile, error) {
|
|
|
|
abi, err := abi.JSON(strings.NewReader(DASignersABI))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DASignersPrecompile{
|
|
|
|
abi: abi,
|
|
|
|
dasignersKeeper: dasignersKeeper,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements vm.PrecompiledContract.
|
|
|
|
func (d *DASignersPrecompile) Address() common.Address {
|
|
|
|
return common.HexToAddress(PrecompileAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiredGas implements vm.PrecompiledContract.
|
|
|
|
func (d *DASignersPrecompile) RequiredGas(input []byte) uint64 {
|
2024-05-10 18:41:14 +00:00
|
|
|
method, err := d.abi.MethodById(input[:4])
|
|
|
|
if err != nil {
|
|
|
|
return RequiredGasMax
|
|
|
|
}
|
|
|
|
if gas, ok := RequiredGasBasic[method.Name]; ok {
|
|
|
|
return gas
|
|
|
|
}
|
|
|
|
return RequiredGasMax
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 17:52:50 +00:00
|
|
|
func (d *DASignersPrecompile) IsTx(method string) bool {
|
|
|
|
switch method {
|
|
|
|
case DASignersFunctionUpdateSocket,
|
|
|
|
DASignersFunctionRegisterSigner,
|
|
|
|
DASignersFunctionRegisterNextEpoch:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-09 18:54:47 +00:00
|
|
|
// Run implements vm.PrecompiledContract.
|
|
|
|
func (d *DASignersPrecompile) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
|
|
|
|
// parse input
|
|
|
|
if len(contract.Input) < 4 {
|
|
|
|
return nil, vm.ErrExecutionReverted
|
|
|
|
}
|
|
|
|
method, err := d.abi.MethodById(contract.Input[:4])
|
|
|
|
if err != nil {
|
|
|
|
return nil, vm.ErrExecutionReverted
|
|
|
|
}
|
|
|
|
args, err := method.Inputs.Unpack(contract.Input[4:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-13 17:52:50 +00:00
|
|
|
// readonly check
|
|
|
|
if readonly && d.IsTx(method.Name) {
|
|
|
|
return nil, fmt.Errorf(precopmiles_common.ErrWriteOnReadOnly)
|
|
|
|
}
|
2024-05-09 18:54:47 +00:00
|
|
|
// get state db and context
|
|
|
|
stateDB, ok := evm.StateDB.(*statedb.StateDB)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(precopmiles_common.ErrGetStateDB)
|
|
|
|
}
|
|
|
|
ctx := stateDB.GetContext()
|
2024-05-10 18:41:14 +00:00
|
|
|
// reset gas config
|
|
|
|
ctx = ctx.WithKVGasConfig(KVGasConfig)
|
2024-05-09 18:54:47 +00:00
|
|
|
initialGas := ctx.GasMeter().GasConsumed()
|
|
|
|
|
|
|
|
var bz []byte
|
|
|
|
switch method.Name {
|
|
|
|
// queries
|
2024-09-24 01:39:16 +00:00
|
|
|
case DASignersFunctionParams:
|
|
|
|
bz, err = d.Params(ctx, evm, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
case DASignersFunctionEpochNumber:
|
|
|
|
bz, err = d.EpochNumber(ctx, evm, method, args)
|
2024-05-16 14:49:29 +00:00
|
|
|
case DASignersFunctionQuorumCount:
|
|
|
|
bz, err = d.QuorumCount(ctx, evm, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
case DASignersFunctionGetSigner:
|
|
|
|
bz, err = d.GetSigner(ctx, evm, method, args)
|
2024-05-16 14:49:29 +00:00
|
|
|
case DASignersFunctionGetQuorum:
|
|
|
|
bz, err = d.GetQuorum(ctx, evm, method, args)
|
2024-06-12 05:27:48 +00:00
|
|
|
case DASignersFunctionGetQuorumRow:
|
|
|
|
bz, err = d.GetQuorumRow(ctx, evm, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
case DASignersFunctionGetAggPkG1:
|
|
|
|
bz, err = d.GetAggPkG1(ctx, evm, method, args)
|
2024-05-18 15:01:28 +00:00
|
|
|
case DASignersFunctionIsSigner:
|
|
|
|
bz, err = d.IsSigner(ctx, evm, method, args)
|
|
|
|
case DASignersFunctionRegisteredEpoch:
|
|
|
|
bz, err = d.RegisteredEpoch(ctx, evm, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
// txs
|
|
|
|
case DASignersFunctionRegisterSigner:
|
2024-10-24 13:23:45 +00:00
|
|
|
bz, err = d.RegisterSigner(ctx, evm, stateDB, contract, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
case DASignersFunctionRegisterNextEpoch:
|
2024-10-24 13:23:45 +00:00
|
|
|
bz, err = d.RegisterNextEpoch(ctx, evm, stateDB, contract, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
case DASignersFunctionUpdateSocket:
|
2024-10-24 13:23:45 +00:00
|
|
|
bz, err = d.UpdateSocket(ctx, evm, stateDB, contract, method, args)
|
2024-05-09 18:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cost := ctx.GasMeter().GasConsumed() - initialGas
|
|
|
|
|
|
|
|
if !contract.UseGas(cost) {
|
|
|
|
return nil, vm.ErrOutOfGas
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|