Commit af06777b authored by Maurelian's avatar Maurelian Committed by GitHub

Add a warning about getMerkleRoot (#336)

parent 0ae66753
...@@ -545,6 +545,9 @@ contract OVM_CanonicalTransactionChain is iOVM_CanonicalTransactionChain, Lib_Ad ...@@ -545,6 +545,9 @@ contract OVM_CanonicalTransactionChain is iOVM_CanonicalTransactionChain, Lib_Ad
blockNumber = lastElement.blockNumber; blockNumber = lastElement.blockNumber;
} }
// For efficiency reasons getMerkleRoot modifies the `leaves` argument in place
// while calculating the root hash therefore any arguments passed to it must not
// be used again afterwards
_appendBatch( _appendBatch(
Lib_MerkleTree.getMerkleRoot(leaves), Lib_MerkleTree.getMerkleRoot(leaves),
totalElementsToAppend, totalElementsToAppend,
...@@ -937,18 +940,18 @@ contract OVM_CanonicalTransactionChain is iOVM_CanonicalTransactionChain, Lib_Ad ...@@ -937,18 +940,18 @@ contract OVM_CanonicalTransactionChain is iOVM_CanonicalTransactionChain, Lib_Ad
// If there are existing elements, this batch must come later. // If there are existing elements, this batch must come later.
if (getTotalElements() > 0) { if (getTotalElements() > 0) {
(,, uint40 lastTimestamp, uint40 lastBlockNumber) = _getBatchExtraData(); (,, uint40 lastTimestamp, uint40 lastBlockNumber) = _getBatchExtraData();
require( require(
_firstContext.blockNumber >= lastBlockNumber, _firstContext.blockNumber >= lastBlockNumber,
"Context block number is lower than last submitted." "Context block number is lower than last submitted."
); );
require( require(
_firstContext.timestamp >= lastTimestamp, _firstContext.timestamp >= lastTimestamp,
"Context timestamp is lower than last submitted." "Context timestamp is lower than last submitted."
); );
} }
// Sequencer cannot submit contexts which are more than the force inclusion period old. // Sequencer cannot submit contexts which are more than the force inclusion period old.
require( require(
_firstContext.timestamp + forceInclusionPeriodSeconds >= block.timestamp, _firstContext.timestamp + forceInclusionPeriodSeconds >= block.timestamp,
......
...@@ -20,7 +20,7 @@ import '@openzeppelin/contracts/math/SafeMath.sol'; ...@@ -20,7 +20,7 @@ import '@openzeppelin/contracts/math/SafeMath.sol';
/** /**
* @title OVM_StateCommitmentChain * @title OVM_StateCommitmentChain
* @dev The State Commitment Chain (SCC) contract contains a list of proposed state roots which * @dev The State Commitment Chain (SCC) contract contains a list of proposed state roots which
* Proposers assert to be a result of each transaction in the Canonical Transaction Chain (CTC). * Proposers assert to be a result of each transaction in the Canonical Transaction Chain (CTC).
* Elements here have a 1:1 correspondence with transactions in the CTC, and should be the unique * Elements here have a 1:1 correspondence with transactions in the CTC, and should be the unique
* state root calculated off-chain by applying the canonical transactions one by one. * state root calculated off-chain by applying the canonical transactions one by one.
* *
...@@ -334,6 +334,9 @@ contract OVM_StateCommitmentChain is iOVM_StateCommitmentChain, Lib_AddressResol ...@@ -334,6 +334,9 @@ contract OVM_StateCommitmentChain is iOVM_StateCommitmentChain, Lib_AddressResol
); );
} }
// For efficiency reasons getMerkleRoot modifies the `_batch` argument in place
// while calculating the root hash therefore any arguments passed to it must not
// be used again afterwards
Lib_OVMCodec.ChainBatchHeader memory batchHeader = Lib_OVMCodec.ChainBatchHeader({ Lib_OVMCodec.ChainBatchHeader memory batchHeader = Lib_OVMCodec.ChainBatchHeader({
batchIndex: getTotalBatches(), batchIndex: getTotalBatches(),
batchRoot: Lib_MerkleTree.getMerkleRoot(_batch), batchRoot: Lib_MerkleTree.getMerkleRoot(_batch),
......
...@@ -16,6 +16,7 @@ library Lib_MerkleTree { ...@@ -16,6 +16,7 @@ library Lib_MerkleTree {
* of leaves passed in is not a power of two, it pads out the tree with zero hashes. * of leaves passed in is not a power of two, it pads out the tree with zero hashes.
* If you do not know the original length of elements for the tree you are verifying, * If you do not know the original length of elements for the tree you are verifying,
* then this may allow empty leaves past _elements.length to pass a verification check down the line. * then this may allow empty leaves past _elements.length to pass a verification check down the line.
* Note that the _elements argument is modified, therefore it must not be used again afterwards
* @param _elements Array of hashes from which to generate a merkle root. * @param _elements Array of hashes from which to generate a merkle root.
* @return Merkle root of the leaves, with zero hashes for non-powers-of-two (see above). * @return Merkle root of the leaves, with zero hashes for non-powers-of-two (see above).
*/ */
...@@ -108,12 +109,12 @@ library Lib_MerkleTree { ...@@ -108,12 +109,12 @@ library Lib_MerkleTree {
/** /**
* Verifies a merkle branch for the given leaf hash. Assumes the original length * Verifies a merkle branch for the given leaf hash. Assumes the original length
* of leaves generated is a known, correct input, and does not return true for indices * of leaves generated is a known, correct input, and does not return true for indices
* extending past that index (even if _siblings would be otherwise valid.) * extending past that index (even if _siblings would be otherwise valid.)
* @param _root The Merkle root to verify against. * @param _root The Merkle root to verify against.
* @param _leaf The leaf hash to verify inclusion of. * @param _leaf The leaf hash to verify inclusion of.
* @param _index The index in the tree of this leaf. * @param _index The index in the tree of this leaf.
* @param _siblings Array of sibline nodes in the inclusion proof, starting from depth 0 (bottom of the tree). * @param _siblings Array of sibline nodes in the inclusion proof, starting from depth 0 (bottom of the tree).
* @param _totalLeaves The total number of leaves originally passed into. * @param _totalLeaves The total number of leaves originally passed into.
* @return Whether or not the merkle branch and leaf passes verification. * @return Whether or not the merkle branch and leaf passes verification.
*/ */
...@@ -188,7 +189,7 @@ library Lib_MerkleTree { ...@@ -188,7 +189,7 @@ library Lib_MerkleTree {
returns ( returns (
uint256 uint256
) )
{ {
require( require(
_in > 0, _in > 0,
"Lib_MerkleTree: Cannot compute ceil(log_2) of 0." "Lib_MerkleTree: Cannot compute ceil(log_2) of 0."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment