@@ -22,7 +22,6 @@ import (
2222
2323 "github.com/XinFinOrg/XDPoSChain/common"
2424 "github.com/XinFinOrg/XDPoSChain/crypto"
25- "github.com/XinFinOrg/XDPoSChain/crypto/keccak"
2625 "github.com/XinFinOrg/XDPoSChain/ethdb"
2726 "github.com/XinFinOrg/XDPoSChain/log"
2827)
@@ -46,45 +45,39 @@ const HashScheme = "hashScheme"
4645// on extra state diffs to survive deep reorg.
4746const PathScheme = "pathScheme"
4847
49- // nodeHasher used to derive the hash of trie node .
50- type nodeHasher struct { sha crypto.KeccakState }
48+ // hasher is used to compute the sha256 hash of the provided data .
49+ type hasher struct { sha crypto.KeccakState }
5150
5251var hasherPool = sync.Pool {
53- New : func () interface {} { return & nodeHasher {sha : keccak . NewLegacyKeccak256 ().( crypto.KeccakState )} },
52+ New : func () interface {} { return & hasher {sha : crypto .NewKeccakState ( )} },
5453}
5554
56- func newNodeHasher () * nodeHasher { return hasherPool .Get ().(* nodeHasher ) }
57- func returnHasherToPool (h * nodeHasher ) { hasherPool .Put (h ) }
55+ func newHasher () * hasher {
56+ return hasherPool .Get ().(* hasher )
57+ }
5858
59- func (h * nodeHasher ) hashData (data []byte ) (n common.Hash ) {
60- h .sha .Reset ()
61- h .sha .Write (data )
62- h .sha .Read (n [:])
63- return n
59+ func (h * hasher ) hash (data []byte ) common.Hash {
60+ return crypto .HashData (h .sha , data )
6461}
6562
66- // ReadAccountTrieNode retrieves the account trie node and the associated node
67- // hash with the specified node path.
68- func ReadAccountTrieNode (db ethdb.KeyValueReader , path []byte ) ([]byte , common.Hash ) {
69- data , err := db .Get (accountTrieNodeKey (path ))
70- if err != nil {
71- return nil , common.Hash {}
72- }
73- hasher := newNodeHasher ()
74- defer returnHasherToPool (hasher )
75- return data , hasher .hashData (data )
63+ func (h * hasher ) release () {
64+ hasherPool .Put (h )
65+ }
66+
67+ // ReadAccountTrieNode retrieves the account trie node with the specified node path.
68+ func ReadAccountTrieNode (db ethdb.KeyValueReader , path []byte ) []byte {
69+ data , _ := db .Get (accountTrieNodeKey (path ))
70+ return data
7671}
7772
78- // HasAccountTrieNode checks the account trie node presence with the specified
79- // node path and the associated node hash.
80- func HasAccountTrieNode (db ethdb.KeyValueReader , path []byte , hash common. Hash ) bool {
81- data , err := db .Get (accountTrieNodeKey (path ))
73+ // HasAccountTrieNode checks the presence of the account trie node with the
74+ // specified node path, regardless of the node hash.
75+ func HasAccountTrieNode (db ethdb.KeyValueReader , path []byte ) bool {
76+ has , err := db .Has (accountTrieNodeKey (path ))
8277 if err != nil {
8378 return false
8479 }
85- hasher := newNodeHasher ()
86- defer returnHasherToPool (hasher )
87- return hasher .hashData (data ) == hash
80+ return has
8881}
8982
9083// WriteAccountTrieNode writes the provided account trie node into database.
@@ -101,28 +94,20 @@ func DeleteAccountTrieNode(db ethdb.KeyValueWriter, path []byte) {
10194 }
10295}
10396
104- // ReadStorageTrieNode retrieves the storage trie node and the associated node
105- // hash with the specified node path.
106- func ReadStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) ([]byte , common.Hash ) {
107- data , err := db .Get (storageTrieNodeKey (accountHash , path ))
108- if err != nil {
109- return nil , common.Hash {}
110- }
111- hasher := newNodeHasher ()
112- defer returnHasherToPool (hasher )
113- return data , hasher .hashData (data )
97+ // ReadStorageTrieNode retrieves the storage trie node with the specified node path.
98+ func ReadStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) []byte {
99+ data , _ := db .Get (storageTrieNodeKey (accountHash , path ))
100+ return data
114101}
115102
116- // HasStorageTrieNode checks the storage trie node presence with the provided
117- // node path and the associated node hash.
118- func HasStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte , hash common. Hash ) bool {
119- data , err := db .Get (storageTrieNodeKey (accountHash , path ))
103+ // HasStorageTrieNode checks the presence of the storage trie node with the
104+ // specified account hash and node path, regardless of the node hash.
105+ func HasStorageTrieNode (db ethdb.KeyValueReader , accountHash common.Hash , path []byte ) bool {
106+ has , err := db .Has (storageTrieNodeKey (accountHash , path ))
120107 if err != nil {
121108 return false
122109 }
123- hasher := newNodeHasher ()
124- defer returnHasherToPool (hasher )
125- return hasher .hashData (data ) == hash
110+ return has
126111}
127112
128113// WriteStorageTrieNode writes the provided storage trie node into database.
@@ -176,54 +161,54 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c
176161 case HashScheme :
177162 return HasLegacyTrieNode (db , hash )
178163 case PathScheme :
164+ var blob []byte
179165 if owner == (common.Hash {}) {
180- return HasAccountTrieNode (db , path , hash )
166+ blob = ReadAccountTrieNode (db , path )
167+ } else {
168+ blob = ReadStorageTrieNode (db , owner , path )
181169 }
182- return HasStorageTrieNode (db , owner , path , hash )
170+ if len (blob ) == 0 {
171+ return false
172+ }
173+ h := newHasher ()
174+ defer h .release ()
175+ return h .hash (blob ) == hash // exists but not match
183176 default :
184177 panic (fmt .Sprintf ("Unknown scheme %v" , scheme ))
185178 }
186179}
187180
188181// ReadTrieNode retrieves the trie node from database with the provided node info
189182// and associated node hash.
190- // hashScheme-based lookup requires the following:
191- // - hash
192- //
193- // pathScheme-based lookup requires the following:
194- // - owner
195- // - path
196183func ReadTrieNode (db ethdb.KeyValueReader , owner common.Hash , path []byte , hash common.Hash , scheme string ) []byte {
197184 switch scheme {
198185 case HashScheme :
199186 return ReadLegacyTrieNode (db , hash )
200187 case PathScheme :
201- var (
202- blob []byte
203- nHash common.Hash
204- )
188+ var blob []byte
205189 if owner == (common.Hash {}) {
206- blob , nHash = ReadAccountTrieNode (db , path )
190+ blob = ReadAccountTrieNode (db , path )
207191 } else {
208- blob , nHash = ReadStorageTrieNode (db , owner , path )
192+ blob = ReadStorageTrieNode (db , owner , path )
209193 }
210- if nHash != hash {
194+ if len ( blob ) == 0 {
211195 return nil
212196 }
197+ h := newHasher ()
198+ defer h .release ()
199+ if h .hash (blob ) != hash {
200+ return nil // exists but not match
201+ }
213202 return blob
214203 default :
215204 panic (fmt .Sprintf ("Unknown scheme %v" , scheme ))
216205 }
217206}
218207
219- // WriteTrieNode writes the trie node into database with the provided node info
220- // and associated node hash.
221- // hashScheme-based lookup requires the following:
222- // - hash
208+ // WriteTrieNode writes the trie node into database with the provided node info.
223209//
224- // pathScheme-based lookup requires the following:
225- // - owner
226- // - path
210+ // hash-scheme requires the node hash as the identifier.
211+ // path-scheme requires the node owner and path as the identifier.
227212func WriteTrieNode (db ethdb.KeyValueWriter , owner common.Hash , path []byte , hash common.Hash , node []byte , scheme string ) {
228213 switch scheme {
229214 case HashScheme :
@@ -239,14 +224,10 @@ func WriteTrieNode(db ethdb.KeyValueWriter, owner common.Hash, path []byte, hash
239224 }
240225}
241226
242- // DeleteTrieNode deletes the trie node from database with the provided node info
243- // and associated node hash.
244- // hashScheme-based lookup requires the following:
245- // - hash
227+ // DeleteTrieNode deletes the trie node from database with the provided node info.
246228//
247- // pathScheme-based lookup requires the following:
248- // - owner
249- // - path
229+ // hash-scheme requires the node hash as the identifier.
230+ // path-scheme requires the node owner and path as the identifier.
250231func DeleteTrieNode (db ethdb.KeyValueWriter , owner common.Hash , path []byte , hash common.Hash , scheme string ) {
251232 switch scheme {
252233 case HashScheme :
0 commit comments