Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teleporter CLI #137

Merged
merged 34 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
45ec606
init decoder
Nov 10, 2023
48c1401
initial decoder
Nov 10, 2023
7be72de
initial decoder
Nov 13, 2023
1fc795c
Merge remote-tracking branch 'origin/main' into decoder
Nov 14, 2023
3260392
decoding transaction Teleporter events
Nov 14, 2023
3f80609
cleaning up comments
Nov 14, 2023
3e12079
add documentation
Nov 15, 2023
6b6fee8
update licensing
Nov 15, 2023
ea0e8d6
Merge remote-tracking branch 'origin/main' into decoder
Nov 15, 2023
ed05abf
refactor event filtering into teleportermessenger package
Nov 15, 2023
d68d809
add abi bindings event
Nov 15, 2023
c833384
update logging
Nov 15, 2023
277d497
cleanup docs
Nov 15, 2023
f498dfe
Merge branch 'main' into decoder
Nov 15, 2023
bdaff96
log tele/warp message id
Nov 16, 2023
0b9ae75
update go work sum
Nov 22, 2023
aad2764
Merge remote-tracking branch 'origin/main' into decoder
Nov 22, 2023
e57ec79
Merge remote-tracking branch 'origin/main' into decoder
Nov 29, 2023
e80a174
extra pre run func and nits
Nov 29, 2023
5c84036
fix typo
Nov 29, 2023
2d0b810
restructure cli directory
Nov 29, 2023
1682fd3
Merge remote-tracking branch 'origin/main' into decoder
Nov 29, 2023
0c3685d
fix linting for go
Nov 29, 2023
42898b4
add event unit test
Nov 29, 2023
7ae5094
add unit tests for event and packing
Nov 29, 2023
fb71ca9
initial root and event tests
Nov 30, 2023
1b24a8d
getting event test to working state
Dec 1, 2023
dd3a74b
basic unit test and ci update
Dec 4, 2023
1cdb979
Merge remote-tracking branch 'origin/main' into decoder
Dec 4, 2023
a4aa24d
update test step name
Dec 4, 2023
bc3e528
Merge remote-tracking branch 'origin/main' into decoder
Dec 4, 2023
c9d2a97
Merge remote-tracking branch 'origin/main' into decoder
Dec 5, 2023
96215f9
remove extra logs
Dec 5, 2023
df5d4d9
update workflow name
Dec 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions .github/workflows/abi_bindings_checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,3 @@ jobs:

- name: Fail if diff exists
run: git --no-pager diff --quiet -- abi-bindings/**.go

unit_tests:
name: Unit tests
runs-on: ubuntu-20.04

steps:
- name: Checkout repositories and submodules
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set Go version
run: |
source ./scripts/versions.sh
GO_VERSION=$GO_VERSION >> $GITHUB_ENV

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Run ABI Binding Unit Tests
run: |
source scripts/constants.sh
cd abi-bindings/go
go test ./...
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ jobs:
export PATH=$PATH:$HOME/.foundry/bin
cd contracts/
forge test -vvv

go-unit-tests:
runs-on: ubuntu-20.04

steps:
- name: Checkout repositories and submodules
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set Go version
run: |
source ./scripts/versions.sh
GO_VERSION=$GO_VERSION >> $GITHUB_ENV

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Run ABI Binding Unit Tests
minghinmatthewlam marked this conversation as resolved.
Show resolved Hide resolved
run: |
source scripts/constants.sh
go test ./...
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Cargo.lock
# IDE configurations
.vscode*

# Binaries
cmd/teleporter-cli/teleporter-cli

# File generated by tests
relayerConfig.json
subnetGenesis_*
Expand Down
101 changes: 101 additions & 0 deletions abi-bindings/go/Teleporter/TeleporterMessenger/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package teleportermessenger

import (
"fmt"
"strings"

"github.com/ethereum/go-ethereum/common"
)

// Event is a Teleporter log event
type Event uint8

const (
Unknown Event = iota
SendCrossChainMessage
ReceiveCrossChainMessage
AddFeeAmount
MessageExecutionFailed
MessageExecuted
RelayerRewardsRedeemed

sendCrossChainMessageStr = "SendCrossChainMessage"
receiveCrossChainMessageStr = "ReceiveCrossChainMessage"
addFeeAmountStr = "AddFeeAmount"
messageExecutionFailedStr = "MessageExecutionFailed"
messageExecutedStr = "MessageExecuted"
relayerRewardsRedeemedStr = "RelayerRewardsRedeemed"
unknownStr = "Unknown"
)

// String returns the string representation of an Event
func (e Event) String() string {
switch e {
case SendCrossChainMessage:
return sendCrossChainMessageStr
case ReceiveCrossChainMessage:
return receiveCrossChainMessageStr
case AddFeeAmount:
return addFeeAmountStr
case MessageExecutionFailed:
return messageExecutionFailedStr
case MessageExecuted:
return messageExecutedStr
case RelayerRewardsRedeemed:
return relayerRewardsRedeemedStr
default:
return unknownStr
}
}

// ToEvent converts a string to an Event
func ToEvent(e string) (Event, error) {
switch strings.ToLower(e) {
case strings.ToLower(sendCrossChainMessageStr):
return SendCrossChainMessage, nil
case strings.ToLower(receiveCrossChainMessageStr):
return ReceiveCrossChainMessage, nil
case strings.ToLower(addFeeAmountStr):
return AddFeeAmount, nil
case strings.ToLower(messageExecutionFailedStr):
return MessageExecutionFailed, nil
case strings.ToLower(messageExecutedStr):
return MessageExecuted, nil
case strings.ToLower(relayerRewardsRedeemedStr):
return RelayerRewardsRedeemed, nil
default:
return Unknown, fmt.Errorf("unknown event %s", e)
}
}

// 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) {
e, err := ToEvent(event)
if err != nil {
return nil, err
}
var out interface{}
switch e {
case SendCrossChainMessage:
out = new(TeleporterMessengerSendCrossChainMessage)
case ReceiveCrossChainMessage:
out = new(TeleporterMessengerReceiveCrossChainMessage)
case AddFeeAmount:
out = new(TeleporterMessengerAddFeeAmount)
case MessageExecutionFailed:
out = new(TeleporterMessengerMessageExecutionFailed)
case MessageExecuted:
out = new(TeleporterMessengerMessageExecuted)
case RelayerRewardsRedeemed:
out = new(TeleporterMessengerRelayerRewardsRedeemed)
default:
return nil, fmt.Errorf("unknown event %s", e.String())
}
if err := UnpackEvent(out, e.String(), topics, data); err != nil {
return nil, err
}
return out, nil
}
143 changes: 143 additions & 0 deletions abi-bindings/go/Teleporter/TeleporterMessenger/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package teleportermessenger

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestEventString(t *testing.T) {
var (
tests = []struct {
event Event
str string
}{
{Unknown, unknownStr},
{SendCrossChainMessage, sendCrossChainMessageStr},
{ReceiveCrossChainMessage, receiveCrossChainMessageStr},
{AddFeeAmount, addFeeAmountStr},
{MessageExecutionFailed, messageExecutionFailedStr},
{MessageExecuted, messageExecutedStr},
{RelayerRewardsRedeemed, relayerRewardsRedeemedStr},
}
)

for _, test := range tests {
t.Run(test.event.String(), func(t *testing.T) {
require.Equal(t, test.event.String(), test.str)
})
}
}

func TestToEvent(t *testing.T) {
var (
tests = []struct {
str string
event Event
isError bool
}{
{unknownStr, Unknown, true},
{sendCrossChainMessageStr, SendCrossChainMessage, false},
{receiveCrossChainMessageStr, ReceiveCrossChainMessage, false},
{addFeeAmountStr, AddFeeAmount, false},
{messageExecutionFailedStr, MessageExecutionFailed, false},
{messageExecutedStr, MessageExecuted, false},
{relayerRewardsRedeemedStr, RelayerRewardsRedeemed, false},
}
)

for _, test := range tests {
t.Run(test.str, func(t *testing.T) {
event, err := ToEvent(test.str)
if test.isError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, test.event, event)
})
}
}

func TestFilterTeleporterEvents(t *testing.T) {
mockBlockchainID := [32]byte{1, 2, 3, 4}
messageID := big.NewInt(1)
message := createTestTeleporterMessage(messageID.Int64())
feeInfo := TeleporterFeeInfo{
FeeTokenAddress: common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567"),
Amount: big.NewInt(1),
}
deliverer := common.HexToAddress("0x0123456789abcdef0123456789abcdef01234567")

teleporterABI, err := TeleporterMessengerMetaData.GetAbi()
require.NoError(t, err)

var (
tests = []struct {
event Event
args []interface{}
expected interface{}
}{
{
event: SendCrossChainMessage,
args: []interface{}{
mockBlockchainID,
messageID,
message,
feeInfo,
},
expected: &TeleporterMessengerSendCrossChainMessage{
DestinationBlockchainID: mockBlockchainID,
MessageID: messageID,
Message: message,
FeeInfo: feeInfo,
},
},
{
event: ReceiveCrossChainMessage,
args: []interface{}{
mockBlockchainID,
messageID,
deliverer,
deliverer,
message,
},
expected: &TeleporterMessengerReceiveCrossChainMessage{
OriginBlockchainID: mockBlockchainID,
MessageID: messageID,
Deliverer: deliverer,
RewardRedeemer: deliverer,
Message: message,
},
},
{
event: MessageExecuted,
args: []interface{}{
mockBlockchainID,
messageID,
},
expected: &TeleporterMessengerMessageExecuted{
OriginBlockchainID: mockBlockchainID,
MessageID: messageID,
},
},
}
)

for _, test := range tests {
t.Run(test.event.String(), func(t *testing.T) {
topics, data, err := teleporterABI.PackEvent(test.event.String(), test.args...)
require.NoError(t, err)

out, err := FilterTeleporterEvents(topics, data, test.event.String())
require.NoError(t, err)

require.Equal(t, test.expected, out)
})
}
}
21 changes: 21 additions & 0 deletions abi-bindings/go/Teleporter/TeleporterMessenger/packing.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,24 @@ func PackMessageReceivedOutput(success bool) ([]byte, error) {

return abi.PackOutput("messageReceived", success)
}

// UnpackEvent unpacks the event data and topics into the provided interface
func UnpackEvent(out interface{}, event string, topics []common.Hash, data []byte) error {
teleporterABI, err := TeleporterMessengerMetaData.GetAbi()
if err != nil {
return fmt.Errorf("failed to get abi: %v", err)
}
if len(data) > 0 {
if err := teleporterABI.UnpackIntoInterface(out, event, data); err != nil {
return err
}
}

var indexed abi.Arguments
for _, arg := range teleporterABI.Events[event].Inputs {
if arg.Indexed {
indexed = append(indexed, arg)
}
}
return abi.ParseTopics(out, indexed, topics[1:])
}
Loading