/
block_reader.go
109 lines (94 loc) · 3.35 KB
/
block_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package powchain
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"go.opencensus.io/trace"
)
// BlockExists returns true if the block exists, it's height and any possible error encountered.
func (s *Service) BlockExists(ctx context.Context, hash common.Hash) (bool, *big.Int, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockExists")
defer span.End()
if exists, blkInfo, err := s.blockCache.BlockInfoByHash(hash); exists || err != nil {
if err != nil {
return false, nil, err
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", true))
return true, blkInfo.Number, nil
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", false))
block, err := s.blockFetcher.BlockByHash(ctx, hash)
if err != nil {
return false, big.NewInt(0), errors.Wrap(err, "could not query block with given hash")
}
if err := s.blockCache.AddBlock(block); err != nil {
return false, big.NewInt(0), err
}
return true, block.Number(), nil
}
// BlockHashByHeight returns the block hash of the block at the given height.
func (s *Service) BlockHashByHeight(ctx context.Context, height *big.Int) (common.Hash, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockHashByHeight")
defer span.End()
if exists, blkInfo, err := s.blockCache.BlockInfoByHeight(height); exists || err != nil {
if err != nil {
return [32]byte{}, err
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", true))
return blkInfo.Hash, nil
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", false))
block, err := s.blockFetcher.BlockByNumber(ctx, height)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not query block with given height")
}
if err := s.blockCache.AddBlock(block); err != nil {
return [32]byte{}, err
}
return block.Hash(), nil
}
// BlockTimeByHeight fetches an eth1.0 block timestamp by its height.
func (s *Service) BlockTimeByHeight(ctx context.Context, height *big.Int) (uint64, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockTimeByHeight")
defer span.End()
block, err := s.blockFetcher.BlockByNumber(ctx, height)
if err != nil {
return 0, errors.Wrap(err, "could not query block with given height")
}
return block.Time(), nil
}
// BlockNumberByTimestamp returns the most recent block number up to a given timestamp.
// This is a naive implementation that will use O(ETH1_FOLLOW_DISTANCE) calls to cache
// or ETH1. This is called for multiple times but only changes every
// SlotsPerEth1VotingPeriod (1024 slots) so the whole method should be cached.
func (s *Service) BlockNumberByTimestamp(ctx context.Context, time uint64) (*big.Int, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockByTimestamp")
defer span.End()
head, err := s.blockFetcher.BlockByNumber(ctx, nil)
if err != nil {
return nil, err
}
for bn := head.Number(); ; bn = big.NewInt(0).Sub(bn, big.NewInt(1)) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
exists, info, err := s.blockCache.BlockInfoByHeight(bn)
if err != nil {
return nil, err
}
if !exists {
blk, err := s.blockFetcher.BlockByNumber(ctx, bn)
if err != nil {
return nil, err
}
if err := s.blockCache.AddBlock(blk); err != nil {
return nil, err
}
info = blockToBlockInfo(blk)
}
if info.Time <= time {
return info.Number, nil
}
}
}