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

Parachain Relayer V2 #1321

Open
wants to merge 23 commits into
base: vincent/v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
6 changes: 1 addition & 5 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import {
import {CallsV1} from "./v1/Calls.sol";
import {HandlersV1} from "./v1/Handlers.sol";

import {
InboundMessage as InboundMessageV2,
Command as CommandV2,
CommandKind
} from "./v2/Types.sol";
import {InboundMessageV2, Command as CommandV2, CommandKind} from "./v2/Types.sol";
import {CallsV2} from "./v2/Calls.sol";
import {HandlersV2} from "./v2/Handlers.sol";
4 changes: 2 additions & 2 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ParaID,
ChannelID
} from "../v1/Types.sol";
import {InboundMessage as InboundMessageV2} from "../v2/Types.sol";
import {InboundMessageV2} from "../v2/Types.sol";
import {Verification} from "../Verification.sol";
import {UD60x18} from "prb/math/src/UD60x18.sol";

Expand Down Expand Up @@ -222,5 +222,5 @@ interface IGateway {
) external payable;

// Check if an inbound message was previously accepted and dispatched
function v2_isDispatched(uint64 nonce) external returns (bool);
function v2_isDispatched(uint64 nonce) external view returns (bool);
}
2 changes: 1 addition & 1 deletion contracts/src/v2/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity 0.8.25;
import {OperatingMode} from "./../types/Common.sol";

// Inbound message from a Polkadot parachain (via BridgeHub)
struct InboundMessage {
struct InboundMessageV2 {
// origin
bytes32 origin;
// Message nonce
Expand Down
423 changes: 422 additions & 1 deletion relayer/contracts/gateway.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions relayer/relays/parachain/beefy-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type BeefyListener struct {
relaychainConn *relaychain.Connection
parachainConnection *parachain.Connection
paraID uint32
tasks chan<- *Task
tasks chan<- *TaskV2
scanner *Scanner
}

Expand All @@ -39,7 +39,7 @@ func NewBeefyListener(
ethereumConn *ethereum.Connection,
relaychainConn *relaychain.Connection,
parachainConnection *parachain.Connection,
tasks chan<- *Task,
tasks chan<- *TaskV2,
) *BeefyListener {
return &BeefyListener{
config: config,
Expand Down Expand Up @@ -160,7 +160,7 @@ func (li *BeefyListener) doScan(ctx context.Context, beefyBlockNumber uint64) er
}
for _, task := range tasks {
paraNonce := (*task.MessageProofs)[0].Message.Nonce
waitingPeriod := (paraNonce + li.scheduleConfig.TotalRelayerCount - li.scheduleConfig.ID) % li.scheduleConfig.TotalRelayerCount
waitingPeriod := (uint64(paraNonce) + li.scheduleConfig.TotalRelayerCount - li.scheduleConfig.ID) % li.scheduleConfig.TotalRelayerCount
err = li.waitAndSend(ctx, task, waitingPeriod)
if err != nil {
return fmt.Errorf("wait task for nonce %d: %w", paraNonce, err)
Expand Down Expand Up @@ -320,17 +320,17 @@ func (li *BeefyListener) generateAndValidateParasHeadsMerkleProof(input *ProofIn
return &merkleProofData, paraHeads, nil
}

func (li *BeefyListener) waitAndSend(ctx context.Context, task *Task, waitingPeriod uint64) error {
func (li *BeefyListener) waitAndSend(ctx context.Context, task *TaskV2, waitingPeriod uint64) error {
paraNonce := (*task.MessageProofs)[0].Message.Nonce
log.Info(fmt.Sprintf("waiting for nonce %d to be picked up by another relayer", paraNonce))
var cnt uint64
var err error
for {
ethInboundNonce, err := li.scanner.findLatestNonce(ctx)
isRelayed, err := li.scanner.isNonceRelayed(ctx, uint64(paraNonce))
if err != nil {
return err
}
if ethInboundNonce >= paraNonce {
if isRelayed {
log.Info(fmt.Sprintf("nonce %d picked up by another relayer, just skip", paraNonce))
return nil
}
Expand Down
25 changes: 14 additions & 11 deletions relayer/relays/parachain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
"fmt"

"github.com/snowfork/snowbridge/relayer/config"
beaconconf "github.com/snowfork/snowbridge/relayer/relays/beacon/config"
)

type Config struct {
Source SourceConfig `mapstructure:"source"`
Sink SinkConfig `mapstructure:"sink"`
Schedule ScheduleConfig `mapstructure:"schedule"`
Source SourceConfig `mapstructure:"source"`
Sink SinkConfig `mapstructure:"sink"`
Schedule ScheduleConfig `mapstructure:"schedule"`
RewardAddress string `mapstructure:"reward-address"`
}

type SourceConfig struct {
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
Parachain config.ParachainConfig `mapstructure:"parachain"`
Ethereum config.EthereumConfig `mapstructure:"ethereum"`
Contracts SourceContractsConfig `mapstructure:"contracts"`
ChannelID ChannelID `mapstructure:"channel-id"`
Polkadot config.PolkadotConfig `mapstructure:"polkadot"`
Parachain config.ParachainConfig `mapstructure:"parachain"`
Ethereum config.EthereumConfig `mapstructure:"ethereum"`
Contracts SourceContractsConfig `mapstructure:"contracts"`
Beacon beaconconf.BeaconConfig `mapstructure:"beacon"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the parachain relayer need the beacon config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

type SourceContractsConfig struct {
Expand Down Expand Up @@ -76,9 +78,6 @@ func (c Config) Validate() error {
if c.Source.Contracts.Gateway == "" {
return fmt.Errorf("source contracts setting [Gateway] is not set")
}
if c.Source.ChannelID == [32]byte{} {
return fmt.Errorf("source setting [channel-id] is not set")
}

// Sink
err = c.Sink.Ethereum.Validate()
Expand All @@ -95,5 +94,9 @@ func (c Config) Validate() error {
return fmt.Errorf("relay config: %w", err)
}

if c.RewardAddress == "" {
return fmt.Errorf("reward address is not set")
}

return nil
}
39 changes: 24 additions & 15 deletions relayer/relays/parachain/ethereum-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,34 @@ import (
"github.com/snowfork/snowbridge/relayer/chain/ethereum"
"github.com/snowfork/snowbridge/relayer/contracts"
"github.com/snowfork/snowbridge/relayer/crypto/keccak"
"github.com/snowfork/snowbridge/relayer/relays/util"

gsrpcTypes "github.com/snowfork/go-substrate-rpc-client/v4/types"

log "github.com/sirupsen/logrus"
)

type EthereumWriter struct {
config *SinkConfig
conn *ethereum.Connection
gateway *contracts.Gateway
tasks <-chan *Task
gatewayABI abi.ABI
config *SinkConfig
conn *ethereum.Connection
gateway *contracts.Gateway
tasks <-chan *TaskV2
gatewayABI abi.ABI
relayConfig *Config
}

func NewEthereumWriter(
config *SinkConfig,
conn *ethereum.Connection,
tasks <-chan *Task,
tasks <-chan *TaskV2,
relayConfig *Config,
) (*EthereumWriter, error) {
return &EthereumWriter{
config: config,
conn: conn,
gateway: nil,
tasks: tasks,
config: config,
conn: conn,
gateway: nil,
tasks: tasks,
relayConfig: relayConfig,
}, nil
}

Expand Down Expand Up @@ -93,7 +97,7 @@ func (wr *EthereumWriter) writeMessagesLoop(ctx context.Context) error {
func (wr *EthereumWriter) WriteChannels(
ctx context.Context,
options *bind.TransactOpts,
task *Task,
task *TaskV2,
) error {
for _, proof := range *task.MessageProofs {
err := wr.WriteChannel(ctx, options, &proof, task.ProofOutput)
Expand All @@ -109,10 +113,10 @@ func (wr *EthereumWriter) WriteChannels(
func (wr *EthereumWriter) WriteChannel(
ctx context.Context,
options *bind.TransactOpts,
commitmentProof *MessageProof,
commitmentProof *MessageProofV2,
proof *ProofOutput,
) error {
message := commitmentProof.Message.IntoInboundMessage()
message := commitmentProof.Message.IntoInboundMessageV2()

convertedHeader, err := convertHeader(proof.Header)
if err != nil {
Expand Down Expand Up @@ -143,8 +147,13 @@ func (wr *EthereumWriter) WriteChannel(
LeafProofOrder: new(big.Int).SetUint64(proof.MMRProof.MerkleProofOrder),
}

tx, err := wr.gateway.SubmitV1(
options, message, commitmentProof.Proof.InnerHashes, verificationProof,
rewardAddress, err := util.HexStringTo32Bytes(wr.relayConfig.RewardAddress)
if err != nil {
return fmt.Errorf("convert to reward address: %w", err)
}

tx, err := wr.gateway.V2Submit(
options, message, commitmentProof.Proof.InnerHashes, verificationProof, rewardAddress,
)
if err != nil {
return fmt.Errorf("send transaction Gateway.submit: %w", err)
Expand Down
9 changes: 4 additions & 5 deletions relayer/relays/parachain/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Hex(b []byte) string {
}

func (wr *EthereumWriter) logFieldsForSubmission(
message contracts.InboundMessage,
message contracts.InboundMessageV2,
messageProof [][32]byte,
proof contracts.VerificationProof,
) log.Fields {
Expand Down Expand Up @@ -43,10 +43,9 @@ func (wr *EthereumWriter) logFieldsForSubmission(

params := log.Fields{
"message": log.Fields{
"channelID": Hex(message.ChannelID[:]),
"nonce": message.Nonce,
"command": message.Command,
"params": Hex(message.Params),
"nonce": message.Nonce,
"commands": message.Commands,
"origin": Hex(message.Origin[:]),
},
"messageProof": messageProofHexes,
"proof": log.Fields{
Expand Down
Loading
Loading