1717package rawdb
1818
1919import (
20+ "encoding/binary"
21+
2022 "github.com/ethereum/go-ethereum/common"
2123 "github.com/ethereum/go-ethereum/ethdb"
2224 "github.com/ethereum/go-ethereum/log"
@@ -28,6 +30,17 @@ func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
2830 return data
2931}
3032
33+ // WritePreimages writes the provided set of preimages to the database.
34+ func WritePreimages (db ethdb.KeyValueWriter , preimages map [common.Hash ][]byte ) {
35+ for hash , preimage := range preimages {
36+ if err := db .Put (preimageKey (hash ), preimage ); err != nil {
37+ log .Crit ("Failed to store trie preimage" , "err" , err )
38+ }
39+ }
40+ preimageCounter .Inc (int64 (len (preimages )))
41+ preimageHitCounter .Inc (int64 (len (preimages )))
42+ }
43+
3144// ReadCode retrieves the contract code of the provided code hash.
3245func ReadCode (db ethdb.KeyValueReader , hash common.Hash ) []byte {
3346 // Try with the prefixed code scheme first, if not then try with legacy
@@ -48,12 +61,6 @@ func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
4861 return data
4962}
5063
51- // ReadTrieNode retrieves the trie node of the provided hash.
52- func ReadTrieNode (db ethdb.KeyValueReader , hash common.Hash ) []byte {
53- data , _ := db .Get (hash .Bytes ())
54- return data
55- }
56-
5764// HasCode checks if the contract code corresponding to the
5865// provided code hash is present in the db.
5966func HasCode (db ethdb.KeyValueReader , hash common.Hash ) bool {
@@ -74,47 +81,101 @@ func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
7481 return ok
7582}
7683
77- // HasTrieNode checks if the trie node with the provided hash is present in db.
78- func HasTrieNode (db ethdb.KeyValueReader , hash common.Hash ) bool {
79- ok , _ := db .Has (hash .Bytes ())
80- return ok
81- }
82-
83- // WritePreimages writes the provided set of preimages to the database.
84- func WritePreimages (db ethdb.KeyValueWriter , preimages map [common.Hash ][]byte ) {
85- for hash , preimage := range preimages {
86- if err := db .Put (preimageKey (hash ), preimage ); err != nil {
87- log .Crit ("Failed to store trie preimage" , "err" , err )
88- }
89- }
90- preimageCounter .Inc (int64 (len (preimages )))
91- preimageHitCounter .Inc (int64 (len (preimages )))
92- }
93-
9484// WriteCode writes the provided contract code database.
9585func WriteCode (db ethdb.KeyValueWriter , hash common.Hash , code []byte ) {
9686 if err := db .Put (codeKey (hash ), code ); err != nil {
9787 log .Crit ("Failed to store contract code" , "err" , err )
9888 }
9989}
10090
101- // WriteTrieNode writes the provided trie node database.
102- func WriteTrieNode (db ethdb.KeyValueWriter , hash common.Hash , node []byte ) {
103- if err := db .Put (hash .Bytes (), node ); err != nil {
104- log .Crit ("Failed to store trie node" , "err" , err )
105- }
106- }
107-
10891// DeleteCode deletes the specified contract code from the database.
10992func DeleteCode (db ethdb.KeyValueWriter , hash common.Hash ) {
11093 if err := db .Delete (codeKey (hash )); err != nil {
11194 log .Crit ("Failed to delete contract code" , "err" , err )
11295 }
11396}
11497
115- // DeleteTrieNode deletes the specified trie node from the database.
116- func DeleteTrieNode (db ethdb.KeyValueWriter , hash common.Hash ) {
117- if err := db .Delete (hash .Bytes ()); err != nil {
118- log .Crit ("Failed to delete trie node" , "err" , err )
98+ // ReadTrieHistory retrieves the trie history with the given id. Calculate
99+ // the real position of trie history in freezer by minus one since the first
100+ // history object is started from one(zero for empty state).
101+ func ReadTrieHistory (db ethdb.AncientReaderOp , id uint64 ) []byte {
102+ blob , err := db .Ancient (trieHistoryTable , id - 1 )
103+ if err != nil {
104+ return nil
105+ }
106+ return blob
107+ }
108+
109+ // WriteTrieHistory writes the provided trie history to database. Calculate the
110+ // real position of trie history in freezer by minus one since the first history
111+ // object is started from one(zero is not existent corresponds to empty state).
112+ func WriteTrieHistory (db ethdb.AncientWriter , id uint64 , blob []byte ) {
113+ db .ModifyAncients (func (op ethdb.AncientWriteOp ) error {
114+ op .AppendRaw (trieHistoryTable , id - 1 , blob )
115+ return nil
116+ })
117+ }
118+
119+ // ReadStateLookup retrieves the state id with the provided state root.
120+ func ReadStateLookup (db ethdb.KeyValueReader , root common.Hash ) (uint64 , bool ) {
121+ data , err := db .Get (stateLookupKey (root ))
122+ if err != nil || len (data ) == 0 {
123+ return 0 , false
124+ }
125+ return binary .BigEndian .Uint64 (data ), true
126+ }
127+
128+ // WriteStateLookup writes the provided state lookup to database.
129+ func WriteStateLookup (db ethdb.KeyValueWriter , root common.Hash , id uint64 ) {
130+ var buff [8 ]byte
131+ binary .BigEndian .PutUint64 (buff [:], id )
132+ if err := db .Put (stateLookupKey (root ), buff [:]); err != nil {
133+ log .Crit ("Failed to store state lookup" , "err" , err )
134+ }
135+ }
136+
137+ // DeleteStateLookup deletes the specified state lookup from the database.
138+ func DeleteStateLookup (db ethdb.KeyValueWriter , root common.Hash ) {
139+ if err := db .Delete (stateLookupKey (root )); err != nil {
140+ log .Crit ("Failed to delete state lookup" , "err" , err )
141+ }
142+ }
143+
144+ // ReadHeadState retrieves the id of the disk state from the database.
145+ func ReadHeadState (db ethdb.KeyValueReader ) uint64 {
146+ data , _ := db .Get (headStateKey )
147+ if len (data ) != 8 {
148+ return 0
149+ }
150+ return binary .BigEndian .Uint64 (data )
151+ }
152+
153+ // WriteHeadState stores the id of the disk state into database.
154+ func WriteHeadState (db ethdb.KeyValueWriter , number uint64 ) {
155+ if err := db .Put (headStateKey , encodeBlockNumber (number )); err != nil {
156+ log .Crit ("Failed to store the head state id" , "err" , err )
157+ }
158+ }
159+
160+ // ReadTrieJournal retrieves the serialized in-memory trie node diff layers saved at
161+ // the last shutdown. The blob is expected to be max a few 10s of megabytes.
162+ func ReadTrieJournal (db ethdb.KeyValueReader ) []byte {
163+ data , _ := db .Get (triesJournalKey )
164+ return data
165+ }
166+
167+ // WriteTrieJournal stores the serialized in-memory trie node diff layers to save at
168+ // shutdown. The blob is expected to be max a few 10s of megabytes.
169+ func WriteTrieJournal (db ethdb.KeyValueWriter , journal []byte ) {
170+ if err := db .Put (triesJournalKey , journal ); err != nil {
171+ log .Crit ("Failed to store tries journal" , "err" , err )
172+ }
173+ }
174+
175+ // DeleteTrieJournal deletes the serialized in-memory trie node diff layers saved at
176+ // the last shutdown
177+ func DeleteTrieJournal (db ethdb.KeyValueWriter ) {
178+ if err := db .Delete (triesJournalKey ); err != nil {
179+ log .Crit ("Failed to remove tries journal" , "err" , err )
119180 }
120181}
0 commit comments