2023-04-20 15:53:37 +00:00
|
|
|
package internal
|
2023-03-05 07:33:51 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-25 09:14:04 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
|
2024-05-29 10:00:12 +00:00
|
|
|
"github.com/cerc-io/plugeth-statediff/indexer/ipld"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2023-03-05 07:33:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
)
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
var (
|
|
|
|
StateTrieCodec uint64 = ipld.MEthStateTrie
|
|
|
|
StorageTrieCodec uint64 = ipld.MEthStorageTrie
|
|
|
|
)
|
|
|
|
|
2023-03-05 07:33:51 +00:00
|
|
|
func Keccak256ToCid(codec uint64, h []byte) (cid.Cid, error) {
|
|
|
|
buf, err := multihash.Encode(h, multihash.KECCAK_256)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Cid{}, err
|
|
|
|
}
|
|
|
|
return cid.NewCidV1(codec, buf), nil
|
|
|
|
}
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// returns a cache config with unique name (groupcache names are global)
|
|
|
|
func MakeCacheConfig(t testing.TB) pgipfsethdb.CacheConfig {
|
|
|
|
return pgipfsethdb.CacheConfig{
|
|
|
|
Name: t.Name(),
|
|
|
|
Size: 3000000, // 3MB
|
|
|
|
ExpiryDuration: time.Hour,
|
|
|
|
}
|
|
|
|
}
|
2024-05-29 10:00:12 +00:00
|
|
|
|
|
|
|
// ReadLegacyTrieNode retrieves the legacy trie node with the given
|
|
|
|
// associated node hash.
|
|
|
|
func ReadLegacyTrieNode(db ethdb.KeyValueReader, hash common.Hash, codec uint64) ([]byte, error) {
|
|
|
|
cid, err := Keccak256ToCid(codec, hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
enc, err := db.Get(cid.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasLegacyTrieNode checks if the trie node with the provided hash is present in db.
|
|
|
|
func HasLegacyTrieNode(db ethdb.KeyValueReader, hash common.Hash, codec uint64) bool {
|
|
|
|
cid, err := Keccak256ToCid(codec, hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ok, _ := db.Has(cid.Bytes())
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteLegacyTrieNode(db ethdb.KeyValueWriter, hash common.Hash, data []byte, codec uint64) error {
|
|
|
|
//
|
|
|
|
panic("Writing to the DB is disabled")
|
|
|
|
cid, err := Keccak256ToCid(codec, hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.Put(cid.Bytes(), data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadCode(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
|
|
|
|
return ReadLegacyTrieNode(db, hash, ipld.RawBinary)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) error {
|
|
|
|
return WriteLegacyTrieNode(db, hash, code, ipld.RawBinary)
|
|
|
|
}
|