erigon-pulse/ethdb/remote/chain_remote.go

189 lines
4.7 KiB
Go
Raw Normal View History

2019-12-06 07:18:26 +00:00
package remote
import (
"bytes"
"encoding/binary"
2019-12-16 14:54:30 +00:00
"fmt"
2019-12-06 07:18:26 +00:00
"math/big"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/hexutil"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/rlp"
)
// ReadTd reimplemented rawdb.ReadTd
2019-12-16 14:54:30 +00:00
func ReadTd(tx *Tx, hash common.Hash, number uint64) (*hexutil.Big, error) {
bucket, err := tx.Bucket(dbutils.HeaderPrefix)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if bucket == nil {
2019-12-16 14:54:30 +00:00
return nil, nil
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
data, err := bucket.Get(dbutils.HeaderTDKey(number, hash))
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if len(data) == 0 {
2019-12-16 14:54:30 +00:00
return nil, nil
2019-12-06 07:18:26 +00:00
}
td := new(big.Int)
if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
2019-12-16 14:54:30 +00:00
return nil, fmt.Errorf("invalid block total difficulty RLP. Hash: %s. Err: %w", hash, err)
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
return (*hexutil.Big)(td), nil
2019-12-06 07:18:26 +00:00
}
// ReadCanonicalHash reimplementation of rawdb.ReadCanonicalHash
2019-12-16 14:54:30 +00:00
func ReadCanonicalHash(tx *Tx, number uint64) (common.Hash, error) {
bucket, err := tx.Bucket(dbutils.HeaderPrefix)
if err != nil {
return common.Hash{}, err
}
2019-12-06 07:18:26 +00:00
if bucket == nil {
2019-12-16 14:54:30 +00:00
return common.Hash{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
data, err := bucket.Get(dbutils.HeaderHashKey(number))
if err != nil {
return common.Hash{}, err
}
2019-12-06 07:18:26 +00:00
if len(data) == 0 {
2019-12-16 14:54:30 +00:00
return common.Hash{}, nil
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
return common.BytesToHash(data), nil
2019-12-06 07:18:26 +00:00
}
// GetBlockByNumber reimplementation of chain.GetBlockByNumber
2019-12-16 14:54:30 +00:00
func GetBlockByNumber(tx *Tx, number uint64) (*types.Block, error) {
hash, err := ReadCanonicalHash(tx, number)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if hash == (common.Hash{}) {
2019-12-16 14:54:30 +00:00
return nil, nil
2019-12-06 07:18:26 +00:00
}
return ReadBlock(tx, hash, number)
}
// ReadBlock reimplementation of rawdb.ReadBlock
2019-12-16 14:54:30 +00:00
func ReadBlock(tx *Tx, hash common.Hash, number uint64) (*types.Block, error) {
header, err := ReadHeader(tx, hash, number)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if header == nil {
2019-12-16 14:54:30 +00:00
return nil, nil
}
body, err := ReadBody(tx, hash, number)
if err != nil {
return nil, err
2019-12-06 07:18:26 +00:00
}
if body == nil {
2019-12-16 14:54:30 +00:00
return nil, nil
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
2019-12-06 07:18:26 +00:00
}
// ReadHeaderRLP reimplementation of rawdb.ReadHeaderRLP
2019-12-16 14:54:30 +00:00
func ReadHeaderRLP(tx *Tx, hash common.Hash, number uint64) (rlp.RawValue, error) {
bucket, err := tx.Bucket(dbutils.HeaderPrefix)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if bucket == nil {
2019-12-16 14:54:30 +00:00
return rlp.RawValue{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
2019-12-06 07:18:26 +00:00
}
return bucket.Get(dbutils.HeaderKey(number, hash))
}
// ReadHeader reimplementation of rawdb.ReadHeader
2019-12-16 14:54:30 +00:00
func ReadHeader(tx *Tx, hash common.Hash, number uint64) (*types.Header, error) {
data, err := ReadHeaderRLP(tx, hash, number)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if len(data) == 0 {
2019-12-16 14:54:30 +00:00
return nil, err
2019-12-06 07:18:26 +00:00
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
2019-12-16 14:54:30 +00:00
return nil, fmt.Errorf("invalid block header RLP. Hash: %s. Err: %w", hash, err)
2019-12-06 07:18:26 +00:00
}
2019-12-16 14:54:30 +00:00
return header, nil
2019-12-06 07:18:26 +00:00
}
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
2019-12-16 14:54:30 +00:00
func ReadBodyRLP(tx *Tx, hash common.Hash, number uint64) (rlp.RawValue, error) {
bucket, err := tx.Bucket(dbutils.BlockBodyPrefix)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if bucket == nil {
2019-12-16 14:54:30 +00:00
return rlp.RawValue{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
2019-12-06 07:18:26 +00:00
}
return bucket.Get(dbutils.BlockBodyKey(number, hash))
}
// ReadBody reimplementation of rawdb.ReadBody
2019-12-16 14:54:30 +00:00
func ReadBody(tx *Tx, hash common.Hash, number uint64) (*types.Body, error) {
data, err := ReadBodyRLP(tx, hash, number)
if err != nil {
return nil, err
}
2019-12-06 07:18:26 +00:00
if len(data) == 0 {
2019-12-16 14:54:30 +00:00
return nil, nil
2019-12-06 07:18:26 +00:00
}
body := new(types.Body)
if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
2019-12-16 14:54:30 +00:00
return nil, fmt.Errorf("invalid block body RLP: %s, %w", hash, err)
2019-12-06 07:18:26 +00:00
}
// Post-processing
body.SendersToTxs()
2019-12-16 14:54:30 +00:00
return body, nil
2019-12-06 07:18:26 +00:00
}
func ReadLastBlockNumber(tx *Tx) (uint64, error) {
b, err := tx.Bucket(dbutils.HeadHeaderKey)
if err != nil {
return 0, err
}
if b == nil {
return 0, fmt.Errorf("bucket %s not found", dbutils.HeadHeaderKey)
}
blockHashData, err := b.Get(dbutils.HeadHeaderKey)
if err != nil {
return 0, err
}
if len(blockHashData) != common.HashLength {
return 0, fmt.Errorf("head header hash not found or wrong size: %x", blockHashData)
}
b1, err := tx.Bucket(dbutils.HeaderNumberPrefix)
if err != nil {
return 0, err
}
if b1 == nil {
return 0, fmt.Errorf("bucket %s not found", dbutils.HeaderNumberPrefix)
}
blockNumberData, err := b1.Get(blockHashData)
if err != nil {
return 0, err
}
if len(blockNumberData) != 8 {
return 0, fmt.Errorf("head block number not found or wrong size: %x", blockNumberData)
}
return binary.BigEndian.Uint64(blockNumberData), nil
}