2021-11-14 04:08:52 +00:00
|
|
|
package snapshotsync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-18 14:07:55 +00:00
|
|
|
"fmt"
|
2021-11-14 04:08:52 +00:00
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockReader can read blocks from db and snapshots
|
|
|
|
type BlockReader struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockReader() *BlockReader {
|
|
|
|
return &BlockReader{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (back *BlockReader) BlockWithSenders(ctx context.Context, tx kv.Tx, hash common.Hash, blockHeight uint64) (block *types.Block, senders []common.Address, err error) {
|
2021-11-18 14:07:55 +00:00
|
|
|
canonicalHash, err := rawdb.ReadCanonicalHash(tx, blockHeight)
|
2021-11-14 04:08:52 +00:00
|
|
|
if err != nil {
|
2021-11-18 14:07:55 +00:00
|
|
|
return nil, nil, fmt.Errorf("requested non-canonical hash %x. canonical=%x", hash, canonicalHash)
|
2021-11-14 04:08:52 +00:00
|
|
|
}
|
2021-11-18 14:07:55 +00:00
|
|
|
if canonicalHash == hash {
|
|
|
|
block, senders, err = rawdb.ReadBlockWithSenders(tx, hash, blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return block, senders, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawdb.NonCanonicalBlockWithSenders(tx, hash, blockHeight)
|
2021-11-14 04:08:52 +00:00
|
|
|
}
|