From 4343c5b3427910a7e89a5521d8c21df404771a39 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 31 May 2022 14:15:30 +0200 Subject: [PATCH 1/2] Delete v043_temp directory --- app/params/config.go | 2 +- v043_temp/address/hash.go | 83 ---------------------- v043_temp/address/hash_test.go | 105 ---------------------------- v043_temp/address/store_key.go | 33 --------- v043_temp/address/store_key_test.go | 46 ------------ v043_temp/conv/doc.go | 2 - v043_temp/conv/string.go | 26 ------- v043_temp/conv/string_test.go | 54 -------------- x/gamm/types/pool.go | 2 +- 9 files changed, 2 insertions(+), 351 deletions(-) delete mode 100644 v043_temp/address/hash.go delete mode 100644 v043_temp/address/hash_test.go delete mode 100644 v043_temp/address/store_key.go delete mode 100644 v043_temp/address/store_key_test.go delete mode 100644 v043_temp/conv/doc.go delete mode 100644 v043_temp/conv/string.go delete mode 100644 v043_temp/conv/string_test.go diff --git a/app/params/config.go b/app/params/config.go index 8b571373742..8b952c2849a 100644 --- a/app/params/config.go +++ b/app/params/config.go @@ -1,7 +1,7 @@ package params import ( - "github.com/osmosis-labs/osmosis/v7/v043_temp/address" + "github.com/cosmos/cosmos-sdk/types/address" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" diff --git a/v043_temp/address/hash.go b/v043_temp/address/hash.go deleted file mode 100644 index 41fda93e4a7..00000000000 --- a/v043_temp/address/hash.go +++ /dev/null @@ -1,83 +0,0 @@ -package address - -import ( - "bytes" - "crypto/sha256" - "fmt" - "sort" - - "github.com/osmosis-labs/osmosis/v7/v043_temp/conv" -) - -// Len is the length of base addresses. -const Len = sha256.Size - -type Addressable interface { - Address() []byte -} - -// Hash creates a new address from address type and key. -func Hash(typ string, key []byte) []byte { - hasher := sha256.New() - _, err := hasher.Write(conv.UnsafeStrToBytes(typ)) - // the error always nil, it's here only to satisfy the io.Writer interface - // errors.AssertNil(err) - // Isn't implemented in 0.42 errors package - // Replaced with underlying function content. - // Source: https://github.com/cosmos/cosmos-sdk/blob/master/types/errors/handle.go - // errors.AssertNil(err) - if err != nil { - panic(fmt.Errorf("logic error - this should never happen. %w", err)) - } - - th := hasher.Sum(nil) - - hasher.Reset() - _, err = hasher.Write(th) - // errors.AssertNil(err) - if err != nil { - panic(fmt.Errorf("logic error - this should never happen. %w", err)) - } - _, err = hasher.Write(key) - // errors.AssertNil(err) - if err != nil { - panic(fmt.Errorf("logic error - this should never happen. %w", err)) - } - return hasher.Sum(nil) -} - -// Compose creates a new address based on sub addresses. -func Compose(typ string, subAddresses []Addressable) ([]byte, error) { - as := make([][]byte, len(subAddresses)) - totalLen := 0 - var err error - for i := range subAddresses { - a := subAddresses[i].Address() - as[i], err = LengthPrefix(a) - if err != nil { - return nil, fmt.Errorf("not compatible sub-adddress=%v at index=%d [%w]", a, i, err) - } - totalLen += len(as[i]) - } - - sort.Slice(as, func(i, j int) bool { return bytes.Compare(as[i], as[j]) <= 0 }) - key := make([]byte, totalLen) - offset := 0 - for i := range as { - copy(key[offset:], as[i]) - offset += len(as[i]) - } - return Hash(typ, key), nil -} - -// Module is a specialized version of a composed address for modules. Each module account -// is constructed from a module name and module account key. -func Module(moduleName string, key []byte) []byte { - mKey := append([]byte(moduleName), 0) - return Hash("module", append(mKey, key...)) -} - -// Derive derives a new address from the main `address` and a derivation `key`. -func Derive(address []byte, key []byte) []byte { - return Hash(conv.UnsafeBytesToStr(address), key) -} diff --git a/v043_temp/address/hash_test.go b/v043_temp/address/hash_test.go deleted file mode 100644 index 06fede65948..00000000000 --- a/v043_temp/address/hash_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package address - -import ( - "crypto/sha256" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/suite" -) - -func TestAddressSuite(t *testing.T) { - suite.Run(t, new(AddressSuite)) -} - -type AddressSuite struct{ suite.Suite } - -func (suite *AddressSuite) TestHash() { - assert := suite.Assert() - typ := "1" - key := []byte{1} - part1 := sha256.Sum256([]byte(typ)) - expected := sha256.Sum256(append(part1[:], key...)) - received := Hash(typ, key) - assert.Equal(expected[:], received, "must create a correct address") - - received = Hash("other", key) - assert.NotEqual(expected[:], received, "must create a correct address") - assert.Len(received, Len, "must have correct length") -} - -func (suite *AddressSuite) TestComposed() { - assert := suite.Assert() - a1 := addrMock{[]byte{11, 12}} - a2 := addrMock{[]byte{21, 22}} - - typ := "multisig" - ac, err := Compose(typ, []Addressable{a1, a2}) - assert.NoError(err) - assert.Len(ac, Len) - - // check if optimizations work - checkingKey := append([]byte{}, a1.AddressWithLen(suite.T())...) - checkingKey = append(checkingKey, a2.AddressWithLen(suite.T())...) - ac2 := Hash(typ, checkingKey) - assert.Equal(ac, ac2, "NewComposed works correctly") - - // changing order of addresses shouldn't impact a composed address - ac2, err = Compose(typ, []Addressable{a2, a1}) - assert.NoError(err) - assert.Len(ac2, Len) - assert.Equal(ac, ac2, "NewComposed is not sensitive for order") - - // changing a type should change composed address - ac2, err = Compose(typ+"other", []Addressable{a2, a1}) - assert.NoError(err) - assert.NotEqual(ac, ac2, "NewComposed must be sensitive to type") - - // changing order of addresses shouldn't impact a composed address - ac2, err = Compose(typ, []Addressable{a1, addrMock{make([]byte, 300, 300)}}) - assert.Error(err) - assert.Contains(err.Error(), "should be max 255 bytes, got 300") -} - -func (suite *AddressSuite) TestModule() { - assert := suite.Assert() - modName, key := "myModule", []byte{1, 2} - addr := Module(modName, key) - assert.Len(addr, Len, "must have address length") - - addr2 := Module("myModule2", key) - assert.NotEqual(addr, addr2, "changing module name must change address") - - addr3 := Module(modName, []byte{1, 2, 3}) - assert.NotEqual(addr, addr3, "changing key must change address") - assert.NotEqual(addr2, addr3, "changing key must change address") -} - -func (suite *AddressSuite) TestDerive() { - assert := suite.Assert() - addr, key1, key2 := []byte{1, 2}, []byte{3, 4}, []byte{1, 2} - d1 := Derive(addr, key1) - d2 := Derive(addr, key2) - d3 := Derive(key1, key2) - assert.Len(d1, Len) - assert.Len(d2, Len) - assert.Len(d3, Len) - - assert.NotEqual(d1, d2) - assert.NotEqual(d1, d3) - assert.NotEqual(d2, d3) -} - -type addrMock struct { - Addr []byte -} - -func (a addrMock) Address() []byte { - return a.Addr -} - -func (a addrMock) AddressWithLen(t *testing.T) []byte { - addr, err := LengthPrefix(a.Addr) - assert.NoError(t, err) - return addr -} diff --git a/v043_temp/address/store_key.go b/v043_temp/address/store_key.go deleted file mode 100644 index 94849197293..00000000000 --- a/v043_temp/address/store_key.go +++ /dev/null @@ -1,33 +0,0 @@ -package address - -import ( - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -) - -// MaxAddrLen is the maximum allowed length (in bytes) for an address. -const MaxAddrLen = 255 - -// LengthPrefix prefixes the address bytes with its length, this is used -// for example for variable-length components in store keys. -func LengthPrefix(bz []byte) ([]byte, error) { - bzLen := len(bz) - if bzLen == 0 { - return bz, nil - } - - if bzLen > MaxAddrLen { - return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "address length should be max %d bytes, got %d", MaxAddrLen, bzLen) - } - - return append([]byte{byte(bzLen)}, bz...), nil -} - -// MustLengthPrefix is LengthPrefix with panic on error. -func MustLengthPrefix(bz []byte) []byte { - res, err := LengthPrefix(bz) - if err != nil { - panic(err) - } - - return res -} diff --git a/v043_temp/address/store_key_test.go b/v043_temp/address/store_key_test.go deleted file mode 100644 index b85ad5bfb95..00000000000 --- a/v043_temp/address/store_key_test.go +++ /dev/null @@ -1,46 +0,0 @@ -package address_test - -import ( - "testing" - - "github.com/stretchr/testify/suite" - - "github.com/osmosis-labs/osmosis/v7/v043_temp/address" -) - -func TestStoreKeySuite(t *testing.T) { - suite.Run(t, new(StoreKeySuite)) -} - -type StoreKeySuite struct{ suite.Suite } - -func (suite *StoreKeySuite) TestLengthPrefix() { - require := suite.Require() - addr10byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - addr20byte := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19} - addr256byte := make([]byte, 256) - - tests := []struct { - name string - addr []byte - expStoreKey []byte - expErr bool - }{ - {"10-byte address", addr10byte, append([]byte{byte(10)}, addr10byte...), false}, - {"20-byte address", addr20byte, append([]byte{byte(20)}, addr20byte...), false}, - {"256-byte address (too long)", addr256byte, nil, true}, - } - - for _, tt := range tests { - tt := tt - suite.Run(tt.name, func() { - storeKey, err := address.LengthPrefix(tt.addr) - if tt.expErr { - require.Error(err) - } else { - require.NoError(err) - require.Equal(tt.expStoreKey, storeKey) - } - }) - } -} diff --git a/v043_temp/conv/doc.go b/v043_temp/conv/doc.go deleted file mode 100644 index 1c86f5c1440..00000000000 --- a/v043_temp/conv/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package conv provides internal functions for convertions and data manipulation -package conv diff --git a/v043_temp/conv/string.go b/v043_temp/conv/string.go deleted file mode 100644 index ab2b7f44b38..00000000000 --- a/v043_temp/conv/string.go +++ /dev/null @@ -1,26 +0,0 @@ -package conv - -import ( - "reflect" - "unsafe" -) - -// UnsafeStrToBytes uses unsafe to convert string into byte array. Returned bytes -// must not be altered after this function is called as it will cause a segmentation fault. -func UnsafeStrToBytes(s string) []byte { - var buf []byte - sHdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) - bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) - bufHdr.Data = sHdr.Data - bufHdr.Cap = sHdr.Len - bufHdr.Len = sHdr.Len - return buf -} - -// UnsafeBytesToStr is meant to make a zero allocation conversion -// from []byte -> string to speed up operations, it is not meant -// to be used generally, but for a specific pattern to delete keys -// from a map. -func UnsafeBytesToStr(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/v043_temp/conv/string_test.go b/v043_temp/conv/string_test.go deleted file mode 100644 index 3a145175318..00000000000 --- a/v043_temp/conv/string_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package conv - -import ( - "runtime" - "strconv" - "testing" - "time" - - "github.com/stretchr/testify/suite" -) - -func TestStringSuite(t *testing.T) { - suite.Run(t, new(StringSuite)) -} - -type StringSuite struct{ suite.Suite } - -func unsafeConvertStr() []byte { - return UnsafeStrToBytes("abc") -} - -func (s *StringSuite) TestUnsafeStrToBytes() { - // we convert in other function to trigger GC. We want to check that - // the underlying array in []bytes is accessible after GC will finish swapping. - for i := 0; i < 5; i++ { - b := unsafeConvertStr() - runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C - b2 := append(b, 'd') - s.Equal("abc", string(b)) - s.Equal("abcd", string(b2)) - } -} - -func unsafeConvertBytes() string { - return UnsafeBytesToStr([]byte("abc")) -} - -func (s *StringSuite) TestUnsafeBytesToStr() { - // we convert in other function to trigger GC. We want to check that - // the underlying array in []bytes is accessible after GC will finish swapping. - for i := 0; i < 5; i++ { - str := unsafeConvertBytes() - runtime.GC() - <-time.NewTimer(2 * time.Millisecond).C - s.Equal("abc", str) - } -} - -func BenchmarkUnsafeStrToBytes(b *testing.B) { - for i := 0; i < b.N; i++ { - UnsafeStrToBytes(strconv.Itoa(i)) - } -} diff --git a/x/gamm/types/pool.go b/x/gamm/types/pool.go index 9f8081784a4..a44f39e712b 100644 --- a/x/gamm/types/pool.go +++ b/x/gamm/types/pool.go @@ -5,7 +5,7 @@ import ( proto "github.com/gogo/protobuf/proto" - "github.com/osmosis-labs/osmosis/v7/v043_temp/address" + "github.com/cosmos/cosmos-sdk/types/address" sdk "github.com/cosmos/cosmos-sdk/types" ) From e79c8655ebed3cc8436558216295e3c0d0329f90 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 31 May 2022 14:21:27 +0200 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8be6354d6c..eba3e6c66c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * [#1312] Stableswap: Createpool logic * [#1230] Stableswap CFMM equations * [#1429] solver for multi-asset CFMM +* [#1630](https://github.com/osmosis-labs/osmosis/pull/1630) Delete the v043_temp module, now that we're on an updated SDK version. ## [v9.0.0 - Nitrogen](https://github.com/osmosis-labs/osmosis/releases/tag/v9.0.0)