From 752b6e3bfa9ff79e4fd5c47e60239065a592acb3 Mon Sep 17 00:00:00 2001 From: Geoff Stuart Date: Mon, 8 Jul 2024 11:45:01 -0400 Subject: [PATCH] Use stringer output --- .../Teleporter/TeleporterMessenger/event.go | 111 ++++++++++++++++-- .../TeleporterMessenger/event_test.go | 10 +- .../TeleporterMessenger/packing_test.go | 35 ++---- cmd/teleporter-cli/event.go | 2 +- cmd/teleporter-cli/transaction.go | 10 +- 5 files changed, 117 insertions(+), 51 deletions(-) diff --git a/abi-bindings/go/Teleporter/TeleporterMessenger/event.go b/abi-bindings/go/Teleporter/TeleporterMessenger/event.go index bb4a23bf0..97fe20751 100644 --- a/abi-bindings/go/Teleporter/TeleporterMessenger/event.go +++ b/abi-bindings/go/Teleporter/TeleporterMessenger/event.go @@ -4,6 +4,7 @@ package teleportermessenger import ( + "encoding/json" "fmt" "math/big" "strings" @@ -81,27 +82,27 @@ 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(ReadableTeleporterMessengerSendCrossChainMessage) + out = new(TeleporterMessengerSendCrossChainMessage) case ReceiveCrossChainMessage: - out = new(ReadableTeleporterMessengerReceiveCrossChainMessage) + out = new(TeleporterMessengerReceiveCrossChainMessage) case AddFeeAmount: - out = new(ReadableTeleporterMessengerAddFeeAmount) + out = new(TeleporterMessengerAddFeeAmount) case MessageExecutionFailed: - out = new(ReadableTeleporterMessengerMessageExecutionFailed) + out = new(TeleporterMessengerMessageExecutionFailed) case MessageExecuted: - out = new(ReadableTeleporterMessengerMessageExecuted) + out = new(TeleporterMessengerMessageExecuted) case RelayerRewardsRedeemed: out = new(TeleporterMessengerRelayerRewardsRedeemed) case ReceiptReceived: - out = new(ReadableTeleporterMessengerReceiptReceived) + out = new(TeleporterMessengerReceiptReceived) default: return nil, fmt.Errorf("unknown event %s", e.String()) } @@ -111,6 +112,18 @@ 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 @@ -119,6 +132,19 @@ type ReadableTeleporterMessengerSendCrossChainMessage struct { 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 @@ -128,12 +154,33 @@ type ReadableTeleporterMessengerReceiveCrossChainMessage struct { 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 @@ -141,12 +188,40 @@ type ReadableTeleporterMessengerMessageExecutionFailed struct { 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 @@ -155,7 +230,25 @@ type ReadableTeleporterMessengerReceiptReceived struct { Raw types.Log } -// TeleporterMessage is an auto generated low-level Go binding around an user-defined struct. +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 diff --git a/abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go b/abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go index 324f3a663..de2f29e02 100644 --- a/abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go +++ b/abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go @@ -68,8 +68,8 @@ func TestToEvent(t *testing.T) { func TestFilterTeleporterEvents(t *testing.T) { mockBlockchainID := ids.ID{1, 2, 3, 4} mockMessageNonce := big.NewInt(8) - mockMessageID := common.Hash{9, 10, 11, 12} - message := createTestReadableTeleporterMessage(mockMessageNonce) + mockMessageID := ids.ID{9, 10, 11, 12} + message := createTestTeleporterMessage(mockMessageNonce) feeInfo := TeleporterFeeInfo{ FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), Amount: big.NewInt(1), @@ -93,7 +93,7 @@ func TestFilterTeleporterEvents(t *testing.T) { message, feeInfo, }, - expected: &ReadableTeleporterMessengerSendCrossChainMessage{ + expected: &TeleporterMessengerSendCrossChainMessage{ MessageID: mockMessageID, DestinationBlockchainID: mockBlockchainID, Message: message, @@ -109,7 +109,7 @@ func TestFilterTeleporterEvents(t *testing.T) { deliverer, message, }, - expected: &ReadableTeleporterMessengerReceiveCrossChainMessage{ + expected: &TeleporterMessengerReceiveCrossChainMessage{ MessageID: mockMessageID, SourceBlockchainID: mockBlockchainID, Deliverer: deliverer, @@ -123,7 +123,7 @@ func TestFilterTeleporterEvents(t *testing.T) { mockMessageID, mockBlockchainID, }, - expected: &ReadableTeleporterMessengerMessageExecuted{ + expected: &TeleporterMessengerMessageExecuted{ MessageID: mockMessageID, SourceBlockchainID: mockBlockchainID, }, diff --git a/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go b/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go index f37557e2e..2b387c64f 100644 --- a/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go +++ b/abi-bindings/go/Teleporter/TeleporterMessenger/packing_test.go @@ -34,27 +34,6 @@ func createTestTeleporterMessage(messageNonce *big.Int) TeleporterMessage { return m } -func createTestReadableTeleporterMessage(messageNonce *big.Int) ReadableTeleporterMessage { - m := ReadableTeleporterMessage{ - MessageNonce: messageNonce, - OriginSenderAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), - DestinationBlockchainID: ids.ID{1, 2, 3, 4}, - DestinationAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), - RequiredGasLimit: big.NewInt(2), - AllowedRelayerAddresses: []common.Address{ - common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), - }, - Receipts: []TeleporterMessageReceipt{ - { - ReceivedMessageNonce: big.NewInt(1), - RelayerRewardAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), - }, - }, - Message: []byte{1, 2, 3, 4}, - } - return m -} - func TestPackUnpackTeleporterMessage(t *testing.T) { message := createTestTeleporterMessage(big.NewInt(4)) @@ -86,7 +65,7 @@ func TestUnpackEvent(t *testing.T) { mockBlockchainID := ids.ID{1, 2, 3, 4} mockMessageNonce := big.NewInt(5) mockMessageID := common.Hash{9, 10, 11, 12} - message := createTestReadableTeleporterMessage(mockMessageNonce) + message := createTestTeleporterMessage(mockMessageNonce) feeInfo := TeleporterFeeInfo{ FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"), Amount: big.NewInt(1), @@ -111,8 +90,8 @@ func TestUnpackEvent(t *testing.T) { message, feeInfo, }, - out: new(ReadableTeleporterMessengerSendCrossChainMessage), - expected: &ReadableTeleporterMessengerSendCrossChainMessage{ + out: new(TeleporterMessengerSendCrossChainMessage), + expected: &TeleporterMessengerSendCrossChainMessage{ DestinationBlockchainID: mockBlockchainID, MessageID: mockMessageID, Message: message, @@ -128,8 +107,8 @@ func TestUnpackEvent(t *testing.T) { deliverer, message, }, - out: new(ReadableTeleporterMessengerReceiveCrossChainMessage), - expected: &ReadableTeleporterMessengerReceiveCrossChainMessage{ + out: new(TeleporterMessengerReceiveCrossChainMessage), + expected: &TeleporterMessengerReceiveCrossChainMessage{ SourceBlockchainID: mockBlockchainID, MessageID: mockMessageID, Deliverer: deliverer, @@ -143,8 +122,8 @@ func TestUnpackEvent(t *testing.T) { mockMessageID, mockBlockchainID, }, - out: new(ReadableTeleporterMessengerMessageExecuted), - expected: &ReadableTeleporterMessengerMessageExecuted{ + out: new(TeleporterMessengerMessageExecuted), + expected: &TeleporterMessengerMessageExecuted{ MessageID: mockMessageID, SourceBlockchainID: mockBlockchainID, }, diff --git a/cmd/teleporter-cli/event.go b/cmd/teleporter-cli/event.go index 0dd50eb28..058066c71 100644 --- a/cmd/teleporter-cli/event.go +++ b/cmd/teleporter-cli/event.go @@ -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) } diff --git a/cmd/teleporter-cli/transaction.go b/cmd/teleporter-cli/transaction.go index 7f3738971..08da40db8 100644 --- a/cmd/teleporter-cli/transaction.go +++ b/cmd/teleporter-cli/transaction.go @@ -74,11 +74,8 @@ func printTeleporterLogs(cmd *cobra.Command, log *types.Log) { out, err := teleportermessenger.FilterTeleporterEvents(log.Topics, log.Data, event.Name) cobra.CheckErr(err) - outJson, err := json.MarshalIndent(out, "", " ") - cobra.CheckErr(err) - cmd.Println(event.Name + " Log:") - cmd.Println(string(outJson) + "\n") + cmd.Println(out.String() + "\n") } func printWarpLogs(cmd *cobra.Command, log *types.Log) { @@ -102,11 +99,8 @@ func printWarpLogs(cmd *cobra.Command, log *types.Log) { teleporterMessage, err := teleportermessenger.UnpackTeleporterMessage(warpPayload.Payload) cobra.CheckErr(err) - teleporterMessageJson, err := json.MarshalIndent(teleporterMessage, "", " ") - cobra.CheckErr(err) - cmd.Println("Teleporter Message:") - cmd.Println(string(teleporterMessageJson)) + cmd.Println(teleporterMessage.String()) } func traceTransaction(cmd *cobra.Command, txHash common.Hash) {