-
Notifications
You must be signed in to change notification settings - Fork 17
/
operation_group.go
122 lines (104 loc) · 2.92 KB
/
operation_group.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
116
117
118
119
120
121
122
package operations
import (
"github.com/baking-bad/bcdhub/internal/bcd"
"github.com/baking-bad/bcdhub/internal/bcd/consts"
"github.com/baking-bad/bcdhub/internal/helpers"
"github.com/baking-bad/bcdhub/internal/noderpc"
"github.com/baking-bad/bcdhub/internal/parsers"
)
// Group -
type Group struct {
*ParseParams
}
// NewGroup -
func NewGroup(params *ParseParams) Group {
return Group{params}
}
// Parse -
func (opg Group) Parse(data noderpc.LightOperationGroup, store parsers.Store) error {
opg.hash = data.Hash
helpers.SetTagSentry("hash", opg.hash)
for idx, item := range data.Contents {
opg.contentIdx = int64(idx)
if !opg.needParse(item) {
continue
}
var operation noderpc.Operation
if err := json.Unmarshal(item.Raw, &operation); err != nil {
return err
}
contentParser := NewContent(opg.ParseParams)
if err := contentParser.Parse(operation, store); err != nil {
return err
}
contentParser.clear()
}
return nil
}
func (Group) needParse(item noderpc.LightOperation) bool {
var destination string
if item.Destination != nil {
destination = *item.Destination
}
prefixCondition := bcd.IsContract(item.Source) || bcd.IsContract(destination)
transactionCondition := item.Kind == consts.Transaction && prefixCondition
originationCondition := (item.Kind == consts.Origination || item.Kind == consts.OriginationNew || item.Kind == consts.TxRollupOrigination)
registerGlobalConstantCondition := item.Kind == consts.RegisterGlobalConstant
return originationCondition || transactionCondition || registerGlobalConstantCondition
}
// Content -
type Content struct {
*ParseParams
}
// NewContent -
func NewContent(params *ParseParams) Content {
return Content{params}
}
// Parse -
func (content Content) Parse(data noderpc.Operation, store parsers.Store) error {
switch data.Kind {
case consts.Origination, consts.OriginationNew:
if err := NewOrigination(content.ParseParams).Parse(data, store); err != nil {
return err
}
case consts.Transaction:
if err := NewTransaction(content.ParseParams).Parse(data, store); err != nil {
return err
}
case consts.RegisterGlobalConstant:
if err := NewRegisterGlobalConstant(content.ParseParams).Parse(data, store); err != nil {
return err
}
case consts.TxRollupOrigination:
if err := NewTxRollupOrigination(content.ParseParams).Parse(data, store); err != nil {
return err
}
default:
return nil
}
if err := content.parseInternal(data, store); err != nil {
return err
}
return nil
}
func (content Content) parseInternal(data noderpc.Operation, store parsers.Store) error {
if data.Metadata == nil {
return nil
}
internals := data.Metadata.Internal
if internals == nil {
internals = data.Metadata.InternalOperations
if internals == nil {
return nil
}
}
for i := range internals {
if err := content.Parse(internals[i], store); err != nil {
return err
}
}
return nil
}
func (content *Content) clear() {
content.main = nil
}