Skip to content

Commit

Permalink
mv make topic
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed May 17, 2023
1 parent c774bcc commit 1d77749
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 2 deletions.
77 changes: 77 additions & 0 deletions contracts/src/Relayer.sol
Original file line number Diff line number Diff line change
@@ -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,
);
}
83 changes: 81 additions & 2 deletions x/cronos/keeper/precompiles/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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, &ethtypes.Log{
Topics: topics[0],
})
}
return ethLogs, nil
}

func unmarshalAndExec[T MsgType, U any](
bc *RelayerContract,
input []byte,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1d77749

Please sign in to comment.