Skip to content

Commit

Permalink
Cell index serialization improved to visit only unique cells
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Dec 4, 2023
1 parent 6f038ab commit a15378d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tvm/cell/cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func TestBOCBomb(t *testing.T) {
if !bytes.Equal(c.Hash(), hash) {
t.Fatal("incorrect hash", hex.EncodeToString(c.Hash()), hex.EncodeToString(hash))
}

if len(c.ToBOCWithFlags(false)) != len(boc) {
t.Fatal("len", len(c.ToBOC()), len(boc))
}
}

func TestCell_TxWithMerkleBody(t *testing.T) {
Expand Down
31 changes: 28 additions & 3 deletions tvm/cell/flattenIndex.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ func flattenIndex(cells []*Cell) ([]*idxItem, map[string]*idxItem) {
for len(cells) > 0 {
next := make([]*Cell, 0, len(cells)*4)
for _, p := range cells {
hash := string(p.Hash())

if _, ok := index[hash]; ok {
continue
}

// move cell forward in boc, because behind reference is not allowed
index[string(p.Hash())] = &idxItem{
index: idx,
index[hash] = &idxItem{
cell: p,
index: idx,
}
idx++

next = append(next, p.refs...)
}
cells = next
Expand All @@ -33,6 +38,26 @@ func flattenIndex(cells []*Cell) ([]*idxItem, map[string]*idxItem) {
idxSlice = append(idxSlice, id)
}

for verifyOrder := true; verifyOrder; {
verifyOrder = false

for _, id := range idxSlice {
for _, ref := range id.cell.refs {
idRef := index[string(ref.Hash())]

if idRef.index < id.index {
// if we found that ref index is behind parent,
// move ref index forward
idRef.index = idx
idx++

// we changed index, so we need to verify order again
verifyOrder = true
}
}
}
}

sort.Slice(idxSlice, func(i, j int) bool {
return idxSlice[i].index < idxSlice[j].index
})
Expand Down

0 comments on commit a15378d

Please sign in to comment.