The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/abacus-xapps/contracts/ping-pong/PingPongRouter.sol

160 lines
5.4 KiB

// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
// ============ External Imports ============
import {TypedMemView} from "@summa-tx/memview-sol/contracts/TypedMemView.sol";
// ============ Internal Imports ============
import {PingPongMessage} from "./PingPongMessage.sol";
import {Router} from "../Router.sol";
import {XAppConnectionClient} from "../XAppConnectionClient.sol";
/*
============ PingPong xApp ============
The PingPong xApp is capable of initiating PingPong "matches" between two chains.
A match consists of "volleys" sent back-and-forth between the two chains via Abacus.
The first volley in a match is always a Ping volley.
When a Router receives a Ping volley, it returns a Pong.
When a Router receives a Pong volley, it returns a Ping.
The Routers keep track of the number of volleys in a given match,
and emit events for each Sent and Received volley so that spectators can watch.
*/
contract PingPongRouter is Router {
// ============ Libraries ============
using TypedMemView for bytes;
using TypedMemView for bytes29;
using PingPongMessage for bytes29;
// ============ Mutable State ============
uint32 nextMatch;
// ============ Events ============
event Received(
uint32 indexed domain,
uint32 indexed matchId,
uint256 count,
bool isPing
);
event Sent(
uint32 indexed domain,
uint32 indexed matchId,
uint256 count,
bool isPing
);
// ============ Constructor ============
constructor(address _xAppConnectionManager) {
require(false, "example xApp, do not deploy");
__XAppConnectionClient_initialize(_xAppConnectionManager);
}
// ============ Handle message functions ============
/**
* @notice Handle "volleys" sent via Abacus from other remote PingPong Routers
* @param _origin The domain the message is coming from
* @param _sender The address the message is coming from
* @param _message The message in the form of raw bytes
*/
function handle(
uint32 _origin,
bytes32 _sender,
bytes memory _message
) external override onlyReplica onlyRemoteRouter(_origin, _sender) {
bytes29 _msg = _message.ref(0);
if (_msg.isPing()) {
_handlePing(_origin, _msg);
} else if (_msg.isPong()) {
_handlePong(_origin, _msg);
} else {
// if _message doesn't match any valid actions, revert
require(false, "!valid action");
}
}
/**
* @notice Handle a Ping volley
* @param _origin The domain that sent the volley
* @param _message The message in the form of raw bytes
*/
function _handlePing(uint32 _origin, bytes29 _message) internal {
bool _isPing = true;
_handle(_origin, _isPing, _message);
}
/**
* @notice Handle a Pong volley
* @param _origin The domain that sent the volley
* @param _message The message in the form of raw bytes
*/
function _handlePong(uint32 _origin, bytes29 _message) internal {
bool _isPing = false;
_handle(_origin, _isPing, _message);
}
/**
* @notice Upon receiving a volley, emit an event, increment the count and return a the opposite volley
* @param _origin The domain that sent the volley
* @param _isPing True if the volley received is a Ping, false if it is a Pong
* @param _message The message in the form of raw bytes
*/
function _handle(
uint32 _origin,
bool _isPing,
bytes29 _message
) internal {
// get the volley count for this game
uint256 _count = _message.count();
uint32 _match = _message.matchId();
// emit a Received event
emit Received(_origin, _match, _count, _isPing);
// send the opposite volley back
_send(_origin, !_isPing, _match, _count + 1);
}
// ============ Dispatch message functions ============
/**
* @notice Initiate a PingPong match with the destination domain
* by sending the first Ping volley.
* @param _destinationDomain The domain to initiate the match with
*/
function initiatePingPongMatch(uint32 _destinationDomain) external {
// the PingPong match always begins with a Ping volley
bool _isPing = true;
// increment match counter
uint32 _match = nextMatch;
nextMatch = _match + 1;
// send the first volley to the destination domain
_send(_destinationDomain, _isPing, _match, 0);
}
/**
* @notice Send a Ping or Pong volley to the destination domain
* @param _destinationDomain The domain to send the volley to
* @param _isPing True if the volley to send is a Ping, false if it is a Pong
* @param _count The number of volleys in this match
*/
function _send(
uint32 _destinationDomain,
bool _isPing,
uint32 _match,
uint256 _count
) internal {
// get the xApp Router at the destinationDomain
bytes32 _remoteRouterAddress = _mustHaveRemote(_destinationDomain);
// format the ping message
test: migrate tests and deploy process to ts (#400) * refactor: migrate js lib to ts * refactor: rewrite extendEnvironment in ts * add: sample tests * add: extend hre * refactor: ts deploy and signer type * refactor: devDeploy for tests * document: isTestDeploy param, add common.test.ts * add: common tests * fix: common tests * clean: remove sample script * refactor: remove Signer type and use ethers.Signer * refactor: typescript directory * build: generate typechain * refactor: typescript code (#399) * refactor: optics-tests/optics-deploy * fix: install typechain * fix * fix: ts errors * add: merkle tests (#417) * add: merkle tests * clean: remove commented code * fix: merkle tests * test: add queue tests (#422) * Add queue tests * fix: add await to fix nonce bug * test: home ts (#401) * add: home tests * add: home tests * add: deploy home (broken) * refactor: add num confirmations to config * refactor: clean up around ts tests (#424) - add testChain.ts with utility functions for making test Chain and Deploy - refactor the Optics hardhat extension - improve typing of enums in Optics lib - remove most references to waffle (prefer ethers) - remove isTestDeploy from deployment args in favor of a test? on Deploys * refactor: clean test deploy, home tests passing * refactor: deployOptics * fix: white space * lint: fix white space * refactor: testCase vectors and imports * fix: rust test vector generation * fix: missing await Co-authored-by: James Prestwich <10149425+prestwich@users.noreply.github.com> * test: add message test (#423) * test: add message test * fix: replace require with import * fix: import for queue.test.ts * enhance: add type check * fix: import conflicts * test: add upgrade tests (#443) * test: add upgrade tests * fix: missing arg * refactor: clean up code * remove: unused code * refactor: deploy proxy * fix: unsaved code * fix: ts conflicts * add: utils ts (#453) * draft * refactor: remove waffle from home tests * fix: types, remove waffle Co-authored-by: yoduyodu <wang7ong@gmail.com> * test: add XAppConnectionManager tests (#456) * test: add XAppConnectionManager tests * test: initial setup for XAppConnectionManager tests * test: most XAppConnectionManager tests passing * fix: failing test * test: SimpleMessage ts (#468) * test: add cross-chain test utils * test: simpleMessage, mostly passing * fix: prove and process test * test: add initial state test, clean up * test: recoverymanager ts (#473) * test: add recoveryManager initial tests * fix: set recoveryManager in ts deploy * fix: set governor * test: replica ts (#483) * test: add replica tests * debug: fix failing tests * WIP: test: governance router/upgrade ts (#470) * test: add governance router tests * debug: some tests * debug: Gov Router tests * refactor: upgradeUtils * fix: missing await * test: clean up ts tests (#484) * clean: imports * refactor: utils * refactor: remove logs during testing * fix: weird bug, ethers.getSigners messes up describe blocks * delete: solidity/optics-core/js * update: pre-commit script for ts * update: pre-commit and scripts * fix: test, update with main * clean: types * fix: bad recipient handle * add: todo for gov router test * add: add back verify deploy stuff in js for now * bug: fix the governance upgrade test (#490) * fix: gov router upgrade test * feature: use TS in both solidity packages * bug: install deps in typescript dir in tests * feature: enhanced github action install * chore: disable automerge, and move lint before test Co-authored-by: Tong Wang <wang7ong@gmail.com> Co-authored-by: James Prestwich <10149425+prestwich@users.noreply.github.com> Co-authored-by: James Prestwich <james@prestwi.ch>
3 years ago
bytes memory _message = _isPing
? PingPongMessage.formatPing(_match, _count)
: PingPongMessage.formatPong(_match, _count);
// send the message to the xApp Router
(_home()).dispatch(_destinationDomain, _remoteRouterAddress, _message);
// emit a Sent event
emit Sent(_destinationDomain, _match, _count, _isPing);
}
}