Skip to content

Commit

Permalink
get everything working e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Nov 13, 2023
1 parent 9ef2225 commit 07927c7
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 67 deletions.
10 changes: 8 additions & 2 deletions parachain/pallets/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ use frame_support::{
use frame_system::pallet_prelude::*;
use snowbridge_core::{
outbound::{Command, Initializer, Message, OperatingMode, SendError, SendMessage},
sibling_sovereign_account, AgentId, Channel, ChannelId, ParaId, BRIDGE_HUB_AGENT_ID,
PRIMARY_GOVERNANCE_CHANNEL, SECONDARY_GOVERNANCE_CHANNEL,
sibling_sovereign_account, AgentId, Channel, ChannelId, ChannelLookup, ParaId,
BRIDGE_HUB_AGENT_ID, PRIMARY_GOVERNANCE_CHANNEL, SECONDARY_GOVERNANCE_CHANNEL,
};

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -564,4 +564,10 @@ pub mod pallet {
Ok(())
}
}

impl<T: Config> ChannelLookup for Pallet<T> {
fn lookup(channel_id: ChannelId) -> Option<Channel> {
Channels::<T>::get(channel_id)
}
}
}
64 changes: 32 additions & 32 deletions relayer/contracts/gateway.go

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion relayer/relays/execution/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package execution

import (
"encoding/hex"
"errors"
"strings"

"github.com/snowfork/snowbridge/relayer/config"
)

Expand All @@ -12,7 +16,7 @@ type Config struct {
type SourceConfig struct {
Ethereum config.EthereumConfig `mapstructure:"ethereum"`
Contracts ContractsConfig `mapstructure:"contracts"`
ChannelID uint32 `mapstructure:"channel-id"`
ChannelID ChannelID `mapstructure:"channel-id"`
}

type ContractsConfig struct {
Expand All @@ -22,3 +26,36 @@ type ContractsConfig struct {
type SinkConfig struct {
Parachain config.ParachainConfig `mapstructure:"parachain"`
}

type ChannelID [32]byte

func (m *ChannelID) UnmarshalJSON(bz []byte) error {
data, err := HexDecodeString(string(bz))
if err != nil {
return err
}
if len(data) != 32 {
return errors.New("Invalid Channel ID")
}

copy(m[:], data)

return nil
}

// HexDecodeString decodes bytes from a hex string. Contrary to hex.DecodeString, this function does not error if "0x"
// is prefixed, and adds an extra 0 if the hex string has an odd length.
func HexDecodeString(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")

if len(s)%2 != 0 {
s = "0" + s
}

b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}

return b, nil
}
12 changes: 6 additions & 6 deletions relayer/relays/execution/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (r *Relay) Start(ctx context.Context, eg *errgroup.Group) error {
"blockHash": ev.Raw.BlockHash.Hex(),
"blockNumber": ev.Raw.BlockNumber,
"txHash": ev.Raw.TxHash.Hex(),
"dest": ev.Destination,
"channelID": types.H256(ev.ChannelID).Hex(),
})

if ev.Nonce <= paraNonce {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (r *Relay) fetchEthereumNonce(ctx context.Context, blockNumber uint64) (uin
BlockNumber: new(big.Int).SetUint64(blockNumber),
Context: ctx,
}
_, ethOutboundNonce, err := r.gatewayContract.ChannelNoncesOf(&opts, big.NewInt(int64(r.config.Source.ChannelID)))
_, ethOutboundNonce, err := r.gatewayContract.ChannelNoncesOf(&opts, r.config.Source.ChannelID)
if err != nil {
return 0, fmt.Errorf("fetch Gateway.ChannelNoncesOf(%v): %w", r.config.Source.ChannelID, err)
}
Expand All @@ -209,7 +209,7 @@ func (r *Relay) findEvents(
start uint64,
) ([]*contracts.GatewayOutboundMessageAccepted, error) {

paraID := r.config.Source.ChannelID
channelID := r.config.Source.ChannelID

var allEvents []*contracts.GatewayOutboundMessageAccepted

Expand All @@ -231,7 +231,7 @@ func (r *Relay) findEvents(
Context: ctx,
}

done, events, err := r.findEventsWithFilter(&opts, paraID, start)
done, events, err := r.findEventsWithFilter(&opts, channelID, start)
if err != nil {
return nil, fmt.Errorf("filter events: %w", err)
}
Expand All @@ -254,8 +254,8 @@ func (r *Relay) findEvents(
return allEvents, nil
}

func (r *Relay) findEventsWithFilter(opts *bind.FilterOpts, paraID uint32, start uint64) (bool, []*contracts.GatewayOutboundMessageAccepted, error) {
iter, err := r.gatewayContract.FilterOutboundMessageAccepted(opts, []*big.Int{big.NewInt(int64(paraID))}, [][32]byte{})
func (r *Relay) findEventsWithFilter(opts *bind.FilterOpts, channelID [32]byte, start uint64) (bool, []*contracts.GatewayOutboundMessageAccepted, error) {
iter, err := r.gatewayContract.FilterOutboundMessageAccepted(opts, [][32]byte{channelID}, [][32]byte{})
if err != nil {
return false, nil, err
}
Expand Down
39 changes: 38 additions & 1 deletion relayer/relays/parachain/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package parachain

import (
"encoding/hex"
"errors"
"strings"

"github.com/snowfork/snowbridge/relayer/config"
)

Expand All @@ -15,7 +19,7 @@ type SourceConfig struct {
Parachain config.ParachainConfig `mapstructure:"parachain"`
Ethereum config.EthereumConfig `mapstructure:"ethereum"`
Contracts SourceContractsConfig `mapstructure:"contracts"`
ChannelID uint32 `mapstructure:"channel-id"`
ChannelID ChannelID `mapstructure:"channel-id"`
}

type SourceContractsConfig struct {
Expand All @@ -31,3 +35,36 @@ type SinkConfig struct {
type SinkContractsConfig struct {
Gateway string `mapstructure:"Gateway"`
}

type ChannelID [32]byte

func (m *ChannelID) UnmarshalJSON(bz []byte) error {
data, err := HexDecodeString(string(bz))
if err != nil {
return err
}
if len(data) != 32 {
return errors.New("Invalid Channel ID")
}

copy(m[:], data)

return nil
}

// HexDecodeString decodes bytes from a hex string. Contrary to hex.DecodeString, this function does not error if "0x"
// is prefixed, and adds an extra 0 if the hex string has an odd length.
func HexDecodeString(s string) ([]byte, error) {
s = strings.TrimPrefix(s, "0x")

if len(s)%2 != 0 {
s = "0" + s
}

b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}

return b, nil
}
6 changes: 3 additions & 3 deletions relayer/relays/parachain/ethereum-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ func (wr *EthereumWriter) WriteChannel(
return fmt.Errorf("unpack event log: %w", err)
}
log.WithFields(log.Fields{
"origin": holder.Origin,
"nonce": holder.Nonce,
"success": holder.Success,
"channelID": Hex(holder.ChannelID[:]),
"nonce": holder.Nonce,
"success": holder.Success,
}).Info("Message dispatched")
}
}
Expand Down
8 changes: 4 additions & 4 deletions relayer/relays/parachain/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (wr *EthereumWriter) logFieldsForSubmission(

params := log.Fields{
"message": log.Fields{
"origin": message.Origin,
"nonce": message.Nonce,
"command": message.Command,
"params": Hex(message.Params),
"channelID": Hex(message.ChannelID[:]),
"nonce": message.Nonce,
"command": message.Command,
"params": Hex(message.Params),
},
"messageProof": messageProofHexes,
"proof": log.Fields{
Expand Down
21 changes: 9 additions & 12 deletions relayer/relays/parachain/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package parachain

import (
"context"
"encoding/binary"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -84,7 +82,7 @@ func (s *Scanner) findTasks(
Pending: true,
Context: ctx,
}
ethInboundNonce, _, err := gatewayContract.ChannelNoncesOf(&options, big.NewInt(int64(s.config.ChannelID)))
ethInboundNonce, _, err := gatewayContract.ChannelNoncesOf(&options, s.config.ChannelID)
if err != nil {
return nil, fmt.Errorf("fetch nonce from gateway contract for channelID '%v': %w", s.config.ChannelID, err)
}
Expand All @@ -94,9 +92,8 @@ func (s *Scanner) findTasks(
}).Info("Checked latest nonce delivered to ethereum gateway")

// Fetch latest nonce in parachain outbound queue
sourceIDBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(sourceIDBytes, s.config.ChannelID)
paraNonceKey, err := types.CreateStorageKey(s.paraConn.Metadata(), "EthereumOutboundQueue", "Nonce", sourceIDBytes, nil)

paraNonceKey, err := types.CreateStorageKey(s.paraConn.Metadata(), "EthereumOutboundQueue", "Nonce", s.config.ChannelID[:], nil)
if err != nil {
return nil, fmt.Errorf("create storage key for parachain outbound queue nonce with channelID '%v': %w", s.config.ChannelID, err)
}
Expand Down Expand Up @@ -126,7 +123,7 @@ func (s *Scanner) findTasks(
tasks, err := s.findTasksImpl(
ctx,
paraBlock,
s.config.ChannelID,
types.H256(s.config.ChannelID),
ethInboundNonce+1,
)
if err != nil {
Expand All @@ -143,7 +140,7 @@ func (s *Scanner) findTasks(
func (s *Scanner) findTasksImpl(
_ context.Context,
lastParaBlockNumber uint64,
channelID uint32,
channelID types.H256,
startingNonce uint64,
) ([]*Task, error) {
log.WithFields(log.Fields{
Expand Down Expand Up @@ -331,7 +328,7 @@ func scanForOutboundQueueProofs(
blockHash types.Hash,
commitmentHash types.H256,
startingNonce uint64,
channelID uint32,
channelID types.H256,
messages []OutboundQueueMessage,
) (*struct {
proofs []MessageProof
Expand Down Expand Up @@ -360,7 +357,7 @@ func scanForOutboundQueueProofs(
for i := len(messages) - 1; i >= 0; i-- {
message := messages[i]

if message.Origin != channelID {
if message.ChannelID != channelID {
continue
}

Expand All @@ -370,7 +367,7 @@ func scanForOutboundQueueProofs(
if messageNonce < startingNonce {
log.Debugf(
"Halting scan for channelID '%v'. Messages not committed yet on outbound channel",
message.Origin,
message.ChannelID.Hex(),
)
scanDone = true
break
Expand All @@ -384,7 +381,7 @@ func scanForOutboundQueueProofs(
if messageProof.Proof.Root != commitmentHash {
return nil, fmt.Errorf(
"Halting scan for channelID '%v'. Outbound queue proof root '%v' doesn't match digest item's commitment hash '%v'",
message.Origin,
message.ChannelID.Hex(),
messageProof.Proof.Root,
commitmentHash,
)
Expand Down
6 changes: 2 additions & 4 deletions relayer/relays/parachain/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package parachain

import (
"math/big"

"github.com/snowfork/go-substrate-rpc-client/v4/types"
"github.com/snowfork/snowbridge/relayer/chain/relaychain"
"github.com/snowfork/snowbridge/relayer/contracts"
Expand Down Expand Up @@ -83,7 +81,7 @@ func NewMerkleProof(rawProof RawMerkleProof) (MerkleProof, error) {
}

type OutboundQueueMessage struct {
Origin uint32
ChannelID types.H256
Nonce uint64
Command uint8
Params []byte
Expand All @@ -95,7 +93,7 @@ type OutboundQueueMessage struct {

func (m OutboundQueueMessage) IntoInboundMessage() contracts.InboundMessage {
return contracts.InboundMessage{
Origin: big.NewInt(int64(m.Origin)),
ChannelID: m.ChannelID,
Nonce: m.Nonce,
Command: m.Command,
Params: m.Params,
Expand Down
2 changes: 1 addition & 1 deletion web/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bridgehub_ws_url="${BRIDGE_HUB_WS_URL:-ws://127.0.0.1:11144}"
bridgehub_seed="${BRIDGE_HUB_SEED:-//Alice}"
bridgehub_pallets_owner="${BRIDGE_HUB_PALLETS_OWNER:-0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d}"
export BRIDGE_HUB_PARAID="${BRIDGE_HUB_PARAID:-1013}"
export BRIDGE_HUB_AGENT_ID="${BRIDGE_HUB_AGENT_ID:-0x05f0ced792884ed09997292bd95f8d0d1094bb3bded91ec3f2f08531624037d6}"
export BRIDGE_HUB_AGENT_ID="${BRIDGE_HUB_AGENT_ID:-0x0000000000000000000000000000000000000000000000000000000000000001}"

assethub_ws_url="${ASSET_HUB_WS_URL:-ws://127.0.0.1:12144}"
assethub_seed="${ASSET_HUB_SEED:-//Alice}"
Expand Down

0 comments on commit 07927c7

Please sign in to comment.