From 63c08259d9c14b1078202fc42f2013b8f6fead1b Mon Sep 17 00:00:00 2001 From: Oleg Baranov Date: Mon, 15 May 2023 09:22:27 +0400 Subject: [PATCH] Fixed bits calculation for message either serialization --- tlb/message.go | 6 +++--- tlb/message_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/tlb/message.go b/tlb/message.go index b46d56f1..e9e00e0c 100644 --- a/tlb/message.go +++ b/tlb/message.go @@ -210,7 +210,7 @@ func (m *InternalMessage) ToCell() (*cell.Cell, error) { } if m.Body != nil { - if b.BitsLeft() < m.Body.BitsSize() { + if int(b.BitsLeft())-1 < int(m.Body.BitsSize()) { b.MustStoreBoolBit(true) b.MustStoreRef(m.Body) } else { @@ -242,7 +242,7 @@ func (m *ExternalMessage) ToCell() (*cell.Cell, error) { return nil, fmt.Errorf("failed to serialize state init: %w", err) } - if builder.BitsLeft()-2 < stateCell.BitsSize() || builder.RefsLeft()-2 < m.Body.RefsNum() { + if int(builder.BitsLeft())-2 < int(stateCell.BitsSize()) || int(builder.RefsLeft())-2 < int(m.Body.RefsNum()) { builder.MustStoreBoolBit(true) // state as ref builder.MustStoreRef(stateCell) } else { @@ -251,7 +251,7 @@ func (m *ExternalMessage) ToCell() (*cell.Cell, error) { } } - if builder.BitsLeft() < m.Body.BitsSize() || builder.RefsLeft() < m.Body.RefsNum() { + if int(builder.BitsLeft())-1 < int(m.Body.BitsSize()) || builder.RefsLeft() < m.Body.RefsNum() { builder.MustStoreBoolBit(true) // body as ref builder.MustStoreRef(m.Body) } else { diff --git a/tlb/message_test.go b/tlb/message_test.go index 29696c04..583be6bc 100644 --- a/tlb/message_test.go +++ b/tlb/message_test.go @@ -1,6 +1,8 @@ package tlb import ( + "bytes" + "encoding/hex" "testing" "github.com/xssnick/tonutils-go/address" @@ -50,6 +52,30 @@ func TestInternalMessage_ToCell(t *testing.T) { // need to deploy contract on te } } +func TestCornerMessage(t *testing.T) { + msgBoc, _ := hex.DecodeString("b5ee9c724101020100860001b36800bf4c6bdca25797e55d700c1a5448e2af5d1ac16f9a9628719a4e1eb2b44d85e33fd104a366f6fb17799871f82e00e4f2eb8ae6aaf6d3e0b3fb346cd0208e23725e14094ba15d20071f12260000446ee17a9b0cc8c028d8c001004d8002b374733831aac3455708e8f1d2c7f129540b982d3a5de8325bf781083a8a3d2a04a7f943813277f3ea") + + c, err := cell.FromBOC(msgBoc) + if err != nil { + t.Fatal(err) + } + + var m InternalMessage + err = LoadFromCell(&m, c.BeginParse()) + if err != nil { + t.Fatal(err) + } + + c2, err := ToCell(m) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(c.Hash(), c2.Hash()) { + t.Fatal("hash not match") + } +} + func TestMessage_LoadFromCell(t *testing.T) { t.Run("internal msg case", func(t *testing.T) { var msg Message