Skip to content

Commit

Permalink
cleanup combo
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed May 27, 2024
1 parent 7b3be13 commit 87550e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 163 deletions.
105 changes: 18 additions & 87 deletions x/cronos/keeper/precompiles/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,24 @@ var (
)

const (
CreateClient = "createClient"
UpdateClient = "updateClient"
UpgradeClient = "upgradeClient"
SubmitMisbehaviour = "submitMisbehaviour"
ConnectionOpenInit = "connectionOpenInit"
ConnectionOpenTry = "connectionOpenTry"
ConnectionOpenAck = "connectionOpenAck"
ConnectionOpenConfirm = "connectionOpenConfirm"
ChannelOpenInit = "channelOpenInit"
ChannelOpenTry = "channelOpenTry"
ChannelOpenAck = "channelOpenAck"
ChannelOpenConfirm = "channelOpenConfirm"
ChannelCloseInit = "channelCloseInit"
ChannelCloseConfirm = "channelCloseConfirm"
RecvPacket = "recvPacket"
Acknowledgement = "acknowledgement"
Timeout = "timeout"
TimeoutOnClose = "timeoutOnClose"
UpdateClientAndConnectionOpenInit = "updateClientAndConnectionOpenInit"
UpdateClientAndConnectionOpenTry = "updateClientAndConnectionOpenTry"
UpdateClientAndConnectionOpenAck = "updateClientAndConnectionOpenAck"
UpdateClientAndConnectionOpenConfirm = "updateClientAndConnectionOpenConfirm"
UpdateClientAndChannelOpenInit = "updateClientAndChannelOpenInit"
UpdateClientAndChannelOpenTry = "updateClientAndChannelOpenTry"
UpdateClientAndChannelOpenAck = "updateClientAndChannelOpenAck"
UpdateClientAndChannelCloseInit = "updateClientAndChannelCloseInit"
UpdateClientAndChannelCloseConfirm = "updateClientAndChannelCloseConfirm"
UpdateClientAndChannelOpenConfirm = "updateClientAndChannelOpenConfirm"
UpdateClientAndRecvPacket = "updateClientAndRecvPacket"
UpdateClientAndAcknowledgement = "updateClientAndAcknowledgement"
UpdateClientAndTimeout = "updateClientAndTimeout"
UpdateClientAndTimeoutOnClose = "updateClientAndTimeoutOnClose"
CreateClient = "createClient"
UpdateClient = "updateClient"
UpgradeClient = "upgradeClient"
SubmitMisbehaviour = "submitMisbehaviour"
ConnectionOpenInit = "connectionOpenInit"
ConnectionOpenTry = "connectionOpenTry"
ConnectionOpenAck = "connectionOpenAck"
ConnectionOpenConfirm = "connectionOpenConfirm"
ChannelOpenInit = "channelOpenInit"
ChannelOpenTry = "channelOpenTry"
ChannelOpenAck = "channelOpenAck"
ChannelOpenConfirm = "channelOpenConfirm"
ChannelCloseInit = "channelCloseInit"
ChannelCloseConfirm = "channelCloseConfirm"
RecvPacket = "recvPacket"
Acknowledgement = "acknowledgement"
Timeout = "timeout"
TimeoutOnClose = "timeoutOnClose"

GasWhenReceiverChainIsSource = 51705
GasWhenReceiverChainIsNotSource = 144025
Expand Down Expand Up @@ -103,30 +89,6 @@ func init() {
relayerGasRequiredByMethod[methodID] = 61781
case Timeout:
relayerGasRequiredByMethod[methodID] = 104283
case UpdateClientAndConnectionOpenTry:
relayerGasRequiredByMethod[methodID] = 150362
case UpdateClientAndConnectionOpenConfirm:
relayerGasRequiredByMethod[methodID] = 124820
case UpdateClientAndChannelOpenTry:
relayerGasRequiredByMethod[methodID] = 182676
case UpdateClientAndChannelOpenConfirm:
relayerGasRequiredByMethod[methodID] = 132734
case UpdateClientAndRecvPacket:
relayerGasRequiredByMethod[methodID] = 257120
case UpdateClientAndConnectionOpenInit:
relayerGasRequiredByMethod[methodID] = 131649
case UpdateClientAndConnectionOpenAck:
relayerGasRequiredByMethod[methodID] = 141558
case UpdateClientAndChannelOpenInit:
relayerGasRequiredByMethod[methodID] = 180815
case UpdateClientAndChannelOpenAck:
relayerGasRequiredByMethod[methodID] = 133834
case UpdateClientAndChannelCloseConfirm:
relayerGasRequiredByMethod[methodID] = 143366
case UpdateClientAndTimeout:
relayerGasRequiredByMethod[methodID] = 230638
case UpdateClientAndAcknowledgement:
relayerGasRequiredByMethod[methodID] = 174785
default:
relayerGasRequiredByMethod[methodID] = 100000
}
Expand Down Expand Up @@ -238,9 +200,6 @@ func (bc *RelayerContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool
input: input,
converter: converter,
}
if len(args) > 1 {
e.input2 = args[1].([]byte)
}
switch method.Name {
case CreateClient:
res, err = exec(e, bc.ibcKeeper.CreateClient)
Expand Down Expand Up @@ -276,34 +235,6 @@ func (bc *RelayerContract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool
res, err = exec(e, bc.ibcKeeper.Timeout)
case TimeoutOnClose:
res, err = exec(e, bc.ibcKeeper.TimeoutOnClose)
case UpdateClientAndConnectionOpenInit:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ConnectionOpenInit)
case UpdateClientAndConnectionOpenTry:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ConnectionOpenTry)
case UpdateClientAndConnectionOpenAck:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ConnectionOpenAck)
case UpdateClientAndConnectionOpenConfirm:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ConnectionOpenConfirm)
case UpdateClientAndChannelOpenInit:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelOpenInit)
case UpdateClientAndChannelOpenTry:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelOpenTry)
case UpdateClientAndChannelCloseInit:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelCloseInit)
case UpdateClientAndChannelCloseConfirm:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelCloseConfirm)
case UpdateClientAndChannelOpenAck:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelOpenAck)
case UpdateClientAndChannelOpenConfirm:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.ChannelOpenConfirm)
case UpdateClientAndRecvPacket:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.RecvPacket)
case UpdateClientAndAcknowledgement:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.Acknowledgement)
case UpdateClientAndTimeout:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.Timeout)
case UpdateClientAndTimeoutOnClose:
res, err = execMultiple(e, bc.ibcKeeper.UpdateClient, bc.ibcKeeper.TimeoutOnClose)
default:
return nil, fmt.Errorf("unknown method: %s", method.Name)
}
Expand Down
76 changes: 0 additions & 76 deletions x/cronos/keeper/precompiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type Executor struct {
caller common.Address
contract common.Address
input []byte
input2 []byte
converter statedb.EventConverter
}

Expand Down Expand Up @@ -64,78 +63,3 @@ func exec[Req any, PReq interface {
}
return output, nil
}

func execMultipleWithHooks[Req any,
PReq interface {
*Req
NativeMessage
},
Resp proto.Message,
Req2 any,
PReq2 interface {
*Req2
NativeMessage
},
Resp2 proto.Message,
](
e *Executor,
preAction func(sdk.Context, PReq, PReq2) error,
action func(context.Context, PReq) (Resp, error),
action2 func(context.Context, PReq2) (Resp2, error),
) ([]byte, error) {
msg := PReq(new(Req))
if err := e.cdc.Unmarshal(e.input, msg); err != nil {
return nil, fmt.Errorf("fail to Unmarshal %T %w", msg, err)
}

msg2 := PReq2(new(Req2))
if err := e.cdc.Unmarshal(e.input2, msg2); err != nil {
return nil, fmt.Errorf("fail to Unmarshal %T %w", msg2, err)
}

signers := msg.GetSigners()
if len(signers) != 1 {
return nil, fmt.Errorf("expected 1 signer, got %d", len(signers))
}
if common.BytesToAddress(signers[0].Bytes()) != e.caller {
return nil, errors.New("caller is not authenticated")
}

var res Resp
if err := e.stateDB.ExecuteNativeAction(e.contract, e.converter, func(ctx sdk.Context) (err error) {
if preAction != nil {
if err = preAction(ctx, msg, msg2); err != nil {
return err
}
}

res, err = action(ctx, msg)
if err == nil && len(e.input2) > 0 {
_, err = action2(ctx, msg2)
}
return
}); err != nil {
return nil, err
}
return e.cdc.Marshal(res)
}

func execMultiple[Req any,
PReq interface {
*Req
NativeMessage
},
Resp proto.Message,
Req2 any,
PReq2 interface {
*Req2
NativeMessage
},
Resp2 proto.Message,
](
e *Executor,
action func(context.Context, PReq) (Resp, error),
action2 func(context.Context, PReq2) (Resp2, error),
) ([]byte, error) {
return execMultipleWithHooks(e, nil, action, action2)
}

0 comments on commit 87550e0

Please sign in to comment.