Skip to content

Commit

Permalink
Merge pull request #15 from sCrypt-Inc/ctc_parameter
Browse files Browse the repository at this point in the history
use ctc parameter
  • Loading branch information
zhfnjust authored Feb 5, 2024
2 parents 06303b4 + d7b1ba9 commit 18ffc29
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
24 changes: 17 additions & 7 deletions src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,26 @@ export class Blockchain extends SmartContractLib {
static txInBlock(
txid: Sha256,
bh: BlockHeader,
merkleProof: MerkleProof
merkleProof: MerkleProof,
depth: number
): boolean {
return MerklePath.calcMerkleRoot(txid, merkleProof) == bh.merkleRoot
return (
MerklePath.calcMerkleRoot(txid, merkleProof, depth) == bh.merkleRoot
)
}

// Is txid the last transaction in a block
@method()
static lastTxInBlock(
txid: Sha256,
bh: BlockHeader,
merkleProof: MerkleProof
merkleProof: MerkleProof,
depth: number
): boolean {
let last = true
let root = txid

for (let i = 0; i < MerklePath.DEPTH; i++) {
for (let i = 0; i < depth; i++) {
const node = merkleProof[i]

if (node.pos != MerklePath.INVALID_NODE) {
Expand Down Expand Up @@ -139,15 +143,21 @@ export class Blockchain extends SmartContractLib {
static blockHeight(
bh: BlockHeader,
coinbaseTx: ByteString,
merkleProof: MerkleProof
merkleProof: MerkleProof,
depth: number
): bigint {
// Ensure coinbase it's in the block.
assert(
Blockchain.txInBlock(Sha256(hash256(coinbaseTx)), bh, merkleProof)
Blockchain.txInBlock(
Sha256(hash256(coinbaseTx)),
bh,
merkleProof,
depth
)
)

// Ensure it's the coinbase.
assert(MerklePath.isCoinbase(merkleProof))
assert(MerklePath.isCoinbase(merkleProof, depth))

return Blockchain.readBlockHeight(coinbaseTx)
}
Expand Down
16 changes: 8 additions & 8 deletions src/merklePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export type Node = {
export type MerkleProof = FixedArray<Node, 32> // If shorter than 32, pad with invalid nodes.

export class MerklePath extends SmartContractLib {
// Maximal depth of a Merkle tree/path, which can support a block with 2^32 transactions.
@prop()
static readonly DEPTH: bigint = 32n // TODO: Make configurable by lib user.

@prop()
static readonly INVALID_NODE: bigint = 0n

Expand All @@ -36,10 +32,14 @@ export class MerklePath extends SmartContractLib {

// According to the given leaf node and merkle path, calculate the hash of the root node of the merkle tree.
@method()
static calcMerkleRoot(leaf: Sha256, merkleProof: MerkleProof): Sha256 {
static calcMerkleRoot(
leaf: Sha256,
merkleProof: MerkleProof,
depth: number
): Sha256 {
let root = leaf

for (let i = 0; i < MerklePath.DEPTH; i++) {
for (let i = 0; i < depth; i++) {
const node = merkleProof[i]
if (node.pos != MerklePath.INVALID_NODE) {
// s is valid
Expand All @@ -55,9 +55,9 @@ export class MerklePath extends SmartContractLib {

// A tx is the blocks coinbase if all nodes on its Merkle path are on the right branch.
@method()
static isCoinbase(merkleproof: MerkleProof): boolean {
static isCoinbase(merkleproof: MerkleProof, depth: number): boolean {
let res = true
for (let i = 0; i < MerklePath.DEPTH; i++) {
for (let i = 0; i < depth; i++) {
const node = merkleproof[i]
if (node.pos != MerklePath.INVALID_NODE) {
// node on the right
Expand Down
6 changes: 3 additions & 3 deletions tests/contracts/blockchainTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class BlockchainTest extends SmartContract {
merkleProof: MerkleProof,
res: boolean
) {
assert(Blockchain.txInBlock(txid, bh, merkleProof) == res)
assert(Blockchain.txInBlock(txid, bh, merkleProof, 32) == res)
}

@method()
Expand All @@ -24,7 +24,7 @@ export class BlockchainTest extends SmartContract {
merkleProof: MerkleProof,
res: boolean
) {
assert(Blockchain.lastTxInBlock(txid, bh, merkleProof) == res)
assert(Blockchain.lastTxInBlock(txid, bh, merkleProof, 32) == res)
}

@method()
Expand All @@ -48,7 +48,7 @@ export class BlockchainTest extends SmartContract {
merkleProof: MerkleProof,
res: bigint
) {
assert(Blockchain.blockHeight(bh, coinbaseTx, merkleProof) == res)
assert(Blockchain.blockHeight(bh, coinbaseTx, merkleProof, 32) == res)
}

//@method()
Expand Down
4 changes: 2 additions & 2 deletions tests/contracts/merklePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { method, assert, SmartContract, Sha256 } from 'scrypt-ts'
export class MerklePathTest extends SmartContract {
@method()
public calcMerkleRoot(leaf: Sha256, merkleProof: MerkleProof, res: Sha256) {
assert(MerklePath.calcMerkleRoot(leaf, merkleProof) == res)
assert(MerklePath.calcMerkleRoot(leaf, merkleProof, 32) == res)
}

@method()
public isCoinbase(merkleProof: MerkleProof, res: boolean) {
assert(MerklePath.isCoinbase(merkleProof) == res)
assert(MerklePath.isCoinbase(merkleProof, 32) == res)
}
}
5 changes: 1 addition & 4 deletions tests/specs/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,10 +1281,7 @@ function prepProofFromWoc(proof: any): MerkleProof {
),
pos: MerklePath.INVALID_NODE,
}
return [
...res,
...Array(Number(MerklePath.DEPTH) - res.length).fill(invalidNode),
] as MerkleProof
return [...res, ...Array(32 - res.length).fill(invalidNode)] as MerkleProof
}

/**
Expand Down

0 comments on commit 18ffc29

Please sign in to comment.