Skip to content

Commit

Permalink
L1: changed to broadcast state root as u256 low high
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Jun 4, 2024
1 parent 6991430 commit bf672eb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
13 changes: 0 additions & 13 deletions ethereum/lib/FormatWords64.sol

This file was deleted.

23 changes: 23 additions & 0 deletions ethereum/lib/Uint256Splitter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

library Uint256Splitter {
uint256 constant _MASK = type(uint128).max;

/// @notice Splits a uint256 into two uint128s (low, high) represented as uint256s.
/// @param a The uint256 to split.
function split128(uint256 a) internal pure returns (uint256 lower, uint256 upper) {
return (a & _MASK, a >> 128);
}

/// @notice Merges two uint128s (low, high) into one uint256.
/// @param lower The lower uint256. The caller is required to pass a value that is less than 2^128 - 1.
/// @param upper The upper uint256.
function merge128(uint256 lower, uint256 upper) internal pure returns (uint256 a) {
require(lower <= _MASK, "Uint256Splitter: lower exceeds uint128");
// return (upper << 128) | lower;
assembly {
a := or(shl(128, upper), lower)
}
}
}
19 changes: 3 additions & 16 deletions ethereum/script/SendMessage.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,9 @@ contract Value is Script {
_l2Selector = vm.envUint("L2_SELECTOR_VALUE");
}

function run() public{
function run() public {
vm.startBroadcast(_privateKey);

// uint256[] memory payload = new uint256[](1);
// payload[0] = 123;

// // Remember that there is a cost of at least 20k wei to send a message.
// // Let's send 30k here to ensure that we pay enough for our payload serialization.
// ContractMsg(_contractMsgAddress).sendMessage{value: 30000}(
// _l2ContractAddress,
// _l2Selector,
// payload);

L1MessagesSender(_l1MessageSenderAddress).sendLatestParentHashToL2{value: 30000}();

vm.stopBroadcast();
Expand All @@ -64,7 +54,7 @@ contract Struct is Script {
_l2Selector = vm.envUint("L2_SELECTOR_STRUCT");
}

function run() public{
function run() public {
vm.startBroadcast(_privateKey);

uint256[] memory payload = new uint256[](2);
Expand All @@ -73,10 +63,7 @@ contract Struct is Script {

// Remember that there is a cost of at least 20k wei to send a message.
// Let's send 30k here to ensure that we pay enough for our payload serialization.
ContractMsg(_contractMsgAddress).sendMessage{value: 30000}(
_l2ContractAddress,
_l2Selector,
payload);
ContractMsg(_contractMsgAddress).sendMessage{value: 30000}(_l2ContractAddress, _l2Selector, payload);

vm.stopBroadcast();
}
Expand Down
24 changes: 13 additions & 11 deletions ethereum/src/L1MessageSender.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { FormatWords64 } from "lib/FormatWords64.sol";
import {Uint256Splitter} from "lib/Uint256Splitter.sol";

import "starknet/IStarknetMessaging.sol";

contract L1MessagesSender {
IStarknetMessaging private _snMessaging;
uint256 public immutable l2RecipientAddr;

using Uint256Splitter for uint256;

/// @dev starknetSelector(receive_from_l1)
uint256 constant SUBMIT_L1_BLOCKHASH_SELECTOR = 598342674068027518481179578557554850038206119856216505601406522348670006916;
uint256 constant SUBMIT_L1_BLOCKHASH_SELECTOR =
598342674068027518481179578557554850038206119856216505601406522348670006916;

// TODO - describe
constructor(address snMessaging, uint256 l2RecipientAddr_) {
Expand All @@ -31,15 +34,14 @@ contract L1MessagesSender {
}

function _sendBlockHashToL2(bytes32 parentHash_, uint256 blockNumber_) internal {
uint256[] memory message = new uint256[](5);
(bytes8 hashWord1, bytes8 hashWord2, bytes8 hashWord3, bytes8 hashWord4) = FormatWords64.fromBytes32(parentHash_);

message[0] = uint256(uint64(hashWord1));
message[1] = uint256(uint64(hashWord2));
message[2] = uint256(uint64(hashWord3));
message[3] = uint256(uint64(hashWord4));
message[4] = blockNumber_;
uint256[] memory message = new uint256[](4);
(uint256 parentHashLow, uint256 parentHashHigh) = uint256(parentHash_).split128();
(uint256 blockNumberLow, uint256 blockNumberHigh) = blockNumber_.split128();
message[0] = parentHashLow;
message[1] = parentHashHigh;
message[2] = blockNumberLow;
message[3] = blockNumberHigh;

_snMessaging.sendMessageToL2{value: msg.value}(l2RecipientAddr, SUBMIT_L1_BLOCKHASH_SELECTOR, message);
}
}
}

0 comments on commit bf672eb

Please sign in to comment.