Skip to content

Commit

Permalink
Update relayers to work with channel IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Nov 13, 2023
1 parent 07927c7 commit 39807ca
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 89 deletions.
52 changes: 51 additions & 1 deletion relayer/cmd/run/execution/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package execution

import (
"context"
"encoding/hex"
"log"
"os"
"os/signal"
"reflect"
"strings"
"syscall"

"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/snowfork/snowbridge/relayer/chain/parachain"
"github.com/snowfork/snowbridge/relayer/relays/execution"
Expand Down Expand Up @@ -50,7 +54,7 @@ func run(_ *cobra.Command, _ []string) error {
}

var config execution.Config
err := viper.Unmarshal(&config)
err := viper.Unmarshal(&config, viper.DecodeHook(HexHookFunc()))
if err != nil {
return err
}
Expand Down Expand Up @@ -101,3 +105,49 @@ func run(_ *cobra.Command, _ []string) error {

return nil
}

func HexHookFunc() mapstructure.DecodeHookFuncType {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
// Check that the data is string
if f.Kind() != reflect.String {
return data, nil
}

// Check that the target type is our custom type
if t != reflect.TypeOf(execution.ChannelID{}) {
return data, nil
}

foo, err := HexDecodeString(data.(string))
if err != nil {
return nil, err
}

var out [32]byte
copy(out[:], foo)

// Return the parsed value
return execution.ChannelID(out), 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
}
52 changes: 51 additions & 1 deletion relayer/cmd/run/parachain/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package parachain

import (
"context"
"encoding/hex"
"log"
"os"
"os/signal"
"reflect"
"strings"
"syscall"

"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"github.com/snowfork/snowbridge/relayer/chain/ethereum"
"github.com/snowfork/snowbridge/relayer/relays/parachain"
Expand Down Expand Up @@ -48,7 +52,7 @@ func run(_ *cobra.Command, _ []string) error {
}

var config parachain.Config
err := viper.Unmarshal(&config)
err := viper.Unmarshal(&config, viper.DecodeHook(HexHookFunc()))
if err != nil {
return err
}
Expand Down Expand Up @@ -97,3 +101,49 @@ func run(_ *cobra.Command, _ []string) error {

return nil
}

func HexHookFunc() mapstructure.DecodeHookFuncType {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
// Check that the data is string
if f.Kind() != reflect.String {
return data, nil
}

// Check that the target type is our custom type
if t != reflect.TypeOf(parachain.ChannelID{}) {
return data, nil
}

foo, err := HexDecodeString(data.(string))
if err != nil {
return nil, err
}

var out [32]byte
copy(out[:], foo)

// Return the parsed value
return parachain.ChannelID(out), 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
}
35 changes: 0 additions & 35 deletions relayer/relays/execution/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package execution

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

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

Expand All @@ -28,34 +24,3 @@ type SinkConfig struct {
}

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
}
2 changes: 1 addition & 1 deletion relayer/relays/execution/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (r *Relay) Start(ctx context.Context, eg *errgroup.Group) error {

log.WithFields(log.Fields{
"ethBlockNumber": executionHeaderState.BlockNumber,
"channelId": r.config.Source.ChannelID,
"channelId": types.H256(r.config.Source.ChannelID).Hex(),
"paraNonce": paraNonce,
"ethNonce": ethNonce,
}).Info("Polled Nonces")
Expand Down
36 changes: 0 additions & 36 deletions relayer/relays/parachain/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package parachain

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

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

Expand All @@ -13,7 +9,6 @@ type Config struct {
Sink SinkConfig `mapstructure:"sink"`
}

// TODO: check whether ChannelID should be uint32 (as in the parachain) or big.Int (uint256, as in the Gateway contract)
type SourceConfig struct {
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
Parachain config.ParachainConfig `mapstructure:"parachain"`
Expand All @@ -37,34 +32,3 @@ type SinkContractsConfig struct {
}

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
}
2 changes: 1 addition & 1 deletion web/packages/test/config/execution-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"contracts": {
"Gateway": null
},
"channel-id": 1000
"channel-id": null
},
"sink": {
"parachain": {
Expand Down
2 changes: 1 addition & 1 deletion web/packages/test/config/parachain-relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"Gateway": null
},
"beefy-activation-block": 0,
"channel-id": 0
"channel-id": null
},
"sink": {
"ethereum": {
Expand Down
5 changes: 5 additions & 0 deletions web/packages/test/scripts/set-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export ASSET_HUB_AGENT_ID="${ASSET_HUB_AGENT_ID:-0x72456f48efed08af20e5b317abf86
export TEMPLATE_PARA_ID="${TEMPLATE_PARA_ID:-1001}"
export TEMPLATE_AGENT_ID="${TEMPLATE_AGENT_ID:-0x2075b9f5bc236462eb1473c9a6236c3588e33ed19ead53aa3d9c62ed941cb793}"

export ASSET_HUB_CHANNEL_ID="0xc173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539"
export TEMPLATE_CHANNEL_ID="0x26c13363ad6499b895574b3ca482545dda41d657ffc5673b39a218cd34053e5b"
export PRIMARY_GOVERNANCE_CHANNEL_ID="0x0000000000000000000000000000000000000000000000000000000000000001"
export SECONDARY_GOVERNANCE_CHANNEL_ID="0x0000000000000000000000000000000000000000000000000000000000000002"

relaychain_ws_url="${RELAYCHAIN_WS_URL:-ws://127.0.0.1:9944}"
relaychain_sudo_seed="${RELAYCHAIN_SUDO_SEED:-//Alice}"

Expand Down
Loading

0 comments on commit 39807ca

Please sign in to comment.