github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/trie/notary.go (about)

     1  // Copyright 2020 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package trie
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/ethdb"
    21  	"github.com/ethereum/go-ethereum/ethdb/memorydb"
    22  )
    23  
    24  // KeyValueNotary tracks which keys have been accessed through a key-value reader
    25  // with te scope of verifying if certain proof datasets are maliciously bloated.
    26  type KeyValueNotary struct {
    27  	ethdb.KeyValueReader
    28  	reads map[string]struct{}
    29  }
    30  
    31  // NewKeyValueNotary wraps a key-value database with an access notary to track
    32  // which items have bene accessed.
    33  func NewKeyValueNotary(db ethdb.KeyValueReader) *KeyValueNotary {
    34  	return &KeyValueNotary{
    35  		KeyValueReader: db,
    36  		reads:          make(map[string]struct{}),
    37  	}
    38  }
    39  
    40  // Get retrieves an item from the underlying database, but also tracks it as an
    41  // accessed slot for bloat checks.
    42  func (k *KeyValueNotary) Get(key []byte) ([]byte, error) {
    43  	k.reads[string(key)] = struct{}{}
    44  	return k.KeyValueReader.Get(key)
    45  }
    46  
    47  // Accessed returns s snapshot of the original key-value store containing only the
    48  // data accessed through the notary.
    49  func (k *KeyValueNotary) Accessed() ethdb.KeyValueStore {
    50  	db := memorydb.New()
    51  	for keystr := range k.reads {
    52  		key := []byte(keystr)
    53  		val, _ := k.KeyValueReader.Get(key)
    54  		db.Put(key, val)
    55  	}
    56  	return db
    57  }