Skip to content

Commit

Permalink
lots of WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Feb 10, 2025
1 parent 3abad20 commit f437c59
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 160 deletions.
1 change: 1 addition & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,7 @@ func SetQuaiConfig(stack *node.Node, cfg *quaiconfig.Config, slicesRunning []com
}
if viper.IsSet(TxLookupLimitFlag.Name) {
cfg.TxLookupLimit = viper.GetUint64(TxLookupLimitFlag.Name)
log.Global.WithField("lookuplimit", cfg.TxLookupLimit).Print("Txlookup limit defaults")
}
if viper.IsSet(CacheFlag.Name) || viper.IsSet(CacheTrieFlag.Name) {
cfg.TrieCleanCache = viper.GetInt(CacheFlag.Name) * viper.GetInt(CacheTrieFlag.Name) / 100
Expand Down
8 changes: 6 additions & 2 deletions core/bodydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type BodyDb struct {
logger *log.Logger
}

func NewBodyDb(db ethdb.Database, engine consensus.Engine, hc *HeaderChain, chainConfig *params.ChainConfig, cacheConfig *CacheConfig, txLookupLimit *uint64, vmConfig vm.Config, slicesRunning []common.Location) (*BodyDb, error) {
func NewBodyDb(db ethdb.Database, engine consensus.Engine, hc *HeaderChain, chainConfig *params.ChainConfig, cacheConfig *CacheConfig, txLookupLimit uint64, vmConfig vm.Config, slicesRunning []common.Location) (*BodyDb, error) {
nodeCtx := chainConfig.Location.Context()

bc := &BodyDb{
Expand All @@ -69,7 +69,11 @@ func NewBodyDb(db ethdb.Database, engine consensus.Engine, hc *HeaderChain, chai

// only start the state processor in zone
if nodeCtx == common.ZONE_CTX && bc.ProcessingState() {
bc.processor = NewStateProcessor(chainConfig, hc, engine, vmConfig, cacheConfig, txLookupLimit)
processor, err := NewStateProcessor(chainConfig, hc, engine, vmConfig, cacheConfig, txLookupLimit)
if err != nil {
return nil, err
}
bc.processor = processor
vm.InitializePrecompiles(chainConfig.Location)
}

Expand Down
7 changes: 4 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Core struct {
logger *log.Logger
}

func NewCore(db ethdb.Database, config *Config, isLocalBlock func(block *types.WorkObject) bool, txConfig *TxPoolConfig, txLookupLimit *uint64, chainConfig *params.ChainConfig, slicesRunning []common.Location, currentExpansionNumber uint8, genesisBlock *types.WorkObject, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis, logger *log.Logger) (*Core, error) {
func NewCore(db ethdb.Database, config *Config, isLocalBlock func(block *types.WorkObject) bool, txConfig *TxPoolConfig, txLookupLimit uint64, chainConfig *params.ChainConfig, slicesRunning []common.Location, currentExpansionNumber uint8, genesisBlock *types.WorkObject, engine consensus.Engine, cacheConfig *CacheConfig, vmConfig vm.Config, genesis *Genesis, logger *log.Logger) (*Core, error) {
slice, err := NewSlice(db, config, txConfig, txLookupLimit, isLocalBlock, chainConfig, slicesRunning, currentExpansionNumber, genesisBlock, engine, cacheConfig, vmConfig, genesis, logger)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1104,7 +1104,8 @@ func (c *Core) Snapshots() *snapshot.Tree {
}

func (c *Core) TxLookupLimit() uint64 {
return c.Processor().txLookupLimit
// return c.Processor().txLookupLimit
return 0
}

func (c *Core) SetExtra(extra []byte) error {
Expand Down Expand Up @@ -1217,7 +1218,7 @@ func (c *Core) GetVMConfig() *vm.Config {

// GetTransactionLookup retrieves the lookup associate with the given transaction
// hash from the cache or database.
func (c *Core) GetTransactionLookup(hash common.Hash) *rawdb.LegacyTxLookupEntry {
func (c *Core) GetTransactionLookup(hash common.Hash) *rawdb.TxLookupEntry {
return c.sl.hc.bc.processor.GetTransactionLookup(hash)
}

Expand Down
2 changes: 1 addition & 1 deletion core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type HeaderChain struct {

// NewHeaderChain creates a new HeaderChain structure. ProcInterrupt points
// to the parent's interrupt semaphore.
func NewHeaderChain(db ethdb.Database, engine consensus.Engine, pEtxsRollupFetcher getPendingEtxsRollup, pEtxsFetcher getPendingEtxs, primeBlockFetcher getPrimeBlock, kQuaiAndUpdateBitGetter getKQuaiAndUpdateBit, chainConfig *params.ChainConfig, cacheConfig *CacheConfig, txLookupLimit *uint64, vmConfig vm.Config, slicesRunning []common.Location, currentExpansionNumber uint8, logger *log.Logger) (*HeaderChain, error) {
func NewHeaderChain(db ethdb.Database, engine consensus.Engine, pEtxsRollupFetcher getPendingEtxsRollup, pEtxsFetcher getPendingEtxs, primeBlockFetcher getPrimeBlock, kQuaiAndUpdateBitGetter getKQuaiAndUpdateBit, chainConfig *params.ChainConfig, cacheConfig *CacheConfig, txLookupLimit uint64, vmConfig vm.Config, slicesRunning []common.Location, currentExpansionNumber uint8, logger *log.Logger) (*HeaderChain, error) {

nodeCtx := chainConfig.Location.Context()

Expand Down
41 changes: 8 additions & 33 deletions core/rawdb/accessors_chain_indexes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/log"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestTxLookupStorage(t *testing.T) {
db := NewMemoryDatabase(log.Global)

if entry := ReadTxLookupEntry(db, common.Hash{1}); entry != nil {
if entry, err := ReadTxLookupEntry(db, common.Hash{1}); err == nil {
t.Fatalf("Non existent tx lookup returned: %v", entry)
}

if entry := ReadTxLookupEntry(db, common.Hash{2}); entry != nil {
if entry, err := ReadTxLookupEntry(db, common.Hash{2}); err == nil {
t.Fatalf("Non existent tx lookup returned: %v", entry)
}

Expand All @@ -34,58 +33,34 @@ func TestTxLookupStorage(t *testing.T) {

WriteTxLookupEntriesByBlock(db, block, common.ZONE_CTX)

if entry := ReadTxLookupEntry(db, common.Hash{1}); *entry != 1 {
if entry, _ := ReadTxLookupEntry(db, common.Hash{1}); entry != 1 {
t.Fatal("Wrong tx lookup returned for hash 1")
}

if entry := ReadTxLookupEntry(db, common.Hash{2}); *entry != 1 {
if entry, _ := ReadTxLookupEntry(db, common.Hash{2}); entry != 1 {
t.Fatal("Wrong tx lookup returned for hash 2")
}

DeleteTxLookupEntries(db, hashes)

// check deleted tx lookups
if entry := ReadTxLookupEntry(db, common.Hash{1}); entry != nil {
if _, err := ReadTxLookupEntry(db, common.Hash{1}); err == nil {
t.Fatal("Deleted lookup returned for hash 1")
}

if entry := ReadTxLookupEntry(db, common.Hash{2}); entry != nil {
if _, err := ReadTxLookupEntry(db, common.Hash{2}); err == nil {
t.Fatal("Deleted lookup returned for hash 2")
}

// txs writen by block
if entry := ReadTxLookupEntry(db, tx1.Hash()); *entry != block.NumberU64(common.ZONE_CTX) {
if entry, _ := ReadTxLookupEntry(db, tx1.Hash()); entry != block.NumberU64(common.ZONE_CTX) {
t.Fatal("Wrong tx lookup returned for tx1 hash")
}

if entry := ReadTxLookupEntry(db, tx2.Hash()); *entry != block.NumberU64(common.ZONE_CTX) {
if entry, _ := ReadTxLookupEntry(db, tx2.Hash()); entry != block.NumberU64(common.ZONE_CTX) {
t.Fatal("Wrong tx lookup returned for tx2 hash")
}

//v4-v5 tx lookup
v4Hash := common.Hash{4}
v4Number := uint64(3)
WriteHeaderNumber(db, v4Hash, v4Number)
writeTxLookupEntry(db, v4Hash, v4Hash.Bytes())

if entry := ReadTxLookupEntry(db, v4Hash); *entry != v4Number {
t.Fatal("Wrong tx lookup returned for v4 hash")
}

//v3 tx lookup
v3Hash := common.Hash{5}
v3ProtoHash := &common.ProtoHash{Value: v3Hash.Bytes()}
v3Number := uint64(4)
v3entry, err := proto.Marshal(&ProtoLegacyTxLookupEntry{BlockIndex: v3Number, Hash: v3ProtoHash})
if err != nil {
t.Fatalf("Failed to marshal ProtoLegacyTxLookupEntry, err %s", err)
}
writeTxLookupEntry(db, v3Hash, v3entry)

if entry := ReadTxLookupEntry(db, v3Hash); *entry != v3Number {
t.Fatal("Wrong tx lookup returned for v4 hash")
}

}

func TestReadTransaction(t *testing.T) {
Expand Down
45 changes: 12 additions & 33 deletions core/rawdb/accessors_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,24 @@ package rawdb

import (
"bytes"
"errors"
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/log"
"google.golang.org/protobuf/proto"
)

// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
// hash to allow retrieving the transaction or receipt by hash.
func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
data, _ := db.Get(txLookupKey(hash))
if len(data) == 0 {
return nil
}
// Database v6 tx lookup just stores the block number
if len(data) < common.HashLength {
number := new(big.Int).SetBytes(data).Uint64()
return &number
}
// Database v4-v5 tx lookup format just stores the hash
if len(data) == common.HashLength {
return ReadHeaderNumber(db, common.BytesToHash(data))
}
// Finally try database v3 tx lookup format
protoLegacyTxLookupEntry := new(ProtoLegacyTxLookupEntry)
err := proto.Unmarshal(data, protoLegacyTxLookupEntry)
func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) (uint64, error) {
data, err := db.Get(txLookupKey(hash))
if err != nil {
db.Logger().WithFields(log.Fields{
"hash": hash,
"blob": data,
"err": err,
}).Error("Invalid transaction lookup entry protobuf")
return nil
return 0, errors.New("unable to find tx in database")
}
entry := new(LegacyTxLookupEntry)
entry.ProtoDecode(protoLegacyTxLookupEntry)
return &entry.BlockIndex
number := new(big.Int).SetBytes(data).Uint64()
return number, nil
}

// writeTxLookupEntry stores a positional metadata for a transaction,
Expand Down Expand Up @@ -102,15 +81,15 @@ func DeleteTxLookupEntries(db ethdb.KeyValueWriter, hashes []common.Hash) {
// ReadTransaction retrieves a specific transaction from the database, along with
// its added positional metadata.
func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
blockNumber := ReadTxLookupEntry(db, hash)
if blockNumber == nil {
blockNumber, err := ReadTxLookupEntry(db, hash)
if err != nil {
return nil, common.Hash{}, 0, 0
}
blockHash := ReadCanonicalHash(db, *blockNumber)
blockHash := ReadCanonicalHash(db, blockNumber)
if blockHash == (common.Hash{}) {
return nil, common.Hash{}, 0, 0
}
wo := ReadWorkObject(db, *blockNumber, blockHash, types.BlockObject)
wo := ReadWorkObject(db, blockNumber, blockHash, types.BlockObject)
if wo == nil {
db.Logger().WithFields(log.Fields{
"number": blockNumber,
Expand All @@ -120,11 +99,11 @@ func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, com
}
for txIndex, tx := range wo.Body().Transactions() {
if tx.Hash() == hash {
return tx, blockHash, *blockNumber, uint64(txIndex)
return tx, blockHash, blockNumber, uint64(txIndex)
}
}
db.Logger().WithFields(log.Fields{
"number": *blockNumber,
"number": blockNumber,
"hash": blockHash,
"txhash": hash,
}).Error("Transaction not found")
Expand Down
81 changes: 28 additions & 53 deletions core/rawdb/db.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions core/rawdb/db.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ syntax = "proto3";
package db;
option go_package = "github.com/dominant-strategies/go-quai/core/rawdb";

import "common/proto_common.proto";

message ProtoNumber { uint64 number = 1; }

message ProtoLegacyTxLookupEntry {
common.ProtoHash hash = 1;
uint64 block_index = 2;
uint64 index = 3;
message ProtoTxLookupEntry {
uint64 block_index = 1;
}
Loading

0 comments on commit f437c59

Please sign in to comment.