-
Notifications
You must be signed in to change notification settings - Fork 45
/
irrfinder.go
106 lines (87 loc) · 3.12 KB
/
irrfinder.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
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eosws
import (
"context"
"net/url"
"strconv"
"time"
"github.com/streamingfast/derr"
"github.com/streamingfast/logging"
"github.com/streamingfast/kvdb"
"go.uber.org/zap"
)
type IrreversibleFinder interface {
IrreversibleIDAtBlockNum(ctx context.Context, blockNum uint32) (string, error)
IrreversibleIDAtBlockID(ctx context.Context, blockID string) (string, error)
}
type DefaultIrreversibleFinder struct {
db DB
}
func NewDBReaderBaseIrrFinder(db DB) *DefaultIrreversibleFinder {
return &DefaultIrreversibleFinder{
db: db,
}
}
func (f *DefaultIrreversibleFinder) IrreversibleIDAtBlockNum(ctx context.Context, blockNum uint32) (out string, err error) {
logging.Logger(ctx, zlog).Debug("fetching irreversible block num", zap.Uint32("block_num", blockNum))
if blockNum == 0 {
return "", derr.RequestValidationError(ctx, url.Values{
"block_num": []string{"The block_num field must be greater than 0"},
})
}
err = Retry(ctx, 5, 500*time.Millisecond, func() error {
blockRef, err := f.db.GetClosestIrreversibleIDAtBlockNum(ctx, blockNum)
if err != nil || blockRef == nil {
if err != kvdb.ErrNotFound {
logging.Logger(ctx, zlog).Error("cannot get irreversible ID at blocknum", zap.Uint32("block_num", blockNum), zap.Error(err))
}
return AppUnableToGetIrreversibleBlockIDError(ctx, strconv.FormatUint(uint64(blockNum), 10))
}
out = blockRef.ID()
return err
})
return
}
func (f *DefaultIrreversibleFinder) IrreversibleIDAtBlockID(ctx context.Context, blockID string) (out string, err error) {
logging.Logger(ctx, zlog).Debug("fetching irreversible block ID", zap.String("block_id", blockID))
err = Retry(ctx, 5, 500*time.Millisecond, func() error {
blockRef, err := f.db.GetIrreversibleIDAtBlockID(ctx, blockID)
if err != nil || blockRef == nil {
if err != kvdb.ErrNotFound {
logging.Logger(ctx, zlog).Error("cannot get irreversible ID at blocknum", zap.String("block_id", blockID), zap.Error(err))
}
return AppUnableToGetIrreversibleBlockIDError(ctx, blockID)
}
out = blockRef.ID()
return err
})
return
}
type TestIrreversibleFinder struct {
irrID string
err error
}
func NewTestIrreversibleFinder(irrID string, err error) *TestIrreversibleFinder {
return &TestIrreversibleFinder{
irrID: irrID,
err: err,
}
}
func (f *TestIrreversibleFinder) IrreversibleIDAtBlockNum(ctx context.Context, blockNum uint32) (string, error) {
return f.irrID, f.err
}
func (f *TestIrreversibleFinder) IrreversibleIDAtBlockID(ctx context.Context, blockID string) (string, error) {
return f.irrID, f.err
}