From 1d77749dca8786cd2f931b30dbbca51f2eaed342 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 17 May 2023 15:02:25 +0800 Subject: [PATCH] mv make topic --- contracts/src/Relayer.sol | 77 ++++++++++++++++++++++++ x/cronos/keeper/precompiles/relayer.go | 83 +++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 contracts/src/Relayer.sol diff --git a/contracts/src/Relayer.sol b/contracts/src/Relayer.sol new file mode 100644 index 0000000000..bf5a01fbb1 --- /dev/null +++ b/contracts/src/Relayer.sol @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +library Cosmos { + struct Coin { + uint256 amount; + string denom; + } +} + +interface IRelayerModule { + // Client + event CreateClient(string indexed client_id, string indexed client_type, string indexed consensus_height); + event UpdateClient(string indexed client_id, string indexed client_type, string indexed consensus_height); + event UpgradeClient(string indexed client_id, string indexed client_type, string indexed consensus_height); + event SubmitMisbehaviour(string indexed subject_id, string indexed client_type); + // Connection + event ConnectionOpenInit(string indexed connection_id, string indexed client_id, string indexed counterparty_client_id, string indexed counterparty_connection_id); + event ConnectionOpenTry(string indexed connection_id, string indexed client_id, string indexed counterparty_client_id, string indexed counterparty_connection_id); + event ConnectionOpenAck(string indexed connection_id, string indexed client_id, string indexed counterparty_client_id, string indexed counterparty_connection_id); + event ConnectionOpenConfirm(string indexed connection_id, string indexed client_id, string indexed counterparty_client_id, string indexed counterparty_connection_id); + // Channel + event ChannelOpenInit(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id, string version); + event ChannelOpenTry(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id, string version); + event ChannelOpenAck(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id); + event ChannelOpenConfirm(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id); + event ChannelCloseInit(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id); + event ChannelCloseConfirm(string indexed port_id, string indexed channel_id, string indexed counterparty_port_id, string indexed counterparty_channel_id, string connection_id); + event RecvPacket( + string indexed packet_data, + string indexed packet_data_hex, + string indexed packet_timeout_height, + string indexed packet_timeout_timestamp, + string packet_sequence, + string packet_src_port, + string packet_src_channel, + string packet_dst_port, + string packet_dst_channel, + string packet_channel_ordering, + string packet_connection, + ); + event WriteAck( + string indexed packet_data, + string indexed packet_data_hex, + string indexed packet_timeout_height, + string indexed packet_timeout_timestamp, + string packet_sequence, + string packet_src_port, + string packet_src_channel, + string packet_dst_port, + string packet_dst_channel, + string packet_ack, + string packet_ack_hex, + string packet_connection, + ); + event AcknowledgePacket( + string indexed packet_timeout_height, + string indexed packet_timeout_timestamp, + string indexed packet_sequence, + string indexed packet_src_port, + string packet_src_channel, + string packet_dst_port, + string packet_dst_channel, + string packet_channel_ordering, + string packet_connection, + ); + event TimeoutPacket( + string indexed packet_timeout_height, + string indexed packet_timeout_timestamp, + string indexed packet_sequence, + string indexed packet_src_port, + string packet_src_channel, + string packet_dst_port, + string packet_dst_channel, + string packet_channel_ordering, + ); +} diff --git a/x/cronos/keeper/precompiles/relayer.go b/x/cronos/keeper/precompiles/relayer.go index e85a49036b..3de6f61de1 100644 --- a/x/cronos/keeper/precompiles/relayer.go +++ b/x/cronos/keeper/precompiles/relayer.go @@ -6,11 +6,12 @@ import ( "errors" "fmt" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" - "github.com/evmos/ethermint/rpc/backend" "github.com/evmos/ethermint/x/evm/keeper/precompiles" "github.com/gogo/protobuf/proto" + abci "github.com/tendermint/tendermint/abci/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -19,6 +20,7 @@ import ( chantypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper" tmtypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types" + ethtypes "github.com/ethereum/go-ethereum/core/types" ) type RelayerContract struct { @@ -78,6 +80,83 @@ type MsgType interface { *chantypes.MsgChannelOpenInit | *chantypes.MsgChannelOpenTry | *chantypes.MsgChannelOpenAck | *chantypes.MsgChannelOpenConfirm | *chantypes.MsgRecvPacket | *chantypes.MsgAcknowledgement | *chantypes.MsgTimeout | *chantypes.MsgTimeoutOnClose } +// AllLogsFromEvents parses all ethereum logs from cosmos events +func AllLogsFromEvents(events []abci.Event) ([][]*ethtypes.Log, error) { + allLogs := make([][]*ethtypes.Log, 0, 4) + for _, event := range events { + logs, err := ParseTxLogsFromEvent(event) + if err != nil { + return nil, err + } + + allLogs = append(allLogs, logs) + } + return allLogs, nil +} + +// ParseTxLogsFromEvent parse tx logs from one event +func ParseTxLogsFromEvent(event abci.Event) ([]*ethtypes.Log, error) { + var ethLogs []*ethtypes.Log + type args struct { + query [][]interface{} + } + var method string + switch event.Type { + case clienttypes.EventTypeCreateClient: + method = "CreateClient(string,string,string)" + case clienttypes.EventTypeUpdateClient: + method = "UpdateClient(string,string,string)" + case clienttypes.EventTypeUpgradeClient: + method = "UpgradeClient(string,string,string)" + case clienttypes.EventTypeSubmitMisbehaviour: + method = "SubmitMisbehaviour(string,string)" + case conntypes.EventTypeConnectionOpenInit: + method = "ConnectionOpenInit(string,string,string,string)" + case conntypes.EventTypeConnectionOpenTry: + method = "ConnectionOpenTry(string,string,string,string)" + case conntypes.EventTypeConnectionOpenAck: + method = "ConnectionOpenAck(string,string,string,string)" + case conntypes.EventTypeConnectionOpenConfirm: + method = "ConnectionOpenConfirm(string,string,string,string)" + case chantypes.EventTypeChannelOpenInit: + method = "ChannelOpenInit(string,string,string,string,string,string)" + case chantypes.EventTypeChannelOpenTry: + method = "ChannelOpenTry(string,string,string,string,string,string)" + case chantypes.EventTypeChannelOpenAck: + method = "ChannelOpenAck(string,string,string,string,string)" + case chantypes.EventTypeChannelOpenConfirm: + method = "ChannelOpenConfirm(string,string,string,string,string)" + case chantypes.EventTypeChannelCloseInit: + method = "ChannelCloseInit(string,string,string,string,string)" + case chantypes.EventTypeChannelCloseConfirm: + method = "ChannelCloseConfirm(string,string,string,string,string)" + case chantypes.EventTypeRecvPacket: + method = "RecvPacket(string,string,string,string,string,string,string,string,string,string,string)" + case chantypes.EventTypeWriteAck: + method = "WriteAck(string,string,string,string,string,string,string,string,string,string,string,string)" + case chantypes.EventTypeAcknowledgePacket: + method = "AcknowledgePacket(string,string,string,string,string,string,string,string,string)" + case chantypes.EventTypeTimeoutPacket: + method = "TimeoutPacket(string,string,string,string,string,string,string,string)" + } + + arg := []interface{}{method} + for _, attr := range event.Attributes { + arg = append(arg, string(attr.Value)) + } + params := args{[][]interface{}{arg}} + topics, err := abi.MakeTopics(params.query...) + if err != nil { + return nil, err + } + if len(topics) > 0 { + ethLogs = append(ethLogs, ðtypes.Log{ + Topics: topics[0], + }) + } + return ethLogs, nil +} + func unmarshalAndExec[T MsgType, U any]( bc *RelayerContract, input []byte, @@ -139,7 +218,7 @@ func unmarshalAndExec[T MsgType, U any]( _, err := callback(ctx, msg) events := ctx.EventManager().Events() if len(events) > startEventIdx { - txLogs, err := backend.AllLogsFromEvents(events[startEventIdx:].ToABCIEvents()) + txLogs, err := AllLogsFromEvents(events[startEventIdx:].ToABCIEvents()) if err != nil { return err }