Skip to content

Commit

Permalink
Implement tlb.Transaction.SourceBoc()
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Feb 2, 2024
1 parent 7c3cd05 commit b08b318
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 22 deletions.
51 changes: 38 additions & 13 deletions boc/boc.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,50 @@ func DeserializeBocHex(boc string) ([]*Cell, error) {
}

func SerializeBoc(cell *Cell, idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
bag := newBagOfCells()
return bag.serializeBoc([]*Cell{cell}, idx, hasCrc32, cacheBits, flags)
bag := NewBagOfCells()
return bag.SerializeBoc([]*Cell{cell}, idx, hasCrc32, cacheBits, flags)
}

// bagOfCells serializes cells to a boc.
// BagOfCells serializes cells to a boc.
//
// Use of BagOfCells is an advanced technique,
// prefer SerializeBoc() function instead.
//
// the serialization algorithms is a golang version of
// https://github.com/ton-blockchain/ton/blob/master/crypto/vm/boc.cpp#
type bagOfCells struct {
type BagOfCells struct {
hasher *Hasher
}

func newBagOfCells() *bagOfCells {
return &bagOfCells{
hasher: NewHasher(),
type BagOfCellsOptions struct {
hasher *Hasher
}

type BagOfCellsOption func(*BagOfCellsOptions)

// BagWithHasher sets the hasher to be used by the BagOfCells.
// The idea is that one can use the same hasher for both deserialization and then serialization
// to speed up the process of hashing cells.
func BagWithHasher(hasher *Hasher) BagOfCellsOption {
return func(options *BagOfCellsOptions) {
options.hasher = hasher
}
}

func NewBagOfCells(opts ...BagOfCellsOption) *BagOfCells {
options := &BagOfCellsOptions{}
for _, opt := range opts {
opt(options)
}
if options.hasher == nil {
options.hasher = NewHasher()
}
return &BagOfCells{
hasher: options.hasher,
}
}

// serializeBoc converts the given list of root cells to a byte representation.
// SerializeBoc converts the given list of root cells to a byte representation.
//
// serialized_boc#672fb0ac has_idx:(## 1) has_crc32c:(## 1)
// has_cache_bits:(## 1) flags:(## 2) { flags = 0 }
Expand All @@ -328,7 +353,7 @@ func newBagOfCells() *bagOfCells {
// index:(cells * ##(off_bytes * 8))
// cell_data:(tot_cells_size * [ uint8 ])
// = BagOfCells;
func (boc *bagOfCells) serializeBoc(rootCells []*Cell, idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
func (boc *BagOfCells) SerializeBoc(rootCells []*Cell, idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
roots, cellInfos, err := boc.importRoots(rootCells)
if err != nil {
return nil, err
Expand Down Expand Up @@ -437,7 +462,7 @@ func (boc *bagOfCells) serializeBoc(rootCells []*Cell, idx bool, hasCrc32 bool,
return resBytes, nil
}

func (boc *bagOfCells) importRoots(rootCells []*Cell) ([]*rootInfo, []*cellInfo, error) {
func (boc *BagOfCells) importRoots(rootCells []*Cell) ([]*rootInfo, []*cellInfo, error) {
roots := make([]*rootInfo, 0, len(rootCells))
state := &orderState{
cells: map[string]int{},
Expand All @@ -457,7 +482,7 @@ func (boc *bagOfCells) importRoots(rootCells []*Cell) ([]*rootInfo, []*cellInfo,
return roots, cellInfos, nil
}

func (boc *bagOfCells) importCell(state *orderState, cell *Cell, depth int) (int, error) {
func (boc *BagOfCells) importCell(state *orderState, cell *Cell, depth int) (int, error) {
if depth > maxDepth {
return 0, ErrDepthIsTooBig
}
Expand Down Expand Up @@ -545,7 +570,7 @@ const (
allocate
)

func (boc *bagOfCells) revisit(state, newState *orderState, cellIndex int, force force) int {
func (boc *BagOfCells) revisit(state, newState *orderState, cellIndex int, force force) int {
dci := state.cellList[cellIndex]
if dci.newIndex >= 0 {
return dci.newIndex
Expand Down Expand Up @@ -589,7 +614,7 @@ func (boc *bagOfCells) revisit(state, newState *orderState, cellIndex int, force
return dci.newIndex

}
func (boc *bagOfCells) reorderCells(roots []*rootInfo, state *orderState) []*cellInfo {
func (boc *BagOfCells) reorderCells(roots []*rootInfo, state *orderState) []*cellInfo {
for i := len(state.cellList) - 1; i >= 0; i-- {
dci := state.cellList[i]
c := dci.refsNumber
Expand Down
10 changes: 5 additions & 5 deletions boc/boc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func TestBigCell(t *testing.T) {
t.Fail()
return
}
bag := newBagOfCells()
x, err := bag.serializeBoc([]*Cell{cells[0]}, false, true, false, 0)
bag := NewBagOfCells()
x, err := bag.SerializeBoc([]*Cell{cells[0]}, false, true, false, 0)
if err != nil {
fmt.Println(err)
t.Fail()
Expand Down Expand Up @@ -379,10 +379,10 @@ func Test_bagOfCells_serializeBoc(t *testing.T) {
if err != nil {
t.Fatalf("failed to deserialize boc: %v", err)
}
bag := newBagOfCells()
bs, err := bag.serializeBoc(cells, tt.hasIndex, tt.hasCrc, tt.hasCacheBits, 0)
bag := NewBagOfCells()
bs, err := bag.SerializeBoc(cells, tt.hasIndex, tt.hasCrc, tt.hasCacheBits, 0)
if err != nil {
t.Fatalf("serializeBoc() failed: %v", err)
t.Fatalf("SerializeBoc() failed: %v", err)
}
if tt.hexBoc != hex.EncodeToString(bs) {
t.Fatalf("serializedBoc differs from hexBoc")
Expand Down
8 changes: 4 additions & 4 deletions boc/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (c *Cell) hash(cache map[*Cell]*immutableCell) ([]byte, error) {
}

func (c *Cell) ToBoc() ([]byte, error) {
bag := newBagOfCells()
return bag.serializeBoc([]*Cell{c}, false, false, false, 0)
bag := NewBagOfCells()
return bag.SerializeBoc([]*Cell{c}, false, false, false, 0)
}

func (c *Cell) ToBocString() (string, error) {
Expand All @@ -128,8 +128,8 @@ func (c *Cell) ToBocBase64() (string, error) {
}

func (c *Cell) ToBocCustom(idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
bag := newBagOfCells()
return bag.serializeBoc([]*Cell{c}, idx, hasCrc32, cacheBits, 0)
bag := NewBagOfCells()
return bag.SerializeBoc([]*Cell{c}, idx, hasCrc32, cacheBits, 0)
}

func (c *Cell) ToBocStringCustom(idx bool, hasCrc32 bool, cacheBits bool, flags uint) (string, error) {
Expand Down
7 changes: 7 additions & 0 deletions tlb/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ func Test_block(b *testing.T) {
if err != nil {
b.Errorf("Unmarshal() failed: %v", err)
}

for _, tx := range block.AllTransactions() {
_, err := tx.SourceBoc()
if err != nil {
b.Errorf("SourceBoc() failed: %v", err)
}
}
}
5 changes: 5 additions & 0 deletions tlb/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,10 @@ func decodeBitString(c *boc.Cell, val reflect.Value) error {
// Hasher returns boc.Hasher that is used to calculate hashes when decoding.
func (dec *Decoder) Hasher() *boc.Hasher {
return dec.hasher
}

// BagOfCells returns boc.BagOfCells with the same hasher as the decoder.
// This speeds up the process of hashing cells.
func (dec *Decoder) BagOfCells() *boc.BagOfCells {
return boc.NewBagOfCells(boc.BagWithHasher(dec.hasher))
}
20 changes: 20 additions & 0 deletions tlb/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,41 @@ type Transaction struct {
Description TransactionDescr `tlb:"^"`

hash Bits256

lazyToBoc func() ([]byte, error)
}

// Hash returns a hash of this transaction.
func (tx *Transaction) Hash() Bits256 {
return tx.hash
}

// SourceBoc returns a BOC of this transaction.
// It works only if the transaction was unmarshalled from a cell.
func (tx *Transaction) SourceBoc() ([]byte, error) {
if tx.lazyToBoc != nil {
return tx.lazyToBoc()
}
return nil, fmt.Errorf("transaction was not unmarshalled from cell")
}

func (tx *Transaction) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
var (
hash []byte
err error
)
if decoder.hasher != nil {
tx.lazyToBoc = func() ([]byte, error) {
bag := decoder.BagOfCells()
c.ResetCounters()
return bag.SerializeBoc([]*boc.Cell{c}, false, false, false, 0)
}
hash, err = decoder.hasher.Hash(c)
} else {
tx.lazyToBoc = func() ([]byte, error) {
c.ResetCounters()
return boc.SerializeBoc(c, false, false, false, 0)
}
hash, err = c.Hash()
}
if err != nil {
Expand Down

0 comments on commit b08b318

Please sign in to comment.