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 3 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
5 changes: 5 additions & 0 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
emit IGateway.InboundMessageDispatched(message.nonce, success, rewardAddress);
}

function isRelayed(uint64 nonce) external view returns (bool) {
CoreStorage.Layout storage $ = CoreStorage.layout();
return $.inboundNonce.get(nonce);
}

function v2_dispatch(InboundMessageV2 calldata message) internal returns (bool) {
for (uint256 i = 0; i < message.commands.length; i++) {
if (gasleft() * 63 / 64 < message.commands[i].gas + DISPATCH_OVERHEAD_GAS_V2)
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {HandlersV1} from "./v1/Handlers.sol";

import {
TransferKind,
InboundMessage as InboundMessageV2,
InboundMessageV2,
Command as CommandV2,
CommandKind
} from "./v2/Types.sol";
Expand Down
12 changes: 12 additions & 0 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ParaID,
ChannelID
} from "../v1/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 @@ -123,6 +124,17 @@ interface IGateway {
Verification.Proof calldata headerProof
) external;

// Submit v2 message from a Polkadot network
function v2_submit(
InboundMessageV2 calldata message,
bytes32[] calldata leafProof,
Verification.Proof calldata headerProof,
bytes32 rewardAddress
) external;

/// @dev Check whether nonce is relayed
function isRelayed(uint64 nonce) external view returns (bool);

/**
* Token Transfers
*/
Expand Down
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
413 changes: 412 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
17 changes: 10 additions & 7 deletions relayer/relays/parachain/ethereum-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type EthereumWriter struct {
config *SinkConfig
conn *ethereum.Connection
gateway *contracts.Gateway
tasks <-chan *Task
tasks <-chan *TaskV2
gatewayABI abi.ABI
}

func NewEthereumWriter(
config *SinkConfig,
conn *ethereum.Connection,
tasks <-chan *Task,
tasks <-chan *TaskV2,
) (*EthereumWriter, error) {
return &EthereumWriter{
config: config,
Expand Down Expand Up @@ -93,7 +93,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 +109,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 +143,11 @@ func (wr *EthereumWriter) WriteChannel(
LeafProofOrder: new(big.Int).SetUint64(proof.MMRProof.MerkleProofOrder),
}

tx, err := wr.gateway.SubmitV1(
options, message, commitmentProof.Proof.InnerHashes, verificationProof,
// Todo: from config
var rewardAddress [32]byte

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
2 changes: 1 addition & 1 deletion relayer/relays/parachain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewRelay(config *Config, keypair *secp256k1.Keypair) (*Relay, error) {
ethereumConnBeefy := ethereum.NewConnection(&config.Source.Ethereum, keypair)

// channel for messages from beefy listener to ethereum writer
var tasks = make(chan *Task, 1)
var tasks = make(chan *TaskV2, 1)

ethereumChannelWriter, err := NewEthereumWriter(
&config.Sink,
Expand Down
Loading
Loading