Skip to content

Commit

Permalink
Added the header number into the header writes to fix ReadAllHashes
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Sep 4, 2024
1 parent 3df837c commit 830cce4
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 84 deletions.
17 changes: 13 additions & 4 deletions core/bodydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (bc *BodyDb) GetBlock(hash common.Hash, number uint64) *types.WorkObject {
if block, ok := bc.blockCache.Get(hash); ok {
return &block
}
block := rawdb.ReadWorkObject(bc.db, hash, types.BlockObject)
block := rawdb.ReadWorkObject(bc.db, number, hash, types.BlockObject)
if block == nil {
return nil
}
Expand All @@ -194,7 +194,12 @@ func (bc *BodyDb) GetWorkObject(hash common.Hash) *types.WorkObject {
if wo, ok := bc.woCache.Get(hash); ok {
return &wo
}
wo := rawdb.ReadWorkObject(bc.db, hash, types.BlockObject)

number := rawdb.ReadHeaderNumber(bc.db, hash)
if number == nil {
return nil
}
wo := rawdb.ReadWorkObject(bc.db, *number, hash, types.BlockObject)
if wo == nil {
return nil
}
Expand All @@ -210,7 +215,11 @@ func (bc *BodyDb) GetWorkObjectWithWorkShares(hash common.Hash) *types.WorkObjec
if termini == nil {
return nil
}
wo := rawdb.ReadWorkObjectWithWorkShares(bc.db, hash)
number := rawdb.ReadHeaderNumber(bc.db, hash)
if number == nil {
return nil
}
wo := rawdb.ReadWorkObjectWithWorkShares(bc.db, *number, hash)
if wo == nil {
return nil
}
Expand All @@ -224,7 +233,7 @@ func (bc *BodyDb) GetBlockOrCandidate(hash common.Hash, number uint64) *types.Wo
return &block
}

block := rawdb.ReadWorkObject(bc.db, hash, types.BlockObject)
block := rawdb.ReadWorkObject(bc.db, number, hash, types.BlockObject)
if block == nil {
return nil
}
Expand Down
28 changes: 21 additions & 7 deletions core/chain_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package core
import (
"context"
"encoding/binary"
"errors"
"fmt"
"runtime/debug"
"sync"
Expand Down Expand Up @@ -263,7 +264,12 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
// TODO: Need to be able to turn on/off indexer and fix corrupted state
if c.indexAddressUtxos {
reorgHeaders := make([]*types.WorkObject, 0)
for prev := prevHeader; prev.Hash() != h.Hash(); prev = rawdb.ReadHeader(c.chainDb, prev.ParentHash(nodeCtx)) {
for prev := prevHeader; prev.Hash() != h.Hash(); {
prevNumber := rawdb.ReadHeaderNumber(c.chainDb, prev.ParentHash(nodeCtx))
if prevNumber == nil {
break
}
prev = rawdb.ReadHeader(c.chainDb, *prevNumber, prev.ParentHash(nodeCtx))
reorgHeaders = append(reorgHeaders, h)
}

Expand All @@ -275,8 +281,13 @@ func (c *ChainIndexer) indexerLoop(currentHeader *types.WorkObject, qiIndexerCh
}

// Add new blocks from current hash back to common ancestor
for curr := block; curr.Hash() != h.Hash(); curr = rawdb.ReadHeader(c.chainDb, curr.ParentHash(nodeCtx)) {
block := rawdb.ReadWorkObject(c.chainDb, curr.Hash(), types.BlockObject)
for curr := block; curr.Hash() != h.Hash(); {
prevNumber := rawdb.ReadHeaderNumber(c.chainDb, curr.ParentHash(nodeCtx))
if prevNumber == nil {
break
}
curr = rawdb.ReadHeader(c.chainDb, *prevNumber, curr.ParentHash(nodeCtx))
block := rawdb.ReadWorkObject(c.chainDb, *prevNumber, curr.Hash(), types.BlockObject)
c.addOutpointsToIndexer(addressOutpoints, nodeCtx, config, block)
}
}
Expand Down Expand Up @@ -457,7 +468,7 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash, node
if hash == (common.Hash{}) {
return common.Hash{}, fmt.Errorf("canonical block #%d unknown", number)
}
header := rawdb.ReadHeader(c.chainDb, hash)
header := rawdb.ReadHeader(c.chainDb, number, hash)
if header == nil {
return common.Hash{}, fmt.Errorf("block #%d [%x..] not found", number, hash[:4])
} else if header.ParentHash(nodeCtx) != lastHead {
Expand Down Expand Up @@ -619,7 +630,7 @@ func (c *ChainIndexer) addOutpointsToIndexer(addressOutpoints map[string]map[str
// This is done in reverse order from the old header to the common ancestor.
func (c *ChainIndexer) reorgUtxoIndexer(headers []*types.WorkObject, addressOutpoints map[string]map[string]*types.OutpointAndDenomination, nodeCtx int, config params.ChainConfig) error {
for _, header := range headers {
block := rawdb.ReadWorkObject(c.chainDb, header.Hash(), types.BlockObject)
block := rawdb.ReadWorkObject(c.chainDb, header.NumberU64(nodeCtx), header.Hash(), types.BlockObject)

for _, tx := range block.QiTransactions() {
for i, out := range tx.TxOut() {
Expand Down Expand Up @@ -649,8 +660,11 @@ func (c *ChainIndexer) reorgUtxoIndexer(headers []*types.WorkObject, addressOutp
for _, in := range tx.TxIn() {
outpoint := in.PreviousOutPoint
address := crypto.PubkeyBytesToAddress(in.PubKey, config.Location).Hex()

parent := rawdb.ReadHeader(c.chainDb, block.ParentHash(nodeCtx))
parentNumber := rawdb.ReadHeaderNumber(c.chainDb, block.ParentHash(nodeCtx))
if parentNumber == nil {
return errors.New("parent number cannot be found")
}
parent := rawdb.ReadHeader(c.chainDb, *parentNumber, block.ParentHash(nodeCtx))

state, err := c.StateAt(parent.EVMRoot(), parent.UTXORoot(), parent.EtxSetRoot())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, nodeLoca
}
// We have the genesis block in database(perhaps in ancient database)
// but the corresponding state is missing.
header := rawdb.ReadHeader(db, stored)
header := rawdb.ReadHeader(db, 0, stored)
if _, err := state.New(header.EVMRoot(), header.UTXORoot(), header.EtxSetRoot(), state.NewDatabaseWithConfig(db, nil), state.NewDatabaseWithConfig(db, nil), state.NewDatabaseWithConfig(db, nil), nil, nodeLocation, logger); err != nil {
if genesis == nil {
genesis = DefaultGenesisBlock()
Expand Down
28 changes: 17 additions & 11 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ func NewHeaderChain(db ethdb.Database, engine consensus.Engine, pEtxsRollupFetch
}

genesisHash := hc.GetGenesisHashes()[0]
hc.genesisHeader = rawdb.ReadWorkObject(db, genesisHash, types.BlockObject)
genesisNumber := rawdb.ReadHeaderNumber(db, genesisHash)
if genesisNumber == nil {
return nil, ErrNoGenesis
}
hc.genesisHeader = rawdb.ReadWorkObject(db, *genesisNumber, genesisHash, types.BlockObject)
if bytes.Equal(chainConfig.Location, common.Location{0, 0}) {
if hc.genesisHeader == nil {
return nil, ErrNoGenesis
Expand Down Expand Up @@ -746,7 +750,11 @@ func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.WorkObject {
if header, ok := hc.headerCache.Get(hash); ok {
return &header
}
header := rawdb.ReadHeader(hc.headerDb, hash)
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
header := rawdb.ReadHeader(hc.headerDb, *number, hash)
if header == nil {
return nil
}
Expand All @@ -762,7 +770,11 @@ func (hc *HeaderChain) GetHeaderOrCandidateByHash(hash common.Hash) *types.WorkO
if header, ok := hc.headerCache.Get(hash); ok {
return &header
}
header := rawdb.ReadHeader(hc.headerDb, hash)
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
header := rawdb.ReadHeader(hc.headerDb, *number, hash)
if header == nil {
return nil
}
Expand Down Expand Up @@ -958,7 +970,7 @@ func (hc *HeaderChain) GetBody(hash common.Hash) *types.WorkObject {
if number == nil {
return nil
}
body := rawdb.ReadWorkObject(hc.headerDb, hash, types.BlockObject)
body := rawdb.ReadWorkObject(hc.headerDb, *number, hash, types.BlockObject)
if body == nil {
return nil
}
Expand Down Expand Up @@ -1062,13 +1074,7 @@ func (hc *HeaderChain) CheckIfEtxIsEligible(etxEligibleSlices common.Hash, to co

// IsGenesisHash checks if a hash is a genesis hash
func (hc *HeaderChain) IsGenesisHash(hash common.Hash) bool {
genesisHashes := rawdb.ReadGenesisHashes(hc.headerDb)
for _, genesisHash := range genesisHashes {
if hash == genesisHash {
return true
}
}
return false
return rawdb.IsGenesisHash(hc.headerDb, hash)
}

// AddGenesisHash appends the given hash to the genesis hash list
Expand Down
67 changes: 43 additions & 24 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,25 @@ func HasHeader(db ethdb.Reader, hash common.Hash, number uint64) bool {
if has, err := db.Ancient(freezerHashTable, number); err == nil && common.BytesToHash(has) == hash {
return true
}
if has, err := db.Has(blockWorkObjectHeaderKey(hash)); !has || err != nil {
if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
return false
}
return true
}

// ReadHeader retrieves the block header corresponding to the hash.
func ReadHeader(db ethdb.Reader, hash common.Hash) *types.WorkObject {
wo := ReadWorkObjectHeaderOnly(db, hash, types.BlockObject)
func ReadHeader(db ethdb.Reader, number uint64, hash common.Hash) *types.WorkObject {
wo := ReadWorkObjectHeaderOnly(db, number, hash, types.BlockObject)
if wo == nil || wo.Body() == nil || wo.Header() == nil {
// Try backup function
return ReadWorkObject(db, hash, types.BlockObject)
return ReadWorkObject(db, number, hash, types.BlockObject)
}
return wo
}

// DeleteHeader removes all block header data associated with a hash.
func DeleteHeader(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
DeleteWorkObjectHeader(db, hash, types.BlockObject)
DeleteWorkObjectHeader(db, number, hash, types.BlockObject)
DeleteHeaderNumber(db, hash)
}

Expand Down Expand Up @@ -326,11 +326,11 @@ func DeleteTermini(db ethdb.KeyValueWriter, hash common.Hash) {
}

// ReadWorkObjectHeader retreive's the work object header stored in hash.
func ReadWorkObjectHeader(db ethdb.Reader, hash common.Hash, woType types.WorkObjectView) *types.WorkObjectHeader {
func ReadWorkObjectHeader(db ethdb.Reader, number uint64, hash common.Hash, woType types.WorkObjectView) *types.WorkObjectHeader {
var key []byte
switch woType {
case types.BlockObject:
key = blockWorkObjectHeaderKey(hash)
key = headerKey(number, hash)
}
data, _ := db.Get(key)
if len(data) == 0 {
Expand Down Expand Up @@ -358,7 +358,7 @@ func WriteWorkObjectHeader(db ethdb.KeyValueWriter, hash common.Hash, workObject
var key []byte
switch woType {
case types.BlockObject:
key = blockWorkObjectHeaderKey(hash)
key = headerKey(workObject.NumberU64(nodeCtx), hash)
}
protoWorkObjectHeader, err := workObject.WorkObjectHeader().ProtoEncode()
if err != nil {
Expand All @@ -374,20 +374,20 @@ func WriteWorkObjectHeader(db ethdb.KeyValueWriter, hash common.Hash, workObject
}

// DeleteWorkObjectHeader deletes the work object header stored for the header hash.
func DeleteWorkObjectHeader(db ethdb.KeyValueWriter, hash common.Hash, woType types.WorkObjectView) {
func DeleteWorkObjectHeader(db ethdb.KeyValueWriter, number uint64, hash common.Hash, woType types.WorkObjectView) {
var key []byte
switch woType {
case types.BlockObject:
key = blockWorkObjectHeaderKey(hash)
key = headerKey(number, hash)
}
if err := db.Delete(key); err != nil {
db.Logger().WithField("err", err).Fatal("Failed to delete work object header ")
}
}

// ReadWorkObject retreive's the work object stored in hash.
func ReadWorkObject(db ethdb.Reader, hash common.Hash, woType types.WorkObjectView) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, hash, woType)
func ReadWorkObject(db ethdb.Reader, number uint64, hash common.Hash, woType types.WorkObjectView) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, number, hash, woType)
if workObjectHeader == nil {
return nil
}
Expand All @@ -399,8 +399,8 @@ func ReadWorkObject(db ethdb.Reader, hash common.Hash, woType types.WorkObjectVi
}

// ReadWorkObjectWithWorkShares retreive's the work object stored in hash.
func ReadWorkObjectWithWorkShares(db ethdb.Reader, hash common.Hash) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, hash, types.BlockObject)
func ReadWorkObjectWithWorkShares(db ethdb.Reader, number uint64, hash common.Hash) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, number, hash, types.BlockObject)
if workObjectHeader == nil {
return nil
}
Expand All @@ -411,8 +411,8 @@ func ReadWorkObjectWithWorkShares(db ethdb.Reader, hash common.Hash) *types.Work
return types.NewWorkObject(workObjectHeader, workObjectBody, nil) //TODO: mmtx transaction
}

func ReadWorkObjectHeaderOnly(db ethdb.Reader, hash common.Hash, woType types.WorkObjectView) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, hash, woType)
func ReadWorkObjectHeaderOnly(db ethdb.Reader, number uint64, hash common.Hash, woType types.WorkObjectView) *types.WorkObject {
workObjectHeader := ReadWorkObjectHeader(db, number, hash, woType)
if workObjectHeader == nil {
return nil
}
Expand All @@ -432,7 +432,7 @@ func WriteWorkObject(db ethdb.KeyValueWriter, hash common.Hash, workObject *type
// DeleteWorkObject deletes the work object stored for the header hash.
func DeleteWorkObject(db ethdb.KeyValueWriter, hash common.Hash, number uint64, woType types.WorkObjectView) {
DeleteWorkObjectBody(db, hash)
DeleteWorkObjectHeader(db, hash, woType) //TODO: mmtx transaction
DeleteWorkObjectHeader(db, number, hash, woType) //TODO: mmtx transaction
DeleteHeader(db, hash, number)
DeleteReceipts(db, hash, number)
}
Expand All @@ -441,7 +441,7 @@ func DeleteWorkObject(db ethdb.KeyValueWriter, hash common.Hash, number uint64,
// the hash to number mapping.
func DeleteBlockWithoutNumber(db ethdb.KeyValueWriter, hash common.Hash, number uint64, woType types.WorkObjectView) {
DeleteWorkObjectBody(db, hash)
DeleteWorkObjectHeader(db, hash, woType) //TODO: mmtx transaction
DeleteWorkObjectHeader(db, number, hash, woType) //TODO: mmtx transaction
DeleteReceipts(db, hash, number)
}

Expand Down Expand Up @@ -720,7 +720,7 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *para
if receipts == nil {
return nil
}
body := ReadWorkObject(db, hash, types.BlockObject)
body := ReadWorkObject(db, number, hash, types.BlockObject)
if body == nil {
db.Logger().WithFields(log.Fields{
"hash": hash,
Expand Down Expand Up @@ -754,29 +754,48 @@ func DeleteReceipts(db ethdb.KeyValueWriter, hash common.Hash, number uint64) {
}
}

func IsGenesisHash(db ethdb.Reader, hash common.Hash) bool {
genesisHashes := ReadGenesisHashes(db)
for _, genesisHash := range genesisHashes {
if hash == genesisHash {
return true
}
}
return false
}

// FindCommonAncestor returns the last common ancestor of two block headers
func FindCommonAncestor(db ethdb.Reader, a, b *types.WorkObject, nodeCtx int) *types.WorkObject {
for bn := b.NumberU64(nodeCtx); a.NumberU64(nodeCtx) > bn; {
a = ReadHeader(db, a.ParentHash(nodeCtx))
a = ReadHeader(db, a.NumberU64(nodeCtx)-1, a.ParentHash(nodeCtx))
if IsGenesisHash(db, b.ParentHash(nodeCtx)) {
return nil
}
if a == nil {
return nil
}
}
for an := a.NumberU64(nodeCtx); an < b.NumberU64(nodeCtx); {
b = ReadHeader(db, b.ParentHash(nodeCtx))
b = ReadHeader(db, b.NumberU64(nodeCtx)-1, b.ParentHash(nodeCtx))
if IsGenesisHash(db, b.ParentHash(nodeCtx)) {
return nil
}
if b == nil {
return nil
}
}
for a.Hash() != b.Hash() {
a = ReadHeader(db, a.ParentHash(nodeCtx))
a = ReadHeader(db, a.NumberU64(nodeCtx)-1, a.ParentHash(nodeCtx))
if a == nil {
return nil
}
b = ReadHeader(db, b.ParentHash(nodeCtx))
b = ReadHeader(db, b.NumberU64(nodeCtx)-1, b.ParentHash(nodeCtx))
if b == nil {
return nil
}
if IsGenesisHash(db, a.ParentHash(nodeCtx)) || IsGenesisHash(db, b.ParentHash(nodeCtx)) {
return nil
}
}
return a
}
Expand All @@ -791,7 +810,7 @@ func ReadHeadBlock(db ethdb.Reader) *types.WorkObject {
if headWorkObjectNumber == nil {
return nil
}
return ReadWorkObject(db, headWorkObjectHash, types.BlockObject)
return ReadWorkObject(db, *headWorkObjectNumber, headWorkObjectHash, types.BlockObject)
}

// ReadPendingEtxsProto retrieves the set of pending ETXs for the given block, in Proto encoding
Expand Down
Loading

0 comments on commit 830cce4

Please sign in to comment.