Skip to content

Commit

Permalink
feat(x/common): remove
Browse files Browse the repository at this point in the history
  • Loading branch information
SpekalsG3 committed Oct 5, 2024
1 parent 2c8ba63 commit 0b9090d
Show file tree
Hide file tree
Showing 46 changed files with 156 additions and 10,603 deletions.
33 changes: 33 additions & 0 deletions e2e/testing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import (
proto "github.com/cosmos/gogoproto/proto"
"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil"

"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"github.com/archway-network/archway/app"
)

Expand Down Expand Up @@ -45,3 +55,26 @@ func (c grpcClient) NewStream(ctx context.Context, desc *grpc.StreamDesc, method
func (chain *TestChain) Client() grpc.ClientConnInterface {
return grpcClient{app: chain.app}
}

// SetupClientCtx configures the client and server contexts and returns the
// resultant 'context.Context'. This is useful for executing CLI commands.
func (chain *TestChain) SetupClientCtx() context.Context {
home := chain.t.TempDir()
logger := chain.app.Logger()
cfg, err := genutiltest.CreateDefaultCometConfig(home)
require.NoError(chain.t, err)

appCodec := moduletestutil.MakeTestEncodingConfig().Codec
testModuleBasicManager := module.NewBasicManager(genutil.AppModuleBasic{})
err = genutiltest.ExecInitCmd(
testModuleBasicManager, home, appCodec)
require.NoError(chain.t, err)

serverCtx := server.NewContext(viper.New(), cfg, logger)
clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home)

ctx := context.Background()
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx)
return ctx
}
2 changes: 1 addition & 1 deletion x/common/codec.go → types/codec/codec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package codec

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion x/common/codec_test.go → types/codec/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package codec

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion x/common/error.go → types/errors/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package errors

import (
"errors"
Expand Down
33 changes: 19 additions & 14 deletions x/common/error_test.go → types/errors/error_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common_test
package errors

import (
"errors"
Expand All @@ -8,8 +8,6 @@ import (

"github.com/stretchr/testify/assert"

"github.com/archway-network/archway/x/common"
"github.com/archway-network/archway/x/common/testutil"
"github.com/archway-network/archway/x/oracle/asset"
"github.com/archway-network/archway/x/oracle/denoms"
)
Expand Down Expand Up @@ -46,7 +44,7 @@ func TestCombineErrors(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
errOut := common.CombineErrors(tc.errs...)
errOut := CombineErrors(tc.errs...)
assert.EqualValuesf(t, tc.errOut, errOut,
"tc.errOut: %s\nerrOut: %s", tc.errOut, errOut)
})
Expand Down Expand Up @@ -102,7 +100,7 @@ func TestCombineErrorsGeneric(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
out, ok := common.CombineErrorsGeneric(tc.in)
out, ok := CombineErrorsGeneric(tc.in)
if tc.fail {
assert.Falsef(t, ok, "out: %v", out)
} else {
Expand All @@ -115,12 +113,15 @@ func TestCombineErrorsGeneric(t *testing.T) {
}

func TestToError(t *testing.T) {
testCases := []testutil.FunctionTestCase{
testCases := []struct {
Name string
Test func()
}{
{
Name: "string nonempty",
Test: func() {
description := "an error description"
out, ok := common.ToError(description)
out, ok := ToError(description)
assert.True(t, ok)
assert.EqualValues(t, out.Error(), description)
},
Expand All @@ -129,7 +130,7 @@ func TestToError(t *testing.T) {
Name: "error nonempty",
Test: func() {
description := "an error description"
out, ok := common.ToError(errors.New(description))
out, ok := ToError(errors.New(description))
assert.True(t, ok)
assert.EqualValues(t, out.Error(), description)
},
Expand All @@ -138,7 +139,7 @@ func TestToError(t *testing.T) {
Name: "empty string creates blank error",
Test: func() {
description := ""
out, ok := common.ToError("")
out, ok := ToError("")
assert.True(t, ok)
assert.EqualValues(t, out.Error(), description)
},
Expand All @@ -147,35 +148,39 @@ func TestToError(t *testing.T) {
Name: "fail - bad type",
Test: func() {
descriptionOfBadType := int64(2200)
_, ok := common.ToError(descriptionOfBadType)
_, ok := ToError(descriptionOfBadType)
assert.False(t, ok)
},
},
{
Name: "nil input returns nil",
Test: func() {
err, ok := common.ToError(nil)
err, ok := ToError(nil)
assert.True(t, ok)
assert.Equal(t, nil, err)
},
},
{
Name: "slice of strings",
Test: func() {
err, ok := common.ToError([]string{"abc", "123"})
err, ok := ToError([]string{"abc", "123"})
assert.True(t, ok)
assert.Equal(t, errors.New("abc: 123"), err)
},
},
{
Name: "slice of error",
Test: func() {
err, ok := common.ToError(newErrors("abc", "123"))
err, ok := ToError(newErrors("abc", "123"))
assert.True(t, ok)
assert.Equal(t, errors.New("abc: 123"), err)
},
},
}

testutil.RunFunctionTests(t, testCases)
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
tc.Test()
})
}
}
7 changes: 4 additions & 3 deletions x/common/dec.go → types/math/dec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package common
package math

import (
"fmt"
"math/big"
"strings"

"cosmossdk.io/math"
"github.com/archway-network/archway/types/errors"
)

const (
Expand Down Expand Up @@ -54,7 +55,7 @@ func MustSqrtDec(dec math.LegacyDec) math.LegacyDec {
// large as 10**99.
func SqrtDec(dec math.LegacyDec) (math.LegacyDec, error) {
var sqrtDec math.LegacyDec
var panicErr error = TryCatch(func() {
var panicErr error = errors.TryCatch(func() {
sqrtDec = MustSqrtDec(dec)
})()
return sqrtDec, panicErr
Expand All @@ -71,7 +72,7 @@ func MustSqrtBigInt(i *big.Int) *big.Int {
// SqrtInt is the panic-safe version of MustSqrtBigInt
func SqrtBigInt(i *big.Int) (*big.Int, error) {
sqrtInt := new(big.Int)
var panicErr error = TryCatch(func() {
var panicErr error = errors.TryCatch(func() {
*sqrtInt = *MustSqrtBigInt(i)
})()
return sqrtInt, panicErr
Expand Down
25 changes: 12 additions & 13 deletions x/common/dec_test.go → types/math/dec_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common_test
package math

import (
"fmt"
Expand All @@ -7,9 +7,8 @@ import (

"cosmossdk.io/math"

"github.com/archway-network/archway/types/errors"
"github.com/stretchr/testify/assert"

"github.com/archway-network/archway/x/common"
)

func TestSqrtBigInt(t *testing.T) {
Expand All @@ -22,19 +21,19 @@ func TestSqrtBigInt(t *testing.T) {
{bigInt: big.NewInt(250_000), sqrtBigInt: big.NewInt(500)},
{bigInt: big.NewInt(4_819_136_400), sqrtBigInt: big.NewInt(69_420)},
{
bigInt: new(big.Int).Mul(big.NewInt(4_819_136_400), common.BigIntPow10(32)),
sqrtBigInt: new(big.Int).Mul(big.NewInt(69_420), common.BigIntPow10(16)),
bigInt: new(big.Int).Mul(big.NewInt(4_819_136_400), BigIntPow10(32)),
sqrtBigInt: new(big.Int).Mul(big.NewInt(69_420), BigIntPow10(16)),
},
{
bigInt: new(big.Int).Mul(big.NewInt(9), common.BigIntPow10(100)),
sqrtBigInt: new(big.Int).Mul(big.NewInt(3), common.BigIntPow10(50)),
bigInt: new(big.Int).Mul(big.NewInt(9), BigIntPow10(100)),
sqrtBigInt: new(big.Int).Mul(big.NewInt(3), BigIntPow10(50)),
},
}

for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf(`bigInt: %s, sqrtBigInt: %s`, tc.bigInt, tc.sqrtBigInt), func(t *testing.T) {
sqrtInt, err := common.SqrtBigInt(tc.bigInt)
sqrtInt, err := SqrtBigInt(tc.bigInt)
assert.NoError(t, err)
assert.Equal(t, tc.sqrtBigInt.String(), sqrtInt.String())
})
Expand Down Expand Up @@ -65,8 +64,8 @@ func TestSqrtDec(t *testing.T) {
}

t.Run("negative sqrt should panic", func(t *testing.T) {
panicString := common.TryCatch(func() {
common.MustSqrtDec(math.LegacyNewDec(-9))
panicString := errors.TryCatch(func() {
MustSqrtDec(math.LegacyNewDec(-9))
})().Error()

assert.Contains(t, panicString, "square root of negative number")
Expand All @@ -75,7 +74,7 @@ func TestSqrtDec(t *testing.T) {
for _, testCase := range testCases {
tc := testCase
t.Run(fmt.Sprintf(`dec: %s, sqrtDec: %s`, tc.dec, tc.sqrtDec), func(t *testing.T) {
sqrtDec, err := common.SqrtDec(tc.dec)
sqrtDec, err := SqrtDec(tc.dec)
assert.NoError(t, err)
assert.Equal(t, tc.sqrtDec.String(), sqrtDec.String())
})
Expand Down Expand Up @@ -125,7 +124,7 @@ func TestBankersRound(t *testing.T) {
if tc.quo != nil {
tcQuo = tc.quo
}
rounded := common.BankersRound(tcQuo, tc.rem, halfPrecision)
rounded := BankersRound(tcQuo, tc.rem, halfPrecision)
assert.EqualValues(t, tc.rounded, rounded)
}
}
Expand Down Expand Up @@ -159,7 +158,7 @@ func TestClamp(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
result := common.Clamp(tt.value, tt.clampValue)
result := Clamp(tt.value, tt.clampValue)
assert.Equal(t, tt.expected, result)
})
}
Expand Down
3 changes: 0 additions & 3 deletions x/common/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions x/common/address.go

This file was deleted.

19 changes: 0 additions & 19 deletions x/common/address_test.go

This file was deleted.

6 changes: 0 additions & 6 deletions x/common/constants.go

This file was deleted.

42 changes: 0 additions & 42 deletions x/common/ewma/ewma.go

This file was deleted.

Loading

0 comments on commit 0b9090d

Please sign in to comment.