From b013f9dfaa10f8aa0e762fe084b2616d33ce8a5f Mon Sep 17 00:00:00 2001 From: Artem Poltorzhitskiy Date: Sun, 27 Oct 2024 11:07:58 +0100 Subject: [PATCH] Fix: position in transaction (#57) --- pkg/indexer/decode/actions.go | 50 +++++++++++++------------- pkg/indexer/decode/context.go | 14 +++----- pkg/indexer/decode/tx.go | 5 +-- pkg/indexer/parser/parseEvents.go | 15 ++++---- pkg/indexer/parser/parseEvents_test.go | 41 +++++++++++++++++++++ 5 files changed, 76 insertions(+), 49 deletions(-) diff --git a/pkg/indexer/decode/actions.go b/pkg/indexer/decode/actions.go index c96e042..2c3a734 100644 --- a/pkg/indexer/decode/actions.go +++ b/pkg/indexer/decode/actions.go @@ -22,7 +22,7 @@ import ( "github.com/shopspring/decimal" ) -func parseActions(height types.Level, blockTime time.Time, from, hash string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) { +func parseActions(height types.Level, blockTime time.Time, from string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) { var ( rawActions = tx.UnsignedTx.GetActions() actions = make([]storage.Action, len(rawActions)) @@ -109,32 +109,30 @@ func parseActions(height types.Level, blockTime time.Time, from, hash string, tx return nil, err } - if txFees, ok := ctx.Fees[hash]; ok { - if actionFee, ok := txFees[int64(i)]; ok { - actionFee.Height = height - actionFee.Time = blockTime - actionFee.Payer = &storage.Address{ - Hash: from, - } - actions[i].Fee = actionFee - - fromAmount := actionFee.Amount.Neg() - addr := ctx.Addresses.Set(from, height, fromAmount, actionFee.Asset, 0, 0) - actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{ - Address: addr, - Height: actions[i].Height, - Currency: actionFee.Asset, - Update: fromAmount, - }) - - to := ctx.Addresses.Set(ctx.Proposer, height, actionFee.Amount, actionFee.Asset, 0, 0) - actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{ - Address: to, - Height: actions[i].Height, - Currency: actionFee.Asset, - Update: actionFee.Amount, - }) + if actionFee, ok := ctx.Fees[int64(i)]; ok { + actionFee.Height = height + actionFee.Time = blockTime + actionFee.Payer = &storage.Address{ + Hash: from, } + actions[i].Fee = actionFee + + fromAmount := actionFee.Amount.Neg() + addr := ctx.Addresses.Set(from, height, fromAmount, actionFee.Asset, 0, 0) + actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{ + Address: addr, + Height: actions[i].Height, + Currency: actionFee.Asset, + Update: fromAmount, + }) + + to := ctx.Addresses.Set(ctx.Proposer, height, actionFee.Amount, actionFee.Asset, 0, 0) + actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{ + Address: to, + Height: actions[i].Height, + Currency: actionFee.Asset, + Update: actionFee.Amount, + }) } if deposit, ok := ctx.Deposits[int64(i)]; ok { diff --git a/pkg/indexer/decode/context.go b/pkg/indexer/decode/context.go index a1ae863..513c8f1 100644 --- a/pkg/indexer/decode/context.go +++ b/pkg/indexer/decode/context.go @@ -25,7 +25,7 @@ type Context struct { ActionTypes storageTypes.Bits Constants map[string]*storage.Constant Bridges map[string]*storage.Bridge - Fees map[string]map[int64]*storage.Fee + Fees map[int64]*storage.Fee Transfers []*storage.Transfer Deposits map[int64]*storage.Deposit Proposer string @@ -42,7 +42,7 @@ func NewContext(bridgeAssets map[string]string) Context { Validators: NewValidators(), Constants: make(map[string]*storage.Constant), Bridges: make(map[string]*storage.Bridge), - Fees: make(map[string]map[int64]*storage.Fee), + Fees: make(map[int64]*storage.Fee), Transfers: make([]*storage.Transfer, 0), Deposits: make(map[int64]*storage.Deposit), @@ -79,14 +79,8 @@ func (ctx *Context) BridgesArray() []*storage.Bridge { return arr } -func (ctx *Context) AddFee(hash string, idx int64, fee *storage.Fee) { - if txFees, ok := ctx.Fees[hash]; ok { - txFees[idx] = fee - } else { - ctx.Fees[hash] = map[int64]*storage.Fee{ - idx: fee, - } - } +func (ctx *Context) AddFee(idx int64, fee *storage.Fee) { + ctx.Fees[idx] = fee } func (ctx *Context) AddBridgeAsset(bridge, asset string) { diff --git a/pkg/indexer/decode/tx.go b/pkg/indexer/decode/tx.go index 1d6962e..22577b9 100644 --- a/pkg/indexer/decode/tx.go +++ b/pkg/indexer/decode/tx.go @@ -4,8 +4,6 @@ package decode import ( - "encoding/hex" - astria "buf.build/gen/go/astria/protocol-apis/protocolbuffers/go/astria/protocol/transaction/v1alpha1" "github.com/celenium-io/astria-indexer/internal/storage" storageTypes "github.com/celenium-io/astria-indexer/internal/storage/types" @@ -50,8 +48,7 @@ func Tx(b types.BlockData, index int, ctx *Context) (d DecodedTx, err error) { d.Signer = ctx.Addresses.Set(address, b.Height, decimal.Zero, "", 0, 1) ctx.Addresses.UpdateNonce(address, d.UnsignedTx.GetParams().GetNonce()) - hash := hex.EncodeToString(b.Block.Txs[index].Hash()) - d.Actions, err = parseActions(b.Height, b.Block.Time, address, hash, &d, ctx) + d.Actions, err = parseActions(b.Height, b.Block.Time, address, &d, ctx) if err != nil { return d, errors.Wrap(err, "parsing actions") } diff --git a/pkg/indexer/parser/parseEvents.go b/pkg/indexer/parser/parseEvents.go index c31b4cf..8cedfd1 100644 --- a/pkg/indexer/parser/parseEvents.go +++ b/pkg/indexer/parser/parseEvents.go @@ -74,10 +74,9 @@ func getAsset(ctx context.Context, api node.Api, val string) (string, error) { func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *decode.Context, api node.Api) error { var ( - fee = new(storage.Fee) - err error - idx int64 - hash string + fee = new(storage.Fee) + err error + idx int64 ) for i := range attrs { switch attrs[i].Key { @@ -92,11 +91,9 @@ func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *d if err != nil { return err } - case "actionType": + case "actionName": fee.ActionType = attrs[i].Value - case "sourceTransactionId": - hash = attrs[i].Value - case "sourceActionIndex": + case "positionInTransaction": actionIndex, err := strconv.ParseInt(attrs[i].Value, 10, 64) if err != nil { return err @@ -106,7 +103,7 @@ func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *d } } - decodeCtx.AddFee(hash, idx, fee) + decodeCtx.AddFee(idx, fee) return nil } diff --git a/pkg/indexer/parser/parseEvents_test.go b/pkg/indexer/parser/parseEvents_test.go index 3d928da..f2f3fd8 100644 --- a/pkg/indexer/parser/parseEvents_test.go +++ b/pkg/indexer/parser/parseEvents_test.go @@ -4,11 +4,14 @@ package parser import ( + "context" "testing" "github.com/celenium-io/astria-indexer/pkg/indexer/decode" + "github.com/celenium-io/astria-indexer/pkg/node/mock" "github.com/celenium-io/astria-indexer/pkg/types" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" ) func Test_parseTxDeposit(t *testing.T) { @@ -51,3 +54,41 @@ func Test_parseTxDeposit(t *testing.T) { require.EqualValues(t, "0x9d0CEC7BEB948Ab046e8b64E9aa6Cc9b731A9613", deposit.DestinationChainAddress) }) } + +func Test_parseTxFee(t *testing.T) { + t.Run("test fee event", func(t *testing.T) { + attrs := []types.EventAttribute{ + { + Key: "actionName", + Value: "astria.protocol.transaction.v1.RollupDataSubmission", + }, { + Key: "asset", + Value: "ibc/704031c868fd3d3c84a1cfa8cb45deba4ea746b44697f7f4a6ed1b8f6c239b82", + }, { + Key: "feeAmount", + Value: "321", + }, { + Key: "positionInTransaction", + Value: "0", + }, + } + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + api := mock.NewMockApi(ctrl) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + decodeCtx := decode.NewContext(map[string]string{}) + err := parseTxFees(ctx, attrs, &decodeCtx, api) + require.NoError(t, err) + require.Len(t, decodeCtx.Fees, 1) + + fee, ok := decodeCtx.Fees[0] + require.True(t, ok) + require.EqualValues(t, "321", fee.Amount.String()) + require.EqualValues(t, "nria", fee.Asset) + require.EqualValues(t, "astria.protocol.transaction.v1.RollupDataSubmission", fee.ActionType) + }) +}