Skip to content

Commit

Permalink
XXX: go/consensus/tendermint: Bump Tendermint Core to 0.34
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Jun 26, 2020
1 parent 925b7f2 commit fb087a0
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 98 deletions.
11 changes: 6 additions & 5 deletions go/consensus/tendermint/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"

"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/kv"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
tmp2p "github.com/tendermint/tendermint/p2p"
tmkeys "github.com/tendermint/tendermint/proto/tendermint/crypto/keys"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
Expand All @@ -35,9 +35,10 @@ func PublicKeyToValidatorUpdate(id signature.PublicKey, power int64) types.Valid
pk, _ := id.MarshalBinary()

return types.ValidatorUpdate{
PubKey: types.PubKey{
Type: types.PubKeyEd25519,
Data: pk,
PubKey: tmkeys.PublicKey{
&tmkeys.PublicKey_Ed25519{
Ed25519: pk,
},
},
Power: power,
}
Expand Down Expand Up @@ -85,7 +86,7 @@ type EventBuilder struct {

// Attribute appends a key/value pair to the event.
func (bld *EventBuilder) Attribute(key, value []byte) *EventBuilder {
bld.ev.Attributes = append(bld.ev.Attributes, kv.Pair{
bld.ev.Attributes = append(bld.ev.Attributes, types.EventAttribute{
Key: key,
Value: value,
})
Expand Down
24 changes: 12 additions & 12 deletions go/consensus/tendermint/apps/scheduler/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.Reque
staticValidators := make(map[signature.PublicKey]int64)
for _, v := range req.Validators {
tmPk := v.GetPubKey()
pk := tmPk.GetEd25519()

if t := tmPk.GetType(); t != types.PubKeyEd25519 {
if pk == nil {
ctx.Logger().Error("invalid static validator public key type",
"public_key", hex.EncodeToString(tmPk.GetData()),
"type", t,
"type", v.GetPubKey(),
)
return fmt.Errorf("scheduler: invalid static validator public key type: '%v'", t)
return fmt.Errorf("scheduler: invalid static validator public key type: '%v'", v.GetPubKey())
}

var id signature.PublicKey
if err = id.UnmarshalBinary(tmPk.GetData()); err != nil {
if err = id.UnmarshalBinary(pk); err != nil {
ctx.Logger().Error("invalid static validator public key",
"err", err,
"public_key", hex.EncodeToString(tmPk.GetData()),
"public_key", hex.EncodeToString(pk),
)
return fmt.Errorf("scheduler: invalid static validator public key: %w", err)
}
Expand Down Expand Up @@ -103,20 +103,20 @@ func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.Reque
currentValidators := make(map[signature.PublicKey]int64)
for _, v := range req.Validators {
tmPk := v.GetPubKey()
pk := tmPk.GetEd25519()

if t := tmPk.GetType(); t != types.PubKeyEd25519 {
if pk == nil {
ctx.Logger().Error("invalid genesis validator public key type",
"public_key", hex.EncodeToString(tmPk.GetData()),
"type", t,
"type", v.GetPubKey(),
)
return fmt.Errorf("scheduler: invalid genesis validator public key type: '%v'", t)
return fmt.Errorf("scheduler: invalid genesis validator public key type: '%v'", v.GetPubKey())
}

var id signature.PublicKey
if err = id.UnmarshalBinary(tmPk.GetData()); err != nil {
if err = id.UnmarshalBinary(pk); err != nil {
ctx.Logger().Error("invalid genesis validator public key",
"err", err,
"public_key", hex.EncodeToString(tmPk.GetData()),
"public_key", hex.EncodeToString(pk),
)
return fmt.Errorf("scheduler: invalid genesis validator public key: %w", err)
}
Expand Down
17 changes: 9 additions & 8 deletions go/consensus/tendermint/crypto/priv_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ import (
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/tempfile"
"github.com/tendermint/tendermint/privval"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
Expand Down Expand Up @@ -234,11 +235,11 @@ const (
stepPrecommit int8 = 3
)

func voteToStep(vote *tmtypes.Vote) int8 {
func voteToStep(vote *tmproto.Vote) int8 {
switch vote.Type {
case tmtypes.PrevoteType:
case tmproto.PrevoteType:
return stepPrevote
case tmtypes.PrecommitType:
case tmproto.PrecommitType:
return stepPrecommit
default:
panic("Unknown vote type")
Expand All @@ -257,15 +258,15 @@ func (pv *privVal) GetPubKey() (tmcrypto.PubKey, error) {
return PublicKeyToTendermint(&pv.PublicKey), nil
}

func (pv *privVal) SignVote(chainID string, vote *tmtypes.Vote) error {
func (pv *privVal) SignVote(chainID string, vote *tmproto.Vote) error {
height, round, step := vote.Height, vote.Round, voteToStep(vote)

doubleSigned, err := pv.CheckHRS(height, round, step)
if err != nil {
return fmt.Errorf("tendermint/crypto: failed to check vote H/R/S: %w", err)
}

signBytes := vote.SignBytes(chainID)
signBytes := tmtypes.VoteSignBytes(chainID, vote)
if doubleSigned {
if bytes.Equal(signBytes, pv.SignBytes) {
vote.Signature = pv.Signature
Expand All @@ -290,15 +291,15 @@ func (pv *privVal) SignVote(chainID string, vote *tmtypes.Vote) error {
return nil
}

func (pv *privVal) SignProposal(chainID string, proposal *tmtypes.Proposal) error {
func (pv *privVal) SignProposal(chainID string, proposal *tmproto.Proposal) error {
height, round, step := proposal.Height, proposal.Round, stepPropose

doubleSigned, err := pv.CheckHRS(height, round, step)
if err != nil {
return fmt.Errorf("tendermint/crypto: failed to check proposal H/R/S: %w", err)
}

signBytes := proposal.SignBytes(chainID)
signBytes := tmtypes.ProposalSignBytes(chainID, proposal)
if doubleSigned {
if bytes.Equal(signBytes, pv.SignBytes) {
proposal.Signature = pv.Signature
Expand All @@ -323,7 +324,7 @@ func (pv *privVal) SignProposal(chainID string, proposal *tmtypes.Proposal) erro
return nil
}

func (pv *privVal) update(height int64, round int, step int8, signBytes, sig []byte) error {
func (pv *privVal) update(height int64, round int32, step int8, signBytes, sig []byte) error {
pv.Height = height
pv.Round = round
pv.Step = step
Expand Down
12 changes: 8 additions & 4 deletions go/consensus/tendermint/crypto/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ var tendermintSignatureContext = signature.NewContext("oasis-core/tendermint")

// PublicKeyToTendermint converts a signature.PublicKey to the
// tendermint equivalent.
func PublicKeyToTendermint(k *signature.PublicKey) tmed.PubKeyEd25519 {
var tk tmed.PubKeyEd25519
func PublicKeyToTendermint(k *signature.PublicKey) tmed.PubKey {
tk := make(tmed.PubKey, tmed.PubKeySize)
copy(tk[:], (*k)[:])
return tk
}

// PublicKeyFromTendermint converts a tendermint public key to a
// signature.PublicKey.
func PublicKeyFromTendermint(tk *tmed.PubKeyEd25519) signature.PublicKey {
func PublicKeyFromTendermint(tk *tmed.PubKey) signature.PublicKey {
var k signature.PublicKey
_ = k.UnmarshalBinary(tk[:])
_ = k.UnmarshalBinary(tk.Bytes())
return k
}

Expand Down Expand Up @@ -56,6 +56,10 @@ func (s *tmSigner) Equals(other crypto.PrivKey) bool {
return s.PubKey().Equals(other.PubKey())
}

func (s *tmSigner) Type() string {
return "ed25519"
}

func init() {
tmed.EnableOasisDomainSeparation(string(tendermintSignatureContext))
}
34 changes: 22 additions & 12 deletions go/consensus/tendermint/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ import (
"context"
"fmt"

tmamino "github.com/tendermint/go-amino"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
tmstate "github.com/tendermint/tendermint/state"

consensusAPI "github.com/oasisprotocol/oasis-core/go/consensus/api"
)

// We must use Tendermint's amino codec as some Tendermint's types are not easily unmarshallable.
var aminoCodec = tmamino.NewCodec()

func init() {
tmrpctypes.RegisterAmino(aminoCodec)
}

// Implements LightClientBackend.
func (t *tendermintService) GetSignedHeader(ctx context.Context, height int64) (*consensusAPI.SignedHeader, error) {
if err := t.ensureStarted(ctx); err != nil {
Expand All @@ -33,9 +24,14 @@ func (t *tendermintService) GetSignedHeader(ctx context.Context, height int64) (
return nil, fmt.Errorf("tendermint: header is nil")
}

meta, err := commit.SignedHeader.ToProto().Marshal()
if err != nil {
return nil, fmt.Errorf("tendermint: failed to marshal signed header: %w", err)
}

return &consensusAPI.SignedHeader{
Height: commit.Header.Height,
Meta: aminoCodec.MustMarshalBinaryBare(commit.SignedHeader),
Meta: meta,
}, nil
}

Expand All @@ -51,9 +47,18 @@ func (t *tendermintService) GetValidatorSet(ctx context.Context, height int64) (
return nil, consensusAPI.ErrVersionNotFound
}

protoVals, err := vals.ToProto()
if err != nil {
return nil, fmt.Errorf("tendermint: failed to marshal validators: %w", err)
}
meta, err := protoVals.Marshal()
if err != nil {
return nil, fmt.Errorf("tendermint: failed to marshal validators: %w", err)
}

return &consensusAPI.ValidatorSet{
Height: height,
Meta: aminoCodec.MustMarshalBinaryBare(vals),
Meta: meta,
}, nil
}

Expand All @@ -68,8 +73,13 @@ func (t *tendermintService) GetParameters(ctx context.Context, height int64) (*c
return nil, fmt.Errorf("%w: tendermint: consensus params query failed: %s", consensusAPI.ErrVersionNotFound, err.Error())
}

meta, err := params.ConsensusParams.Marshal()
if err != nil {
return nil, fmt.Errorf("tendermint: failed to marshal consensus params: %w", err)
}

return &consensusAPI.Parameters{
Height: params.BlockHeight,
Meta: aminoCodec.MustMarshalBinaryBare(params.ConsensusParams),
Meta: meta,
}, nil
}
17 changes: 10 additions & 7 deletions go/consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
tmmempool "github.com/tendermint/tendermint/mempool"
tmnode "github.com/tendermint/tendermint/node"
tmp2p "github.com/tendermint/tendermint/p2p"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmproxy "github.com/tendermint/tendermint/proxy"
tmcli "github.com/tendermint/tendermint/rpc/client/local"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
Expand Down Expand Up @@ -1215,17 +1216,19 @@ func genesisToTendermint(d *genesisAPI.Document) (*tmtypes.GenesisDoc, error) {
doc := tmtypes.GenesisDoc{
ChainID: d.ChainContext()[:tmtypes.MaxChainIDLen],
GenesisTime: d.Time,
ConsensusParams: &tmtypes.ConsensusParams{
Block: tmtypes.BlockParams{
ConsensusParams: &tmproto.ConsensusParams{
Block: tmproto.BlockParams{
MaxBytes: int64(d.Consensus.Parameters.MaxBlockSize),
MaxGas: maxBlockGas,
TimeIotaMs: 1000,
},
Evidence: tmtypes.EvidenceParams{
MaxAgeNumBlocks: int64(d.Consensus.Parameters.MaxEvidenceAgeBlocks),
MaxAgeDuration: d.Consensus.Parameters.MaxEvidenceAgeTime,
Evidence: tmproto.EvidenceParams{
MaxAgeNumBlocks: int64(d.Consensus.Parameters.MaxEvidenceAgeBlocks),
MaxAgeDuration: d.Consensus.Parameters.MaxEvidenceAgeTime,
MaxNum: 50,
ProofTrialPeriod: 50_000,
},
Validator: tmtypes.ValidatorParams{
Validator: tmproto.ValidatorParams{
PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519},
},
},
Expand Down Expand Up @@ -1313,7 +1316,7 @@ func (t *tendermintService) syncWorker() {
}
}()

return t.node.ConsensusReactor().FastSync(), nil
return t.node.ConsensusReactor().WaitSync(), nil
}

for {
Expand Down
12 changes: 6 additions & 6 deletions go/consensus/tendermint/tests/evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/oasisprotocol/oasis-core/go/common/identity"
Expand Down Expand Up @@ -58,10 +59,7 @@ func MakeDoubleSignEvidence(t *testing.T, ident *identity.Identity) consensus.Ev
},
}
now := time.Now()
pk, err := pv1.GetPubKey()
require.NoError(err, "GetPubKey")
ev := &tmtypes.DuplicateVoteEvidence{
PubKey: pk,
// NOTE: ChainID must match the unit test genesis block.
VoteA: makeVote(pv1, genesisTestHelpers.TestChainID, 0, 1, 2, 1, blockID1, now),
VoteB: makeVote(pv2, genesisTestHelpers.TestChainID, 0, 1, 2, 1, blockID2, now),
Expand All @@ -70,7 +68,7 @@ func MakeDoubleSignEvidence(t *testing.T, ident *identity.Identity) consensus.Ev
}

// makeVote copied from Tendermint test suite.
func makeVote(val tmtypes.PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID tmtypes.BlockID, ts time.Time) *tmtypes.Vote {
func makeVote(val tmtypes.PrivValidator, chainID string, valIndex int32, height int64, round, step int, blockID tmtypes.BlockID, ts time.Time) *tmtypes.Vote {
pk, err := val.GetPubKey()
if err != nil {
panic(err)
Expand All @@ -81,13 +79,15 @@ func makeVote(val tmtypes.PrivValidator, chainID string, valIndex int, height in
ValidatorIndex: valIndex,
Height: height,
Round: round,
Type: tmtypes.SignedMsgType(step),
Type: tmproto.SignedMsgType(step),
BlockID: blockID,
Timestamp: ts,
}
err = val.SignVote(chainID, v)
vpb := v.ToProto()
err = val.SignVote(chainID, vpb)
if err != nil {
panic(err)
}
v.Signature = vpb.Signature
return v
}
Loading

0 comments on commit fb087a0

Please sign in to comment.