Skip to content

Commit

Permalink
feat: public data tree as indexed tree (#3566)
Browse files Browse the repository at this point in the history
Resolves #501 

This PR changes the Public Data tree to an indexed tree of height 40.
This optimizes insertions at the base rollup, reducing hashing and
allowing batch insertion. In order to do this:
 - Now blocks use a snapshot for the public data tree, not just a root
- Now indexed leaves have an updateTo method that allows updates.
Nullifiers disallow updates.
- The standard indexed tree realizes when a low leaf is an exact match
and updates the low leaf instead.
- The base rollup does indexed tree insertions for the public data tree,
applying the update logic.
- When fetching a slot from the public data tree, we get the low leaf
for the given slot. If the low leaf is not an exact match, the value
hasn't been inserted and the stored value is zero. If it's an exact
match, the latest value is contained in the leaf.
  • Loading branch information
sirasistant authored Dec 14, 2023
1 parent 260c7c3 commit 4711ef7
Show file tree
Hide file tree
Showing 78 changed files with 2,175 additions and 789 deletions.
4 changes: 3 additions & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ library Constants {
uint256 internal constant FUNCTION_TREE_HEIGHT = 5;
uint256 internal constant CONTRACT_TREE_HEIGHT = 16;
uint256 internal constant NOTE_HASH_TREE_HEIGHT = 32;
uint256 internal constant PUBLIC_DATA_TREE_HEIGHT = 254;
uint256 internal constant PUBLIC_DATA_TREE_HEIGHT = 40;
uint256 internal constant NULLIFIER_TREE_HEIGHT = 20;
uint256 internal constant L1_TO_L2_MSG_TREE_HEIGHT = 16;
uint256 internal constant ROLLUP_VK_TREE_HEIGHT = 8;
Expand All @@ -56,8 +56,10 @@ library Constants {
uint256 internal constant NOTE_HASH_SUBTREE_HEIGHT = 7;
uint256 internal constant NOTE_HASH_SUBTREE_SIBLING_PATH_LENGTH = 25;
uint256 internal constant NULLIFIER_SUBTREE_HEIGHT = 7;
uint256 internal constant PUBLIC_DATA_SUBTREE_HEIGHT = 4;
uint256 internal constant ARCHIVE_HEIGHT = 16;
uint256 internal constant NULLIFIER_SUBTREE_SIBLING_PATH_LENGTH = 13;
uint256 internal constant PUBLIC_DATA_SUBTREE_SIBLING_PATH_LENGTH = 36;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_HEIGHT = 4;
uint256 internal constant L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH = 12;
uint256 internal constant FUNCTION_SELECTOR_NUM_BYTES = 4;
Expand Down
70 changes: 36 additions & 34 deletions l1-contracts/src/core/libraries/Decoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,41 @@ import {Hash} from "./Hash.sol";
* | 0x00c4 | 0x04 | startNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x00c8 | 0x20 | startContractTreeSnapshot.root
* | 0x00e8 | 0x04 | startContractTreeSnapshot.nextAvailableLeafIndex
* | 0x00ec | 0x20 | startPublicDataTreeRoot
* | 0x010c | 0x20 | startL1ToL2MessageTreeSnapshot.root
* | 0x012c | 0x04 | startL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex
* | 0x0130 | 0x20 | startArchiveSnapshot.root
* | 0x0150 | 0x04 | startArchiveSnapshot.nextAvailableLeafIndex
* | 0x0154 | 0x20 | endNoteHashTreeSnapshot.root
* | 0x0174 | 0x04 | endNoteHashTreeSnapshot.nextAvailableLeafIndex
* | 0x0178 | 0x20 | endNullifierTreeSnapshot.root
* | 0x0198 | 0x04 | endNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x019c | 0x20 | endContractTreeSnapshot.root
* | 0x01bc | 0x04 | endContractTreeSnapshot.nextAvailableLeafIndex
* | 0x01c0 | 0x20 | endPublicDataTreeRoot
* | 0x01e0 | 0x20 | endL1ToL2MessageTreeSnapshot.root
* | 0x0200 | 0x04 | endL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex
* | 0x0204 | 0x20 | endArchiveSnapshot.root
* | 0x0224 | 0x04 | endArchiveSnapshot.nextAvailableLeafIndex
* | 0x0228 | 0x04 | len(newCommitments) (denoted a)
* | 0x022c | a * 0x20 | newCommitments
* | 0x022c + a * 0x20 | 0x04 | len(newNullifiers) (denoted b)
* | 0x0230 + a * 0x20 | b * 0x20 | newNullifiers
* | 0x0230 + a * 0x20 + b * 0x20 | 0x04 | len(newPublicDataWrites) (denoted c)
* | 0x0234 + a * 0x20 + b * 0x20 | c * 0x40 | newPublicDataWrites
* | 0x0234 + a * 0x20 + b * 0x20 + c * 0x40 | 0x04 | len(newL2ToL1Msgs) (denoted d)
* | 0x0238 + a * 0x20 + b * 0x20 + c * 0x40 | d * 0x20 | newL2ToL1Msgs
* | 0x0238 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 | 0x04 | len(contracts) (denoted e)
* | 0x023c + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 | e * 0x20 | newContracts
* | 0x023c + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x20 | e * 0x34 | newContractsData
* | 0x023c + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 | 0x04 | len(newL1ToL2Msgs) (denoted f)
* | 0x0240 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 | f * 0x20 | newL1ToL2Msgs
* | 0x0240 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 | 0x04 | byteLen(newEncryptedLogs) (denoted g)
* | 0x0244 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 | g | newEncryptedLogs
* | 0x0244 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 + g | 0x04 | byteLen(newUnencryptedLogs) (denoted h)
* | 0x0248 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 + g | h | newUnencryptedLogs
* | 0x00ec | 0x20 | startPublicDataTreeSnapshot.root
* | 0x010c | 0x04 | startPublicDataTreeSnapshot.nextAvailableLeafIndex
* | 0x0110 | 0x20 | startL1ToL2MessageTreeSnapshot.root
* | 0x0130 | 0x04 | startL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex
* | 0x0134 | 0x20 | startArchiveSnapshot.root
* | 0x0154 | 0x04 | startArchiveSnapshot.nextAvailableLeafIndex
* | 0x0158 | 0x20 | endNoteHashTreeSnapshot.root
* | 0x0178 | 0x04 | endNoteHashTreeSnapshot.nextAvailableLeafIndex
* | 0x017c | 0x20 | endNullifierTreeSnapshot.root
* | 0x019c | 0x04 | endNullifierTreeSnapshot.nextAvailableLeafIndex
* | 0x01a0 | 0x20 | endContractTreeSnapshot.root
* | 0x01c0 | 0x04 | endContractTreeSnapshot.nextAvailableLeafIndex
* | 0x01c4 | 0x20 | endPublicDataTreeSnapshot.root
* | 0x01e4 | 0x04 | endPublicDataTreeSnapshot.nextAvailableLeafIndex
* | 0x01e8 | 0x20 | endL1ToL2MessageTreeSnapshot.root
* | 0x0208 | 0x04 | endL1ToL2MessageTreeSnapshot.nextAvailableLeafIndex
* | 0x020c | 0x20 | endArchiveSnapshot.root
* | 0x022c | 0x04 | endArchiveSnapshot.nextAvailableLeafIndex
* | 0x0230 | 0x04 | len(newCommitments) (denoted a)
* | 0x0234 | a * 0x20 | newCommitments
* | 0x0234 + a * 0x20 | 0x04 | len(newNullifiers) (denoted b)
* | 0x0238 + a * 0x20 | b * 0x20 | newNullifiers
* | 0x0238 + a * 0x20 + b * 0x20 | 0x04 | len(newPublicDataWrites) (denoted c)
* | 0x023c + a * 0x20 + b * 0x20 | c * 0x40 | newPublicDataWrites
* | 0x023c + a * 0x20 + b * 0x20 + c * 0x40 | 0x04 | len(newL2ToL1Msgs) (denoted d)
* | 0x0240 + a * 0x20 + b * 0x20 + c * 0x40 | d * 0x20 | newL2ToL1Msgs
* | 0x0240 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 | 0x04 | len(contracts) (denoted e)
* | 0x0244 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 | e * 0x20 | newContracts
* | 0x0244 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x20 | e * 0x34 | newContractsData
* | 0x0244 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 | 0x04 | len(newL1ToL2Msgs) (denoted f)
* | 0x0248 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 | f * 0x20 | newL1ToL2Msgs
* | 0x0248 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 | 0x04 | byteLen(newEncryptedLogs) (denoted g)
* | 0x024c + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 | g | newEncryptedLogs
* | 0x024c + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 + g | 0x04 | byteLen(newUnencryptedLogs) (denoted h)
* | 0x0250 + a * 0x20 + b * 0x20 + c * 0x40 + d * 0x20 + e * 0x54 + f * 0x20 + g | h | newUnencryptedLogs
* | --- | --- | ---
*/
library Decoder {
Expand Down Expand Up @@ -97,7 +99,7 @@ library Decoder {
uint256 private constant START_TREES_BLOCK_HEADER_OFFSET = 0x80;

// The size of the block header elements
uint256 private constant TREES_BLOCK_HEADER_SIZE = 0xd4;
uint256 private constant TREES_BLOCK_HEADER_SIZE = 0xd8;

// Where the end of trees metadata begins in the block
uint256 private constant END_TREES_BLOCK_HEADER_OFFSET =
Expand Down
Loading

0 comments on commit 4711ef7

Please sign in to comment.