Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
92 changes: 87 additions & 5 deletions relayer/logpoller/tx_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"

Expand All @@ -13,6 +14,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/types/query"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"

module_offramp "github.com/smartcontractkit/chainlink-aptos/bindings/ccip_offramp/offramp"
codec0 "github.com/smartcontractkit/chainlink-aptos/codec"
"github.com/smartcontractkit/chainlink-aptos/relayer/chainreader/db"
crutils "github.com/smartcontractkit/chainlink-aptos/relayer/chainreader/utils"
Expand Down Expand Up @@ -180,9 +182,10 @@ func (l *AptosLogPoller) syncTransmitterTxs(ctx context.Context, transmitter apt
boundAddress := parts[0]
moduleName := parts[1]
expectedFunction := fmt.Sprintf("%s::%s::execute", boundAddress, moduleName)
// if the vmStatus of the failed tx contains the following,
// we ignore the tx because it was reverted before the receiver
ignoredVmError := fmt.Sprintf("%s::%s", boundAddress, moduleName)
var offRampAddress aptos.AccountAddress
if err := offRampAddress.ParseStringRelaxed(boundAddress); err != nil {
return 0, fmt.Errorf("parse offramp address %s: %w", boundAddress, err)
}

select {
case <-ctx.Done():
Expand Down Expand Up @@ -243,8 +246,9 @@ func (l *AptosLogPoller) syncTransmitterTxs(ctx context.Context, transmitter apt
continue
}

// Skip if error is from the module itself
if strings.Contains(userTxn.VmStatus, ignoredVmError) {
// Skip only a Move abort the VM placed in the offramp at one of the
// codes asserted before the receiver runs. See preDispatchAbort.
if preDispatchAbort(userTxn.VmStatus, offRampAddress, moduleName) {
l.lggr.Debugw("Skipping non-receiver originated transaction", "transmitter", transmitter.String(),
"sequenceNumber", userTxn.SequenceNumber, "vmStatus", userTxn.VmStatus)
continue
Expand Down Expand Up @@ -484,3 +488,81 @@ func (l *AptosLogPoller) getSourceChainConfig(ctx context.Context, sourceChainSe

return &configEvent.SourceChainConfig, nil
}

// moveAbortPrefix opens the status a fullnode reports for a Move abort, as in
// "Move abort in 0xa1::offramp: 0x5000b". A node holding the module's error
// map names the code, "E_SOURCE_CHAIN_NOT_ENABLED(0x5000b)". Runtime failures
// open with "Execution failed in " and name no abort code.
const moveAbortPrefix = "Move abort in "

// preDispatchAbort reports whether vmStatus is a Move abort the VM placed in the
// bound offramp at one of the seven codes asserted before the receiver runs. A
// receiver re-entering the module can raise any other abort there.
func preDispatchAbort(vmStatus string, offRamp aptos.AccountAddress, module string) bool {
rest, ok := strings.CutPrefix(vmStatus, moveAbortPrefix)
if !ok {
return false
}
location, codeText, ok := strings.Cut(rest, ": ")
if !ok {
return false
}
// The location is "0xADDR::module" or "0xADDR::module::function".
segments := strings.Split(location, "::")
if len(segments) < 2 {
return false
}
var address aptos.AccountAddress
if err := address.ParseStringRelaxed(segments[0]); err != nil {
return false
}
if address != offRamp || segments[1] != module {
return false
}
code, ok := abortCode(codeText)
if !ok {
return false
}
// A canonical abort code is (category << 16) + reason, where the reason is
// the module constant and the category is the std::error constructor it is
// raised under. invalid_argument is 0x1 and permission_denied is 0x5.
const (
invalidArgument uint64 = 0x1 << 16
invalidState uint64 = 0x3 << 16
permissionDenied uint64 = 0x5 << 16
)
switch code {
case invalidArgument + module_offramp.E_UNKNOWN_SOURCE_CHAIN_SELECTOR,
invalidArgument + module_offramp.E_TOKEN_DATA_MISMATCH,
invalidArgument + module_offramp.E_ROOT_NOT_COMMITTED,
permissionDenied + module_offramp.E_SOURCE_CHAIN_NOT_ENABLED,
invalidState + module_offramp.E_UNSUPPORTED_TOKEN,
invalidState + module_offramp.E_FUNGIBLE_ASSET_TYPE_MISMATCH,
invalidState + module_offramp.E_FUNGIBLE_ASSET_AMOUNT_MISMATCH:
return true
}
return false
}

// abortCode reads the abort code out of the text that follows a Move abort's
// location. A node with the module's error map renders "E_NAME(0x10009)", one
// without it renders the bare "0x10009"; the offramp publishes no error map.
func abortCode(codeText string) (uint64, bool) {
hexText := codeText
if open := strings.IndexByte(codeText, '('); open >= 0 {
end := strings.IndexByte(codeText[open+1:], ')')
if end < 0 {
return 0, false
}
hexText = codeText[open+1 : open+1+end]
}
digits, ok := strings.CutPrefix(hexText, "0x")
if !ok {
return 0, false
}
code, err := strconv.ParseUint(digits, 16, 64)
if err != nil {
return 0, false
}
return code, true
}
141 changes: 141 additions & 0 deletions relayer/logpoller/tx_poller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package logpoller

import (
"testing"

aptos "github.com/aptos-labs/aptos-go-sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPreDispatchAbort(t *testing.T) {
t.Parallel()

var offRamp aptos.AccountAddress
require.NoError(t, offRamp.ParseStringRelaxed("0xa1"))
zeroPaddedOffRamp := "0x00000000000000000000000000000000000000000000000000000000000000a1"

tests := []struct {
name string
vmStatus string
want bool
}{
{
name: "unknown source chain selector is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_UNKNOWN_SOURCE_CHAIN_SELECTOR(0x10004) at code offset 30",
want: true,
},
{
name: "token data mismatch is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_TOKEN_DATA_MISMATCH(0x10008)",
want: true,
},
{
name: "root not committed is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_ROOT_NOT_COMMITTED(0x10009) at code offset 12",
want: true,
},
{
name: "source chain not enabled is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_SOURCE_CHAIN_NOT_ENABLED(0x5000b)",
want: true,
},
{
name: "unsupported token is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_UNSUPPORTED_TOKEN(0x30011)",
want: true,
},
{
name: "fungible asset type mismatch is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_FUNGIBLE_ASSET_TYPE_MISMATCH(0x30014)",
want: true,
},
{
name: "fungible asset amount mismatch is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_FUNGIBLE_ASSET_AMOUNT_MISMATCH(0x30015)",
want: true,
},
{
name: "bare token pool abort code is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: 0x30011",
want: true,
},
{
name: "zero padded offramp address still places the abort",
vmStatus: "Move abort in " + zeroPaddedOffRamp + "::offramp: E_ROOT_NOT_COMMITTED(0x10009)",
want: true,
},
{
name: "bare abort code is pre-dispatch when the code is",
vmStatus: "Move abort in 0xa1::offramp: 0x10009",
want: true,
},
{
name: "bare abort code with zero padded address is pre-dispatch",
vmStatus: "Move abort in " + zeroPaddedOffRamp + "::offramp: 0x10004",
want: true,
},
{
name: "named abort code with description is pre-dispatch",
vmStatus: "Move abort in 0xa1::offramp: E_ROOT_NOT_COMMITTED(0x10009): Root not committed",
want: true,
},
{
name: "bare receiver induced abort code is a failure",
vmStatus: "Move abort in 0xa1::offramp: 0x1001a",
want: false,
},
{
name: "abort in an offramp function is pre-dispatch when the code is",
vmStatus: "Move abort in 0xa1::offramp::execute_single_report: E_ROOT_NOT_COMMITTED(0x10009)",
want: true,
},
{
name: "receiver induced abort inside the offramp is a failure",
vmStatus: "Move abort in 0xa1::offramp::calculate_message_hash: E_CALCULATE_MESSAGE_HASH_INVALID_ARGUMENTS(0x1001a)",
want: false,
},
{
name: "runtime failure in the offramp is a failure",
vmStatus: "Execution failed in 0xa1::offramp::execute at code offset 12",
want: false,
},
{
name: "other offramp abort codes are failures",
vmStatus: "Move abort in 0xa1::offramp: E_DEST_CHAIN_SELECTOR_MISMATCH(0x10007)",
want: false,
},
{
name: "abort at the receiver is a failure",
vmStatus: "Move abort in 0xd4::receiver: E_REJECTED(0x1)",
want: false,
},
{
name: "abort at the ccip package is a failure",
vmStatus: "Move abort in 0x2::ccip: E_INVALID_MESSAGE(0x1000c)",
want: false,
},
{
name: "abort naming the offramp outside its location is a failure",
vmStatus: "Move abort in 0xd4::receiver: E_SOME_ERROR(0x1) at code offset 8 in 0xa1::offramp",
want: false,
},
{
name: "abort without a code is a failure",
vmStatus: "Move abort in 0xa1::offramp",
want: false,
},
{
name: "unparseable status is a failure",
vmStatus: "Out of gas",
want: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, test.want, preDispatchAbort(test.vmStatus, offRamp, "offramp"))
})
}
}
Loading