From b98f2e386647f2d12a9f12c3aef951b057ed5b4e Mon Sep 17 00:00:00 2001 From: delovoyhomie Date: Mon, 20 Apr 2026 10:42:52 +0300 Subject: [PATCH 1/3] refactor(foundations): change Merkle proofs code sample to Tolk --- foundations/proofs/overview.mdx | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/foundations/proofs/overview.mdx b/foundations/proofs/overview.mdx index 3737e0dd8..0cf8735f8 100644 --- a/foundations/proofs/overview.mdx +++ b/foundations/proofs/overview.mdx @@ -202,11 +202,18 @@ const patchedShardState = rebuild(newShardState, path, accountState) // And repl Another interesting point is how we access the hash of the last known `ShardBlock`. -```tact title="Tact" -inline fun findShardInBinTree(root: Cell, address: Address, shardBitLen: Int): ShardDescr { - let curCs = root.beginParse(); - // It's std address, but we parse it as VarAddress to get hash part as Slice, not as Int - let accountId = myParseVarAddress(address.asSlice()).address; +```tolk title="Tolk" +@pure +fun slice.preloadIthRef(self, idx: int): cell + asm "PLDREFVAR" + +@inline +fun findShardInBinTree(root: cell, addr: address, shardBitLen: int): ShardDescr { + var curCs = root.beginParse(); + // Cast the address to a slice and skip the 11-bit prefix + // (3 tag bits + 8 workchain bits) to reach the account ID bits. + var accountId = addr as slice; + accountId.skipBits(11); repeat (shardBitLen) { if (accountId.loadBool()) { // If the bit is 1, we go to the right child @@ -222,12 +229,12 @@ inline fun findShardInBinTree(root: Cell, address: Address, shardBitLen: Int): S // ... -let mcBlockExtra = McBlockExtra.fromCell(blockHeader.extra.loadRef().beginParse().preloadIthRef(3)); -// shardHashes is a hashmap (workchain -> ShardDescr) -// Therefore, we only need to retrieve the ShardDescr for workchain 0, as we are working in basechain. -// We can use a non-null assertion, as we already proved that it is a valid block, and a valid masterchain block must have a ShardDescr for workchain 0. -let binTreeWithShardDescr: Cell = mcBlockExtra.shardHashes.get(0)!!; -let shardDescr = findShardInBinTree(binTreeWithShardDescr, jettonMaster, shardBitLen); +val mcBlockExtra = McBlockExtra.fromCell(blockHeader.extra.loadRef().beginParse().preloadIthRef(3)); +// shardHashes is a hashmap (workchain -> ShardDescr). +// We only need to retrieve the ShardDescr for workchain 0, since we are working in basechain. +// `mustGet` is safe here, as we already proved that it is a valid block, and a valid masterchain block must have a ShardDescr for workchain 0. +val binTreeWithShardDescr: cell = mcBlockExtra.shardHashes.mustGet(0); +val shardDescr = findShardInBinTree(binTreeWithShardDescr, jettonMaster, shardBitLen); ``` ```tlb From c67740ff79ba3e8f33e6dbb747fc8f4a164ed168 Mon Sep 17 00:00:00 2001 From: delovoyhomie Date: Thu, 23 Apr 2026 15:56:27 +0300 Subject: [PATCH 2/3] fix(proofs): align Tolk shard lookup example --- foundations/proofs/overview.mdx | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/foundations/proofs/overview.mdx b/foundations/proofs/overview.mdx index 0cf8735f8..272c26bd0 100644 --- a/foundations/proofs/overview.mdx +++ b/foundations/proofs/overview.mdx @@ -210,20 +210,19 @@ fun slice.preloadIthRef(self, idx: int): cell @inline fun findShardInBinTree(root: cell, addr: address, shardBitLen: int): ShardDescr { var curCs = root.beginParse(); - // Cast the address to a slice and skip the 11-bit prefix - // (3 tag bits + 8 workchain bits) to reach the account ID bits. - var accountId = addr as slice; - accountId.skipBits(11); + val (_, accountId) = addr.getWorkchainAndHash(); + var bitMask = 1 << 255; repeat (shardBitLen) { - if (accountId.loadBool()) { - // If the bit is 1, we go to the right child + if ((accountId & bitMask) != 0) { + // If the bit is 1, go to the right child. curCs = curCs.preloadIthRef(1).beginParse(); } else { - // If the bit is 0, we go to the left child + // If the bit is 0, go to the left child. curCs = curCs.preloadIthRef(0).beginParse(); } + bitMask = bitMask >> 1; } - curCs.skipBits(1); // We need to skip 1 bit - leaf tag of the tree node + curCs.skipBits(1); // Skip the leaf tag of the tree node. return ShardDescr.fromSlice(curCs); } @@ -231,8 +230,8 @@ fun findShardInBinTree(root: cell, addr: address, shardBitLen: int): ShardDescr val mcBlockExtra = McBlockExtra.fromCell(blockHeader.extra.loadRef().beginParse().preloadIthRef(3)); // shardHashes is a hashmap (workchain -> ShardDescr). -// We only need to retrieve the ShardDescr for workchain 0, since we are working in basechain. -// `mustGet` is safe here, as we already proved that it is a valid block, and a valid masterchain block must have a ShardDescr for workchain 0. +// Only the ShardDescr for workchain 0 is needed, since the target account is in basechain. +// `mustGet` is safe here because the block is already validated, and every valid masterchain block has a ShardDescr for workchain 0. val binTreeWithShardDescr: cell = mcBlockExtra.shardHashes.mustGet(0); val shardDescr = findShardInBinTree(binTreeWithShardDescr, jettonMaster, shardBitLen); ``` From 6a46d093808f260ebc417a14683f0ccb8aa4ee41 Mon Sep 17 00:00:00 2001 From: delovoyhomie Date: Thu, 23 Apr 2026 16:06:04 +0300 Subject: [PATCH 3/3] fix(proofs): avoid hidden slice mutability in Tolk example --- foundations/proofs/overview.mdx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/foundations/proofs/overview.mdx b/foundations/proofs/overview.mdx index 272c26bd0..26ed957da 100644 --- a/foundations/proofs/overview.mdx +++ b/foundations/proofs/overview.mdx @@ -228,7 +228,8 @@ fun findShardInBinTree(root: cell, addr: address, shardBitLen: int): ShardDescr // ... -val mcBlockExtra = McBlockExtra.fromCell(blockHeader.extra.loadRef().beginParse().preloadIthRef(3)); +var blockExtra = blockHeader.extra; +val mcBlockExtra = McBlockExtra.fromCell(blockExtra.loadRef().beginParse().preloadIthRef(3)); // shardHashes is a hashmap (workchain -> ShardDescr). // Only the ShardDescr for workchain 0 is needed, since the target account is in basechain. // `mustGet` is safe here because the block is already validated, and every valid masterchain block has a ShardDescr for workchain 0.