Skip to content

Commit 7365278

Browse files
committed
Add DecodeJSONMapFromCell tests
1 parent 23b0de2 commit 7365278

File tree

2 files changed

+153
-3
lines changed

2 files changed

+153
-3
lines changed

pkg/ton/debug/lib/lib.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func NewMessageInfo(name string, msg any) (MessageInfo, error) {
7171

7272
// NewMessageInfoFromCell attempts to decode the given cell using the provided TL-B candidates mapped by their opcodes.
7373
func NewMessageInfoFromCell(t cldf.ContractType, msg *cell.Cell, tlbs map[uint64]interface{}) (MessageInfo, error) {
74-
typeName, m, err := DecodeJSONMapFromCell(t, msg, tlbs)
74+
typeName, m, err := DecodeJSONMapFromCell(msg, tlbs)
7575
if err != nil {
7676
return nil, fmt.Errorf("failed to decode message for contract %s: %w", t, err)
7777
}
@@ -82,7 +82,7 @@ func NewMessageInfoFromCell(t cldf.ContractType, msg *cell.Cell, tlbs map[uint64
8282
}
8383

8484
// DecodeJSONMapFromCell attempts to decode the given cell using the provided TL-B candidates mapped by their opcodes.
85-
func DecodeJSONMapFromCell(t cldf.ContractType, msg *cell.Cell, tlbs map[uint64]interface{}) (string, map[string]interface{}, error) {
85+
func DecodeJSONMapFromCell(msg *cell.Cell, tlbs map[uint64]interface{}) (string, map[string]interface{}, error) {
8686
// 1.1 Try to decode *cell.Cell as one of the TLBs type by reading the opcode
8787
if msg == nil {
8888
return "", nil, &UnknownMessageError{}
@@ -152,7 +152,7 @@ func DecodeJSONMapFromCell(t cldf.ContractType, msg *cell.Cell, tlbs map[uint64]
152152
}
153153

154154
// 3.3. Try to decode recursively using NewMessageInfoFromCell
155-
_, cMap, err := DecodeJSONMapFromCell(t, cVal, tlbs)
155+
_, cMap, err := DecodeJSONMapFromCell(cVal, tlbs)
156156
if err != nil {
157157
// fallback to original BOC representation if fails
158158
continue

pkg/ton/debug/lib/lib_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package lib
2+
3+
import (
4+
"math/big"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/xssnick/tonutils-go/address"
9+
"github.com/xssnick/tonutils-go/tlb"
10+
"github.com/xssnick/tonutils-go/tvm/cell"
11+
)
12+
13+
type Foo struct {
14+
_ tlb.Magic `tlb:"#00000001"` //nolint:revive // Ignore opcode tag
15+
Any *cell.Cell `tlb:"^"`
16+
}
17+
18+
type Bar struct {
19+
_ tlb.Magic `tlb:"#00000002"` //nolint:revive // Ignore opcode tag
20+
Val *big.Int `tlb:"## 32"`
21+
}
22+
23+
type Baz struct {
24+
_ tlb.Magic `tlb:"#00000003"` //nolint:revive // Ignore opcode tag
25+
Val *address.Address `tlb:"addr"`
26+
}
27+
28+
var TLBs = MustNewTLBMap([]interface{}{Foo{}, Bar{}, Baz{}})
29+
30+
func mustToCell(v interface{}) *cell.Cell {
31+
c, err := tlb.ToCell(v)
32+
if err != nil {
33+
panic(err)
34+
}
35+
return c
36+
}
37+
38+
func TestDecodeJSONMapFromCell(t *testing.T) {
39+
tests := []struct {
40+
name string
41+
cell *cell.Cell
42+
wantType string
43+
wantMap map[string]interface{}
44+
expectErr bool
45+
}{
46+
{
47+
name: "Decode Foo",
48+
cell: mustToCell(Foo{Any: cell.BeginCell().MustStoreBigInt(big.NewInt(42), 256).EndCell()}),
49+
wantType: "Foo",
50+
wantMap: map[string]interface{}{
51+
"Any": "te6cckEBAQEAIgAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAqudxe9A==",
52+
},
53+
expectErr: false,
54+
},
55+
{
56+
name: "Decode Bar",
57+
cell: mustToCell(Bar{Val: big.NewInt(1234567890)}),
58+
wantType: "Bar",
59+
wantMap: map[string]interface{}{
60+
"Val": float64(1234567890),
61+
},
62+
expectErr: false,
63+
},
64+
{
65+
name: "Decode Baz",
66+
cell: mustToCell(Baz{Val: address.MustParseAddr("EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8")}),
67+
wantType: "Baz",
68+
wantMap: map[string]interface{}{
69+
"Val": "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8",
70+
},
71+
expectErr: false,
72+
},
73+
{
74+
name: "Unknown opcode",
75+
cell: cell.BeginCell().MustStoreBigInt(big.NewInt(42), 32).EndCell(), // not matching any TLB
76+
wantType: "",
77+
wantMap: nil,
78+
expectErr: true,
79+
},
80+
{
81+
name: "Nil cell",
82+
cell: nil,
83+
wantType: "",
84+
wantMap: nil,
85+
expectErr: true,
86+
},
87+
{
88+
name: "Empty cell",
89+
cell: cell.BeginCell().EndCell(),
90+
wantType: "",
91+
wantMap: nil,
92+
expectErr: true,
93+
},
94+
{
95+
name: "Decode Foo with unknown Any",
96+
cell: mustToCell(Foo{Any: cell.BeginCell().MustStoreBigInt(big.NewInt(1), 32).EndCell()}), // not matching any TLB
97+
wantType: "Foo",
98+
wantMap: map[string]interface{}{
99+
"Any": "te6cckEBAQEABgAACAAAAAHgg8T9",
100+
},
101+
expectErr: false,
102+
},
103+
{
104+
name: "Decode Foo with Bar in Any",
105+
cell: mustToCell(Foo{Any: mustToCell(Bar{Val: big.NewInt(987654321)})}),
106+
wantType: "Foo",
107+
wantMap: map[string]interface{}{
108+
"Any": map[string]interface{}{
109+
"Val": float64(987654321),
110+
},
111+
},
112+
expectErr: false,
113+
},
114+
{
115+
name: "Decode Foo with Baz in Any",
116+
cell: mustToCell(Foo{Any: mustToCell(Baz{Val: address.MustParseAddr("EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8")})}),
117+
wantType: "Foo",
118+
wantMap: map[string]interface{}{
119+
"Any": map[string]interface{}{
120+
"Val": "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8",
121+
},
122+
},
123+
expectErr: false,
124+
},
125+
{
126+
name: "Decode Foo with empty cell in Any",
127+
cell: mustToCell(Foo{Any: cell.BeginCell().EndCell()}),
128+
wantType: "Foo",
129+
wantMap: map[string]interface{}{
130+
"Any": "te6cckEBAQEAAgAAAEysuc0=",
131+
},
132+
expectErr: false,
133+
},
134+
}
135+
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
gotType, gotMap, err := DecodeJSONMapFromCell(tt.cell, TLBs)
139+
if (err != nil) != tt.expectErr {
140+
t.Errorf("DecodeJSONMapFromCell() error = %v, expectErr %v", err, tt.expectErr)
141+
}
142+
if gotType != tt.wantType {
143+
t.Errorf("DecodeJSONMapFromCell() gotType = %v, want %v", gotType, tt.wantType)
144+
}
145+
if !reflect.DeepEqual(gotMap, tt.wantMap) {
146+
t.Errorf("DecodeJSONMapFromCell() gotMap = %v, want %v", gotMap, tt.wantMap)
147+
}
148+
})
149+
}
150+
}

0 commit comments

Comments
 (0)