erigon-pulse/ethdb/remote/remotechain/chain_remote.go
Alex Sharov c5ffc971c5
[WIP] Badger v2 (#378)
* badger v2 investigation

* buf pool - use native New method and avoid double checks

* db.Open prototype

* db.Tx/Bucket/Cursor prototypes

* Chained config

* Item concept added

* save changes to test on master

* make hack resumable

* Design document v0

* Cursor concept

* less brackets syntax of cursor builder

* benchmarks

* cleanup fs

* test for context cancelations

* test for context cancelations

* test for cursor.Prefix option

* add ForEachKey method

* add ForEachKey method

* add naming explanation

* experiment of non-pointers cursor/bucket

* .Bucket() and .Cursor() doesn't returns error

* .Bucket() and .Cursor() doesn't returns error

* .Bucket() and .Cursor() doesn't returns error

* remove CursorOpts concept

* more test-cases

* simplify open api

* Tx, Bucket, Cursor - now are interfaces

* Tx, Bucket, Cursor - now are interfaces

* switch to interfaces

* rebase master

Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-11 11:02:37 +00:00

169 lines
4.6 KiB
Go

package remotechain
import (
"bytes"
"encoding/binary"
"fmt"
"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/ethdb/remote"
"github.com/ledgerwatch/turbo-geth/rlp"
)
// ReadTd reimplemented rawdb.ReadTd
func ReadTd(tx *remote.Tx, hash common.Hash, number uint64) (*hexutil.Big, error) {
bucket := tx.Bucket(dbutils.HeaderPrefix)
if bucket == nil {
return nil, nil
}
data, err := bucket.Get(dbutils.HeaderTDKey(number, hash))
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
td := new(big.Int)
if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
return nil, fmt.Errorf("invalid block total difficulty RLP. Hash: %s. Err: %w", hash, err)
}
return (*hexutil.Big)(td), nil
}
// ReadCanonicalHash reimplementation of rawdb.ReadCanonicalHash
func ReadCanonicalHash(tx *remote.Tx, number uint64) (common.Hash, error) {
bucket := tx.Bucket(dbutils.HeaderPrefix)
if bucket == nil {
return common.Hash{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
}
data, err := bucket.Get(dbutils.HeaderHashKey(number))
if err != nil {
return common.Hash{}, err
}
if len(data) == 0 {
return common.Hash{}, nil
}
return common.BytesToHash(data), nil
}
// GetBlockByNumber reimplementation of chain.GetBlockByNumber
func GetBlockByNumber(tx *remote.Tx, number uint64) (*types.Block, error) {
hash, err := ReadCanonicalHash(tx, number)
if err != nil {
return nil, err
}
if hash == (common.Hash{}) {
return nil, nil
}
return ReadBlock(tx, hash, number)
}
// ReadBlock reimplementation of rawdb.ReadBlock
func ReadBlock(tx *remote.Tx, hash common.Hash, number uint64) (*types.Block, error) {
header, err := ReadHeader(tx, hash, number)
if err != nil {
return nil, err
}
if header == nil {
return nil, nil
}
body, err := ReadBody(tx, hash, number)
if err != nil {
return nil, err
}
if body == nil {
return nil, nil
}
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles), nil
}
// ReadHeaderRLP reimplementation of rawdb.ReadHeaderRLP
func ReadHeaderRLP(tx *remote.Tx, hash common.Hash, number uint64) (rlp.RawValue, error) {
bucket := tx.Bucket(dbutils.HeaderPrefix)
if bucket == nil {
return rlp.RawValue{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
}
return bucket.Get(dbutils.HeaderKey(number, hash))
}
// ReadHeader reimplementation of rawdb.ReadHeader
func ReadHeader(tx *remote.Tx, hash common.Hash, number uint64) (*types.Header, error) {
data, err := ReadHeaderRLP(tx, hash, number)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, err
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
return nil, fmt.Errorf("invalid block header RLP. Hash: %s. Err: %w", hash, err)
}
return header, nil
}
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
func ReadBodyRLP(tx *remote.Tx, hash common.Hash, number uint64) (rlp.RawValue, error) {
bucket := tx.Bucket(dbutils.BlockBodyPrefix)
if bucket == nil {
return rlp.RawValue{}, fmt.Errorf("bucket %s not found", dbutils.HeaderPrefix)
}
return bucket.Get(dbutils.BlockBodyKey(number, hash))
}
// ReadBody reimplementation of rawdb.ReadBody
func ReadBody(tx *remote.Tx, hash common.Hash, number uint64) (*types.Body, error) {
data, err := ReadBodyRLP(tx, hash, number)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, nil
}
body := new(types.Body)
if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
return nil, fmt.Errorf("invalid block body RLP: %s, %w", hash, err)
}
// Post-processing
body.SendersToTxs()
return body, nil
}
func ReadLastBlockNumber(tx *remote.Tx) (uint64, error) {
b := tx.Bucket(dbutils.HeadHeaderKey)
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 := tx.Bucket(dbutils.HeaderNumberPrefix)
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
}