Skip to content

Commit

Permalink
Made PendingHeader atrtibutes private and added helper functions to a…
Browse files Browse the repository at this point in the history
…ccess and encode/decode

Made Termini a struct as well, previously termini was a array of common.Hash
  • Loading branch information
gameofpointers committed Aug 22, 2023
1 parent 3d26f9d commit fce724a
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 148 deletions.
4 changes: 2 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (c *Core) SubscribePendingEtxsRollup(ch chan<- types.PendingEtxsRollup) eve
return c.sl.SubscribePendingEtxsRollup(ch)
}

func (c *Core) GenerateRecoveryPendingHeader(pendingHeader *types.Header, checkpointHashes []common.Hash) error {
func (c *Core) GenerateRecoveryPendingHeader(pendingHeader *types.Header, checkpointHashes types.Termini) error {
return c.sl.GenerateRecoveryPendingHeader(pendingHeader, checkpointHashes)
}

Expand Down Expand Up @@ -498,7 +498,7 @@ func (c *Core) GetBodyRLP(hash common.Hash) rlp.RawValue {
}

// GetTerminiByHash retrieves the termini stored for a given header hash
func (c *Core) GetTerminiByHash(hash common.Hash) []common.Hash {
func (c *Core) GetTerminiByHash(hash common.Hash) *types.Termini {
return c.sl.hc.GetTerminiByHash(hash)
}

Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (g *Genesis) Commit(db ethdb.Database) (*types.Block, error) {
if config == nil {
config = params.AllProgpowProtocolChanges
}
rawdb.WriteTermini(db, block.Hash(), nil)
rawdb.WriteTermini(db, block.Hash(), types.EmptyTermini())
rawdb.WriteBlock(db, block)
rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), nil)
rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64())
Expand Down
2 changes: 1 addition & 1 deletion core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (hc *HeaderChain) GetBlockNumber(hash common.Hash) *uint64 {
return number
}

func (hc *HeaderChain) GetTerminiByHash(hash common.Hash) []common.Hash {
func (hc *HeaderChain) GetTerminiByHash(hash common.Hash) *types.Termini {
termini := rawdb.ReadTermini(hc.headerDb, hash)
return termini
}
Expand Down
71 changes: 17 additions & 54 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,22 +506,21 @@ func DeleteAllPbBodyKeys(db ethdb.KeyValueWriter) {
}

// ReadHeadsHashes retreive's the heads hashes of the blockchain.
func ReadTermini(db ethdb.Reader, hash common.Hash) []common.Hash {
func ReadTermini(db ethdb.Reader, hash common.Hash) *types.Termini {
key := terminiKey(hash)
data, _ := db.Get(key)
if len(data) == 0 {
return nil
}
hashes := []common.Hash{}
if err := rlp.DecodeBytes(data, &hashes); err != nil {
var termini types.Termini
if err := rlp.DecodeBytes(data, &termini); err != nil {
return nil
}
return hashes
return &termini
}

// WriteHeadsHashes writes the heads hashes of the blockchain.
func WriteTermini(db ethdb.KeyValueWriter, index common.Hash, hashes []common.Hash) {
log.Debug("WriteTermini:", "hashes:", hashes, "index:", index)
// WriteTermini writes the heads hashes of the blockchain.
func WriteTermini(db ethdb.KeyValueWriter, index common.Hash, hashes types.Termini) {
key := terminiKey(index)
data, err := rlp.EncodeToBytes(hashes)
if err != nil {
Expand All @@ -542,23 +541,24 @@ func DeleteTermini(db ethdb.KeyValueWriter, hash common.Hash) {
}

// ReadPendingHeader retreive's the pending header stored in hash.
func ReadPendingHeader(db ethdb.Reader, hash common.Hash) *types.Header {
func ReadPendingHeader(db ethdb.Reader, hash common.Hash) *types.PendingHeader {
key := pendingHeaderKey(hash)
data, _ := db.Get(key)
if len(data) == 0 {
log.Error("Pending Header is nil", "Key", key)
return nil
}

header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
pendingHeader := new(types.PendingHeader)
if err := rlp.Decode(bytes.NewReader(data), pendingHeader); err != nil {
log.Error("Invalid pendingHeader RLP")
return nil
}
return header
return pendingHeader
}

// WritePendingHeader writes the pending header of the terminus hash.
func WritePendingHeader(db ethdb.KeyValueWriter, hash common.Hash, pendingHeader *types.Header) {
func WritePendingHeader(db ethdb.KeyValueWriter, hash common.Hash, pendingHeader types.PendingHeader) {
key := pendingHeaderKey(hash)

// Write the encoded pending header
Expand All @@ -580,42 +580,6 @@ func DeletePendingHeader(db ethdb.KeyValueWriter, hash common.Hash) {
}
}

// ReadPhCacheTermini retreive's the pending header termini stored in hash.
func ReadPhCacheTermini(db ethdb.Reader, hash common.Hash) []common.Hash {
key := phBodyTerminiKey(hash)
data, _ := db.Get(key)
if len(data) == 0 {
return nil
}
termini := []common.Hash{}
if err := rlp.Decode(bytes.NewReader(data), &termini); err != nil {
log.Error("Invalid pendingHeader RLP")
return nil
}
return termini
}

// WritePhCacheTermini writes the pending header termini of the terminus hash.
func WritePhCacheTermini(db ethdb.KeyValueWriter, hash common.Hash, termini []common.Hash) {
key := phBodyTerminiKey(hash)
// Write the encoded pending header
data, err := rlp.EncodeToBytes(termini)
if err != nil {
log.Fatal("Failed to RLP encode pending header", "err", err)
}
if err := db.Put(key, data); err != nil {
log.Fatal("Failed to store header", "err", err)
}
}

// DeletePhCacheTermini deletes the pending header termini stored for the header hash.
func DeletePhCacheTermini(db ethdb.KeyValueWriter, hash common.Hash) {
key := phBodyTerminiKey(hash)
if err := db.Delete(key); err != nil {
log.Fatal("Failed to delete slice pending header ", "err", err)
}
}

// ReadPhCache retreive's the heads hashes of the blockchain.
func ReadPhCache(db ethdb.Reader) map[common.Hash]types.PendingHeader {
data, _ := db.Get(phCacheKey)
Expand All @@ -631,10 +595,10 @@ func ReadPhCache(db ethdb.Reader) map[common.Hash]types.PendingHeader {
phCache := make(map[common.Hash]types.PendingHeader)
// Read the pending header and phBody.
for _, hash := range hashes {
header := ReadPendingHeader(db, hash)
termini := ReadPhCacheTermini(db, hash)
pendingHeader := types.PendingHeader{Header: header, Termini: termini}
phCache[hash] = pendingHeader
pendingHeader := ReadPendingHeader(db, hash)
if pendingHeader != nil {
phCache[hash] = *pendingHeader
}
}
return phCache
}
Expand All @@ -644,8 +608,7 @@ func WritePhCache(db ethdb.KeyValueWriter, phCache map[common.Hash]types.Pending
var hashes []common.Hash
for hash, pendingHeader := range phCache {
hashes = append(hashes, hash)
WritePendingHeader(db, hash, pendingHeader.Header)
WritePhCacheTermini(db, hash, pendingHeader.Termini)
WritePendingHeader(db, hash, pendingHeader)
}

data, err := rlp.EncodeToBytes(hashes)
Expand Down
Loading

0 comments on commit fce724a

Please sign in to comment.