From 873dd86f2e296ec9520c1e2a034fc6cda4b7216b Mon Sep 17 00:00:00 2001 From: Spite Date: Thu, 18 Jan 2024 00:49:45 +0400 Subject: [PATCH] fix(Message): edge case with ExtraCurrency for storeMessage + test --- src/types/Message.spec.ts | 23 +++++++++++++++++++++++ src/types/Message.ts | 12 +++--------- 2 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 src/types/Message.spec.ts diff --git a/src/types/Message.spec.ts b/src/types/Message.spec.ts new file mode 100644 index 0000000..16a6327 --- /dev/null +++ b/src/types/Message.spec.ts @@ -0,0 +1,23 @@ +/** + * Copyright (c) Whales Corp. + * All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { beginCell } from "../boc/Builder"; +import { Cell } from "../boc/Cell"; +import {loadMessage, storeMessage} from "./Message"; + +describe('Message', () => { + it('should handle edge case with extra currency', () => { + const tx = 'te6cckEBBwEA3QADs2gB7ix8WDhQdzzFOCf6hmZ2Dzw2vFNtbavUArvbhXqqqmEAMpuMhx8zp7O3wqMokkuyFkklKpftc4Dh9_5bvavmCo-UXR6uVOIGMkCwAAAAAAC3GwLLUHl_4AYCAQCA_____________________________________________________________________________________gMBPAUEAwFDoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOAUACAAAAAAAAAANoAAAAAEIDF-r-4Q'; + const cell = Cell.fromBase64(tx); + const message = loadMessage(cell.beginParse()); + let stored = beginCell() + .store(storeMessage(message)) + .endCell(); + expect(stored.equals(cell)).toBe(true); + }); +}); \ No newline at end of file diff --git a/src/types/Message.ts b/src/types/Message.ts index 1bdd594..77ed887 100644 --- a/src/types/Message.ts +++ b/src/types/Message.ts @@ -59,10 +59,8 @@ export function storeMessage(message: Message, opts?: { forceRef?: boolean }) { let needRef = false; if (opts && opts.forceRef) { needRef = true; - } else if (builder.availableBits - 2 /* At least on byte for ref flag */ >= initCell.bits) { - needRef = false; } else { - needRef = true; + needRef = builder.availableBits - 2 /* At least two bits for ref flags */ < initCell.bits + message.body.bits.length; } // Persist init @@ -82,12 +80,8 @@ export function storeMessage(message: Message, opts?: { forceRef?: boolean }) { if (opts && opts.forceRef) { needRef = true; } else { - if (builder.availableBits - 1 /* At least on byte for ref flag */ >= message.body.bits.length && - builder.refs + message.body.refs.length <= 4) { - needRef = false; - } else { - needRef = true; - } + needRef = builder.availableBits - 1 /* At least one bit for ref flag */ < message.body.bits.length || + builder.refs + message.body.refs.length > 4; } if (needRef) { builder.storeBit(true);