This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocks.go
64 lines (55 loc) · 1.73 KB
/
blocks.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
package services
import (
"fmt"
"strconv"
"github.com/everstake/teztracker/models"
"github.com/guregu/null"
)
// ErrNotFound is an error returned when the requested entity doesn't exist in the repository.
var ErrNotFound = fmt.Errorf("not found")
// HeadBlock retrieves the last added block from the repository.
func (t *TezTracker) HeadBlock() (models.Block, error) {
r := t.repoProvider.GetBlock()
return r.Last()
}
// BlockList retrives up to limit of blocks before the specified level.
func (t *TezTracker) BlockList(beforeLevel uint64, limits Limiter) ([]models.Block, int64, error) {
r := t.repoProvider.GetBlock()
last, err := r.Last()
if err != nil {
return nil, 0, err
}
blocks, err := r.ListExtended(limits.Limit(), limits.Offset(), beforeLevel)
return blocks, last.Level.Int64 + 1, err
}
// GetBlockWithOperationGroups retrieves a block by hash or by level. It loads OperationGroups into the found block.
func (t *TezTracker) GetBlockWithOperationGroups(hashOrLevel string) (block models.Block, err error) {
r := t.repoProvider.GetBlock()
var filter models.Block
if i, e := strconv.ParseInt(hashOrLevel, 10, 64); e == nil {
filter.Level = null.IntFrom(i)
} else {
filter.Hash = null.StringFrom(hashOrLevel)
}
found, block, err := r.FindExtended(filter)
if err != nil {
return block, err
}
if !found {
return block, ErrNotFound
}
found, prevBlock, err := r.Find(models.Block{Level: null.IntFrom(block.Level.Int64 - 1)})
if err != nil {
return block, err
}
if found {
block.BlockTime = int64(block.Timestamp.Sub(prevBlock.Timestamp).Seconds())
}
ogr := t.repoProvider.GetOperationGroup()
ogs, err := ogr.GetGroupsFor(block)
if err != nil {
return block, err
}
block.OperationGroups = ogs
return block, nil
}