Skip to content

Commit 083fc97

Browse files
Add register_global_constant
1 parent 90d7908 commit 083fc97

File tree

7 files changed

+138
-99
lines changed

7 files changed

+138
-99
lines changed

cmd/mempool/config/validators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (cfg MempoolDataSource) Validate() error {
4141
func (cfg Filters) Validate() error {
4242
switch {
4343
case len(cfg.Kinds) == 0:
44-
cfg.Kinds = append(cfg.Kinds, node.KindTransaction)
44+
cfg.Kinds = []string{node.KindTransaction}
4545
default:
4646
if err := validateKinds(cfg.Kinds...); err != nil {
4747
return err
@@ -62,7 +62,7 @@ func validateKinds(kinds ...string) error {
6262
for _, valid := range []string{
6363
node.KindActivation, node.KindBallot, node.KindDelegation, node.KindDoubleBaking, node.KindDoubleEndorsing,
6464
node.KindEndorsement, node.KindNonceRevelation, node.KindOrigination, node.KindProposal,
65-
node.KindReveal, node.KindTransaction,
65+
node.KindReveal, node.KindTransaction, node.KindRegisterGlobalConstant,
6666
} {
6767
if kind == valid {
6868
found = true

cmd/mempool/handlers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ func (indexer *Indexer) handleContent(db *gorm.DB, content node.Content, operati
178178
return handleReveal(db, content, operation, indexer.filters.Accounts...)
179179
case node.KindTransaction:
180180
return handleTransaction(db, content, operation, indexer.filters.Accounts...)
181+
case node.KindRegisterGlobalConstant:
182+
return handleRegisterGloabalConstant(db, content, operation)
181183
default:
182184
return errors.Wrap(node.ErrUnknownKind, content.Kind)
183185
}
@@ -345,6 +347,15 @@ func handleProposal(db *gorm.DB, content node.Content, operation models.MempoolO
345347
return nil
346348
}
347349

350+
func handleRegisterGloabalConstant(db *gorm.DB, content node.Content, operation models.MempoolOperation) error {
351+
var registerGlobalConstant models.RegisterGlobalConstant
352+
if err := json.Unmarshal(content.Body, &registerGlobalConstant); err != nil {
353+
return err
354+
}
355+
registerGlobalConstant.MempoolOperation = operation
356+
return db.Clauses(clause.OnConflict{DoNothing: true}).Create(&registerGlobalConstant).Error
357+
}
358+
348359
func (indexer *Indexer) isKindAvailiable(kind string) bool {
349360
for _, availiable := range indexer.filters.Kinds {
350361
if strings.HasPrefix(kind, availiable) {

cmd/mempool/models/mempool_operation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ func getModelByKind(kind string) (interface{}, error) {
192192
return &Reveal{}, nil
193193
case node.KindTransaction:
194194
return &Transaction{}, nil
195+
case node.KindRegisterGlobalConstant:
196+
return &RegisterGlobalConstant{}, nil
195197
default:
196198
return nil, errors.Wrap(node.ErrUnknownKind, kind)
197199
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package models
2+
3+
import "gorm.io/datatypes"
4+
5+
// RegisterGlobalConstant -
6+
type RegisterGlobalConstant struct {
7+
MempoolOperation
8+
Source string `json:"source"`
9+
Fee string `json:"fee"`
10+
Counter string `json:"counter"`
11+
GasLimit string `json:"gas_limit"`
12+
StorageLimit string `json:"storage_limit"`
13+
Value datatypes.JSON `json:"value"`
14+
}
15+
16+
// TableName -
17+
func (RegisterGlobalConstant) TableName() string {
18+
return "register_global_constant"
19+
}

cmd/mempool/tzkt/tzkt.go

Lines changed: 87 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,16 @@ func (tzkt *TzKT) Blocks() <-chan BlockMessage {
114114
func (tzkt *TzKT) handleBlockMessage(msg events.Message) error {
115115
switch msg.Type {
116116
case events.MessageTypeData:
117-
data, ok := msg.Body.([]interface{})
118-
if !ok {
119-
return errors.Wrapf(ErrInvalidBlockType, "%v", msg.Body)
120-
}
121-
if len(data) == 0 {
117+
if msg.Body == nil {
122118
return nil
123119
}
124-
blockData, ok := data[0].(map[string]interface{})
125-
if !ok {
126-
return errors.Wrapf(ErrInvalidBlockType, "%v", data[0])
127-
}
128-
hash, err := getString(blockData, "hash")
129-
if err != nil {
130-
return err
131-
}
132-
level, err := getUint64(blockData, "level")
133-
if err != nil {
134-
return err
135-
}
136-
tzkt.blocks <- BlockMessage{
137-
Hash: hash,
138-
Level: level,
139-
Type: msg.Type,
120+
blocks := msg.Body.([]events.Block)
121+
for i := range blocks {
122+
tzkt.blocks <- BlockMessage{
123+
Hash: blocks[i].Hash,
124+
Level: blocks[i].Level,
125+
Type: msg.Type,
126+
}
140127
}
141128
case events.MessageTypeState, events.MessageTypeReorg:
142129
tzkt.blocks <- BlockMessage{
@@ -153,7 +140,11 @@ func (tzkt *TzKT) handleBlockMessage(msg events.Message) error {
153140
func (tzkt *TzKT) handleOperationMessage(msg events.Message) error {
154141
switch msg.Type {
155142
case events.MessageTypeData:
156-
return tzkt.handleUpdateMessage(msg)
143+
if msg.Body == nil {
144+
return nil
145+
}
146+
operations := msg.Body.([]interface{})
147+
return tzkt.handleUpdateMessage(operations)
157148
case events.MessageTypeState, events.MessageTypeReorg:
158149
default:
159150
return errors.Wrapf(ErrUnknownMessageType, "%d", msg.Type)
@@ -162,19 +153,11 @@ func (tzkt *TzKT) handleOperationMessage(msg events.Message) error {
162153
return nil
163154
}
164155

165-
func (tzkt *TzKT) handleUpdateMessage(msg events.Message) error {
156+
func (tzkt *TzKT) handleUpdateMessage(operations []interface{}) error {
166157
message := newOperationMessage()
167158

168-
body, ok := msg.Body.([]interface{})
169-
if !ok {
170-
return errors.Wrapf(ErrInvalidBodyType, "%T", msg.Body)
171-
}
172-
for i := range body {
173-
operation, ok := body[i].(map[string]interface{})
174-
if !ok {
175-
return errors.Wrapf(ErrInvalidOperationType, "%T", body[i])
176-
}
177-
if err := tzkt.processOperation(operation, &message); err != nil {
159+
for i := range operations {
160+
if err := tzkt.processOperation(operations[i], &message); err != nil {
178161
return err
179162
}
180163
}
@@ -184,33 +167,81 @@ func (tzkt *TzKT) handleUpdateMessage(msg events.Message) error {
184167
return nil
185168
}
186169

187-
func getString(data map[string]interface{}, key string) (string, error) {
188-
value, ok := data[key]
189-
if !ok {
190-
return "", errors.Wrapf(ErrOperationDoesNotContain, "field=%s data=%v", key, data)
191-
}
192-
s, ok := value.(string)
193-
if !ok {
194-
return "", errors.Wrapf(ErrInvalidFieldType, "field=%s expected_type=string type=%T data=%v", key, value, value)
195-
}
196-
return s, nil
197-
}
170+
func (tzkt *TzKT) getAPIOperation(data interface{}) (api.Operation, error) {
171+
switch operation := data.(type) {
172+
173+
case *events.Delegation:
174+
tx := api.Operation{
175+
ID: operation.ID,
176+
Level: operation.Level,
177+
Hash: operation.Hash,
178+
Kind: toNodeKinds[operation.Type],
179+
Block: operation.Block,
180+
GasUsed: &operation.GasUsed,
181+
BakerFee: &operation.BakerFee,
182+
}
183+
if operation.NewDelegate != nil {
184+
tx.Delegate = &api.Address{
185+
Alias: operation.NewDelegate.Alias,
186+
Address: operation.NewDelegate.Address,
187+
}
188+
}
189+
return tx, nil
190+
191+
case *events.Origination:
192+
tx := api.Operation{
193+
ID: operation.ID,
194+
Level: operation.Level,
195+
Hash: operation.Hash,
196+
Kind: toNodeKinds[operation.Type],
197+
Block: operation.Block,
198+
GasUsed: &operation.GasUsed,
199+
BakerFee: &operation.BakerFee,
200+
}
201+
return tx, nil
202+
203+
case *events.Reveal:
204+
tx := api.Operation{
205+
ID: operation.ID,
206+
Level: operation.Level,
207+
Hash: operation.Hash,
208+
Kind: toNodeKinds[operation.Type],
209+
Block: operation.Block,
210+
GasUsed: &operation.GasUsed,
211+
BakerFee: &operation.BakerFee,
212+
}
213+
return tx, nil
214+
215+
case *events.Transaction:
216+
tx := api.Operation{
217+
ID: operation.ID,
218+
Level: operation.Level,
219+
Hash: operation.Hash,
220+
Kind: toNodeKinds[operation.Type],
221+
Block: operation.Block,
222+
GasUsed: &operation.GasUsed,
223+
BakerFee: &operation.BakerFee,
224+
}
225+
if operation.Parameter != nil {
226+
tx.Parameters = &api.Parameters{
227+
Entrypoint: operation.Parameter.Entrypoint,
228+
Value: operation.Parameter.Value,
229+
}
230+
}
231+
return tx, nil
198232

199-
func getUint64(data map[string]interface{}, key string) (uint64, error) {
200-
value, ok := data[key]
201-
if !ok {
202-
return 0, errors.Wrapf(ErrOperationDoesNotContain, "field=%s data=%v", key, data)
203-
}
204-
f, ok := value.(float64)
205-
if !ok {
206-
return 0, errors.Wrapf(ErrInvalidFieldType, "field=%s expected_type=float64 type=%T data=%v", key, value, value)
233+
case map[string]interface{}:
234+
var general api.Operation
235+
err := mapstructure.Decode(data, &general)
236+
return general, err
237+
default:
238+
return api.Operation{}, errors.Wrapf(ErrInvalidOperationType, "%T", data)
207239
}
208-
return uint64(f), nil
209240
}
210241

211-
func (tzkt *TzKT) processOperation(data map[string]interface{}, message *OperationMessage) error {
212-
var operation api.Operation
213-
if err := mapstructure.Decode(data, &operation); err != nil {
242+
func (tzkt *TzKT) processOperation(data interface{}, message *OperationMessage) error {
243+
operation, err := tzkt.getAPIOperation(data)
244+
if err != nil {
214245
return err
215246
}
216247
operation.Kind = toNodeKinds[operation.Kind]

go.mod

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ module github.com/dipdup-net/mempool
33
go 1.15
44

55
require (
6-
github.com/allegro/bigcache v1.2.1 // indirect
76
github.com/btcsuite/btcutil v1.0.2
8-
github.com/dipdup-net/go-lib v0.1.26
9-
github.com/gofrs/uuid v4.0.0+incompatible // indirect
7+
github.com/dipdup-net/go-lib v0.1.30
108
github.com/json-iterator/go v1.1.12 // indirect
119
github.com/karlseguin/ccache v2.0.3+incompatible
1210
github.com/karlseguin/expect v1.0.8 // indirect
@@ -15,13 +13,12 @@ require (
1513
github.com/pkg/errors v0.9.1
1614
github.com/sirupsen/logrus v1.8.1
1715
github.com/stretchr/testify v1.7.0
18-
github.com/syndtr/goleveldb v1.0.0 // indirect
1916
github.com/ubiq/go-ubiq v3.0.1+incompatible
2017
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
21-
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect
18+
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
2219
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2320
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2421
gorm.io/datatypes v1.0.2
25-
gorm.io/driver/postgres v1.1.1 // indirect
26-
gorm.io/gorm v1.21.15
22+
gorm.io/driver/postgres v1.1.2 // indirect
23+
gorm.io/gorm v1.21.16
2724
)

0 commit comments

Comments
 (0)