2023-05-05 02:20:40 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
2023-05-23 07:49:17 +00:00
|
|
|
"context"
|
2023-05-05 02:20:40 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-05-23 07:49:17 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/services"
|
2023-05-05 02:20:40 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Implements consensus.ChainReader
|
|
|
|
type ChainReaderImpl struct {
|
2023-05-23 07:49:17 +00:00
|
|
|
Cfg chain.Config
|
|
|
|
Db kv.Getter
|
|
|
|
BlockReader services.FullBlockReader
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config retrieves the blockchain's chain configuration.
|
|
|
|
func (cr ChainReaderImpl) Config() *chain.Config {
|
|
|
|
return &cr.Cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentHeader retrieves the current header from the local chain.
|
|
|
|
func (cr ChainReaderImpl) CurrentHeader() *types.Header {
|
|
|
|
hash := rawdb.ReadHeadHeaderHash(cr.Db)
|
|
|
|
number := rawdb.ReadHeaderNumber(cr.Db, hash)
|
2023-05-23 07:49:17 +00:00
|
|
|
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, *number)
|
|
|
|
return h
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader retrieves a block header from the database by hash and number.
|
|
|
|
func (cr ChainReaderImpl) GetHeader(hash libcommon.Hash, number uint64) *types.Header {
|
2023-05-23 07:49:17 +00:00
|
|
|
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, number)
|
|
|
|
return h
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderByNumber retrieves a block header from the database by number.
|
|
|
|
func (cr ChainReaderImpl) GetHeaderByNumber(number uint64) *types.Header {
|
2023-05-31 06:41:10 +00:00
|
|
|
h, _ := cr.BlockReader.HeaderByNumber(context.Background(), cr.Db, number)
|
2023-05-23 07:49:17 +00:00
|
|
|
return h
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeaderByHash retrieves a block header from the database by its hash.
|
|
|
|
func (cr ChainReaderImpl) GetHeaderByHash(hash libcommon.Hash) *types.Header {
|
|
|
|
number := rawdb.ReadHeaderNumber(cr.Db, hash)
|
2023-05-23 07:49:17 +00:00
|
|
|
h, _ := cr.BlockReader.Header(context.Background(), cr.Db, hash, *number)
|
|
|
|
return h
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock retrieves a block from the database by hash and number.
|
|
|
|
func (cr ChainReaderImpl) GetBlock(hash libcommon.Hash, number uint64) *types.Block {
|
2023-05-23 07:49:17 +00:00
|
|
|
b, _, _ := cr.BlockReader.BlockWithSenders(context.Background(), cr.Db, hash, number)
|
|
|
|
return b
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasBlock retrieves a block from the database by hash and number.
|
|
|
|
func (cr ChainReaderImpl) HasBlock(hash libcommon.Hash, number uint64) bool {
|
2023-05-23 07:49:17 +00:00
|
|
|
b, _ := cr.BlockReader.BodyRlp(context.Background(), cr.Db, hash, number)
|
|
|
|
return b != nil
|
2023-05-05 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTd retrieves the total difficulty from the database by hash and number.
|
|
|
|
func (cr ChainReaderImpl) GetTd(hash libcommon.Hash, number uint64) *big.Int {
|
|
|
|
td, err := rawdb.ReadTd(cr.Db, hash, number)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ReadTd failed", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return td
|
|
|
|
}
|
2023-07-10 16:35:27 +00:00
|
|
|
|
|
|
|
func (cr ChainReaderImpl) FrozenBlocks() uint64 {
|
|
|
|
return cr.BlockReader.FrozenBlocks()
|
|
|
|
}
|
2023-11-04 10:59:07 +00:00
|
|
|
|
|
|
|
func (cr ChainReaderImpl) BorSpan(spanId uint64) []byte {
|
|
|
|
spanBytes, err := cr.BlockReader.Span(context.Background(), cr.Db, spanId)
|
|
|
|
if err != nil {
|
2024-01-10 17:12:15 +00:00
|
|
|
log.Error("[consensus] BorSpan failed", "err", err)
|
2023-11-04 10:59:07 +00:00
|
|
|
}
|
|
|
|
return spanBytes
|
|
|
|
}
|