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
/
operations.go
115 lines (99 loc) · 3.63 KB
/
operations.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
110
111
112
113
114
115
package services
import (
"strconv"
"github.com/everstake/teztracker/models"
"github.com/guregu/null"
)
// GetOperations gets the operations filtering by operation kinds and blocks wiht pagination.
func (t *TezTracker) GetOperations(ids, kinds, inBlocks, accountIDs []string, limits Limiter, before int64) (operations []models.Operation, count int64, err error) {
lastBlock, err := t.repoProvider.GetBlock().Last()
if err != nil {
return nil, 0, err
}
r := t.repoProvider.GetOperation()
if len(ids) == 0 && len(inBlocks) == 0 && len(accountIDs) == 0 {
repo := t.repoProvider.GetOperationCounter()
count, err = repo.OperationsCount(kinds)
if err != nil {
return nil, 0, err
}
} else {
count, err = r.Count(ids, kinds, inBlocks, accountIDs, 0)
if err != nil {
return nil, 0, err
}
}
operations, err = r.List(ids, kinds, inBlocks, accountIDs, limits.Limit(), limits.Offset(), before, nil)
if err != nil {
return nil, 0, err
}
for i := range operations {
switch operations[i].Kind.String {
case string(models.DoubleOperationTypeEndorsement), string(models.DoubleOperationTypeBaking):
r := t.repoProvider.GetDoubleEndorsement()
options := models.DoubleOperationEvidenceQueryOptions{
OperationIDs: []int64{operations[i].OperationID.Int64},
Type: models.DoubleOperationType(operations[i].Kind.String),
Limit: limits.Limit(),
Offset: limits.Offset(),
}
count, doubleOperation, err := r.List(options)
if err != nil {
return nil, 0, err
}
if count == 1 {
operations[i].DoubleOperationEvidenceExtended = &doubleOperation[0]
}
}
operations[i].Confirmations = lastBlock.MetaLevel - operations[i].BlockLevel.Int64
}
return operations, count, err
}
// GetBlockEndorsements finds a block and returns endorsements for it.
func (t *TezTracker) GetBlockEndorsements(hashOrLevel string) (operations []models.Operation, count int64, 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.Find(filter)
if err != nil {
return nil, 0, err
}
if !found {
return nil, 0, ErrNotFound
}
or := t.repoProvider.GetOperation()
operations, err = or.EndorsementsFor(block.Level.Int64)
return operations, int64(len(operations)), err
}
// GetOperations gets the operations filtering by operation kinds and blocks wiht pagination.
func (t *TezTracker) GetDoubleBakings(hashes, inBlocks []string, limits Limiter) (operations []models.DoubleOperationEvidenceExtended, count int64, err error) {
r := t.repoProvider.GetDoubleBaking()
options := models.DoubleOperationEvidenceQueryOptions{
BlockIDs: inBlocks,
OperationHashes: hashes,
Type: models.DoubleOperationTypeBaking,
LoadOperation: true,
Limit: limits.Limit(),
Offset: limits.Offset(),
}
count, operations, err = r.List(options)
return operations, count, err
}
// GetOperations gets the operations filtering by operation kinds and blocks wiht pagination.
func (t *TezTracker) GetDoubleEndorsements(hashes, inBlocks []string, limits Limiter) (operations []models.DoubleOperationEvidenceExtended, count int64, err error) {
r := t.repoProvider.GetDoubleEndorsement()
options := models.DoubleOperationEvidenceQueryOptions{
BlockIDs: inBlocks,
OperationHashes: hashes,
Type: models.DoubleOperationTypeEndorsement,
LoadOperation: true,
Limit: limits.Limit(),
Offset: limits.Offset(),
}
count, operations, err = r.List(options)
return operations, count, err
}