Last active
August 30, 2022 11:20
-
-
Save fredlacs/69e421052825ea699644a5626b9694ea to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: Unlicense | |
pragma solidity ^0.8.0; | |
contract L1Sender { | |
address payable immutable l2Receiver; | |
constructor(address _l2Receiver) { | |
l2Receiver = payable(_l2Receiver); | |
} | |
function sendToL2(uint256 numOfHashes) external { | |
require(numOfHashes < 256, "MAX_LOOKUP"); | |
uint256 head = block.number-1; | |
bytes32[] memory blockHashes = new bytes32[](numOfHashes); | |
for(uint8 i = 0; i<numOfHashes; i++) { | |
blockHashes[i] = blockhash(head-i); | |
} | |
L2Receiver(l2Receiver).receiveFromL1(msg.sender, head, blockHashes); | |
} | |
} | |
contract L2Receiver { | |
address immutable l1Sender; | |
uint256 immutable bountyPerBlock; | |
mapping(uint256 => bytes32) blockNumToHash; | |
constructor(uint256 _bountyPerBlock, address _l1Sender) { | |
bountyPerBlock = _bountyPerBlock; | |
l1Sender = _l1Sender; | |
} | |
function receiveFromL1(address rewardAddress, uint256 head, bytes32[] calldata blockHashes) external { | |
require(msg.sender == l1Sender, "NOT_L1_SENDER"); | |
for(uint256 i = 0; i< blockHashes.length; i++) { | |
if(blockNumToHash[head-i] == bytes32(0)) { | |
// send bounty if value not set | |
(bool success, ) = rewardAddress.call{ value: bountyPerBlock }(""); | |
require(success, "TRANSFER_FAIL"); | |
} | |
blockNumToHash[head-i] = blockHashes[i]; | |
} | |
} | |
receive() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment