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 37 commits into
base: vincent/v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c8fc265
Parachain relayer V2
yrong Oct 22, 2024
a64c2a5
Rename to PendingOrder
yrong Oct 24, 2024
f72e9c3
Decode compact int
yrong Oct 24, 2024
2d9aaf9
Merge branch 'vincent/v2' into ron/v2
yrong Oct 25, 2024
c5f0a24
Improve doc
yrong Oct 27, 2024
e611e3a
Remove unused
yrong Oct 27, 2024
e3888db
Submit delivery proof
yrong Oct 28, 2024
bdb0ac1
Merge branch 'vincent/v2' into ron/v2
yrong Oct 28, 2024
af374a6
Rename to InboundMessageV2
yrong Oct 28, 2024
2e8d519
Mark as view function
yrong Oct 28, 2024
7187bf3
Filter with reward address
yrong Oct 28, 2024
f214dfc
V2 smoke tests (#1327)
yrong Nov 4, 2024
2581866
Register PNA with OutbountQueueV2
yrong Nov 6, 2024
49089a0
Fix storage key
yrong Nov 6, 2024
c87192e
Comment out config
yrong Nov 6, 2024
6a79b20
Merge branch 'vincent/v2' into ron/v2
yrong Nov 6, 2024
2b3201f
Revert contract changes
yrong Nov 6, 2024
42bc03d
Fix function
yrong Nov 6, 2024
78dd144
Regenerate binds & update package path
yrong Nov 6, 2024
b88756f
Format
yrong Nov 6, 2024
d56ce55
Format
yrong Nov 6, 2024
aa75d79
Add todo
yrong Nov 6, 2024
d740321
Update bindings
yrong Nov 6, 2024
284e6f0
Update script
yrong Nov 8, 2024
dec8316
Update smoke tests
yrong Nov 10, 2024
4419cd7
Merge branch 'vincent/v2' into ron/v2
yrong Nov 11, 2024
400b332
More refactoring
yrong Nov 14, 2024
10f8550
Verify V2 digest for commitment
yrong Nov 14, 2024
f5c9cd9
Call Contract with value
yrong Nov 24, 2024
f54ad7c
Fix smoke test for V2
yrong Dec 8, 2024
1e168c3
Fix test
yrong Dec 8, 2024
0d0046b
Merge branch 'vincent/v2' into ron/v2
yrong Dec 8, 2024
9081585
Fix unlock WETH
yrong Dec 8, 2024
b75a480
Sync with contract
yrong Dec 8, 2024
d162fd3
Fix smoke test
yrong Dec 8, 2024
8ec8306
Merge branch 'vincent/v2' into ron/v2
yrong Dec 11, 2024
8b63e4a
Merge branch 'vincent/v2' into ron/v2
yrong Dec 11, 2024
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"`
claravanstaden marked this conversation as resolved.
Show resolved Hide resolved
}

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