github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/txtracer/store.go (about) 1 package txtrace 2 3 import ( 4 "github.com/unicornultrafoundation/go-u2u/common" 5 6 "github.com/unicornultrafoundation/go-helios/u2udb" 7 "github.com/unicornultrafoundation/go-u2u/logger" 8 ) 9 10 // Store is a transaction traces persistent storage working over physical key-value database. 11 type Store struct { 12 mainDB u2udb.Store 13 logger.Instance 14 } 15 16 // NewStore creates store over key-value db. 17 func NewStore(mainDB u2udb.Store) *Store { 18 s := &Store{ 19 mainDB: mainDB, 20 Instance: logger.New("TxTrace Store"), 21 } 22 return s 23 } 24 25 // Close closes underlying database. 26 func (s *Store) Close() { 27 _ = s.mainDB.Close() 28 } 29 30 // SetTxTrace stores []byte representation of transaction traces. 31 func (s *Store) SetTxTrace(txID common.Hash, txTraces []byte) error { 32 return s.mainDB.Put(txID.Bytes(), txTraces) 33 } 34 35 // GetTx returns stored transaction traces. 36 func (s *Store) GetTx(txID common.Hash) []byte { 37 38 buf, err := s.mainDB.Get(txID.Bytes()) 39 if err != nil { 40 s.Log.Crit("Failed to get key-value", "err", err) 41 } 42 if buf == nil { 43 return nil 44 } 45 return buf 46 } 47 48 // RemoveTxTrace removes key and []byte representation of transaction traces. 49 func (s *Store) RemoveTxTrace(txID common.Hash) error { 50 return s.mainDB.Delete(txID.Bytes()) 51 } 52 53 // HasTxTrace stores []byte representation of transaction traces. 54 func (s *Store) HasTxTrace(txID common.Hash) (bool, error) { 55 return s.mainDB.Has(txID.Bytes()) 56 } 57 58 // ForEachTxtrace returns iterator for all transaction traces in db 59 func (s *Store) ForEachTxtrace(onEvent func(key common.Hash, traces []byte) bool) { 60 it := s.mainDB.NewIterator(nil, nil) 61 defer it.Release() 62 for it.Next() { 63 if !onEvent(common.BytesToHash(it.Key()), it.Value()) { 64 return 65 } 66 } 67 }