Production-grade guide to cross chain messaging covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.
Cross chain messaging enables systems on one blockchain to send and receive messages—data, instructions, or events—to systems on another blockchain. It is essential when a dApp spans multiple chains, when assets or state must be synchronized across chains, or when a decentralized network needs to coordinate logic across heterogeneous blockchains. This becomes critical when a user deposits ETH on Ethereum and must be credited with an equivalent asset on Polygon, or when a smart contract on Solana triggers a payment on Bitcoin via a message relay.
To send a message from one blockchain to another, a system must serialize data, submit it to a source chain, and ensure it reaches the destination chain. The process begins with constructing a message payload, typically a JSON object or a binary encoding like CBOR or Protobuf, and then submitting it to a message inbox on the source chain.
A message consists of:
messageId: a unique identifier (e.g., a UUID or a 32-byte hash)senderChainId: the ID of the originating chain (e.g., 1 for Ethereum, 10 for Optimism)recipientChainId: the target chain IDpayload: the actual data, often a structured buffer of bytesnonce: a sequential number used to prevent replay attackstimestamp: when the message was createdThe sender submits this message to a message inbox contract on the source chain using a transaction. The contract records the message and emits a MessageSent event.
function sendMessage(
bytes32 messageId,
uint32 senderChainId,
uint32 recipientChainId,
bytes calldata payload,
uint256 nonce,
uint64 timestamp
) external returns (bool success);
The send transaction is typically sent via a client like web3.py or ethers.js.
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://mainnet.infura.io/v3/..."))
inbox_contract = w3.eth.contract(
address="0x76372123456789abcdef0123456789abcdef012345",
abi=[...]
)
tx_hash = inbox_contract.functions.sendMessage(
messageId=b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10",
senderChainId=1,
recipientChainId=10,
payload=b"\x01\x02\x03\x04\x05\x06",
nonce=12345,
timestamp=1712345678,
).transact({
"from": "0x742d35Cc6634BBB2A351b3935d2235C4712B5bC1",
"gas": 200000,
"gasPrice": w3.to_wei(50, "gwei"),
})
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
The MessageSent event includes the messageId and timestamp, which are used to track message status.
If the sender chain is Ethereum and the destination is Solana, the message must be serialized using a format like borsh or cbor. The payload field is then passed to a bridge contract on Solana, which processes the message and emits a MessageReceived event.
The most common failure mode is message replay. If the source chain fails to confirm the MessageSent transaction, the message is not finalized, and the destination chain may process an outdated or duplicate message. This occurs when the sender assumes the message is submitted but the transaction is not yet mined or not yet confirmed.
To prevent replay, the nonce is essential. The destination chain maintains a processedNonce mapping: messageId → bool. When a message is processed, the chain checks that the nonce has not been processed before, and if so, it rejects the message.
Once a message is sent, the destination chain must read it from the source chain’s inbox and process it. This is typically done via a message relayer, a service that watches for new messages on the source chain and submits a transaction to the destination chain to process them.
The relayer operates in a loop:
MessageSent events.getMessage or getMessagesByChainId.processMessage.curl -X POST https://mainnet.infura.io/v3/... \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getLogs",
"params": [
{
"address": "0x76372123456789abcdef0123456789abcdef012345",
"fromBlock": "0x7d4824",
"toBlock": "0x7d4825",
"topics": [
"0x70c5246a9910d82f331e7857c53833a7e4c5e10f8c9c3d6e3d2f8e4b6c9a1b2c"
]
}
],
"id": 1
}'
The response includes an array of Log objects, each with data, topics, and transactionHash.
The relayer parses each Log to extract:
messageIdsenderChainIdrecipientChainIdpayloadnoncetimestampIt then constructs a processMessage call on the destination chain:
function processMessage(
bytes32 messageId,
uint32 senderChainId,
uint32 recipientChainId,
bytes calldata payload,
uint256 nonce,
uint64 timestamp
) external returns (bool success);
The relayer uses a client like solana-cli to send the transaction:
solana program invoke \
--program-id 76372123456789abcdef0123456789abcdef012345 \
--args "0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" \
--signer ~/.config/solana/wallets/relayer.json \
--url https://api.mainnet-beta.solana.com
The relayer must ensure that the processMessage transaction is confirmed on the destination chain. It tracks processed messages via a database table:
CREATE TABLE message_receipts (
message_id BYTEA PRIMARY KEY,
chain_id INT NOT NULL,
processed_at TIMESTAMPTZ NOT NULL,
status TEXT CHECK (status IN ('pending', 'success', 'failed')),
error TEXT,
block_number BIGINT
);
When a message is processed, the relayer updates the message_receipts table with status = 'success'. If the transaction fails, it logs the error in error and marks status = 'failed'.
A subtle but common failure is message gap. The relayer polls the source chain every 30 seconds, but messages arrive more frequently. If two messages are sent within 10 seconds, the relayer may miss the second one if it polls at 0, 30, 60, etc. The fix is to use block number-based polling, where the relayer maintains a lastBlock counter and fetches logs from lastBlock + 1 to currentBlock, ensuring no gaps.
Another silent failure is message processing order. The destination chain assumes that messages are processed in the order they are sent, but due to network delays or batch processing, messages arrive out of order. The relayer must verify that nonce is increasing and that timestamp is monotonic. If a message with nonce=5 arrives before nonce=4, the relayer logs a warning and can trigger a recovery process.
For end-to-end reliability, a system must confirm that a message has been delivered and processed. This is achieved via message confirmation mechanisms.
The sender chain can verify that a message has been processed on the destination chain by calling a isMessageProcessed function on the destination inbox contract.
function isMessageProcessed(
bytes32 messageId
) external view returns (bool processed);
The sender checks this after submitting the message:
w3 = Web3(Web3.HTTPProvider("https://mainnet.infura.io/v3/..."))
inbox_contract_dst = w3.eth.contract(
address="0x456789abcdef0123456789abcdef0123456789abcdef012345",
abi=[...]
)
message_id = b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
while True:
processed = inbox_contract_dst.functions.isMessageProcessed(message_id).call()
if processed:
print("Message confirmed on destination chain")
break
time.sleep(10)
The confirmation process is not instant. The destination chain may take minutes or hours to process a message, especially if it is batched. The sender can use event-driven confirmation, where the destination chain emits a MessageProcessed event when it processes a message.
The sender listens for this event and marks the message as fully delivered.
A common pattern is two-phase confirmation:
The sender can use confirmMessage to track both phases:
function confirmMessage(
bytes32 messageId,
bytes32 confirmationHash
) external returns (bool success);
The confirmation hash is computed from the messageId, senderChainId, and recipientChainId. The sender uses this to track progress from submission to finality.
The most common error is message not received. The destination chain fails to process a message due to:
processMessagepayload fieldsenderChainIdThe error is logged in the relayer’s database and surfaced via alerts.
Another failure is message delay. The message is sent, but the relayer takes 5 minutes to process it. The sender expects near real-time delivery, but it takes 15 minutes. The fix is to increase polling frequency and batch messages to reduce overhead.
The most insidious failure is message loss. The relayer processes a message but crashes before writing to message_receipts. On restart, it processes the same message again. The solution is idempotent processing: the processMessage function checks whether the messageId has already been processed, and if so, it skips the work.
Idempotency is implemented using a processed mapping:
mapping(bytes32 => bool) public processedMessages;
function processMessage(
bytes32 messageId,
uint32 senderChainId,
uint32 recipientChainId,
bytes calldata payload,
uint256 nonce,
uint64 timestamp
) external returns (bool success) {
if (processedMessages[messageId]) {
return true; // already processed
}
// Process the message
// Update state, emit events, etc.
processedMessages[messageId] = true;
emit MessageProcessed(messageId, senderChainId, recipientChainId, payload, nonce, timestamp);
return true;
}
This prevents duplicate processing and ensures eventual consistency.
Finally, cross chain message signing is critical. The sender must sign the message with a cryptographic key, and the destination chain verifies the signature. The signature is included in the payload as a signature field, and the destination chain validates it using verifyMessageSignature.
The signature is computed as:
from hashlib import sha256
import hmac
def sign_message(message_id, sender_chain_id, recipient_chain_id, payload, private_key):
message = (
message_id +
sender_chain_id.to_bytes(4, 'big') +
recipient_chain_id.to_bytes(4, 'big') +
payload
)
return hmac.new(private_key, message, sha256).digest()
The destination chain uses ecdsa_verify to validate the signature and accept the message.
Failure occurs when:
messageId is missingpayload is malformednonce is incorrectAll of these are tracked in the message_receipts table and can be used to trigger alerts or recovery jobs.
This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy