Skip to content

Commit

Permalink
Fixed bits calculation for message either serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed May 15, 2023
1 parent 5ef35a5 commit 63c0825
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tlb/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions tlb/message_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tlb

import (
"bytes"
"encoding/hex"
"testing"

"github.com/xssnick/tonutils-go/address"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 63c0825

Please sign in to comment.