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

System router: flexible optimistic period, multicall #182

Merged
merged 37 commits into from
Sep 13, 2022

Conversation

ChiTimesChi
Copy link
Collaborator

@ChiTimesChi ChiTimesChi commented Sep 8, 2022

Description

SystemRouter contract is revisited to introduce the chain agnostic system calls. This also includes the ability to batch such calls in a package, where either all calls succeed or none do (multicall). Multicalls are currently only possible to a single destination chain, otherwise the execution invariant doesn't hold.

System call: sending

Any system contract is able to call another system contract on any chain without knowing its address. All that's needed for a call is:

  • Domain of chain where call is supposed to happen (local domain for call on the local chain)
  • Optimistic period that is required for executing this call (ignored for calls on the local chain)
  • Which system contract to call, i.e. Origin, Destination, etc (no exact address knowledge is required)
  • Data payload for the call

This is possible by SystemRouter storing all system addresses for the chain where it's deployed and sending system messages to the system routers on remote chains (which will handle the routing on destination chain).

System call: receiving

Regardless on what chain the recipient is located, when the call is routed to them, they will receive the data payload with following appended information:

  • Domain of chain where call originated (local domain for call on the local chain)
  • Which system contract originated the system call, i.e. Origin, Destination, etc (no exact address knowledge is required)
  • Timestamp when the merkle root (used for executing the cross-chain message) was submitted to Destination (block.timestamp for local chain calls)

function _formatCalldata(SystemEntity _caller, bytes memory _data)
internal
view
returns (bytes memory)
{
/**
* @dev Payload for contract call is:
* ====== ENCODED ON ORIGIN CHAIN ======
* 1. Function selector and params (_data)
* 2. (domain, caller) are the following two arguments
* ====== ENCODED ON REMOTE CHAIN ======
* 3. Root timestamp is the last argument,
* and will be appended before the call on destination chain.
*/
return abi.encodePacked(_data, abi.encode(localDomain, _caller));
}

function _localSystemCall(
uint8 _recipient,
bytes memory _payload,
uint256 _rootSubmittedAt
) internal {
address recipient = _getSystemAddress(_recipient);
require(recipient != address(0), "System Contract not set");
// this will call recipient and bubble the revert from the external call
recipient.functionCall(abi.encodePacked(_payload, _rootSubmittedAt));
/// @dev add root timestamp as the last argument
/// uint256 type allows us to use encodePacked w/o casting
}

Using these three parameters, a system contract can enforce any types of checks for external functions, e.g.

  • Function can be only called by Origin
  • Function can be only called from a local chain
  • Function can be only called from Synapse chain
  • Function can be only called if optimistic period of X hours has passed
  • Etc (including any combination of the above)

/**
* @dev Modifier for functions that are supposed to be called only from
* System Contracts on Synapse chain.
* Note: has to be used alongside with `onlySystemRouter`
* See `onlySystemRouter` for details about the functions protected by such modifiers.
*/
modifier onlySynapseChain(uint32 _originDomain) {
require(_originDomain == SYNAPSE_DOMAIN, "!synapseDomain");
_;
}

TODO

Metadata:

@github-actions github-actions bot added 2-reviewers C-Protocol-Critical PRs that modify protocol-critical code. M-contracts Module: Contracts labels Sep 8, 2022
@codecov
Copy link

codecov bot commented Sep 8, 2022

Codecov Report

Merging #182 (3d61027) into master (0cbebef) will increase coverage by 0.41843%.
The diff coverage is 93.22034%.

@@                 Coverage Diff                 @@
##              master        #182         +/-   ##
===================================================
+ Coverage   50.72043%   51.13886%   +0.41843%     
===================================================
  Files            190         191          +1     
  Lines           8606        8649         +43     
  Branches          82          87          +5     
===================================================
+ Hits            4365        4423         +58     
+ Misses          3818        3803         -15     
  Partials         423         423                 
Impacted Files Coverage Δ
...contracts-core/contracts/context/DomainContext.sol 100.00000% <ø> (ø)
...ntracts-core/test/harnesses/DestinationHarness.sol 100.00000% <ø> (ø)
...es/contracts-core/test/harnesses/OriginHarness.sol 66.66667% <ø> (-8.33334%) ⬇️
...s/contracts-core/contracts/system/SystemRouter.sol 90.69767% <90.00000%> (+14.22708%) ⬆️
...contracts-core/contracts/system/SystemContract.sol 66.66667% <100.00000%> (+33.33333%) ⬆️
...acts-core/test/harnesses/SystemContractHarness.sol 100.00000% <100.00000%> (ø)
services/scribe/backfill/contract.go 76.92308% <0.00000%> (+1.28204%) ⬆️
ethergo/chain/block.go 70.16129% <0.00000%> (+1.61289%) ⬆️
ethergo/signer/signer/kmssigner/signing.go 42.39130% <0.00000%> (+3.26087%) ⬆️
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@ChiTimesChi ChiTimesChi changed the title [WIP] System router: flexible optimistic period, multicall System router: flexible optimistic period, multicall Sep 8, 2022
@ChiTimesChi ChiTimesChi marked this pull request as ready for review September 8, 2022 13:05
@github-actions github-actions bot added go Pull requests that update Go code and removed needs-go-generate-agents labels Sep 11, 2022
Copy link
Contributor

@trajan0x trajan0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished a first pass review. Going to finish up a few other things then go through this again

@trajan0x
Copy link
Contributor

LGTM

@ChiTimesChi ChiTimesChi merged commit 3beecfa into master Sep 13, 2022
@ChiTimesChi ChiTimesChi deleted the feat/system-router branch September 13, 2022 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2-reviewers C-Protocol-Critical PRs that modify protocol-critical code. go Pull requests that update Go code M-contracts Module: Contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internal messaging
3 participants