Skip to content

Commit bc99456

Browse files
authored
Merge pull request #11 from initia-labs/feat/move-events
Implement move events indexer
2 parents 4e69e56 + e4d0257 commit bc99456

File tree

5 files changed

+540
-5
lines changed

5 files changed

+540
-5
lines changed

informative-indexer/db/db.go

+24
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ func InsertTransactionEventsIgnoreConflict(ctx context.Context, dbTx Queryable,
7272
return BulkInsert(ctx, dbTx, "transaction_events", columns, values, "ON CONFLICT DO NOTHING")
7373
}
7474

75+
func InsertMoveEventsIgnoreConflict(ctx context.Context, dbTx Queryable, moveEvents []*MoveEvent) error {
76+
span := sentry.StartSpan(ctx, "InsertMoveEvents")
77+
span.Description = "Bulk insert move_events into the database"
78+
defer span.Finish()
79+
80+
if len(moveEvents) == 0 {
81+
return nil
82+
}
83+
84+
columns := getColumns(moveEvents[0])
85+
var values [][]interface{}
86+
for _, moveEvent := range moveEvents {
87+
values = append(values, []interface{}{
88+
moveEvent.TypeTag,
89+
moveEvent.Data,
90+
moveEvent.BlockHeight,
91+
moveEvent.TransactionHash,
92+
moveEvent.EventIndex,
93+
})
94+
}
95+
96+
return BulkInsert(ctx, dbTx, "move_events", columns, values, "ON CONFLICT DO NOTHING")
97+
}
98+
7599
func InsertFinalizeBlockEventsIgnoreConflict(ctx context.Context, dbTx Queryable, blockEvents []*FinalizeBlockEvent) error {
76100
span := sentry.StartSpan(ctx, "InsertFinalizeBlockEvents")
77101
span.Description = "Bulk insert finalize_block_events into the database"

informative-indexer/db/move_event.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package db
2+
3+
import (
4+
"github.com/jackc/pgx/v5"
5+
)
6+
7+
type MoveEvent struct {
8+
TypeTag string `json:"type_tag"`
9+
Data string `json:"data"`
10+
BlockHeight int64 `json:"block_height"`
11+
TransactionHash string `json:"transaction_hash"`
12+
EventIndex int `json:"event_index"`
13+
}
14+
15+
func (m *MoveEvent) Unmarshal(rows pgx.Rows) (map[string]interface{}, error) {
16+
err := rows.Scan(&m.TypeTag, &m.Data, &m.BlockHeight, &m.TransactionHash, &m.EventIndex)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
row := map[string]interface{}{
22+
"type_tag": m.TypeTag,
23+
"data": m.Data,
24+
"block_height": m.BlockHeight,
25+
"transaction_hash": m.TransactionHash,
26+
"event_index": m.EventIndex,
27+
}
28+
29+
return row, nil
30+
}

informative-indexer/flusher/block_results.go

+48
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
movetypes "github.com/initia-labs/initia/x/move/types"
78
"github.com/jackc/pgx/v5"
89

910
"github.com/initia-labs/core-indexer/informative-indexer/common"
@@ -44,6 +45,47 @@ func (f *Flusher) parseAndInsertTransactionEvents(parentCtx context.Context, dbT
4445
return nil
4546
}
4647

48+
func (f *Flusher) parseAndInsertMoveEvents(parentCtx context.Context, dbTx pgx.Tx, blockResults *common.BlockResultMsg) error {
49+
span, ctx := common.StartSentrySpan(parentCtx, "parseAndInsertMoveEvents", "Parse block_results message and insert move_events into the database")
50+
defer span.Finish()
51+
52+
moveEvents := make([]*db.MoveEvent, 0)
53+
for _, tx := range blockResults.Txs {
54+
if tx.ExecTxResults.Log == "tx parse error" {
55+
continue
56+
}
57+
58+
// idx ensures EventIndex is unique within each transaction.
59+
idx := 0
60+
for _, event := range tx.ExecTxResults.Events {
61+
if event.Type == movetypes.EventTypeMove {
62+
moveEvent := &db.MoveEvent{
63+
BlockHeight: blockResults.Height,
64+
TransactionHash: tx.Hash,
65+
EventIndex: idx,
66+
}
67+
for _, attr := range event.Attributes {
68+
switch attr.Key {
69+
case movetypes.AttributeKeyTypeTag:
70+
moveEvent.TypeTag = attr.Value
71+
case movetypes.AttributeKeyData:
72+
moveEvent.Data = attr.Value
73+
}
74+
}
75+
moveEvents = append(moveEvents, moveEvent)
76+
idx++
77+
}
78+
}
79+
}
80+
81+
if err := db.InsertMoveEventsIgnoreConflict(ctx, dbTx, moveEvents); err != nil {
82+
logger.Error().Msgf("Error inserting move_events: %v", err)
83+
return err
84+
}
85+
86+
return nil
87+
}
88+
4789
func (f *Flusher) parseAndInsertFinalizeBlockEvents(parentCtx context.Context, dbTx pgx.Tx, blockResults *common.BlockResultMsg) error {
4890
span, ctx := common.StartSentrySpan(parentCtx, "parseAndInsertFinalizeBlockEvents", "Parse block_results message and insert finalize_block_events into the database")
4991
defer span.Finish()
@@ -109,6 +151,12 @@ func (f *Flusher) processBlockResults(parentCtx context.Context, blockResults *c
109151
return err
110152
}
111153

154+
err = f.parseAndInsertMoveEvents(ctx, dbTx, blockResults)
155+
if err != nil {
156+
logger.Error().Int64("height", blockResults.Height).Msgf("Error inserting move_events: %v", err)
157+
return err
158+
}
159+
112160
err = f.parseAndInsertFinalizeBlockEvents(ctx, dbTx, blockResults)
113161
if err != nil {
114162
logger.Error().Int64("height", blockResults.Height).Msgf("Error inserting finalize_block_events: %v", err)

informative-indexer/go.mod

+22-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ require (
4343
filippo.io/edwards25519 v1.1.0 // indirect
4444
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
4545
github.com/99designs/keyring v1.2.2 // indirect
46+
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
4647
github.com/DataDog/zstd v1.5.5 // indirect
4748
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.1 // indirect
4849
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 // indirect
4950
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
51+
github.com/IGLOU-EU/go-wildcard v1.0.3 // indirect
52+
github.com/aptos-labs/serde-reflection/serde-generate/runtime/golang v0.0.0-20231213012317-73b6bbf74833 // indirect
5053
github.com/aws/aws-sdk-go-v2 v1.32.5 // indirect
5154
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect
5255
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 // indirect
@@ -64,6 +67,7 @@ require (
6467
github.com/aws/smithy-go v1.22.1 // indirect
6568
github.com/beorn7/perks v1.0.1 // indirect
6669
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
70+
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
6771
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
6872
github.com/cespare/xxhash/v2 v2.3.0 // indirect
6973
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
@@ -78,31 +82,32 @@ require (
7882
github.com/cosmos/cosmos-db v1.0.2 // indirect
7983
github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect
8084
github.com/cosmos/go-bip39 v1.0.0 // indirect
85+
github.com/cosmos/gogogateway v1.2.0 // indirect
8186
github.com/cosmos/gogoproto v1.7.0 // indirect
8287
github.com/cosmos/iavl v1.2.0 // indirect
8388
github.com/cosmos/ics23/go v0.11.0 // indirect
8489
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
8590
github.com/danieljoos/wincred v1.1.2 // indirect
8691
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
8792
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
93+
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
8894
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
8995
github.com/dgraph-io/ristretto v0.1.1 // indirect
9096
github.com/dustin/go-humanize v1.0.1 // indirect
9197
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
9298
github.com/emicklei/dot v1.6.1 // indirect
9399
github.com/envoyproxy/go-control-plane v0.13.0 // indirect
94100
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
101+
github.com/fatih/color v1.17.0 // indirect
95102
github.com/felixge/httpsnoop v1.0.4 // indirect
96103
github.com/fsnotify/fsnotify v1.7.0 // indirect
97104
github.com/go-kit/kit v0.13.0 // indirect
98105
github.com/go-kit/log v0.2.1 // indirect
99106
github.com/go-logfmt/logfmt v0.6.0 // indirect
100107
github.com/go-logr/logr v1.4.2 // indirect
101108
github.com/go-logr/stdr v1.2.2 // indirect
102-
github.com/go-ole/go-ole v1.3.0 // indirect
103-
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
104109
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
105-
github.com/gofrs/flock v0.12.1 // indirect
110+
github.com/gogo/googleapis v1.4.1 // indirect
106111
github.com/gogo/protobuf v1.3.2 // indirect
107112
github.com/golang/glog v1.2.2 // indirect
108113
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -115,17 +120,26 @@ require (
115120
github.com/google/uuid v1.6.0 // indirect
116121
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
117122
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
123+
github.com/gorilla/handlers v1.5.2 // indirect
124+
github.com/gorilla/mux v1.8.1 // indirect
118125
github.com/gorilla/websocket v1.5.3 // indirect
126+
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
119127
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
120128
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
129+
github.com/hashicorp/go-hclog v1.5.0 // indirect
121130
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
122131
github.com/hashicorp/go-metrics v0.5.3 // indirect
132+
github.com/hashicorp/go-plugin v1.5.2 // indirect
123133
github.com/hashicorp/go-uuid v1.0.3 // indirect
124134
github.com/hashicorp/golang-lru v1.0.2 // indirect
125135
github.com/hashicorp/hcl v1.0.0 // indirect
136+
github.com/hashicorp/yamux v0.1.1 // indirect
126137
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
138+
github.com/huandu/skiplist v1.2.0 // indirect
127139
github.com/iancoleman/strcase v0.3.0 // indirect
140+
github.com/improbable-eng/grpc-web v0.15.0 // indirect
128141
github.com/inconshreveable/mousetrap v1.1.0 // indirect
142+
github.com/initia-labs/movevm v0.6.1 // indirect
129143
github.com/jackc/pgpassfile v1.0.0 // indirect
130144
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
131145
github.com/jackc/puddle/v2 v2.2.2 // indirect
@@ -137,10 +151,12 @@ require (
137151
github.com/magiconair/properties v1.8.7 // indirect
138152
github.com/mattn/go-colorable v0.1.13 // indirect
139153
github.com/mattn/go-isatty v0.0.20 // indirect
154+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
140155
github.com/mitchellh/mapstructure v1.5.0 // indirect
141156
github.com/mtibben/percent v0.2.1 // indirect
142157
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
143158
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
159+
github.com/oklog/run v1.1.0 // indirect
144160
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
145161
github.com/petermattis/goid v0.0.0-20240813172612-4fcff4a6cae7 // indirect
146162
github.com/pkg/errors v0.9.1 // indirect
@@ -152,9 +168,11 @@ require (
152168
github.com/prometheus/procfs v0.15.1 // indirect
153169
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
154170
github.com/rogpeppe/go-internal v1.12.0 // indirect
171+
github.com/rs/cors v1.11.1 // indirect
155172
github.com/sagikazarmark/locafero v0.4.0 // indirect
156173
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
157174
github.com/sasha-s/go-deadlock v0.3.5 // indirect
175+
github.com/skip-mev/connect/v2 v2.0.1 // indirect
158176
github.com/sourcegraph/conc v0.3.0 // indirect
159177
github.com/spf13/afero v1.11.0 // indirect
160178
github.com/spf13/cast v1.7.0 // indirect
@@ -195,6 +213,7 @@ require (
195213
gopkg.in/ini.v1 v1.67.0 // indirect
196214
gopkg.in/yaml.v3 v3.0.1 // indirect
197215
gotest.tools/v3 v3.5.1 // indirect
216+
nhooyr.io/websocket v1.8.6 // indirect
198217
pgregory.net/rapid v1.1.0 // indirect
199218
sigs.k8s.io/yaml v1.4.0 // indirect
200219
)

0 commit comments

Comments
 (0)