actually ignore the subtries that are not along the paths of interest

This commit is contained in:
i-norden 2022-06-16 10:45:44 -05:00 committed by nabarun
parent 038b25a41d
commit 9856419371
2 changed files with 94 additions and 45 deletions

View File

@ -82,6 +82,7 @@ func NewInPlaceSnapshotConfig() *Config {
&EthConfig{},
&DBConfig{},
&FileConfig{},
&ServiceConfig{},
}
ret.DB.Init()

View File

@ -87,7 +87,7 @@ type SnapshotParams struct {
func (s *Service) CreateSnapshot(params SnapshotParams) error {
paths := make([][]byte, 0, len(params.WatchedAddresses))
for addr := range params.WatchedAddresses {
paths = append(paths, keybytesToHex(crypto.Keccak256(addr.Bytes())))
paths = append(paths, crypto.Keccak256(addr.Bytes()))
}
s.watchingAddresses = len(paths) > 0
// extract header from lvldb and publish to PG-IPFS
@ -214,6 +214,7 @@ func validPath(currentPath []byte, seekingPaths [][]byte) bool {
}
return false
}
func (s *Service) createSnapshot(it trie.NodeIterator, headerID string, height *big.Int, seekingPaths [][]byte) error {
tx, err := s.ipfsPublisher.BeginTx()
if err != nil {
@ -226,16 +227,64 @@ func (s *Service) createSnapshot(it trie.NodeIterator, headerID string, height *
}
}()
for it.Next(true) {
// iterate all the nodes at this level
for it.Next(false) {
// ignore node if it is not along paths of interest
if s.watchingAddresses && !validPath(it.Path(), seekingPaths) {
continue
}
// if the node is along paths of interest
// create snapshot of node, if it is a leaf this will also create snapshot of entire storage trie
if err := s.createNodeSnapshot(tx, it, headerID, height, seekingPaths); err != nil {
return err
}
// create subTrie iterator for this node
subTrie, err := s.stateDB.OpenTrie(it.Hash())
if err != nil {
return err
}
subTrieIt := subTrie.NodeIterator(nil)
// traverse and process the next level of this subTrie
if err := s.createSubTrieSnapshot(tx, subTrieIt, headerID, height, seekingPaths); err != nil {
return err
}
}
return it.Error()
}
func (s *Service) createSubTrieSnapshot(tx Tx, it trie.NodeIterator, headerID string, height *big.Int, seekingPaths [][]byte) error {
// iterate all the nodes at this level
for it.Next(false) {
// ignore node if it is not along paths of interest
if s.watchingAddresses && !validPath(it.Path(), seekingPaths) {
continue
}
// if the node is along paths of interest
// create snapshot of node, if it is a leaf this will also create snapshot of entire storage trie
if err := s.createNodeSnapshot(tx, it, headerID, height, seekingPaths); err != nil {
return err
}
// create subTrie iterator for this node
subTrie, err := s.stateDB.OpenTrie(it.Hash())
if err != nil {
return err
}
subTrieIt := subTrie.NodeIterator(nil)
// traverse and process the next level of this subTrie
if err := s.createSubTrieSnapshot(tx, subTrieIt, headerID, height, seekingPaths); err != nil {
return err
}
}
return it.Error()
}
func (s *Service) createNodeSnapshot(tx Tx, it trie.NodeIterator, headerID string, height *big.Int, seekingPaths [][]byte) error {
res, err := resolveNode(it, s.stateDB.TrieDB())
if err != nil {
return err
}
if res == nil {
continue
return nil
}
tx, err = s.ipfsPublisher.PrepareTxForBatch(tx, s.maxBatchSize)
@ -287,7 +336,6 @@ func (s *Service) createSnapshot(it trie.NodeIterator, headerID string, height *
default:
return errors.New("unexpected node type")
}
}
return it.Error()
}