Skip to content

Commit

Permalink
Fix: duplicate in actions count (#26)
Browse files Browse the repository at this point in the history
* Fix: duplicate in actions count

* Fix: tests
  • Loading branch information
aopoltorzhicky authored Jul 8, 2024
1 parent 12cce1a commit 7663fa3
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 18 deletions.
51 changes: 33 additions & 18 deletions pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down
253 changes: 253 additions & 0 deletions pkg/indexer/decode/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 7663fa3

Please sign in to comment.