Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fee parsing #53

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"github.com/shopspring/decimal"
)

func parseActions(height types.Level, blockTime time.Time, from string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) {
func parseActions(height types.Level, blockTime time.Time, from, hash string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) {
var (
feeCounter = 0
rawActions = tx.UnsignedTx.GetActions()
actions = make([]storage.Action, len(rawActions))
)
Expand All @@ -36,80 +35,65 @@ func parseActions(height types.Level, blockTime time.Time, from string, tx *Deco
actions[i].BalanceUpdates = make([]storage.BalanceUpdate, 0)

var (
err error
feeType string
err error
)

switch val := rawActions[i].GetValue().(type) {
case *astria.Action_Ibc:
tx.ActionTypes.Set(storageTypes.ActionTypeIbcRelayBits)
err = parseIbcAction(val, &actions[i])
feeType = "penumbra.core.component.ibc.v1.IbcRelay"

case *astria.Action_Ics20Withdrawal:
tx.ActionTypes.Set(storageTypes.ActionTypeIcs20WithdrawalBits)
err = parseIcs20Withdrawal(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.Ics20Withdrawal"

case *astria.Action_RollupDataSubmission:
tx.ActionTypes.Set(storageTypes.ActionTypeRollupDataSubmissionBits)
err = parseRollupDataSubmission(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.RollupDataSubmission"

case *astria.Action_SudoAddressChange:
tx.ActionTypes.Set(storageTypes.ActionTypeSudoAddressChangeBits)
err = parseSudoAddressChangeAction(val, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.SudoAddressChange"

case *astria.Action_Transfer:
tx.ActionTypes.Set(storageTypes.ActionTypeTransferBits)
err = parseTransferAction(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.Transfer"

case *astria.Action_ValidatorUpdate:
tx.ActionTypes.Set(storageTypes.ActionTypeValidatorUpdateBits)
err = parseValidatorUpdateAction(val, height, ctx, &actions[i])
feeType = "tendermint.abci.ValidatorUpdate"

case *astria.Action_BridgeLock:
tx.ActionTypes.Set(storageTypes.ActionTypeBridgeLockBits)
err = parseBridgeLock(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.BridgeLock"

case *astria.Action_FeeAssetChange:
tx.ActionTypes.Set(storageTypes.ActionTypeFeeAssetChangeBits)
err = parseFeeAssetChange(val, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.FeeAssetChange"

case *astria.Action_IbcRelayerChange:
tx.ActionTypes.Set(storageTypes.ActionTypeIbcRelayerChangeBits)
err = parseIbcRelayerChange(val, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.IbcRelayerChange"

case *astria.Action_InitBridgeAccount:
tx.ActionTypes.Set(storageTypes.ActionTypeInitBridgeAccountBits)
err = parseInitBridgeAccount(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.InitBridgeAccount"

case *astria.Action_BridgeSudoChange:
tx.ActionTypes.Set(storageTypes.ActionTypeBridgeSudoChangeBits)
err = parseBridgeSudoChange(val, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.BridgeSudoChange"

case *astria.Action_BridgeUnlock:
tx.ActionTypes.Set(storageTypes.ActionTypeBridgeUnlockBits)
err = parseBridgeUnlock(val, from, height, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.BridgeUnlock"

case *astria.Action_FeeChange:
tx.ActionTypes.Set(storageTypes.ActionTypeFeeChangeBits)
err = parseFeeChange(val, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.FeeChange"

case *astria.Action_IbcSudoChange:
tx.ActionTypes.Set(storageTypes.ActionTypeIbcSudoChangeBits)
err = parseIbcSudoChangeAction(val, ctx, &actions[i])
feeType = "astria.protocol.transaction.v1alpha1.IbcSudoChange"

default:
return nil, errors.Errorf(
Expand All @@ -121,32 +105,31 @@ func parseActions(height types.Level, blockTime time.Time, from string, tx *Deco
return nil, err
}

// merge fees
if len(ctx.Fees) > feeCounter {
if ctx.Fees[feeCounter].ActionType == feeType {
ctx.Fees[feeCounter].Height = height
ctx.Fees[feeCounter].Time = blockTime
ctx.Fees[feeCounter].Payer = &storage.Address{
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 = ctx.Fees[feeCounter]
fromAmount := ctx.Fees[feeCounter].Amount.Neg()
addr := ctx.Addresses.Set(from, height, fromAmount, ctx.Fees[feeCounter].Asset, 0, 0)
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: ctx.Fees[feeCounter].Asset,
Currency: actionFee.Asset,
Update: fromAmount,
})

to := ctx.Addresses.Set(ctx.Proposer, height, ctx.Fees[feeCounter].Amount, ctx.Fees[feeCounter].Asset, 0, 0)
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: ctx.Fees[feeCounter].Asset,
Update: ctx.Fees[feeCounter].Amount,
Currency: actionFee.Asset,
Update: actionFee.Amount,
})
feeCounter++
}
}

Expand Down
14 changes: 10 additions & 4 deletions pkg/indexer/decode/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Context struct {
ActionTypes storageTypes.Bits
Constants map[string]*storage.Constant
Bridges map[string]*storage.Bridge
Fees []*storage.Fee
Fees map[string]map[int64]*storage.Fee
Transfers []*storage.Transfer
Deposits map[int64]*storage.Deposit
Proposer string
Expand All @@ -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([]*storage.Fee, 0),
Fees: make(map[string]map[int64]*storage.Fee),
Transfers: make([]*storage.Transfer, 0),
Deposits: make(map[int64]*storage.Deposit),

Expand Down Expand Up @@ -79,8 +79,14 @@ func (ctx *Context) BridgesArray() []*storage.Bridge {
return arr
}

func (ctx *Context) AddFee(fee *storage.Fee) {
ctx.Fees = append(ctx.Fees, fee)
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) AddBridgeAsset(bridge, asset string) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/indexer/decode/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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"
Expand Down Expand Up @@ -48,7 +50,8 @@ 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())

d.Actions, err = parseActions(b.Height, b.Block.Time, address, &d, ctx)
hash := hex.EncodeToString(b.Block.Txs[index].Hash())
d.Actions, err = parseActions(b.Height, b.Block.Time, address, hash, &d, ctx)
if err != nil {
return d, errors.Wrap(err, "parsing actions")
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/indexer/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (p *Module) parse(ctx context.Context, b types.BlockData) error {
decodeCtx := decode.NewContext(p.bridgeAssets)
decodeCtx.Proposer = proposer

if err := parseEvents(ctx, b.FinalizeBlockEvents, &decodeCtx, p.api); err != nil {
return errors.Wrap(err, "parse finalize events")
}

txs, err := parseTxs(ctx, b, &decodeCtx, p.api)
if err != nil {
return errors.Wrapf(err, "while parsing block on level=%d", b.Height)
Expand Down
16 changes: 13 additions & 3 deletions pkg/indexer/parser/parseEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ 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
fee = new(storage.Fee)
err error
idx int64
hash string
)
for i := range attrs {
switch attrs[i].Key {
Expand All @@ -92,11 +94,19 @@ func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *d
}
case "actionType":
fee.ActionType = attrs[i].Value
case "sourceTransactionId":
hash = attrs[i].Value
case "sourceActionIndex":
actionIndex, err := strconv.ParseInt(attrs[i].Value, 10, 64)
if err != nil {
return err
}
idx = actionIndex
default:
}
}

decodeCtx.AddFee(fee)
decodeCtx.AddFee(hash, idx, fee)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/indexer/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func getBlock() types.BlockData {
},
},
ResultBlockResults: types.ResultBlockResults{
Height: 100,
TxsResults: nil,
BeginBlockEvents: nil,
EndBlockEvents: nil,
ValidatorUpdates: nil,
Height: 100,
TxsResults: nil,
BeginBlockEvents: nil,
FinalizeBlockEvents: nil,
ValidatorUpdates: nil,
ConsensusParamUpdates: &types.ConsensusParams{
Block: &types.BlockParams{
MaxBytes: 0,
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/block_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ResultBlockResults struct {
Height Level `json:"height,string"`
TxsResults []*ResponseDeliverTx `json:"txs_results"`
BeginBlockEvents []Event `json:"begin_block_events"`
EndBlockEvents []Event `json:"end_block_events"`
FinalizeBlockEvents []Event `json:"finalize_block_events"`
ValidatorUpdates []ValidatorUpdate `json:"validator_updates"`
ConsensusParamUpdates *ConsensusParams `json:"consensus_param_updates"`
}
Expand Down
Loading