Self Broadcasting

Set isSelfBroadcasting to true and reduce gas fees

The sending library handles the payments of the oracle and relayer, and ordering of ordered messages. However, it does not affect how messages are received. As a result, after configuring with endpoint, an application can self-broadcast messages as opposed to sending them via the endpoint.

The advantage for this is that you can reduce the amount of gas spent sending messages; fewer function calls, etc.

The downside however is that the application will need to handle the ordering of its own ordered messages along with payment of the oracle and relayer.

To save on gas this way, you need to:

  1. Know/Get the addresses of an oracle and relayer that support this functionality,

  2. If using the Rukh library, make sure there are sufficient disputers that support this functionality,

  3. Implement the self-broadcast functionality.

Here is an example of how one can implement self-broadcasting.

We intentionally left out the helper functions to get the oracle, relayer and protocol fees; you can look at the PingPong and MockApp for examples.

// mapping for app-receiver nonces. Note: gives independent nonces to each app-receiver pair.
// receiver chain id => receiver app => Nonces struct
mapping(uint256 => mapping(bytes => Nonces)) public receiverToNonces;

/**
 * @dev - Private function that helps us broadcast messages that need to be sent.
 * @param _isOrderedMsg - bool indicating whether this is an ordered or unordered message
 * @param _receiverChainId - uint256 indicating the chain id of the receiver
 * @param _receiver - bytes indicating the address of the receiver
 * @param _payload - bytes indicating the message payload
 */
function _broadcastMsg(
    bool _isOrderedMsg,
    uint256 _receiverChainId,
    bytes memory _receiver,
    bytes memory _payload
) private {
    if (_isOrderedMsg) {
        uint256nonce = receiverToNonces[_receiverChainId][_receiver].ordered;
        emit SelfBroadcastMessage(_receiverChainId, _receiver, nonce, true, _payload); // Broadcast Message
        receiverToNonces[_receiverChainId][_receiver].ordered = nonce + 1;
    } else {
        uint256 nonce = receiverToNonces[_receiverChainId][_receiver].unordered;
        if (nonce == 0) nonce = 2 ** 256 - 1;
        emit SelfBroadcastMessage(_receiverChainId, _receiver, nonce, false, _payload); // Broadcast Message
        receiverToNonces[_receiverChainId][_receiver].unordered = nonce - 1;
    }
}

Our team is available to help if you need assistance implementing/testing this. Reach out if you are interested in building an oracle or relayer or if you have any questions; our discord is the best place to get in touch with us!

Last updated