Skip to content

Commit

Permalink
Feature: add IBC messages decoding (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Feb 18, 2025
1 parent 5185129 commit ab76113
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 128 deletions.
2 changes: 1 addition & 1 deletion build/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23-alpine as builder
FROM golang:1.23.5-alpine as builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion build/indexer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23-alpine as builder
FROM golang:1.23.5-alpine as builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
2 changes: 1 addition & 1 deletion build/private_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------------------------------
# The first stage container, for building the application
# ---------------------------------------------------------------------
FROM golang:1.23-alpine as builder
FROM golang:1.23.5-alpine as builder

ENV CGO_ENABLED=0
ENV GO111MODULE=on
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/celenium-io/astria-indexer

go 1.23.2
go 1.23.5

require (
buf.build/gen/go/astria/primitives/protocolbuffers/go v1.35.1-20240911152449-eeebd3decdce.1
Expand All @@ -12,10 +12,11 @@ require (
github.com/dipdup-io/workerpool v0.0.4
github.com/dipdup-net/go-lib v0.3.6
github.com/dipdup-net/indexer-sdk v0.0.7
github.com/getsentry/sentry-go v0.30.0
github.com/fatih/structs v1.1.0
github.com/getsentry/sentry-go v0.28.1
github.com/go-playground/validator/v10 v10.15.0
github.com/go-testfixtures/testfixtures/v3 v3.9.0
github.com/goccy/go-json v0.10.2
github.com/goccy/go-json v0.9.11
github.com/gogo/protobuf v1.3.2
github.com/gorilla/websocket v1.5.3
github.com/gosimple/slug v1.14.0
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand All @@ -280,8 +282,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo=
github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -348,8 +350,8 @@ github.com/go-testfixtures/testfixtures/v3 v3.9.0/go.mod h1:cdsKD2ApFBjdog9jRsz6
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0=
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand All @@ -372,8 +374,6 @@ github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EO
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.2.4 h1:CNNw5U8lSiiBk7druxtSHHTsRWcxKoac6kZKm2peBBc=
github.com/golang/glog v1.2.4/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/glog v1.2.3 h1:oDTdz9f5VGVVNGu/Q7UXKWYsD0873HXLHdJUNBsSEKM=
github.com/golang/glog v1.2.3/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
97 changes: 2 additions & 95 deletions pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ import (

primitive "buf.build/gen/go/astria/primitives/protocolbuffers/go/astria/primitive/v1"
astria "buf.build/gen/go/astria/protocol-apis/protocolbuffers/go/astria/protocol/transaction/v1alpha1"
internalAstria "github.com/celenium-io/astria-indexer/internal/astria"
"github.com/celenium-io/astria-indexer/internal/currency"
"github.com/celenium-io/astria-indexer/internal/storage"
storageTypes "github.com/celenium-io/astria-indexer/internal/storage/types"
"github.com/celenium-io/astria-indexer/pkg/types"
channelTypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
"github.com/goccy/go-json"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
)
Expand Down Expand Up @@ -159,97 +155,8 @@ func parseIbcAction(body *astria.Action_Ibc, ctx *Context, action *storage.Actio
return nil
}

switch typ {
case "/ibc.core.channel.v1.MsgRecvPacket":
var msg channelTypes.MsgRecvPacket
if err := proto.Unmarshal(data, &msg); err != nil {
return err
}
var transfer IbcTransfer
if err := json.Unmarshal(msg.Packet.Data, &transfer); err != nil {
return nil
}
asset := fmt.Sprintf("%s/%s/%s", msg.Packet.GetDestPort(), msg.Packet.GetDestChannel(), transfer.Denom)
var addr string
var amount decimal.Decimal

switch {
case internalAstria.IsAddress(transfer.Receiver):
addr = transfer.Receiver
amount = transfer.Amount.Copy()
case internalAstria.IsCompatAddress(transfer.Receiver):
a, err := internalAstria.CompatToAstria(transfer.Receiver)
if err != nil {
return err
}
addr = a
amount = transfer.Amount.Copy()
case internalAstria.IsAddress(transfer.Sender):
addr = transfer.Sender
amount = transfer.Amount.Neg()
case internalAstria.IsCompatAddress(transfer.Sender):
a, err := internalAstria.CompatToAstria(transfer.Sender)
if err != nil {
return err
}
addr = a
amount = transfer.Amount.Neg()
}

if addr != "" {
address := ctx.Addresses.Set(addr, action.Height, amount, asset, 0, 0)
action.BalanceUpdates = append(action.BalanceUpdates, storage.BalanceUpdate{
Address: address,
Height: action.Height,
Currency: asset,
Update: amount,
})
}
case "/ibc.core.channel.v1.MsgTimeout":
var msg channelTypes.MsgTimeout
if err := proto.Unmarshal(data, &msg); err != nil {
return err
}
var transfer IbcTransfer
if err := json.Unmarshal(msg.Packet.Data, &transfer); err != nil {
return nil
}
var addr string
var amount decimal.Decimal

switch {
case internalAstria.IsAddress(transfer.Receiver):
addr = transfer.Receiver
amount = transfer.Amount.Neg()
case internalAstria.IsCompatAddress(transfer.Receiver):
a, err := internalAstria.CompatToAstria(transfer.Receiver)
if err != nil {
return err
}
addr = a
amount = transfer.Amount.Neg()
case internalAstria.IsAddress(transfer.Sender):
addr = transfer.Sender
amount = transfer.Amount.Copy()
case internalAstria.IsCompatAddress(transfer.Sender):
a, err := internalAstria.CompatToAstria(transfer.Sender)
if err != nil {
return err
}
addr = a
amount = transfer.Amount.Copy()
}

if addr != "" {
address := ctx.Addresses.Set(addr, action.Height, amount, transfer.Denom, 0, 0)
action.BalanceUpdates = append(action.BalanceUpdates, storage.BalanceUpdate{
Address: address,
Height: action.Height,
Currency: transfer.Denom,
Update: amount,
})
}
default:
if err := parseIbcMessages(typ, data, action, ctx); err != nil {
return errors.Wrap(err, "parse ibc message")
}
}
return nil
Expand Down
32 changes: 12 additions & 20 deletions pkg/indexer/decode/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ func TestDecodeActions(t *testing.T) {
wantAction := storage.Action{
Height: 1000,
Type: types.ActionTypeIbcRelay,
Data: map[string]any{
"raw": raw,
"type": "/ibc.core.channel.v1.MsgRecvPacket",
},
BalanceUpdates: []storage.BalanceUpdate{
{
Height: 1000,
Expand All @@ -105,7 +101,9 @@ func TestDecodeActions(t *testing.T) {
ctx := NewContext(nil)
err = parseIbcAction(message, &ctx, &action)
require.NoError(t, err)
require.Equal(t, wantAction, action)
require.Equal(t, wantAction.Height, action.Height)
require.Equal(t, wantAction.Type, action.Type)
require.Equal(t, wantAction.BalanceUpdates, action.BalanceUpdates)
require.Len(t, ctx.Addresses, 1)
})

Expand All @@ -126,10 +124,6 @@ func TestDecodeActions(t *testing.T) {
wantAction := storage.Action{
Height: 1000,
Type: types.ActionTypeIbcRelay,
Data: map[string]any{
"raw": raw,
"type": "/ibc.core.channel.v1.MsgTimeout",
},
BalanceUpdates: []storage.BalanceUpdate{
{
Height: 1000,
Expand All @@ -155,7 +149,9 @@ func TestDecodeActions(t *testing.T) {
ctx := NewContext(nil)
err = parseIbcAction(message, &ctx, &action)
require.NoError(t, err)
require.Equal(t, wantAction, action)
require.Equal(t, wantAction.Height, action.Height)
require.Equal(t, wantAction.Type, action.Type)
require.Equal(t, wantAction.BalanceUpdates, action.BalanceUpdates)
require.Len(t, ctx.Addresses, 1)
})

Expand All @@ -176,10 +172,6 @@ func TestDecodeActions(t *testing.T) {
wantAction := storage.Action{
Height: 1000,
Type: types.ActionTypeIbcRelay,
Data: map[string]any{
"raw": raw,
"type": "/ibc.core.channel.v1.MsgRecvPacket",
},
}

action := storage.Action{
Expand All @@ -188,7 +180,9 @@ func TestDecodeActions(t *testing.T) {
ctx := NewContext(nil)
err = parseIbcAction(message, &ctx, &action)
require.NoError(t, err)
require.Equal(t, wantAction, action)
require.Equal(t, wantAction.Height, action.Height)
require.Equal(t, wantAction.Type, action.Type)
require.Equal(t, wantAction.BalanceUpdates, action.BalanceUpdates)
require.Len(t, ctx.Addresses, 0)
})

Expand All @@ -209,10 +203,6 @@ func TestDecodeActions(t *testing.T) {
wantAction := storage.Action{
Height: 1000,
Type: types.ActionTypeIbcRelay,
Data: map[string]any{
"raw": raw,
"type": "/ibc.core.channel.v1.MsgRecvPacket",
},
BalanceUpdates: []storage.BalanceUpdate{
{
Height: 1000,
Expand All @@ -238,7 +228,9 @@ func TestDecodeActions(t *testing.T) {
ctx := NewContext(nil)
err = parseIbcAction(message, &ctx, &action)
require.NoError(t, err)
require.Equal(t, wantAction, action)
require.Equal(t, wantAction.Height, action.Height)
require.Equal(t, wantAction.Type, action.Type)
require.Equal(t, wantAction.BalanceUpdates, action.BalanceUpdates)
require.Len(t, ctx.Addresses, 1)
})

Expand Down
Loading

0 comments on commit ab76113

Please sign in to comment.