Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tmpnet
Browse files Browse the repository at this point in the history
  • Loading branch information
feuGeneA committed Jul 11, 2024
2 parents 733c359 + 1420417 commit 999202a
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 41 deletions.
156 changes: 154 additions & 2 deletions abi-bindings/go/Teleporter/TeleporterMessenger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
package teleportermessenger

import (
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -78,12 +82,12 @@ func ToEvent(e string) (Event, error) {
}

// FilterTeleporterEvents parses the topics and data of a Teleporter log into the corresponding Teleporter event
func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (interface{}, error) {
func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (fmt.Stringer, error) {
e, err := ToEvent(event)
if err != nil {
return nil, err
}
var out interface{}
var out fmt.Stringer
switch e {
case SendCrossChainMessage:
out = new(TeleporterMessengerSendCrossChainMessage)
Expand All @@ -107,3 +111,151 @@ func FilterTeleporterEvents(topics []common.Hash, data []byte, event string) (in
}
return out, nil
}

func (t TeleporterMessengerSendCrossChainMessage) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerSendCrossChainMessage{
MessageID: common.Hash(t.MessageID),
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
Message: toReadableTeleporterMessage(t.Message),
FeeInfo: t.FeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerSendCrossChainMessage struct {
MessageID common.Hash
DestinationBlockchainID ids.ID
Message ReadableTeleporterMessage
FeeInfo TeleporterFeeInfo
Raw types.Log
}

func (t TeleporterMessengerReceiveCrossChainMessage) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerReceiveCrossChainMessage{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Deliverer: t.Deliverer,
RewardRedeemer: t.RewardRedeemer,
Message: toReadableTeleporterMessage(t.Message),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerReceiveCrossChainMessage struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Deliverer common.Address
RewardRedeemer common.Address
Message ReadableTeleporterMessage
Raw types.Log
}

func (t TeleporterMessengerAddFeeAmount) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerAddFeeAmount{
MessageID: common.Hash(t.MessageID),
UpdatedFeeInfo: t.UpdatedFeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerAddFeeAmount struct {
MessageID common.Hash
UpdatedFeeInfo TeleporterFeeInfo
Raw types.Log
}

func (t TeleporterMessengerMessageExecutionFailed) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerMessageExecutionFailed{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Message: toReadableTeleporterMessage(t.Message),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerMessageExecutionFailed struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Message ReadableTeleporterMessage
Raw types.Log
}

func (t TeleporterMessengerMessageExecuted) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerMessageExecuted{
MessageID: common.Hash(t.MessageID),
SourceBlockchainID: ids.ID(t.SourceBlockchainID),
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerMessageExecuted struct {
MessageID common.Hash
SourceBlockchainID ids.ID
Raw types.Log
}

func (t TeleporterMessengerRelayerRewardsRedeemed) String() string {
outJson, _ := json.MarshalIndent(t, "", " ")

return string(outJson)
}

func (t TeleporterMessengerReceiptReceived) String() string {
outJson, _ := json.MarshalIndent(ReadableTeleporterMessengerReceiptReceived{
MessageID: common.Hash(t.MessageID),
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
RelayerRewardAddress: t.RelayerRewardAddress,
FeeInfo: t.FeeInfo,
Raw: t.Raw,
}, "", " ")

return string(outJson)
}

type ReadableTeleporterMessengerReceiptReceived struct {
MessageID common.Hash
DestinationBlockchainID ids.ID
RelayerRewardAddress common.Address
FeeInfo TeleporterFeeInfo
Raw types.Log
}

func toReadableTeleporterMessage(t TeleporterMessage) ReadableTeleporterMessage {
return ReadableTeleporterMessage{
MessageNonce: t.MessageNonce,
OriginSenderAddress: t.OriginSenderAddress,
DestinationBlockchainID: ids.ID(t.DestinationBlockchainID),
DestinationAddress: t.DestinationAddress,
RequiredGasLimit: t.RequiredGasLimit,
AllowedRelayerAddresses: t.AllowedRelayerAddresses,
Receipts: t.Receipts,
Message: t.Message,
}
}

func (t TeleporterMessage) String() string {
outJson, _ := json.MarshalIndent(toReadableTeleporterMessage(t), "", " ")

return string(outJson)
}

type ReadableTeleporterMessage struct {
MessageNonce *big.Int
OriginSenderAddress common.Address
DestinationBlockchainID ids.ID
DestinationAddress common.Address
RequiredGasLimit *big.Int
AllowedRelayerAddresses []common.Address
Receipts []TeleporterMessageReceipt
Message []byte
}
3 changes: 2 additions & 1 deletion abi-bindings/go/Teleporter/TeleporterMessenger/packing.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func PackReceiveCrossChainMessage(messageIndex uint32, relayerRewardAddress comm
func PackCalculateMessageID(
sourceBlockchainID [32]byte,
destinationBlockchainID [32]byte,
nonce *big.Int) ([]byte, error) {
nonce *big.Int,
) ([]byte, error) {
abi, err := TeleporterMessengerMetaData.GetAbi()
if err != nil {
return nil, errors.Wrap(err, "failed to get abi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestPackUnpackTeleporterMessage(t *testing.T) {
func TestUnpackEvent(t *testing.T) {
mockBlockchainID := ids.ID{1, 2, 3, 4}
mockMessageNonce := big.NewInt(5)
mockMessageID := ids.ID{9, 10, 11, 12}
mockMessageID := common.Hash{9, 10, 11, 12}
message := createTestTeleporterMessage(mockMessageNonce)
feeInfo := TeleporterFeeInfo{
FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
Expand Down
2 changes: 1 addition & 1 deletion cmd/teleporter-cli/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func eventRun(cmd *cobra.Command, args []string) {

out, err := teleportermessenger.FilterTeleporterEvents(topics, data, event.Name)
cobra.CheckErr(err)
logger.Info("Parsed Teleporter event", zap.String("name", event.Name), zap.Any("event", out))
logger.Info("Parsed Teleporter event", zap.String("name", event.Name), zap.String("event", out.String()))
cmd.Println("Event command ran successfully for", event.Name)
}

Expand Down
129 changes: 93 additions & 36 deletions cmd/teleporter-cli/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ package main

import (
"context"
"encoding/json"

warpPayload "github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/eth/tracers"
"github.com/ava-labs/subnet-evm/ethclient"
"github.com/ava-labs/subnet-evm/precompile/contracts/warp"
teleportermessenger "github.com/ava-labs/teleporter/abi-bindings/go/Teleporter/TeleporterMessenger"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

const (
warpPrecompileAddress = "0x0200000000000000000000000000000000000005"
warpPrecompileAddressHex = "0x0200000000000000000000000000000000000005"
)

var (
debug bool
rpcEndpoint string
teleporterAddress common.Address
client ethclient.Client
Expand All @@ -30,46 +33,99 @@ var transactionCmd = &cobra.Command{
Short: "Parses relevant Teleporter logs from a transaction",
Long: `Given a transaction this command looks through the transaction's receipt
for Teleporter and Warp log events. When corresponding log events are found,
the command parses to log event fields to a more human readable format.`,
the command parses to log event fields to a more human readable format. Optionally pass -d
or --debug for extra transaction output. This may require enabling debug enpoints on your RPC node`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
receipt, err := client.TransactionReceipt(context.Background(),
common.HexToHash(args[0]))
cobra.CheckErr(err)

for _, log := range receipt.Logs {
if log.Address == teleporterAddress {
logger.Info("Processing Teleporter log", zap.Any("log", log))

event, err := teleporterABI.EventByID(log.Topics[0])
cobra.CheckErr(err)

out, err := teleportermessenger.FilterTeleporterEvents(log.Topics, log.Data, event.Name)
cobra.CheckErr(err)
logger.Info("Parsed Teleporter event", zap.String("name", event.Name), zap.Any("event", out))
}

if log.Address == common.HexToAddress(warpPrecompileAddress) {
logger.Debug("Processing Warp log", zap.Any("log", log))

unsignedMsg, err := warp.UnpackSendWarpEventDataToMessage(log.Data)
cobra.CheckErr(err)

warpPayload, err := warpPayload.ParseAddressedCall(unsignedMsg.Payload)
cobra.CheckErr(err)

teleporterMessage, err := teleportermessenger.UnpackTeleporterMessage(warpPayload.Payload)
cobra.CheckErr(err)
logger.Info("Parsed Teleporter message",
zap.String("warpMessageID", unsignedMsg.ID().Hex()),
zap.String("teleporterMessageNonce", teleporterMessage.MessageNonce.String()),
zap.Any("message", teleporterMessage))
}
txHash := common.HexToHash(args[0])
if debug {
printTransaction(cmd, txHash)
traceTransaction(cmd, txHash)
}
checkReceipt(cmd, txHash)
cmd.Println("Transaction command ran successfully")
},
}

func checkReceipt(cmd *cobra.Command, txHash common.Hash) {
receipt, err := client.TransactionReceipt(context.Background(), txHash)
cobra.CheckErr(err)

warpPrecompileAddress := common.HexToAddress(warpPrecompileAddressHex)
for _, log := range receipt.Logs {
switch log.Address {
case teleporterAddress:
printTeleporterLogs(cmd, log)
case warpPrecompileAddress:
printWarpLogs(cmd, log)
}
}
}

func printTeleporterLogs(cmd *cobra.Command, log *types.Log) {
logJson, err := json.MarshalIndent(log, "", " ")
cobra.CheckErr(err)

cmd.Println("Teleporter Log:\n" + string(logJson) + "\n")

event, err := teleporterABI.EventByID(log.Topics[0])
cobra.CheckErr(err)

out, err := teleportermessenger.FilterTeleporterEvents(log.Topics, log.Data, event.Name)
cobra.CheckErr(err)

cmd.Println(event.Name + " Log:")
cmd.Println(out.String() + "\n")
}

func printWarpLogs(cmd *cobra.Command, log *types.Log) {
logJson, err := json.MarshalIndent(log, "", " ")
cobra.CheckErr(err)

cmd.Println("Warp Log:\n" + string(logJson) + "\n")

unsignedMsg, err := warp.UnpackSendWarpEventDataToMessage(log.Data)
cobra.CheckErr(err)
cmd.Println("Warp Message ID: " + unsignedMsg.ID().Hex())

warpPayload, err := warpPayload.ParseAddressedCall(unsignedMsg.Payload)
cobra.CheckErr(err)

warpPayloadJson, err := json.MarshalIndent(warpPayload, "", " ")
cobra.CheckErr(err)
cmd.Println("Warp Payload:")
cmd.Println(string(warpPayloadJson))

teleporterMessage, err := teleportermessenger.UnpackTeleporterMessage(warpPayload.Payload)
cobra.CheckErr(err)

cmd.Println("Teleporter Message:")
cmd.Println(teleporterMessage.String())
}

func traceTransaction(cmd *cobra.Command, txHash common.Hash) {
var result interface{}
ct := "callTracer"
err := client.Client().Call(&result, "debug_traceTransaction", txHash.String(), tracers.TraceConfig{Tracer: &ct})
if err != nil {
cmd.PrintErr("Error calling debug_traceTransaction: " + err.Error())
return
}
json, err := json.MarshalIndent(result, "", " ")
cobra.CheckErr(err)

cmd.Println("Transaction Trace:\n" + string(json) + "\n")
}

func printTransaction(cmd *cobra.Command, txHash common.Hash) {
tx, _, err := client.TransactionByHash(context.Background(), txHash)
cobra.CheckErr(err)
json, err := json.MarshalIndent(tx, "", " ")
cobra.CheckErr(err)

cmd.Println("Transaction:\n" + string(json) + "\n")
}

func init() {
rootCmd.AddCommand(transactionCmd)
transactionCmd.PersistentFlags().StringVar(&rpcEndpoint, "rpc", "", "RPC endpoint to connect to the node")
Expand All @@ -78,6 +134,7 @@ func init() {
cobra.CheckErr(err)
err = transactionCmd.MarkPersistentFlagRequired("teleporter-address")
cobra.CheckErr(err)
transactionCmd.Flags().BoolVarP(&debug, "debug", "d", false, "default: false.")
transactionCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
return transactionPreRunE(cmd, args, address)
}
Expand All @@ -95,5 +152,5 @@ func transactionPreRunE(cmd *cobra.Command, args []string, address *string) erro
}

client = c
return err
return nil
}

0 comments on commit 999202a

Please sign in to comment.