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

DevEx workflow improvement #88

Closed
platocrat opened this issue Apr 24, 2021 · 5 comments
Closed

DevEx workflow improvement #88

platocrat opened this issue Apr 24, 2021 · 5 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request

Comments

@platocrat
Copy link
Contributor

platocrat commented Apr 24, 2021

Description
@TransmissionsDev suggested that docs should more clearly advise that your tests work on jsvm and local l2geth before testing against Kovan or in a CI.
It can also be more explicit that debugging first with the jsvm and local l2geth is far easier than against Kovan or in a CI.

There should be more emphasis on this suggestion.

Motivation
Quoting @TransmissionsDev from discord:
"That way all the existing tooling will work and you get nice revert messages.
If you use xDomainMessenger and other stuff, you are going to have to mock that out but it is worth it."

@TransmissionsDev could you expand on what exactly to mock and give an example?

Additional motivation:

  • Improve our documentation by being explicit and concise
  • Incorporate user and community member feedback to improve documentation and DevEx

Estimated effort and complexity

  • Effort: 2-4
  • Complexity: 0

Tags to related packages

  • l2geth

Update

Include content in docs that addresses t11s' reply below:

It can also be more explicit that debugging first with the jsvm and local l2geth is far easier than against Kovan or in a CI.

Mainly was emphasizing that debugging with jsvm specifically is much easier than l2geth. l2geth doesn't give revert messages, will only show gas estimations for l2 contracts with solidity-gas-reporter (even if you have l1 contracts you interact with in your tests) and is incompatible with solidity-coverage. You also can't use hardhat's console.sol with l2geth of course.

@transmissions11
Copy link

transmissions11 commented Apr 24, 2021

could you expand on what exactly to mock and give an example?

Basically any Optimism precompiles that you use when running your tests on l2geth (OVM_ETH, cross domain messengers, etc, etc) need to be manually deployed in the jsvm (where as on L2 geth they are assigned addresses at geth startup and useable without setup in tests).

However some of those contracts are pretty complex to setup manually on L1 and require external relayers, etc so for some I would recommend writing a custom "mock" version.

Here's my extremely insecure (but simple) implementation of a mock "cross domain messenger":

// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.7.6;

contract MockCrossDomainMessenger {
   struct xDomainMessage {
       address _target;
       bytes _message;
       uint32 _gasLimit;
       address sender;
   }

   xDomainMessage currentMessage;

   function xDomainMessageSender() external view returns (address) {
       return currentMessage.sender;
   }

   function sendMessage(
       address _target,
       bytes memory _message,
       uint32 _gasLimit
   ) external {
       uint256 startingGas = gasleft();
       uint256 gasToConsume = _gasLimit / 32;

       currentMessage = xDomainMessage(_target, _message, _gasLimit, msg.sender);

       // Mimic enqueue gas burn (https://github.com/ethereum-optimism/optimism/blob/master/packages/contracts/contracts/optimistic-ethereum/OVM/chain/OVM_CanonicalTransactionChain.sol)
       uint256 i;
       while (startingGas - gasleft() < gasToConsume) {
           i++;
       }
   }

   function relayCurrentMessage() external {
       (bool success, bytes memory result) = currentMessage._target.call(currentMessage._message);

       require(success, _getRevertMsg(result));
       delete currentMessage;
   }

   function _getRevertMsg(bytes memory _returnData) private pure returns (string memory) {
       // If the _res length is less than 68, then the transaction failed silently (without a revert message)
       if (_returnData.length < 68) return "Transaction reverted silently";

       assembly {
           // Slice the sighash.
           _returnData := add(_returnData, 0x04)
       }
       return abi.decode(_returnData, (string)); // All that remains is the revert string
   }
}

Contracts can interact with this "cross domain messenger" with the same interface as is given by the l2geth precompile but the mock simplifies relaying the message (and doesn't actually do any "cross domain" calls as everything is happening in the jsvm)

@transmissions11
Copy link

transmissions11 commented Apr 24, 2021

It can also be more explicit that debugging first with the jsvm and local l2geth is far easier than against Kovan or in a CI.

Mainly was emphasizing that debugging with jsvm specifically is much easier than l2geth. l2geth doesn't give revert messages, will only show gas estimations for l2 contracts with solidity-gas-reporter (even if you have l1 contracts you interact with in your tests) and is incompatible with solidity-coverage. You also can't use hardhat's console.sol with l2geth of course.

@platocrat
Copy link
Contributor Author

Contracts can interact with this "cross domain messenger" with the same interface as is given by the l2geth precompile but the mock simplifies relaying the message (and doesn't actually do any "cross domain" calls as everything is happening in the jsvm)

Sorry, I was being a numpty and didn't realize I was overlooking the cross-domain messaging 😅.

l2geth doesn't give revert messages, will only show gas estimations for l2 contracts with solidity-gas-reporter (even if you have l1 contracts you interact with in your tests) and is incompatible with solidity-coverage.

ethereum-optimism/optimism/issues/474 should fix the issue with revert reasons not being returned when using l2geth.

@platocrat
Copy link
Contributor Author

Per this comment on ethereum-optimism/optimism/issues/474, the fix for revert reasons issue will not be pushed to production until 05/03/21

@platocrat platocrat added enhancement New feature or request bug Something isn't working documentation Improvements or additions to documentation labels Apr 30, 2021
@platocrat
Copy link
Contributor Author

Stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants