mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 06:31:57 +00:00
proto: leftover amino (#4986)
This commit is contained in:
@@ -18,8 +18,8 @@ import (
|
||||
var _ crypto.PrivKey = PrivKey{}
|
||||
|
||||
const (
|
||||
PrivKeyAminoName = "tendermint/PrivKeyEd25519"
|
||||
PubKeyAminoName = "tendermint/PubKeyEd25519"
|
||||
PrivKeyName = "tendermint/PrivKeyEd25519"
|
||||
PubKeyName = "tendermint/PubKeyEd25519"
|
||||
// PubKeySize is is the size, in bytes, of public keys as used in this package.
|
||||
PubKeySize = 32
|
||||
// PrivateKeySize is the size, in bytes, of private keys as used in this package.
|
||||
@@ -35,8 +35,8 @@ const (
|
||||
)
|
||||
|
||||
func init() {
|
||||
tmjson.RegisterType(PubKey{}, PubKeyAminoName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyAminoName)
|
||||
tmjson.RegisterType(PubKey{}, PubKeyName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyName)
|
||||
}
|
||||
|
||||
// PrivKey implements crypto.PrivKey.
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package ed25519
|
||||
|
||||
import (
|
||||
amino "github.com/tendermint/go-amino"
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PubKey{},
|
||||
PubKeyAminoName, nil)
|
||||
|
||||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PrivKey{},
|
||||
PrivKeyAminoName, nil)
|
||||
}
|
||||
@@ -30,9 +30,9 @@ func init() {
|
||||
|
||||
// TODO: Have amino provide a way to go from concrete struct to route directly.
|
||||
// Its currently a private API
|
||||
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyName
|
||||
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyName
|
||||
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyName
|
||||
}
|
||||
|
||||
// PubkeyAminoName returns the amino route of a pubkey
|
||||
@@ -48,19 +48,19 @@ func RegisterAmino(cdc *amino.Codec) {
|
||||
// These are all written here instead of
|
||||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
||||
cdc.RegisterConcrete(ed25519.PubKey{},
|
||||
ed25519.PubKeyAminoName, nil)
|
||||
ed25519.PubKeyName, nil)
|
||||
cdc.RegisterConcrete(sr25519.PubKey{},
|
||||
sr25519.PubKeyAminoName, nil)
|
||||
sr25519.PubKeyName, nil)
|
||||
cdc.RegisterConcrete(secp256k1.PubKey{},
|
||||
secp256k1.PubKeyAminoName, nil)
|
||||
secp256k1.PubKeyName, nil)
|
||||
|
||||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
|
||||
cdc.RegisterConcrete(ed25519.PrivKey{},
|
||||
ed25519.PrivKeyAminoName, nil)
|
||||
ed25519.PrivKeyName, nil)
|
||||
cdc.RegisterConcrete(sr25519.PrivKey{},
|
||||
sr25519.PrivKeyAminoName, nil)
|
||||
sr25519.PrivKeyName, nil)
|
||||
cdc.RegisterConcrete(secp256k1.PrivKey{},
|
||||
secp256k1.PrivKeyAminoName, nil)
|
||||
secp256k1.PrivKeyName, nil)
|
||||
}
|
||||
|
||||
// RegisterKeyType registers an external key type to allow decoding it from bytes
|
||||
|
||||
@@ -135,9 +135,9 @@ func TestPubkeyAminoName(t *testing.T) {
|
||||
want string
|
||||
found bool
|
||||
}{
|
||||
{ed25519.PubKey{}, ed25519.PubKeyAminoName, true},
|
||||
{sr25519.PubKey{}, sr25519.PubKeyAminoName, true},
|
||||
{secp256k1.PubKey{}, secp256k1.PubKeyAminoName, true},
|
||||
{ed25519.PubKey{}, ed25519.PubKeyName, true},
|
||||
{sr25519.PubKey{}, sr25519.PubKeyName, true},
|
||||
{secp256k1.PubKey{}, secp256k1.PubKeyName, true},
|
||||
}
|
||||
for i, tc := range tests {
|
||||
got, found := PubkeyAminoName(cdc, tc.key)
|
||||
@@ -218,7 +218,7 @@ func TestRegisterKeyType(t *testing.T) {
|
||||
cdc = amino.NewCodec()
|
||||
nameTable = make(map[reflect.Type]string, 3)
|
||||
RegisterAmino(cdc)
|
||||
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyAminoName
|
||||
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyName
|
||||
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyName
|
||||
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyName
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package merkle
|
||||
|
||||
import (
|
||||
amino "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
var cdc *amino.Codec
|
||||
|
||||
func init() {
|
||||
cdc = amino.NewCodec()
|
||||
cdc.Seal()
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#! /bin/bash
|
||||
|
||||
protoc --gogo_out=. -I $GOPATH/src/ -I . -I $GOPATH/src/github.com/gogo/protobuf/protobuf merkle.proto
|
||||
echo "--> adding nolint declarations to protobuf generated files"
|
||||
awk '/package merkle/ { print "//nolint: gas"; print; next }1' merkle.pb.go > merkle.pb.go.new
|
||||
mv merkle.pb.go.new merkle.pb.go
|
||||
@@ -134,6 +134,7 @@ func ProofFromProto(pb *tmmerkle.Proof) (*Proof, error) {
|
||||
if pb == nil {
|
||||
return nil, errors.New("nil proof")
|
||||
}
|
||||
|
||||
sp := new(Proof)
|
||||
|
||||
sp.Total = pb.Total
|
||||
|
||||
+39
-17
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
amino "github.com/tendermint/go-amino"
|
||||
"github.com/stretchr/testify/require"
|
||||
tmmerkle "github.com/tendermint/tendermint/proto/crypto/merkle"
|
||||
)
|
||||
|
||||
@@ -28,21 +28,17 @@ func NewDominoOp(key, input, output string) DominoOp {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func DominoOpDecoder(pop tmmerkle.ProofOp) (ProofOperator, error) {
|
||||
if pop.Type != ProofOpDomino {
|
||||
panic("unexpected proof op type")
|
||||
}
|
||||
var op DominoOp // a bit strange as we'll discard this, but it works.
|
||||
err := amino.UnmarshalBinaryLengthPrefixed(pop.Data, &op)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding ProofOp.Data into ValueOp: %w", err)
|
||||
}
|
||||
return NewDominoOp(string(pop.Key), op.Input, op.Output), nil
|
||||
}
|
||||
|
||||
func (dop DominoOp) ProofOp() tmmerkle.ProofOp {
|
||||
bz := amino.MustMarshalBinaryLengthPrefixed(dop)
|
||||
dopb := tmmerkle.DominoOp{
|
||||
Key: dop.key,
|
||||
Input: dop.Input,
|
||||
Output: dop.Output,
|
||||
}
|
||||
bz, err := dopb.Marshal()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return tmmerkle.ProofOp{
|
||||
Type: ProofOpDomino,
|
||||
Key: []byte(dop.key),
|
||||
@@ -72,8 +68,6 @@ func TestProofOperators(t *testing.T) {
|
||||
|
||||
// ProofRuntime setup
|
||||
// TODO test this somehow.
|
||||
// prt := NewProofRuntime()
|
||||
// prt.RegisterOpDecoder(ProofOpDomino, DominoOpDecoder)
|
||||
|
||||
// ProofOperators setup
|
||||
op1 := NewDominoOp("KEY1", "INPUT1", "INPUT2")
|
||||
@@ -175,3 +169,31 @@ func TestProofValidateBasic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestVoteProtobuf(t *testing.T) {
|
||||
|
||||
_, proofs := ProofsFromByteSlices([][]byte{
|
||||
[]byte("apple"),
|
||||
[]byte("watermelon"),
|
||||
[]byte("kiwi"),
|
||||
})
|
||||
testCases := []struct {
|
||||
testName string
|
||||
v1 *Proof
|
||||
expPass bool
|
||||
}{
|
||||
{"empty proof", &Proof{}, false},
|
||||
{"failure nil", nil, false},
|
||||
{"success", proofs[0], true},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
pb := tc.v1.ToProto()
|
||||
|
||||
v, err := ProofFromProto(pb)
|
||||
if tc.expPass {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.v1, v, tc.testName)
|
||||
} else {
|
||||
require.Error(t, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ const ProofOpValue = "simple:v"
|
||||
// ValueOp takes a key and a single value as argument and
|
||||
// produces the root hash. The corresponding tree structure is
|
||||
// the SimpleMap tree. SimpleMap takes a Hasher, and currently
|
||||
// Tendermint uses aminoHasher. ValueOp should support
|
||||
// the hash function as used in aminoHasher. TODO support
|
||||
// Tendermint uses tmhash. SimpleValueOp should support
|
||||
// the hash function as used in tmhash. TODO support
|
||||
// additional hash functions here as options/args to this
|
||||
// operator.
|
||||
//
|
||||
@@ -41,16 +41,28 @@ func ValueOpDecoder(pop tmmerkle.ProofOp) (ProofOperator, error) {
|
||||
if pop.Type != ProofOpValue {
|
||||
return nil, fmt.Errorf("unexpected ProofOp.Type; got %v, want %v", pop.Type, ProofOpValue)
|
||||
}
|
||||
var op ValueOp // a bit strange as we'll discard this, but it works.
|
||||
err := cdc.UnmarshalBinaryLengthPrefixed(pop.Data, &op)
|
||||
var pbop tmmerkle.ValueOp // a bit strange as we'll discard this, but it works.
|
||||
err := pbop.Unmarshal(pop.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decoding ProofOp.Data into ValueOp: %w", err)
|
||||
}
|
||||
return NewValueOp(pop.Key, op.Proof), nil
|
||||
|
||||
sp, err := ProofFromProto(pbop.Proof)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewValueOp(pop.Key, sp), nil
|
||||
}
|
||||
|
||||
func (op ValueOp) ProofOp() tmmerkle.ProofOp {
|
||||
bz := cdc.MustMarshalBinaryLengthPrefixed(op)
|
||||
pbval := tmmerkle.ValueOp{
|
||||
Key: op.key,
|
||||
Proof: op.Proof.ToProto(),
|
||||
}
|
||||
bz, err := pbval.Marshal()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return tmmerkle.ProofOp{
|
||||
Type: ProofOpValue,
|
||||
Key: op.key,
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package merkle
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
amino "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
// Tree is a Merkle tree interface.
|
||||
@@ -29,5 +28,12 @@ type Tree interface {
|
||||
|
||||
// Uvarint length prefixed byteslice
|
||||
func encodeByteSlice(w io.Writer, bz []byte) (err error) {
|
||||
return amino.EncodeByteSlice(w, bz)
|
||||
var buf [binary.MaxVarintLen64]byte
|
||||
n := binary.PutUvarint(buf[:], uint64(len(bz)))
|
||||
_, err = w.Write(buf[0:n])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = w.Write(bz)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,28 +1,15 @@
|
||||
package secp256k1
|
||||
|
||||
import (
|
||||
amino "github.com/tendermint/go-amino"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
)
|
||||
|
||||
const (
|
||||
PrivKeyAminoName = "tendermint/PrivKeySecp256k1"
|
||||
PubKeyAminoName = "tendermint/PubKeySecp256k1"
|
||||
PrivKeyName = "tendermint/PrivKeySecp256k1"
|
||||
PubKeyName = "tendermint/PubKeySecp256k1"
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PubKey{},
|
||||
PubKeyAminoName, nil)
|
||||
|
||||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PrivKey{},
|
||||
PrivKeyAminoName, nil)
|
||||
|
||||
tmjson.RegisterType(PubKey{}, PubKeyAminoName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyAminoName)
|
||||
tmjson.RegisterType(PubKey{}, PubKeyName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyName)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ const (
|
||||
// PrivKey implements PrivKey.
|
||||
type PrivKey []byte
|
||||
|
||||
// Bytes marshalls the private key using amino encoding.
|
||||
// Bytes returns the byte representation of the Private Key.
|
||||
func (privKey PrivKey) Bytes() []byte {
|
||||
return []byte(privKey)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package sr25519
|
||||
|
||||
import (
|
||||
"github.com/tendermint/go-amino"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
)
|
||||
@@ -10,25 +8,16 @@ import (
|
||||
var _ crypto.PrivKey = PrivKey{}
|
||||
|
||||
const (
|
||||
PrivKeyAminoName = "tendermint/PrivKeySr25519"
|
||||
PubKeyAminoName = "tendermint/PubKeySr25519"
|
||||
PrivKeyName = "tendermint/PrivKeySr25519"
|
||||
PubKeyName = "tendermint/PubKeySr25519"
|
||||
|
||||
// SignatureSize is the size of an Edwards25519 signature. Namely the size of a compressed
|
||||
// Sr25519 point, and a field element. Both of which are 32 bytes.
|
||||
SignatureSize = 64
|
||||
)
|
||||
|
||||
var cdc = amino.NewCodec()
|
||||
|
||||
func init() {
|
||||
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PubKey{},
|
||||
PubKeyAminoName, nil)
|
||||
|
||||
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
|
||||
cdc.RegisterConcrete(PrivKey{},
|
||||
PrivKeyAminoName, nil)
|
||||
|
||||
tmjson.RegisterType(PubKey{}, PubKeyAminoName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyAminoName)
|
||||
tmjson.RegisterType(PubKey{}, PubKeyName)
|
||||
tmjson.RegisterType(PrivKey{}, PrivKeyName)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ const PrivKeySize = 32
|
||||
// PrivKeySr25519 implements crypto.PrivKey.
|
||||
type PrivKey []byte
|
||||
|
||||
// Bytes marshals the privkey using amino encoding.
|
||||
// Bytes returns the byte representation of the PrivKey.
|
||||
func (privKey PrivKey) Bytes() []byte {
|
||||
return []byte(privKey)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func (pubKey PubKey) Address() crypto.Address {
|
||||
return crypto.Address(tmhash.SumTruncated(pubKey[:]))
|
||||
}
|
||||
|
||||
// Bytes marshals the PubKey using amino encoding.
|
||||
// Bytes returns the byte representation of the PubKey.
|
||||
func (pubKey PubKey) Bytes() []byte {
|
||||
return []byte(pubKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user