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 2790170
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions boc/boc.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func newBagOfCells() *bagOfCells {
}
}

// 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 @@ -327,7 +327,7 @@ func newBagOfCells() *bagOfCells {
// tot_cells_size:(##(off_bytes * 8))
// index:(cells * ##(off_bytes * 8))
// cell_data:(tot_cells_size * [ uint8 ])
// = BagOfCells;
// = bagOfCells;
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 {
Expand Down
7 changes: 7 additions & 0 deletions boc/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func (c *Cell) ToBocCustom(idx bool, hasCrc32 bool, cacheBits bool, flags uint)
return bag.serializeBoc([]*Cell{c}, idx, hasCrc32, cacheBits, 0)
}

func (c *Cell) ToBocCustomWithHasher(hasher *Hasher, idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
bag := &bagOfCells{
hasher: hasher,
}
return bag.serializeBoc([]*Cell{c}, idx, hasCrc32, cacheBits, 0)
}

func (c *Cell) ToBocStringCustom(idx bool, hasCrc32 bool, cacheBits bool, flags uint) (string, error) {
boc, err := c.ToBocCustom(idx, hasCrc32, cacheBits, flags)
if err != nil {
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)
}
}
}
1 change: 0 additions & 1 deletion tlb/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,4 @@ 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

}
19 changes: 19 additions & 0 deletions tlb/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,40 @@ type Transaction struct {
Description TransactionDescr `tlb:"^"`

hash Bits256

lazySourceBoc 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.lazySourceBoc != nil {
return tx.lazySourceBoc()
}
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.lazySourceBoc = func() ([]byte, error) {
c.ResetCounters()
return c.ToBocCustomWithHasher(decoder.Hasher(), false, false, false, 0)
}
hash, err = decoder.hasher.Hash(c)
} else {
tx.lazySourceBoc = 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 2790170

Please sign in to comment.