mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 08:45:19 +00:00
99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
package dasigners
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
precopmiles_common "github.com/0glabs/0g-chain/precompiles/common"
|
|
)
|
|
|
|
func (d *DASignersPrecompile) RegisterSigner(
|
|
ctx sdk.Context,
|
|
evm *vm.EVM,
|
|
stateDB *statedb.StateDB,
|
|
contract *vm.Contract,
|
|
method *abi.Method,
|
|
args []interface{},
|
|
) ([]byte, error) {
|
|
msg, err := NewMsgRegisterSigner(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// validation
|
|
sender := precopmiles_common.ToLowerHexWithoutPrefix(evm.Origin)
|
|
if sender != msg.Signer.Account {
|
|
return nil, fmt.Errorf(ErrInvalidSender, sender, msg.Signer.Account)
|
|
}
|
|
if contract.CallerAddress != evm.Origin {
|
|
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
|
|
}
|
|
// execute
|
|
_, err = d.dasignersKeeper.RegisterSigner(sdk.WrapSDKContext(ctx), msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// emit events
|
|
err = d.EmitNewSignerEvent(ctx, stateDB, args[0].(IDASignersSignerDetail))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return method.Outputs.Pack()
|
|
}
|
|
|
|
func (d *DASignersPrecompile) RegisterNextEpoch(
|
|
ctx sdk.Context,
|
|
evm *vm.EVM,
|
|
stateDB *statedb.StateDB,
|
|
contract *vm.Contract,
|
|
method *abi.Method,
|
|
args []interface{},
|
|
) ([]byte, error) {
|
|
msg, err := NewMsgRegisterNextEpoch(args, precopmiles_common.ToLowerHexWithoutPrefix(evm.Origin))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// validation
|
|
if contract.CallerAddress != evm.Origin {
|
|
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
|
|
}
|
|
// execute
|
|
_, err = d.dasignersKeeper.RegisterNextEpoch(sdk.WrapSDKContext(ctx), msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return method.Outputs.Pack()
|
|
}
|
|
|
|
func (d *DASignersPrecompile) UpdateSocket(
|
|
ctx sdk.Context,
|
|
evm *vm.EVM,
|
|
stateDB *statedb.StateDB,
|
|
contract *vm.Contract,
|
|
method *abi.Method,
|
|
args []interface{},
|
|
) ([]byte, error) {
|
|
msg, err := NewMsgUpdateSocket(args, precopmiles_common.ToLowerHexWithoutPrefix(evm.Origin))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// validation
|
|
if contract.CallerAddress != evm.Origin {
|
|
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
|
|
}
|
|
// execute
|
|
_, err = d.dasignersKeeper.UpdateSocket(sdk.WrapSDKContext(ctx), msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// emit events
|
|
err = d.EmitSocketUpdatedEvent(ctx, stateDB, evm.Origin, args[0].(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return method.Outputs.Pack()
|
|
}
|