github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/topicsdb/record.go (about)

     1  package topicsdb
     2  
     3  import (
     4  	"github.com/unicornultrafoundation/go-helios/u2udb"
     5  	"github.com/unicornultrafoundation/go-u2u/common"
     6  	"github.com/unicornultrafoundation/go-u2u/core/types"
     7  )
     8  
     9  type (
    10  	logrec struct {
    11  		ID          ID
    12  		topicsCount uint8
    13  		result      *types.Log
    14  		err         error
    15  
    16  		matched int
    17  	}
    18  )
    19  
    20  func newLogrec(rec ID, topicCount uint8) *logrec {
    21  	return &logrec{
    22  		ID:          rec,
    23  		topicsCount: topicCount,
    24  	}
    25  }
    26  
    27  // fetch record's data.
    28  func (rec *logrec) fetch(
    29  	logrecTable u2udb.Reader,
    30  ) {
    31  	r := &types.Log{
    32  		BlockNumber: rec.ID.BlockNumber(),
    33  		TxHash:      rec.ID.TxHash(),
    34  		Index:       rec.ID.Index(),
    35  		Topics:      make([]common.Hash, rec.topicsCount),
    36  	}
    37  
    38  	var (
    39  		buf    []byte
    40  		offset int
    41  	)
    42  	buf, rec.err = logrecTable.Get(rec.ID.Bytes())
    43  	if rec.err != nil {
    44  		return
    45  	}
    46  
    47  	// topics
    48  	for i := 0; i < len(r.Topics); i++ {
    49  		r.Topics[i] = common.BytesToHash(buf[offset : offset+common.HashLength])
    50  		offset += common.HashLength
    51  	}
    52  
    53  	// fields
    54  	r.BlockHash = common.BytesToHash(buf[offset : offset+common.HashLength])
    55  	offset += common.HashLength
    56  	r.Address = common.BytesToAddress(buf[offset : offset+common.AddressLength])
    57  	offset += common.AddressLength
    58  	r.Data = buf[offset:]
    59  
    60  	rec.result = r
    61  	return
    62  }