From 264405151408d36b1541af7a2d6542ac9c96659e Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 8 Jul 2024 13:51:32 +0200 Subject: [PATCH 1/3] Fix: duplicate in actions count --- pkg/indexer/decode/actions.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/indexer/decode/actions.go b/pkg/indexer/decode/actions.go index 6f147f4..1f08507 100644 --- a/pkg/indexer/decode/actions.go +++ b/pkg/indexer/decode/actions.go @@ -440,7 +440,12 @@ func parseBridgeSudoChange(body *astria.Action_BridgeSudoChangeAction, height ty if sudo != "" { action.Data["sudo"] = sudo - addr := ctx.Addresses.Set(sudo, height, decimal.Zero, "", 1, 0) + + sudoActionsCount := 1 + if bridgeAddress == sudo { + sudoActionsCount = 0 + } + addr := ctx.Addresses.Set(sudo, height, decimal.Zero, "", sudoActionsCount, 0) action.Addresses = append(action.Addresses, &storage.AddressAction{ Address: addr, Action: action, @@ -450,9 +455,15 @@ func parseBridgeSudoChange(body *astria.Action_BridgeSudoChangeAction, height ty }) bridge.Sudo = addr } + if withdrawer != "" { action.Data["withdrawer"] = withdrawer - addr := ctx.Addresses.Set(withdrawer, height, decimal.Zero, "", 1, 0) + + withdrawerActionsCount := 1 + if bridgeAddress == withdrawer || sudo == withdrawer { + withdrawerActionsCount = 0 + } + addr := ctx.Addresses.Set(withdrawer, height, decimal.Zero, "", withdrawerActionsCount, 0) action.Addresses = append(action.Addresses, &storage.AddressAction{ Address: addr, Action: action, From 40f23c71543074c6e46f8cf411c69fe419ab0ac0 Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 8 Jul 2024 14:42:26 +0200 Subject: [PATCH 2/3] Fix: tests --- pkg/indexer/decode/actions.go | 52 +++--- pkg/indexer/decode/actions_test.go | 253 +++++++++++++++++++++++++++++ 2 files changed, 281 insertions(+), 24 deletions(-) diff --git a/pkg/indexer/decode/actions.go b/pkg/indexer/decode/actions.go index 1f08507..4a1a932 100644 --- a/pkg/indexer/decode/actions.go +++ b/pkg/indexer/decode/actions.go @@ -435,43 +435,47 @@ func parseBridgeSudoChange(body *astria.Action_BridgeSudoChangeAction, height ty }) bridge := storage.Bridge{ - Address: bridgeAddr, + Address: bridgeAddr, + Sudo: bridgeAddr, + Withdrawer: bridgeAddr, } if sudo != "" { action.Data["sudo"] = sudo - sudoActionsCount := 1 - if bridgeAddress == sudo { - sudoActionsCount = 0 + if bridgeAddress != sudo { + addr := ctx.Addresses.Set(sudo, height, decimal.Zero, "", 1, 0) + action.Addresses = append(action.Addresses, &storage.AddressAction{ + Address: addr, + Action: action, + Time: action.Time, + Height: action.Height, + ActionType: action.Type, + }) + bridge.Sudo = addr } - addr := ctx.Addresses.Set(sudo, height, decimal.Zero, "", sudoActionsCount, 0) - action.Addresses = append(action.Addresses, &storage.AddressAction{ - Address: addr, - Action: action, - Time: action.Time, - Height: action.Height, - ActionType: action.Type, - }) - bridge.Sudo = addr } if withdrawer != "" { action.Data["withdrawer"] = withdrawer - withdrawerActionsCount := 1 - if bridgeAddress == withdrawer || sudo == withdrawer { - withdrawerActionsCount = 0 + actions := 1 + if sudo == withdrawer || bridgeAddress == withdrawer { + actions = 0 } - addr := ctx.Addresses.Set(withdrawer, height, decimal.Zero, "", withdrawerActionsCount, 0) - action.Addresses = append(action.Addresses, &storage.AddressAction{ - Address: addr, - Action: action, - Time: action.Time, - Height: action.Height, - ActionType: action.Type, - }) + addr := ctx.Addresses.Set(withdrawer, height, decimal.Zero, "", actions, 0) bridge.Withdrawer = addr + + if bridgeAddress != withdrawer && sudo != withdrawer { + action.Addresses = append(action.Addresses, &storage.AddressAction{ + Address: addr, + Action: action, + Time: action.Time, + Height: action.Height, + ActionType: action.Type, + }) + } + } if feeAsset != "" { diff --git a/pkg/indexer/decode/actions_test.go b/pkg/indexer/decode/actions_test.go index d29f7c6..ab23d33 100644 --- a/pkg/indexer/decode/actions_test.go +++ b/pkg/indexer/decode/actions_test.go @@ -1179,4 +1179,257 @@ func TestDecodeActions(t *testing.T) { require.NoError(t, err) require.Equal(t, wantAction, action) }) + + t.Run("bridge sudo change: bridge is suor", func(t *testing.T) { + decodeContext := NewContext() + bridge := testsuite.RandomAddress() + sudo := bridge + withdrawer := testsuite.RandomAddress() + + message := &astria.Action_BridgeSudoChangeAction{ + BridgeSudoChangeAction: &astria.BridgeSudoChangeAction{ + FeeAsset: feeAssetId, + BridgeAddress: &primitivev1.Address{Bech32M: bridge}, + NewWithdrawerAddress: &primitivev1.Address{Bech32M: withdrawer}, + NewSudoAddress: &primitivev1.Address{Bech32M: sudo}, + }, + } + + wantAction := storage.Action{ + Type: types.ActionTypeBridgeSudoChangeAction, + Data: map[string]any{ + "fee_asset": feeAssetId, + "withdrawer": withdrawer, + "sudo": sudo, + "bridge": bridge, + }, + Height: 1000, + Addresses: make([]*storage.AddressAction, 0), + } + + wantAction.Addresses = append(wantAction.Addresses, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: bridge, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: withdrawer, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }) + + action := storage.Action{ + Height: 1000, + } + err := parseBridgeSudoChange(message, 1000, &decodeContext, &action) + require.NoError(t, err) + require.Equal(t, wantAction, action) + }) + + t.Run("bridge sudo change: bridge is withdrawer", func(t *testing.T) { + decodeContext := NewContext() + bridge := testsuite.RandomAddress() + sudo := testsuite.RandomAddress() + withdrawer := bridge + + message := &astria.Action_BridgeSudoChangeAction{ + BridgeSudoChangeAction: &astria.BridgeSudoChangeAction{ + FeeAsset: feeAssetId, + BridgeAddress: &primitivev1.Address{Bech32M: bridge}, + NewWithdrawerAddress: &primitivev1.Address{Bech32M: withdrawer}, + NewSudoAddress: &primitivev1.Address{Bech32M: sudo}, + }, + } + + wantAction := storage.Action{ + Type: types.ActionTypeBridgeSudoChangeAction, + Data: map[string]any{ + "fee_asset": feeAssetId, + "withdrawer": withdrawer, + "sudo": sudo, + "bridge": bridge, + }, + Height: 1000, + Addresses: make([]*storage.AddressAction, 0), + } + + wantAction.Addresses = append(wantAction.Addresses, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: bridge, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: sudo, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }) + + action := storage.Action{ + Height: 1000, + } + err := parseBridgeSudoChange(message, 1000, &decodeContext, &action) + require.NoError(t, err) + require.Equal(t, wantAction, action) + }) + + t.Run("bridge sudo change: sudo is withdrawer", func(t *testing.T) { + decodeContext := NewContext() + bridge := testsuite.RandomAddress() + sudo := testsuite.RandomAddress() + withdrawer := sudo + + message := &astria.Action_BridgeSudoChangeAction{ + BridgeSudoChangeAction: &astria.BridgeSudoChangeAction{ + FeeAsset: feeAssetId, + BridgeAddress: &primitivev1.Address{Bech32M: bridge}, + NewWithdrawerAddress: &primitivev1.Address{Bech32M: withdrawer}, + NewSudoAddress: &primitivev1.Address{Bech32M: sudo}, + }, + } + + wantAction := storage.Action{ + Type: types.ActionTypeBridgeSudoChangeAction, + Data: map[string]any{ + "fee_asset": feeAssetId, + "withdrawer": withdrawer, + "sudo": sudo, + "bridge": bridge, + }, + Height: 1000, + Addresses: make([]*storage.AddressAction, 0), + } + + wantAction.Addresses = append(wantAction.Addresses, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: bridge, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: sudo, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }) + + action := storage.Action{ + Height: 1000, + } + err := parseBridgeSudoChange(message, 1000, &decodeContext, &action) + require.NoError(t, err) + require.Equal(t, wantAction, action) + }) + + t.Run("bridge sudo change: all equals", func(t *testing.T) { + decodeContext := NewContext() + bridge := testsuite.RandomAddress() + sudo := bridge + withdrawer := bridge + + message := &astria.Action_BridgeSudoChangeAction{ + BridgeSudoChangeAction: &astria.BridgeSudoChangeAction{ + FeeAsset: feeAssetId, + BridgeAddress: &primitivev1.Address{Bech32M: bridge}, + NewWithdrawerAddress: &primitivev1.Address{Bech32M: withdrawer}, + NewSudoAddress: &primitivev1.Address{Bech32M: sudo}, + }, + } + + wantAction := storage.Action{ + Type: types.ActionTypeBridgeSudoChangeAction, + Data: map[string]any{ + "fee_asset": feeAssetId, + "withdrawer": withdrawer, + "sudo": sudo, + "bridge": bridge, + }, + Height: 1000, + Addresses: make([]*storage.AddressAction, 0), + } + + wantAction.Addresses = append(wantAction.Addresses, &storage.AddressAction{ + Height: 1000, + Address: &storage.Address{ + Height: 1000, + Hash: bridge, + ActionsCount: 1, + Balance: []*storage.Balance{ + { + Currency: currency.DefaultCurrency, + Total: decimal.Zero, + }, + }, + }, + ActionType: types.ActionTypeBridgeSudoChangeAction, + Action: &wantAction, + }) + + action := storage.Action{ + Height: 1000, + } + err := parseBridgeSudoChange(message, 1000, &decodeContext, &action) + require.NoError(t, err) + require.Equal(t, wantAction, action) + }) } From 08436050e4e851106889bc97f9541c9571becb7a Mon Sep 17 00:00:00 2001 From: Artem Date: Mon, 8 Jul 2024 16:38:50 +0200 Subject: [PATCH 3/3] Fix: init bridge account actions count --- pkg/indexer/decode/actions.go | 2 +- pkg/indexer/decode/actions_test.go | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pkg/indexer/decode/actions.go b/pkg/indexer/decode/actions.go index 4a1a932..9a8a812 100644 --- a/pkg/indexer/decode/actions.go +++ b/pkg/indexer/decode/actions.go @@ -362,7 +362,7 @@ func parseInitBridgeAccount(body *astria.Action_InitBridgeAccountAction, from st InitHeight: height, Asset: body.InitBridgeAccountAction.GetAsset(), FeeAsset: body.InitBridgeAccountAction.GetFeeAsset(), - Address: ctx.Addresses.Set(from, height, decimal.Zero, "", 1, 0), + Address: ctx.Addresses.Set(from, height, decimal.Zero, "", 0, 0), Rollup: rollup, } diff --git a/pkg/indexer/decode/actions_test.go b/pkg/indexer/decode/actions_test.go index ab23d33..a1b7860 100644 --- a/pkg/indexer/decode/actions_test.go +++ b/pkg/indexer/decode/actions_test.go @@ -791,19 +791,20 @@ func TestDecodeActions(t *testing.T) { Addresses: make([]*storage.AddressAction, 0), } wantAction.RollupAction.Action = &wantAction - wantAction.Addresses = append(wantAction.Addresses, &storage.AddressAction{ - Address: sudoAddr, - Height: 1000, - Time: wantAction.Time, - Action: &wantAction, - ActionType: types.ActionTypeInitBridgeAccount, - }, &storage.AddressAction{ - Address: wdwAddr, - Height: 1000, - Time: wantAction.Time, - Action: &wantAction, - ActionType: types.ActionTypeInitBridgeAccount, - }) + wantAction.Addresses = append(wantAction.Addresses, + &storage.AddressAction{ + Address: sudoAddr, + Height: 1000, + Time: wantAction.Time, + Action: &wantAction, + ActionType: types.ActionTypeInitBridgeAccount, + }, &storage.AddressAction{ + Address: wdwAddr, + Height: 1000, + Time: wantAction.Time, + Action: &wantAction, + ActionType: types.ActionTypeInitBridgeAccount, + }) action := storage.Action{ Height: 1000,