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(twap/e2e): set geometric accum to zero; reproduce testnet bug #3972

Merged
merged 3 commits into from
Jan 10, 2023
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
39 changes: 38 additions & 1 deletion tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package e2e
import (
"encoding/json"
"fmt"
ibchookskeeper "github.com/osmosis-labs/osmosis/x/ibc-hooks/keeper"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"

ibchookskeeper "github.com/osmosis-labs/osmosis/x/ibc-hooks/keeper"

paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"

ibcratelimittypes "github.com/osmosis-labs/osmosis/v13/x/ibc-rate-limit/types"
Expand All @@ -24,6 +25,42 @@ import (
"github.com/osmosis-labs/osmosis/v13/tests/e2e/initialization"
)

// TestGeometricTwapMigration tests that the geometric twap record
// migration runs succesfully. It does so by attempting to execute
// the swap on the pool created pre-upgrade. When a pool is created
// pre-upgrade, twap records are initialized for a pool. By runnning
// a swap post-upgrade, we confirm that the geometric twap was initialized
// correctly and does not cause a chain halt. This test was created
// in-response to a testnet incident when performing the geometric twap
// upgrade. Upon adding the migrations logic, the tests began to pass.
func (s *IntegrationTestSuite) TestGeometricTwapMigration() {
if s.skipUpgrade {
s.T().Skip("Skipping upgrade tests")
}

const (
// Configurations for tests/e2e/scripts/pool1A.json
// This pool gets initialized pre-upgrade.
oldPoolId = 1
minAmountOut = "1"
otherDenom = "ibc/ED07A3391A112B175915CD8FAF43A2DA8E4790EDE12566649D0C2F97716B8518"
migrationWallet = "migration"
)

chainA := s.configurer.GetChainConfig(0)
node, err := chainA.GetDefaultNode()
s.Require().NoError(err)

uosmoIn := fmt.Sprintf("1000000%s", "uosmo")

swapWalletAddr := node.CreateWallet(migrationWallet)

node.BankSend(uosmoIn, chainA.NodeConfigs[0].PublicAddress, swapWalletAddr)

// Swap to create new twap records on the pool that was created pre-upgrade.
node.SwapExactAmountIn(uosmoIn, minAmountOut, fmt.Sprintf("%d", oldPoolId), otherDenom, swapWalletAddr)
}

// TestIBCTokenTransfer tests that IBC token transfers work as expected.
// Additionally, it attempst to create a pool with IBC denoms.
func (s *IntegrationTestSuite) TestIBCTokenTransferAndCreatePool() {
Expand Down
3 changes: 3 additions & 0 deletions x/twap/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,8 @@ func ParseTwapFromBz(bz []byte) (twap TwapRecord, err error) {
return TwapRecord{}, errors.New("twap not found")
}
err = proto.Unmarshal(bz, &twap)
if twap.GeometricTwapAccumulator.IsNil() {
twap.GeometricTwapAccumulator = sdk.ZeroDec()
}
return twap, err
}
48 changes: 36 additions & 12 deletions x/twap/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,52 @@ func TestFormatHistoricalTwapKeys(t *testing.T) {

func TestParseTwapFromBz(t *testing.T) {
baseTime := time.Unix(1257894000, 0).UTC()
baseParseRecord := TwapRecord{
PoolId: 123,
Asset0Denom: "B",
Asset1Denom: "A",
Height: 1,
Time: baseTime,
P0LastSpotPrice: sdk.NewDecWithPrec(1, 5),
P1LastSpotPrice: sdk.NewDecWithPrec(2, 5), // inconsistent value
P0ArithmeticTwapAccumulator: sdk.ZeroDec(),
P1ArithmeticTwapAccumulator: sdk.ZeroDec(),
}

withGeomAcc := func(r TwapRecord, acc sdk.Dec) TwapRecord {
r.GeometricTwapAccumulator = acc
return r
}

tests := map[string]struct {
record TwapRecord
record TwapRecord
isGeometricAccumNil bool
}{
"standard": {TwapRecord{
PoolId: 123,
Asset0Denom: "B",
Asset1Denom: "A",
Height: 1,
Time: baseTime,
P0LastSpotPrice: sdk.NewDecWithPrec(1, 5),
P1LastSpotPrice: sdk.NewDecWithPrec(2, 5), // inconsistent value
P0ArithmeticTwapAccumulator: sdk.ZeroDec(),
P1ArithmeticTwapAccumulator: sdk.ZeroDec(),
}},
"standard": {
baseParseRecord,
false,
},
"with nil geometric twap accumulator -> set to zero": {
withGeomAcc(baseParseRecord, sdk.Dec{}),
true,
},
"with non-nil geometric twap accumulator -> not overwritten": {
withGeomAcc(baseParseRecord, sdk.OneDec()),
false,
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
bz, err := proto.Marshal(&tt.record)
require.NoError(t, err)
record, err := ParseTwapFromBz(bz)
require.NoError(t, err)

if tt.isGeometricAccumNil {
tt.record.GeometricTwapAccumulator = sdk.ZeroDec()
}

require.Equal(t, tt.record, record)
})
}
Expand Down