Skip to content

Commit

Permalink
wip: porting bls-into-sdk2 to cosmos-sdk 0.50.10
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Nov 16, 2024
1 parent 6dc6e8b commit 625a647
Show file tree
Hide file tree
Showing 18 changed files with 872 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ ifeq (legacy,$(findstring legacy,$(COSMOS_BUILD_OPTIONS)))
build_tags += app_v1
endif

build_tags += blst

whitespace :=
whitespace += $(whitespace)
comma := ,
Expand Down
44 changes: 22 additions & 22 deletions collections/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func iteratorFromRanger[K, V any](ctx context.Context, m Map[K, V], r Ranger[K])
return iter, err
}
} else {
endBytes = nextBytesPrefixKey(m.prefix)
endBytes = NextBytesPrefixKey(m.prefix)
}

return newIterator(ctx, startBytes, endBytes, order, m)
Expand Down Expand Up @@ -187,10 +187,10 @@ func newIterator[K, V any](ctx context.Context, start, end []byte, order Order,
}

return Iterator[K, V]{
kc: m.kc,
vc: m.vc,
iter: iter,
prefixLength: len(m.prefix),
KeyCodec: m.kc,
ValueCodec: m.vc,
Iter: iter,
PrefixLength: len(m.prefix),
}, nil
}

Expand All @@ -199,31 +199,31 @@ func newIterator[K, V any](ctx context.Context, start, end []byte, order Order,
// it assumes all the keys and values contained within the storetypes.Iterator
// range are the same.
type Iterator[K, V any] struct {
kc codec.KeyCodec[K]
vc codec.ValueCodec[V]
KeyCodec codec.KeyCodec[K]
ValueCodec codec.ValueCodec[V]

iter store.Iterator
Iter store.Iterator

prefixLength int // prefixLength refers to the bytes provided by Prefix.Bytes, not Ranger.RangeValues() prefix.
PrefixLength int // PrefixLength refers to the bytes provided by Prefix.Bytes, not Ranger.RangeValues() prefix.
}

// Value returns the current iterator value bytes decoded.
func (i Iterator[K, V]) Value() (V, error) {
return i.vc.Decode(i.iter.Value())
return i.ValueCodec.Decode(i.Iter.Value())
}

// Key returns the current storetypes.Iterator decoded key.
func (i Iterator[K, V]) Key() (K, error) {
bytesKey := i.iter.Key()[i.prefixLength:] // strip prefix namespace
bytesKey := i.Iter.Key()[i.PrefixLength:] // strip prefix namespace

read, key, err := i.kc.Decode(bytesKey)
read, key, err := i.KeyCodec.Decode(bytesKey)
if err != nil {
var k K
return k, err
}
if read != len(bytesKey) {
var k K
return k, fmt.Errorf("%w: key decoder didn't fully consume the key: %T %x %d", ErrEncoding, i.kc, bytesKey, read)
return k, fmt.Errorf("%w: key decoder didn't fully consume the key: %T %x %d", ErrEncoding, i.KeyCodec, bytesKey, read)
}
return key, nil
}
Expand All @@ -233,7 +233,7 @@ func (i Iterator[K, V]) Values() ([]V, error) {
defer i.Close()

var values []V
for ; i.iter.Valid(); i.iter.Next() {
for ; i.Iter.Valid(); i.Iter.Next() {
value, err := i.Value()
if err != nil {
return nil, err
Expand All @@ -248,7 +248,7 @@ func (i Iterator[K, V]) Keys() ([]K, error) {
defer i.Close()

var keys []K
for ; i.iter.Valid(); i.iter.Next() {
for ; i.Iter.Valid(); i.Iter.Next() {
key, err := i.Key()
if err != nil {
return nil, err
Expand Down Expand Up @@ -278,7 +278,7 @@ func (i Iterator[K, V]) KeyValues() ([]KeyValue[K, V], error) {
defer i.Close()

var kvs []KeyValue[K, V]
for ; i.iter.Valid(); i.iter.Next() {
for ; i.Iter.Valid(); i.Iter.Next() {
kv, err := i.KeyValue()
if err != nil {
return nil, err
Expand All @@ -289,9 +289,9 @@ func (i Iterator[K, V]) KeyValues() ([]KeyValue[K, V], error) {
return kvs, nil
}

func (i Iterator[K, V]) Close() error { return i.iter.Close() }
func (i Iterator[K, V]) Next() { i.iter.Next() }
func (i Iterator[K, V]) Valid() bool { return i.iter.Valid() }
func (i Iterator[K, V]) Close() error { return i.Iter.Close() }
func (i Iterator[K, V]) Next() { i.Iter.Next() }
func (i Iterator[K, V]) Valid() bool { return i.Iter.Valid() }

// KeyValue represent a Key and Value pair of an iteration.
type KeyValue[K, V any] struct {
Expand All @@ -311,7 +311,7 @@ func encodeRangeBound[T any](prefix []byte, keyCodec codec.KeyCodec[T], bound *R
case rangeKeyNext:
return nextBytesKey(key), nil
case rangeKeyPrefixEnd:
return nextBytesPrefixKey(key), nil
return NextBytesPrefixKey(key), nil
default:
panic("undefined bound kind")
}
Expand All @@ -322,10 +322,10 @@ func nextBytesKey(b []byte) []byte {
return append(b, 0)
}

// nextBytesPrefixKey returns the []byte that would end a
// NextBytesPrefixKey returns the []byte that would end a
// range query for all []byte with a certain prefix
// Deals with last byte of prefix being FF without overflowing
func nextBytesPrefixKey(prefix []byte) []byte {
func NextBytesPrefixKey(prefix []byte) []byte {
if len(prefix) == 0 {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion collections/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestIteratorBasic(t *testing.T) {
require.Equal(t, uint64(1), value)

// assert expected prefixing on iter
require.Equal(t, m.prefix, iter.iter.Key()[:len(m.prefix)])
require.Equal(t, m.prefix, iter.Iter.Key()[:len(m.prefix)])

// advance iter
iter.Next()
Expand Down
10 changes: 5 additions & 5 deletions collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (m Map[K, V]) IterateRaw(ctx context.Context, start, end []byte, order Orde
prefixedStart := append(m.prefix, start...)
var prefixedEnd []byte
if end == nil {
prefixedEnd = nextBytesPrefixKey(m.prefix)
prefixedEnd = NextBytesPrefixKey(m.prefix)
} else {
prefixedEnd = append(m.prefix, end...)
}
Expand All @@ -185,10 +185,10 @@ func (m Map[K, V]) IterateRaw(ctx context.Context, start, end []byte, order Orde
return Iterator[K, V]{}, ErrInvalidIterator
}
return Iterator[K, V]{
kc: m.kc,
vc: m.vc,
iter: storeIter,
prefixLength: len(m.prefix),
KeyCodec: m.kc,
ValueCodec: m.vc,
Iter: storeIter,
PrefixLength: len(m.prefix),
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cometbft/cometbft/crypto/sr25519"

"github.com/cosmos/cosmos-sdk/codec"
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -20,6 +21,7 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
ed25519.PubKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName, nil)
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute, nil)

Expand All @@ -30,4 +32,5 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
ed25519.PrivKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName, nil)
cdc.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName, nil)
}
11 changes: 11 additions & 0 deletions crypto/codec/cmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"

"cosmossdk.io/errors"
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -24,6 +25,10 @@ func FromCmtProtoPublicKey(protoPk cmtprotocrypto.PublicKey) (cryptotypes.PubKey
return &secp256k1.PubKey{
Key: protoPk.Secp256K1,
}, nil
case *cmtprotocrypto.PublicKey_Bls12381:

Check failure on line 28 in crypto/codec/cmt.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: cmtprotocrypto.PublicKey_Bls12381 (typecheck)
return &bls12_381.PubKey{
Key: protoPk.Bls12381,
}, nil
default:
return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from Tendermint public key", protoPk)
}
Expand All @@ -44,6 +49,12 @@ func ToCmtProtoPublicKey(pk cryptotypes.PubKey) (cmtprotocrypto.PublicKey, error
Secp256K1: pk.Key,
},
}, nil
case *bls12_381.PubKey:
return cmtprotocrypto.PublicKey{
Sum: &cmtprotocrypto.PublicKey_Bls12381{

Check failure on line 54 in crypto/codec/cmt.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: cmtprotocrypto.PublicKey_Bls12381 (typecheck)
Bls12381: pk.Key,
},
}, nil
default:
return cmtprotocrypto.PublicKey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk)
}
Expand Down
3 changes: 3 additions & 0 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package codec

import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -16,10 +17,12 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations(pk, &ed25519.PubKey{})
registry.RegisterImplementations(pk, &secp256k1.PubKey{})
registry.RegisterImplementations(pk, &multisig.LegacyAminoPubKey{})
registry.RegisterImplementations(pk, &bls12_381.PubKey{})

var priv *cryptotypes.PrivKey
registry.RegisterInterface("cosmos.crypto.PrivKey", priv)
registry.RegisterImplementations(priv, &secp256k1.PrivKey{})
registry.RegisterImplementations(priv, &ed25519.PrivKey{})
secp256r1.RegisterInterfaces(registry)
registry.RegisterImplementations(priv, &bls12_381.PrivKey{})
}
3 changes: 3 additions & 0 deletions crypto/hd/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
// Ed25519Type represents the Ed25519Type signature system.
// It is currently not supported for end-user keys (wallets/ledgers).
Ed25519Type = PubKeyType("ed25519")
// Ed25519Type represents the Ed25519Type signature system.
// It is currently not supported for end-user keys (wallets/ledgers).
Bls12_381Type = PubKeyType("bls12_381")
// Sr25519Type represents the Sr25519Type signature system.
Sr25519Type = PubKeyType("sr25519")
)
Expand Down
Loading

0 comments on commit 625a647

Please sign in to comment.