0g-chain/precompiles/dasigners/tx.go

99 lines
2.4 KiB
Go
Raw Normal View History

2024-05-09 18:54:47 +00:00
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"
2024-10-24 13:23:45 +00:00
precopmiles_common "github.com/0glabs/0g-chain/precompiles/common"
2024-05-09 18:54:47 +00:00
)
2024-10-24 13:23:45 +00:00
func (d *DASignersPrecompile) RegisterSigner(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-05-09 18:54:47 +00:00
msg, err := NewMsgRegisterSigner(args)
if err != nil {
return nil, err
}
// validation
2024-10-24 13:23:45 +00:00
sender := precopmiles_common.ToLowerHexWithoutPrefix(evm.Origin)
2024-05-09 18:54:47 +00:00
if sender != msg.Signer.Account {
return nil, fmt.Errorf(ErrInvalidSender, sender, msg.Signer.Account)
}
2024-10-24 13:23:45 +00:00
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
}
2024-05-09 18:54:47 +00:00
// 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()
}
2024-10-24 13:23:45 +00:00
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))
2024-05-09 18:54:47 +00:00
if err != nil {
return nil, err
}
2024-10-24 13:23:45 +00:00
// validation
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
}
2024-05-09 18:54:47 +00:00
// execute
_, err = d.dasignersKeeper.RegisterNextEpoch(sdk.WrapSDKContext(ctx), msg)
if err != nil {
return nil, err
}
return method.Outputs.Pack()
}
2024-10-24 13:23:45 +00:00
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))
2024-05-09 18:54:47 +00:00
if err != nil {
return nil, err
}
2024-10-24 13:23:45 +00:00
// validation
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precopmiles_common.ErrSenderNotOrigin)
}
2024-05-09 18:54:47 +00:00
// 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()
}