Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions tlb/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,26 @@ func vmTupleInner(n uint16, c *boc.Cell) (*VmTuple, error) {
if head != nil {
vmTuple.Head = *head
}
c1, err := c.NextRef()
if err != nil {
return nil, err
}
vmStackValue := VmStackValue{}
err = Unmarshal(c1, &vmStackValue)
if err != nil {
return nil, err
if n > 0 { // if input n was 1 then we dont have any tail
c1, err := c.NextRef()
if err != nil {
return nil, err
}
vmStackValue := VmStackValue{}
err = Unmarshal(c1, &vmStackValue)
if err != nil {
return nil, err
}
vmTuple.Tail = vmStackValue
}
vmTuple.Tail = vmStackValue
return &vmTuple, nil
}
return nil, nil
}

func vmTupleRefInner(n uint16, c *boc.Cell) (*VmTupleRef, error) {
vmTupleRef := VmTupleRef{}
if n == 1 {
if n == 0 || n == 1 {
c1, err := c.NextRef()
if err != nil {
return nil, err
Expand Down
142 changes: 141 additions & 1 deletion tlb/stack_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tlb

import (
"github.com/tonkeeper/tongo/boc"
"math/big"
"testing"

"github.com/tonkeeper/tongo/boc"
)

func TestInt257(t *testing.T) {
Expand Down Expand Up @@ -73,3 +74,142 @@ func Test_StackUnmarshal(t *testing.T) {
}

}

func Test_IntMatrixTupleUnmarshal(t *testing.T) {
type test struct {
name string
hex string
intsResultMatrix [][]int64
}

tests := []test{
{
name: "tuple 0x0",
hex: "b5ee9c7201010301001100020c000001070001010200000006070000",
intsResultMatrix: [][]int64{{}},
},
{
name: "tuple 0x1",
hex: "b5ee9c7201010401001d00020c000001070001010200000106070001030012010000000000000001",
intsResultMatrix: [][]int64{{1}},
},
{
name: "tuple 0x2",
hex: "b5ee9c7201010501002900020c000001070001010200000206070002030400120100000000000000010012010000000000000002",
intsResultMatrix: [][]int64{{1, 2}},
},
{
name: "tuple 0x3",
hex: "b5ee9c7201010701003800020c000001070001010200000206070003030402000506001201000000000000000300120100000000000000010012010000000000000002",
intsResultMatrix: [][]int64{{1, 2, 3}},
},
{
name: "tuple 1x0",
hex: "b5ee9c7201010301001200030c00000107000201020200000006070000",
intsResultMatrix: [][]int64{{}, {}},
},
{
name: "tuple 1x1",
hex: "b5ee9c7201010601002f00030c000001070002010203000001060700010401060700010500120100000000000000010012010000000000000002",
intsResultMatrix: [][]int64{{1}, {2}},
},
{
name: "tuple 1x2",
hex: "b5ee9c7201010801004700030c000001070002010203000002060700020405020607000206070012010000000000000001001201000000000000000200120100000000000000030012010000000000000004",
intsResultMatrix: [][]int64{{1, 2}, {3, 4}},
},
{
name: "tuple 1x3",
hex: "b5ee9c7201010c01006500030c0000010700020102030000020607000304050206070003060702000809001201000000000000000302000a0b00120100000000000000060012010000000000000001001201000000000000000200120100000000000000040012010000000000000005",
intsResultMatrix: [][]int64{{1, 2, 3}, {4, 5, 6}},
},
{
name: "tuple 2x0",
hex: "b5ee9c7201010401001600030c0000010700030102030000020003030006070000",
intsResultMatrix: [][]int64{{}, {}, {}},
},
{
name: "tuple 2x1",
hex: "b5ee9c7201010901004400030c000001070003010203000002000405010607000106010607000107010607000108001201000000000000000300120100000000000000010012010000000000000002",
intsResultMatrix: [][]int64{{1}, {2}, {3}},
},
{
name: "tuple 2x2",
hex: "b5ee9c7201010c01006800030c000001070003010203000002000405020607000206070206070002080902060700020a0b001201000000000000000500120100000000000000060012010000000000000001001201000000000000000200120100000000000000030012010000000000000004",
intsResultMatrix: [][]int64{{1, 2}, {3, 4}, {5, 6}},
},
{
name: "tuple 2x3",
hex: "b5ee9c7201011201009500030c000001070003010203000002000405020607000306070206070003080902060700030a0b02000c0d001201000000000000000902000e0f0012010000000000000003020010110012010000000000000006001201000000000000000700120100000000000000080012010000000000000001001201000000000000000200120100000000000000040012010000000000000005",
intsResultMatrix: [][]int64{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
},
{
name: "tuple 3x0",
hex: "b5ee9c7201010501001a00030c000001070004010204000002000304020004040006070000",
intsResultMatrix: [][]int64{{}, {}, {}, {}},
},
{
name: "tuple 3x1",
hex: "b5ee9c7201010c01005900030c00000107000401020300000200040501060700010602000708010607000109001201000000000000000401060700010a01060700010b001201000000000000000300120100000000000000010012010000000000000002",
intsResultMatrix: [][]int64{{1}, {2}, {3}, {4}},
},
{
name: "tuple 3x2",
hex: "b5ee9c7201011001008900030c000001070004010203000002000405020607000206070200080902060700020a0b0012010000000000000007001201000000000000000802060700020c0d02060700020e0f001201000000000000000500120100000000000000060012010000000000000001001201000000000000000200120100000000000000030012010000000000000004",
intsResultMatrix: [][]int64{{1, 2}, {3, 4}, {5, 6}, {7, 8}},
},
{
name: "tuple 3x3",
hex: "b5ee9c720101180100c500030c000001070004010203000002000405020607000306070200080902060700030a0b02000c0d001201000000000000000c02060700030e0f02060700031011020012130012010000000000000009001201000000000000000a001201000000000000000b020014150012010000000000000003020016170012010000000000000006001201000000000000000700120100000000000000080012010000000000000001001201000000000000000200120100000000000000040012010000000000000005",
intsResultMatrix: [][]int64{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cell, err := boc.DeserializeSinglRootHex(tt.hex)
if err != nil {
t.Fatal(err)
}
var stack VmStack
err = Unmarshal(cell, &stack)
if err != nil {
t.Fatal(err)
}
val := stack[0]
if val.SumType != "VmStkTuple" {
t.Errorf("Stack value must be tuple, got %v", val.SumType)
}
tuple := val.VmStkTuple
if int(tuple.Len) != len(tt.intsResultMatrix) {
t.Errorf("want %v tuple len, got %v", len(tt.intsResultMatrix), tuple.Len)
}
values, err := tuple.Data.RecursiveToSlice(len(tt.intsResultMatrix))
if err != nil {
t.Fatal(err)
}
for i, v := range values {
if v.SumType != "VmStkTuple" {
t.Errorf("want values[%v] to be VmStkTuple, got %v", i, v.SumType)
}
if int(v.VmStkTuple.Len) != len(tt.intsResultMatrix[i]) {
t.Errorf("want %v tuple[%v] len, got %v", len(tt.intsResultMatrix[i]), i, v.VmStkTuple.Len)
}
if v.VmStkTuple.Data == nil { // for test case with 0 values in a tuple
return
}
innerValues, err := v.VmStkTuple.Data.RecursiveToSlice(len(tt.intsResultMatrix[i]))
if err != nil {
t.Fatal(err)
}
for j, iV := range innerValues {
if iV.SumType != "VmStkTinyInt" {
t.Errorf("want values[%v][%v] to be VmStkTinyInt, got %v", i, j, iV.SumType)
}
if iV.VmStkTinyInt != tt.intsResultMatrix[i][j] {
t.Errorf("want values[%v][%v] == %v, got %v", i, j, tt.intsResultMatrix[i][j], iV.VmStkTinyInt)
}
}
}
})
}
}
22 changes: 12 additions & 10 deletions tlb/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ func (t *VmStkTuple) RecursiveToSlice() ([]VmStackValue, error) {
}

func (t VmTuple) RecursiveToSlice(depth int) ([]VmStackValue, error) {
if t.Head.Entry == nil && t.Head.Ref == nil {
return nil, fmt.Errorf("can't decode tuple by unknown reason")
}

var sl []VmStackValue
var err error
if depth == 1 {
return append(sl, t.Tail), nil
}
if depth == 2 {
if t.Head.Entry == nil {
return nil, fmt.Errorf("stack tuple invalid depth")
}
if t.Head.Ref == nil {
sl = append(sl, *t.Head.Entry)
} else if t.Head.Ref == nil {
return nil, fmt.Errorf("can't decode tuple by unknown reason")
} else {
sl, err = t.Head.Ref.RecursiveToSlice(depth - 1)
if err != nil {
return nil, err
}
}
if t.Tail.SumType != "" { // tail can be null only if len(tuple) <= 1
sl = append(sl, t.Tail)
}
return append(sl, t.Tail), err
return sl, nil
}
Loading