diff --git a/pkg/indexer/decode/actions.go b/pkg/indexer/decode/actions.go index 6f147f4..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, } @@ -435,32 +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 - 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 + + 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 + } } + if withdrawer != "" { action.Data["withdrawer"] = withdrawer - addr := ctx.Addresses.Set(withdrawer, 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, - }) + + actions := 1 + if sudo == withdrawer || bridgeAddress == withdrawer { + actions = 0 + } + 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..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, @@ -1179,4 +1180,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) + }) }