github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/core/rawdb/accessors_data_prune.go (about) 1 package rawdb 2 3 import ( 4 "encoding/binary" 5 "github.com/intfoundation/intchain/common" 6 "github.com/intfoundation/intchain/intdb" 7 "github.com/intfoundation/intchain/log" 8 ) 9 10 // ReadDataPruneTrieRootHash retrieves the root hash of a data prune process trie 11 func ReadDataPruneTrieRootHash(db intdb.Reader, scan, prune uint64) common.Hash { 12 data, _ := db.Get(dataPruneNumberKey(scan, prune)) 13 if len(data) == 0 { 14 return common.Hash{} 15 } 16 return common.BytesToHash(data) 17 } 18 19 // WriteCanonicalHash stores the hash assigned to a canonical block number. 20 func WriteDataPruneTrieRootHash(db intdb.Writer, hash common.Hash, scan, prune uint64) { 21 if err := db.Put(dataPruneNumberKey(scan, prune), hash.Bytes()); err != nil { 22 log.Crit("Failed to store number to hash mapping", "err", err) 23 } 24 } 25 26 // DeleteCanonicalHash removes the number to hash canonical mapping. 27 func DeleteDataPruneTrieRootHash(db intdb.Writer, scan, prune uint64) { 28 if err := db.Delete(dataPruneNumberKey(scan, prune)); err != nil { 29 log.Crit("Failed to delete number to hash mapping", "err", err) 30 } 31 } 32 33 // ReadHeadScanNumber retrieves the latest scaned number. 34 func ReadHeadScanNumber(db intdb.Reader) *uint64 { 35 data, _ := db.Get(headDataScanKey) 36 if len(data) != 8 { 37 return nil 38 } 39 number := binary.BigEndian.Uint64(data) 40 return &number 41 } 42 43 // WriteHeadScanNumber stores the number of the latest scaned block. 44 func WriteHeadScanNumber(db intdb.Writer, scan uint64) { 45 if err := db.Put(headDataScanKey, encodeBlockNumber(scan)); err != nil { 46 log.Crit("Failed to store last scan number", "err", err) 47 } 48 } 49 50 // ReadHeadPruneNumber retrieves the latest pruned number. 51 func ReadHeadPruneNumber(db intdb.Reader) *uint64 { 52 data, _ := db.Get(headDataPruneKey) 53 if len(data) != 8 { 54 return nil 55 } 56 number := binary.BigEndian.Uint64(data) 57 return &number 58 } 59 60 // WriteHeadPruneNumber stores the number of the latest pruned block. 61 func WriteHeadPruneNumber(db intdb.Writer, prune uint64) { 62 if err := db.Put(headDataPruneKey, encodeBlockNumber(prune)); err != nil { 63 log.Crit("Failed to store last prune number", "err", err) 64 } 65 }