Skip to content

Commit

Permalink
fix: prunned branch hash and serialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlkv committed Mar 19, 2024
1 parent c88c2c0 commit 1eafdbf
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## Fixed
- `getRepr` and `getRefsDescriptor` now using level mask instead of level
- Cells with prunned branches hash and serialization fixes

## [0.56.1] - 2024-02-27

## Fixed
Expand Down
1 change: 1 addition & 0 deletions src/boc/cell/__testdata__/block2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
te6ccgECXgEADm8ABIGZDxeo9bqiaPkbDbaH4Q5dowDtAq+qyRgSE1ipNTD1xEqBcI0s97FaGzYvv2SIBFHWmEYfUvBfFFs2wIUX12hzwAELQUQJRgNzZHuaNKfUF5RFudjuCG3GyJLxqp0cq+PoJYyeE/XJwwAjAiQQEe9Vqv///xEDBgcKAqCbx6mHAAAAAIABAn5yiQAAAAEAAAAAAAAAAAAAAAAAZcnUTAAAKIVFNxhAAAAohUU3GFBHe31xAAghegIlq6cCJZCxxAAAAAUAAAAAAAAALgQFAJgAACiFRRiTxgIlq6e1lL+DC9ezi9DS8WhGM0tKJnHoXXHuNv8r1mdhKgBirHc2rOru18TI3aGBqa06Wqt9K8B0qUmJ2697zLjTu9L4AJgAACiFRSfWHgJ+cohdkvfYhv3hlgbh842HQVAD/v2wQONwCnTdkRctg53twXwAdyVJ1kAIeB/6OVtpi8CrkeBVhzHrd1oBUGuY1yjTKEgBAeIVQmVT0rfdJcMAYnyfpdi+TtkXZn0Of/1zEAoiLpZyAAMqigRaj2nM+fSsM/qseYYsv9oNnHRI7FxTMoldAXrPIIVT+K0W+94bj4V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flWtJS2AAE=
8 changes: 4 additions & 4 deletions src/boc/cell/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import { Cell } from "../Cell";
import { CellType } from "../CellType";
import { bitsToPaddedBuffer } from "../utils/paddedBits";

export function getRefsDescriptor(refs: Cell[], level: number, type: CellType) {
return refs.length + (type !== CellType.Ordinary ? 1 : 0) * 8 + level * 32;
export function getRefsDescriptor(refs: Cell[], levelMask: number, type: CellType) {
return refs.length + (type !== CellType.Ordinary ? 1 : 0) * 8 + levelMask * 32;
}

export function getBitsDescriptor(bits: BitString) {
let len = bits.length;
return Math.ceil(len / 8) + Math.floor(len / 8);
}

export function getRepr(originalBits: BitString, bits: BitString, refs: Cell[], level: number, type: CellType) {
export function getRepr(originalBits: BitString, bits: BitString, refs: Cell[], level: number, levelMask: number, type: CellType) {

// Allocate
const bitsLen = Math.ceil(bits.length / 8);
const repr = Buffer.alloc(2 + bitsLen + (2 + 32) * refs.length);

// Write descriptors
let reprCursor = 0;
repr[reprCursor++] = getRefsDescriptor(refs, level, type);
repr[reprCursor++] = getRefsDescriptor(refs, levelMask, type);
repr[reprCursor++] = getBitsDescriptor(originalBits);

// Write bits
Expand Down
7 changes: 7 additions & 0 deletions src/boc/cell/serialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ describe('boc', () => {
expect(cell.hash().toString('hex')).toBe(testCase.hash);
});

it('should deserialize block 2', () => {
let testCase = fs.readFileSync(__dirname + '/__testdata__/block2.txt', 'utf8');
let cell = Cell.fromBase64(testCase);

expect(cell.hash().toString('hex')).toBe('25e19f8c4574804a8cabade6bab736a27a67f4f6696a8a0feb93b3dfbfab7fcf');
});

it('should serialize boc with index', () => {
let cell = beginCell()
.storeUint(228, 32)
Expand Down
2 changes: 1 addition & 1 deletion src/boc/cell/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export function deserializeBoc(src: Buffer) {
}

function writeCellToBuilder(cell: Cell, refs: number[], sizeBytes: number, to: BitBuilder) {
let d1 = getRefsDescriptor(cell.refs, cell.level(), cell.type);
let d1 = getRefsDescriptor(cell.refs, cell.mask.value, cell.type);
let d2 = getBitsDescriptor(cell.bits);
to.writeUint(d1, 8);
to.writeUint(d2, 8);
Expand Down
2 changes: 1 addition & 1 deletion src/boc/cell/wonderCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function wonderCalculator(type: CellType, bits: BitString, refs: Cell[]):
// Hash
//

let repr = getRepr(bits, currentBits, refs, levelI, type);
let repr = getRepr(bits, currentBits, refs, levelI, levelMask.apply(levelI).value, type);
let hash = sha256_sync(repr);

//
Expand Down

0 comments on commit 1eafdbf

Please sign in to comment.