Browse Source

encoding: remove codecs (#4996)

## Description

This pr removes amino from tendermint. 

Closes: #4278
pull/5010/head
Marko 5 years ago
committed by GitHub
parent
commit
b8b50733f0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1 additions and 425 deletions
  1. +0
    -93
      consensus/types/round_state_test.go
  2. +0
    -82
      crypto/encoding/amino/amino.go
  3. +0
    -224
      crypto/encoding/amino/encode_test.go
  4. +1
    -0
      crypto/merkle/proof_test.go
  5. +0
    -1
      go.mod
  6. +0
    -2
      go.sum
  7. +0
    -13
      node/codec.go
  8. +0
    -10
      state/main_test.go

+ 0
- 93
consensus/types/round_state_test.go View File

@ -1,93 +0,0 @@
package types
import (
"testing"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/tmhash"
tmrand "github.com/tendermint/tendermint/libs/rand"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
func BenchmarkRoundStateDeepCopy(b *testing.B) {
b.StopTimer()
// Random validators
nval, ntxs := 100, 100
vset, _ := types.RandValidatorSet(nval, 1)
commitSigs := make([]types.CommitSig, nval)
blockID := types.BlockID{
Hash: tmrand.Bytes(tmhash.Size),
PartsHeader: types.PartSetHeader{
Hash: tmrand.Bytes(tmhash.Size),
Total: 1000,
},
}
sig := make([]byte, ed25519.SignatureSize)
for i := 0; i < nval; i++ {
commitSigs[i] = (&types.Vote{
ValidatorAddress: types.Address(tmrand.Bytes(20)),
Timestamp: tmtime.Now(),
BlockID: blockID,
Signature: sig,
}).CommitSig()
}
txs := make([]types.Tx, ntxs)
for i := 0; i < ntxs; i++ {
txs[i] = tmrand.Bytes(100)
}
// Random block
block := &types.Block{
Header: types.Header{
ChainID: tmrand.Str(12),
Time: tmtime.Now(),
LastBlockID: blockID,
LastCommitHash: tmrand.Bytes(20),
DataHash: tmrand.Bytes(20),
ValidatorsHash: tmrand.Bytes(20),
ConsensusHash: tmrand.Bytes(20),
AppHash: tmrand.Bytes(20),
LastResultsHash: tmrand.Bytes(20),
EvidenceHash: tmrand.Bytes(20),
},
Data: types.Data{
Txs: txs,
},
Evidence: types.EvidenceData{},
LastCommit: types.NewCommit(1, 0, blockID, commitSigs),
}
parts := block.MakePartSet(4096)
// Random Proposal
proposal := &types.Proposal{
Timestamp: tmtime.Now(),
BlockID: blockID,
Signature: sig,
}
// Random HeightVoteSet
// TODO: hvs :=
rs := &RoundState{
StartTime: tmtime.Now(),
CommitTime: tmtime.Now(),
Validators: vset,
Proposal: proposal,
ProposalBlock: block,
ProposalBlockParts: parts,
LockedBlock: block,
LockedBlockParts: parts,
ValidBlock: block,
ValidBlockParts: parts,
Votes: nil, // TODO
LastCommit: nil, // TODO
LastValidators: vset,
}
b.StartTimer()
for i := 0; i < b.N; i++ {
amino.DeepCopy(rs)
}
}

+ 0
- 82
crypto/encoding/amino/amino.go View File

@ -1,82 +0,0 @@
package cryptoamino
import (
"reflect"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
)
var cdc = amino.NewCodec()
// nameTable is used to map public key concrete types back
// to their registered amino names. This should eventually be handled
// by amino. Example usage:
// nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyAminoName
var nameTable = make(map[reflect.Type]string, 3)
func init() {
// NOTE: It's important that there be no conflicts here,
// as that would change the canonical representations,
// and therefore change the address.
// TODO: Remove above note when
// https://github.com/tendermint/go-amino/issues/9
// is resolved
RegisterAmino(cdc)
// 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.PubKeyName
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyName
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyName
}
// PubkeyAminoName returns the amino route of a pubkey
// cdc is currently passed in, as eventually this will not be using
// a package level codec.
func PubkeyAminoName(cdc *amino.Codec, key crypto.PubKey) (string, bool) {
route, found := nameTable[reflect.TypeOf(key)]
return route, found
}
// RegisterAmino registers all crypto related types in the given (amino) codec.
func RegisterAmino(cdc *amino.Codec) {
// These are all written here instead of
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
cdc.RegisterConcrete(ed25519.PubKey{},
ed25519.PubKeyName, nil)
cdc.RegisterConcrete(sr25519.PubKey{},
sr25519.PubKeyName, nil)
cdc.RegisterConcrete(secp256k1.PubKey{},
secp256k1.PubKeyName, nil)
cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
cdc.RegisterConcrete(ed25519.PrivKey{},
ed25519.PrivKeyName, nil)
cdc.RegisterConcrete(sr25519.PrivKey{},
sr25519.PrivKeyName, nil)
cdc.RegisterConcrete(secp256k1.PrivKey{},
secp256k1.PrivKeyName, nil)
}
// RegisterKeyType registers an external key type to allow decoding it from bytes
func RegisterKeyType(o interface{}, name string) {
cdc.RegisterConcrete(o, name, nil)
nameTable[reflect.TypeOf(o)] = name
}
// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey
func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) {
err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey)
return
}
// PubKeyFromBytes unmarshals public key bytes and returns a PubKey
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) {
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
return
}

+ 0
- 224
crypto/encoding/amino/encode_test.go View File

@ -1,224 +0,0 @@
package cryptoamino
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
tmjson "github.com/tendermint/tendermint/libs/json"
)
type AminoMarshal interface {
AminoMarshal() ([]byte, error)
AminoUnmarshal([]byte) error
}
func checkAminoBinary(t *testing.T, src, dst interface{}, size int) {
// Marshal to binary bytes.
bz, err := cdc.MarshalBinaryBare(src)
require.Nil(t, err, "%+v", err)
if byterSrc, ok := src.(AminoMarshal); ok {
// Make sure this is compatible with current (Bytes()) encoding.
bza, err := byterSrc.AminoMarshal()
assert.NoError(t, err)
assert.Equal(t, bza, bz, "Amino binary vs Bytes() mismatch")
}
// Make sure we have the expected length.
assert.Equal(t, size, len(bz), "Amino binary size mismatch")
// Unmarshal.
err = cdc.UnmarshalBinaryBare(bz, dst)
require.Nil(t, err, "%+v", err)
}
func checkJSON(t *testing.T, src interface{}, dst interface{}, isNil bool) {
// Marshal to JSON bytes.
js, err := tmjson.Marshal(src)
require.Nil(t, err, "%+v", err)
if isNil {
assert.Equal(t, string(js), `null`)
} else {
assert.Contains(t, string(js), `"type":`)
assert.Contains(t, string(js), `"value":`)
}
// Unmarshal.
err = tmjson.Unmarshal(js, dst)
require.Nil(t, err, "%+v", err)
}
func TestKeyEncodings(t *testing.T) {
cases := []struct {
privKey crypto.PrivKey
privSize, pubSize, sigSize int // binary sizes
}{
{
privKey: ed25519.GenPrivKey(),
privSize: 69,
pubSize: 37,
sigSize: 65,
},
{
privKey: sr25519.GenPrivKey(),
privSize: 37,
pubSize: 37,
sigSize: 65,
},
{
privKey: secp256k1.GenPrivKey(),
privSize: 37,
pubSize: 38,
sigSize: 65,
},
}
for tcIndex, tc := range cases {
// Check (de/en)codings of PrivKeys.
var priv2, priv3 crypto.PrivKey
checkAminoBinary(t, tc.privKey, &priv2, tc.privSize)
assert.EqualValues(t, tc.privKey, priv2, "tc #%d", tcIndex)
checkJSON(t, tc.privKey, &priv3, false) // TODO also check Prefix bytes.
assert.EqualValues(t, tc.privKey, priv3, "tc #%d", tcIndex)
// Check (de/en)codings of Signatures.
var sig1, sig2 []byte
sig1, err := tc.privKey.Sign([]byte("something"))
assert.NoError(t, err, "tc #%d", tcIndex)
checkAminoBinary(t, sig1, &sig2, tc.sigSize)
assert.EqualValues(t, sig1, sig2, "tc #%d", tcIndex)
// Check (de/en)codings of PubKeys.
pubKey := tc.privKey.PubKey()
var pub2, pub3 crypto.PubKey
checkAminoBinary(t, pubKey, &pub2, tc.pubSize)
assert.EqualValues(t, pubKey, pub2, "tc #%d", tcIndex)
checkJSON(t, pubKey, &pub3, false) // TODO also check Prefix bytes.
assert.EqualValues(t, pubKey, pub3, "tc #%d", tcIndex)
}
}
func TestNilEncodings(t *testing.T) {
// Check nil Signature.
var a, b []byte
checkJSON(t, &a, &b, true)
assert.EqualValues(t, a, b)
// Check nil PubKey.
var c, d crypto.PubKey
checkJSON(t, &c, &d, true)
assert.EqualValues(t, c, d)
// Check nil PrivKey.
var e, f crypto.PrivKey
checkJSON(t, &e, &f, true)
assert.EqualValues(t, e, f)
}
func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
pk, err := PubKeyFromBytes([]byte("foo"))
require.NotNil(t, err)
require.Nil(t, pk)
}
func TestPubkeyAminoName(t *testing.T) {
tests := []struct {
key crypto.PubKey
want string
found bool
}{
{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)
require.Equal(t, tc.found, found, "not equal on tc %d", i)
if tc.found {
require.Equal(t, tc.want, got, "not equal on tc %d", i)
}
}
}
var _ crypto.PrivKey = testPriv{}
var _ crypto.PubKey = testPub{}
var testCdc = amino.NewCodec()
type testPriv []byte
func (privkey testPriv) PubKey() crypto.PubKey { return testPub{} }
func (privkey testPriv) Bytes() []byte {
return testCdc.MustMarshalBinaryBare(privkey)
}
func (privkey testPriv) Sign(msg []byte) ([]byte, error) { return []byte{}, nil }
func (privkey testPriv) Equals(other crypto.PrivKey) bool { return true }
func (privkey testPriv) Type() string { return "testPriv" }
type testPub []byte
func (key testPub) Address() crypto.Address { return crypto.Address{} }
func (key testPub) Bytes() []byte {
return testCdc.MustMarshalBinaryBare(key)
}
func (key testPub) VerifyBytes(msg []byte, sig []byte) bool { return true }
func (key testPub) Equals(other crypto.PubKey) bool { return true }
func (key testPub) String() string { return "" }
func (key testPub) Type() string { return "testPub" }
var (
privAminoName = "registerTest/Priv"
pubAminoName = "registerTest/Pub"
)
func TestRegisterKeyType(t *testing.T) {
RegisterAmino(testCdc)
testCdc.RegisterConcrete(testPriv{}, privAminoName, nil)
testCdc.RegisterConcrete(testPub{}, pubAminoName, nil)
pub := testPub{0x1}
priv := testPriv{0x2}
// Check to make sure key cannot be decoded before registering
_, err := PrivKeyFromBytes(priv.Bytes())
require.Error(t, err)
_, err = PubKeyFromBytes(pub.Bytes())
require.Error(t, err)
// Check that name is not registered
_, found := PubkeyAminoName(testCdc, pub)
require.False(t, found)
// Register key types
RegisterKeyType(testPriv{}, privAminoName)
RegisterKeyType(testPub{}, pubAminoName)
// Name should exist after registering
name, found := PubkeyAminoName(testCdc, pub)
require.True(t, found)
require.Equal(t, name, pubAminoName)
// Decode keys using the encoded bytes from encoding with the other codec
decodedPriv, err := PrivKeyFromBytes(priv.Bytes())
require.NoError(t, err)
require.Equal(t, priv, decodedPriv)
decodedPub, err := PubKeyFromBytes(pub.Bytes())
require.NoError(t, err)
require.Equal(t, pub, decodedPub)
// Reset module codec after testing
cdc = amino.NewCodec()
nameTable = make(map[reflect.Type]string, 3)
RegisterAmino(cdc)
nameTable[reflect.TypeOf(ed25519.PubKey{})] = ed25519.PubKeyName
nameTable[reflect.TypeOf(sr25519.PubKey{})] = sr25519.PubKeyName
nameTable[reflect.TypeOf(secp256k1.PubKey{})] = secp256k1.PubKeyName
}

+ 1
- 0
crypto/merkle/proof_test.go View File

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
tmmerkle "github.com/tendermint/tendermint/proto/crypto/merkle"
)


+ 0
- 1
go.mod View File

@ -25,7 +25,6 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.7.0
github.com/stretchr/testify v1.6.1
github.com/tendermint/go-amino v0.14.1
github.com/tendermint/tm-db v0.5.1
golang.org/x/crypto v0.0.0-20200406173513-056763e48d71
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e


+ 0
- 2
go.sum View File

@ -446,8 +446,6 @@ github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok=
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=
github.com/tendermint/go-amino v0.14.1 h1:o2WudxNfdLNBwMyl2dqOJxiro5rfrEaU0Ugs6offJMk=
github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso=
github.com/tendermint/tm-db v0.5.1 h1:H9HDq8UEA7Eeg13kdYckkgwwkQLBnJGgX4PgLJRhieY=
github.com/tendermint/tm-db v0.5.1/go.mod h1:g92zWjHpCYlEvQXvy9M168Su8V1IBEeawpXVVBaK4f4=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=


+ 0
- 13
node/codec.go View File

@ -1,13 +0,0 @@
package node
import (
amino "github.com/tendermint/go-amino"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
)
var cdc = amino.NewCodec()
func init() {
cryptoamino.RegisterAmino(cdc)
}

+ 0
- 10
state/main_test.go View File

@ -1,10 +0,0 @@
package state_test
import (
"os"
"testing"
)
func TestMain(m *testing.M) {
os.Exit(m.Run())
}

Loading…
Cancel
Save