Skip to content

Commit

Permalink
Merge pull request #120 from ryandgoulding/fix/ping_pong_unit_test
Browse files Browse the repository at this point in the history
Fix PingPong Unit Test
  • Loading branch information
TRileySchwarz authored Oct 25, 2023
2 parents 984a487 + d287ee0 commit 486adbf
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 233 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
### Install & Run tests
```shell
yarn install
npx hardhat test
yarn test
```

* The code in the `/contracts` folder demonstrates LayerZero behaviours.
Expand Down
57 changes: 37 additions & 20 deletions contracts/examples/PingPong.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: MIT

//
// Note: you will need to fund each deployed contract with gas
// Note: You will need to fund each deployed contract with gas.
//
// PingPong sends a LayerZero message back and forth between chains
// until it is paused or runs out of gas!
// a predetermined number of times (or until it runs out of gas).
//
// Demonstrates:
// 1. a recursive feature of calling send() from inside lzReceive()
Expand All @@ -16,57 +16,74 @@ pragma abicoder v2;

import "../lzApp/NonblockingLzApp.sol";

/// @title PingPong
/// @notice Sends a LayerZero message back and forth between chains a predetermined number of times.
contract PingPong is NonblockingLzApp {
// event emitted every ping() to keep track of consecutive pings count
event Ping(uint pings);

// constructor requires the LayerZero endpoint for this chain
/// @dev event emitted every ping() to keep track of consecutive pings count
event Ping(uint256 pingCount);

/// @param _endpoint The LayerZero endpoint address.
constructor(address _endpoint) NonblockingLzApp(_endpoint) {}

// pings the destination chain, along with the current number of pings sent
/// @notice Pings the destination chain, along with the current number of pings sent.
/// @param _dstChainId The destination chain ID.
/// @param _totalPings The total number of pings to send.
function ping(
uint16 _dstChainId
uint16 _dstChainId,
uint256 _totalPings
) public {
_ping(_dstChainId, 0);
_ping(_dstChainId, 0, _totalPings);
}

// pings the destination chain, along with the current number of pings sent
/// @dev Internal function to ping the destination chain, along with the current number of pings sent.
/// @param _dstChainId The destination chain ID.
/// @param _pings The current ping count.
/// @param _totalPings The total number of pings to send.
function _ping(
uint16 _dstChainId,
uint _ping
uint256 _pings,
uint256 _totalPings
) internal {
require(address(this).balance > 0, "This contract ran out of money.");

// encode the payload with the number of pings
bytes memory payload = abi.encode(_ping);
bytes memory payload = abi.encode(_pings, _totalPings);

// encode the adapter parameters
uint16 version = 1;
uint256 gasForDestinationLzReceive = 350000;
bytes memory adapterParams = abi.encodePacked(version, gasForDestinationLzReceive);

// send LayerZero message
_lzSend( // {value: messageFee} will be paid out of this contract!
_dstChainId, // destination chainId
payload, // abi.encode()'ed bytes
payable(this), // (msg.sender will be this contract) refund address (LayerZero will refund any extra gas back to caller of send()
address(0x0), // future param, unused for this example
_lzSend( // {value: messageFee} will be paid out of this contract!
_dstChainId, // destination chainId
payload, // abi.encode()'ed bytes
payable(this), // (msg.sender will be this contract) refund address (LayerZero will refund any extra gas back to caller of send())
address(0x0), // future param, unused for this example
adapterParams, // v1 adapterParams, specify custom destination gas qty
address(this).balance
);
}

/// @dev Internal function to handle incoming Ping messages.
/// @param _srcChainId The source chain ID from which the message originated.
/// @param _payload The payload of the incoming message.
function _nonblockingLzReceive(
uint16 _srcChainId,
bytes memory _srcAddress,
bytes memory, /*_srcAddress*/
uint64, /*_nonce*/
bytes memory _payload
) internal override {
// decode the number of pings sent thus far
uint pings = abi.decode(_payload, (uint)) + 1;
emit Ping(pings);
(uint256 pingCount, uint256 totalPings) = abi.decode(_payload, (uint256, uint256));
++pingCount;
emit Ping(pingCount);

// *pong* back to the other side
_ping(_srcChainId,pings);
if (pingCount < totalPings) {
_ping(_srcChainId, pingCount, totalPings);
}
}

// allow this contract to receive ether
Expand Down
1 change: 0 additions & 1 deletion contracts/lzApp/NonblockingLzApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ abstract contract NonblockingLzApp is LzApp {
150,
abi.encodeWithSelector(this.nonblockingLzReceive.selector, _srcChainId, _srcAddress, _nonce, _payload)
);
// try-catch all errors/exceptions
if (!success) {
_storeFailedMessage(_srcChainId, _srcAddress, _nonce, _payload, reason);
}
Expand Down
Loading

0 comments on commit 486adbf

Please sign in to comment.