Skip to content

Commit

Permalink
Fix up after tendermint upgrade
Browse files Browse the repository at this point in the history
untangle some crypto in process

Signed-off-by: Silas Davis <[email protected]>
  • Loading branch information
Silas Davis committed Aug 16, 2018
1 parent b0be9b3 commit e149519
Show file tree
Hide file tree
Showing 22 changed files with 936 additions and 238 deletions.
10 changes: 5 additions & 5 deletions acm/validator/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ func NewRing(initialSet Iterable, windowSize int) *Ring {

// Implement Reader
// Get power at index from the delta bucket then falling through to the cumulative
func (vc *Ring) PowerAt(index int64, id crypto.Addressable) *big.Int {
func (vc *Ring) PowerAt(index int64, id crypto.PublicKey) *big.Int {
power := vc.Head().MaybePower(id)
if power != nil {
return power
}
return vc.Cum().Power(id)
}

func (vc *Ring) Power(id crypto.Addressable) *big.Int {
func (vc *Ring) Power(id crypto.PublicKey) *big.Int {
return vc.PowerAt(vc.head, id)
}

Expand All @@ -67,7 +67,7 @@ func (vc *Ring) Resultant(index int64) *Set {
i := vc.index(index)
cum := CopyTrim(vc.cum[i])
vc.delta[i].Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
cum.AlterPower(id, power)
cum.AlterPower(id.PublicKey(), power)
return
})
return cum
Expand All @@ -78,7 +78,7 @@ func (vc *Ring) TotalPower() *big.Int {
}

// Updates the current head bucket (accumulator) with some safety checks
func (vc *Ring) AlterPower(id crypto.Addressable, power *big.Int) (*big.Int, error) {
func (vc *Ring) AlterPower(id crypto.PublicKey, power *big.Int) (*big.Int, error) {
if power.Sign() == -1 {
return nil, fmt.Errorf("cannot set negative validator power: %v", power)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (vc *Ring) AlterPower(id crypto.Addressable, power *big.Int) (*big.Int, err
}

// Returns the flow that would be induced by a validator change by comparing the head accumulater with the current set
func (vc *Ring) Flow(id crypto.Addressable, power *big.Int) *big.Int {
func (vc *Ring) Flow(id crypto.PublicKey, power *big.Int) *big.Int {
flow := new(big.Int)
return flow.Abs(flow.Sub(power, vc.Cum().Power(id)))
}
Expand Down
12 changes: 6 additions & 6 deletions acm/validator/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func NewTrimSet() *Set {
}

// Implements Writer, but will never error
func (vs *Set) AlterPower(id crypto.Addressable, power *big.Int) (flow *big.Int, err error) {
func (vs *Set) AlterPower(id crypto.PublicKey, power *big.Int) (flow *big.Int, err error) {
return vs.ChangePower(id, power), nil
}

// Add the power of a validator and returns the flow into that validator
func (vs *Set) ChangePower(id crypto.Addressable, power *big.Int) *big.Int {
func (vs *Set) ChangePower(id crypto.PublicKey, power *big.Int) *big.Int {
address := id.Address()
// Calculcate flow into this validator (postive means in, negative means out)
flow := new(big.Int).Sub(power, vs.Power(id))
Expand All @@ -56,7 +56,7 @@ func (vs *Set) ChangePower(id crypto.Addressable, power *big.Int) *big.Int {
delete(vs.publicKeys, address)
delete(vs.powers, address)
} else {
vs.publicKeys[address] = crypto.MemoizeAddressable(id)
vs.publicKeys[address] = crypto.NewAddressable(id)
vs.powers[address] = new(big.Int).Set(power)
}
return flow
Expand All @@ -67,14 +67,14 @@ func (vs *Set) TotalPower() *big.Int {
}

// Returns the power of id but only if it is set
func (vs *Set) MaybePower(id crypto.Addressable) *big.Int {
func (vs *Set) MaybePower(id crypto.PublicKey) *big.Int {
if vs.powers[id.Address()] == nil {
return nil
}
return new(big.Int).Set(vs.powers[id.Address()])
}

func (vs *Set) Power(id crypto.Addressable) *big.Int {
func (vs *Set) Power(id crypto.PublicKey) *big.Int {
if vs.powers[id.Address()] == nil {
return new(big.Int)
}
Expand All @@ -87,7 +87,7 @@ func (vs *Set) Equal(vsOther *Set) bool {
}
// Stop iteration IFF we find a non-matching validator
return !vs.Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
otherPower := vsOther.Power(id)
otherPower := vsOther.Power(id.PublicKey())
if otherPower.Cmp(power) != 0 {
return true
}
Expand Down
22 changes: 11 additions & 11 deletions acm/validator/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type Writer interface {
AlterPower(id crypto.Addressable, power *big.Int) (flow *big.Int, err error)
AlterPower(id crypto.PublicKey, power *big.Int) (flow *big.Int, err error)
}

type Reader interface {
Power(id crypto.Addressable) *big.Int
Power(id crypto.PublicKey) *big.Int
}

type Iterable interface {
Expand All @@ -35,34 +35,34 @@ type IterableReaderWriter interface {
Iterable
}

type WriterFunc func(id crypto.Addressable, power *big.Int) (flow *big.Int, err error)
type WriterFunc func(id crypto.PublicKey, power *big.Int) (flow *big.Int, err error)

func SyncWriter(locker sync.Locker, writerFunc WriterFunc) WriterFunc {
return WriterFunc(func(id crypto.Addressable, power *big.Int) (flow *big.Int, err error) {
return WriterFunc(func(id crypto.PublicKey, power *big.Int) (flow *big.Int, err error) {
locker.Lock()
defer locker.Unlock()
return writerFunc(id, power)
})
}

func (wf WriterFunc) AlterPower(id crypto.Addressable, power *big.Int) (flow *big.Int, err error) {
func (wf WriterFunc) AlterPower(id crypto.PublicKey, power *big.Int) (flow *big.Int, err error) {
return wf(id, power)
}

func AddPower(vs ReaderWriter, id crypto.Addressable, power *big.Int) error {
func AddPower(vs ReaderWriter, id crypto.PublicKey, power *big.Int) error {
// Current power + power
_, err := vs.AlterPower(id, new(big.Int).Add(vs.Power(id), power))
return err
}

func SubtractPower(vs ReaderWriter, id crypto.Addressable, power *big.Int) error {
func SubtractPower(vs ReaderWriter, id crypto.PublicKey, power *big.Int) error {
_, err := vs.AlterPower(id, new(big.Int).Sub(vs.Power(id), power))
return err
}

func Alter(vs Writer, vsOther Iterable) (err error) {
vsOther.Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
_, err = vs.AlterPower(id, power)
_, err = vs.AlterPower(id.PublicKey(), power)
if err != nil {
return true
}
Expand All @@ -74,7 +74,7 @@ func Alter(vs Writer, vsOther Iterable) (err error) {
// Adds vsOther to vs
func Add(vs ReaderWriter, vsOther Iterable) (err error) {
vsOther.Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
err = AddPower(vs, id, power)
err = AddPower(vs, id.PublicKey(), power)
if err != nil {
return true
}
Expand All @@ -86,7 +86,7 @@ func Add(vs ReaderWriter, vsOther Iterable) (err error) {
// Subtracts vsOther from vs
func Subtract(vs ReaderWriter, vsOther Iterable) (err error) {
vsOther.Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
err = SubtractPower(vs, id, power)
err = SubtractPower(vs, id.PublicKey(), power)
if err != nil {
return true
}
Expand All @@ -98,7 +98,7 @@ func Subtract(vs ReaderWriter, vsOther Iterable) (err error) {
func Copy(vs Iterable) *Set {
vsCopy := NewSet()
vs.Iterate(func(id crypto.Addressable, power *big.Int) (stop bool) {
vsCopy.ChangePower(id, power)
vsCopy.ChangePower(id.PublicKey(), power)
return
})
return vsCopy
Expand Down
17 changes: 9 additions & 8 deletions cmd/burrow/commands/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
"github.com/hyperledger/burrow/genesis/spec"
"github.com/hyperledger/burrow/keys"
"github.com/hyperledger/burrow/logging"
logging_config "github.com/hyperledger/burrow/logging/logconfig"
"github.com/hyperledger/burrow/logging/logconfig"
"github.com/hyperledger/burrow/logging/logconfig/presets"
cli "github.com/jawher/mow.cli"
amino "github.com/tendermint/go-amino"
tm_crypto "github.com/tendermint/tendermint/crypto"
"github.com/jawher/mow.cli"
"github.com/tendermint/go-amino"
tmEd25519 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/p2p"
)

Expand Down Expand Up @@ -130,7 +131,7 @@ func Configure(output Output) func(cmd *cli.Cmd) {
}

cdc := amino.NewCodec()
tm_crypto.RegisterAmino(cdc)
cryptoAmino.RegisterAmino(cdc)

pkg = deployment.Config{Keys: make(map[crypto.Address]deployment.Key)}

Expand All @@ -154,8 +155,8 @@ func Configure(output Output) func(cmd *cli.Cmd) {
}

if nodeKey {
privKey := tm_crypto.GenPrivKeyEd25519()
copy(privKey[:], key.PrivateKey.Key)
privKey := tmEd25519.GenPrivKey()
copy(privKey[:], key.PrivateKey.PrivateKey)
nodeKey := &p2p.NodeKey{
PrivKey: privKey,
}
Expand Down Expand Up @@ -232,7 +233,7 @@ func Configure(output Output) func(cmd *cli.Cmd) {
output.Fatalf("could not build logging configuration: %v\n\nTo see possible logging "+
"instructions run:\n burrow configure --describe-logging", err)
}
conf.Logging = &logging_config.LoggingConfig{
conf.Logging = &logconfig.LoggingConfig{
RootSink: sinkConfig,
}
}
Expand Down
12 changes: 12 additions & 0 deletions consensus/tendermint/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tendermint

import (
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/encoding/amino"
)

var cdc = amino.NewCodec()

func init() {
cryptoAmino.RegisterAmino(cdc)
}
45 changes: 20 additions & 25 deletions consensus/tendermint/priv_validator_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package tendermint

import (
"github.com/hyperledger/burrow/crypto"
tm_crypto "github.com/tendermint/tendermint/crypto"
tm_types "github.com/tendermint/tendermint/types"
tmCrypto "github.com/tendermint/tendermint/crypto"
tmTypes "github.com/tendermint/tendermint/types"
)

type privValidatorMemory struct {
crypto.Addressable
signer goCryptoSigner
signer func(msg []byte) tmCrypto.Signature
lastSignedInfo *LastSignedInfo
}

var _ tm_types.PrivValidator = &privValidatorMemory{}
var _ tmTypes.PrivValidator = &privValidatorMemory{}

// Create a PrivValidator with in-memory state that takes an addressable representing the validator identity
// and a signer providing private signing for that identity.
Expand All @@ -24,38 +24,33 @@ func NewPrivValidatorMemory(addressable crypto.Addressable, signer crypto.Signer
}
}

func (pvm *privValidatorMemory) GetAddress() tm_types.Address {
func asTendermintSigner(signer crypto.Signer) func(msg []byte) tmCrypto.Signature {
return func(msg []byte) tmCrypto.Signature {
sig, err := signer.Sign(msg)
if err != nil {
return nil
}
return sig.TendermintSignature()
}
}

func (pvm *privValidatorMemory) GetAddress() tmTypes.Address {
return pvm.Address().Bytes()
}

func (pvm *privValidatorMemory) GetPubKey() tm_crypto.PubKey {
tm := tm_crypto.PubKeyEd25519{}
copy(tm[:], pvm.PublicKey().RawBytes())
return tm
func (pvm *privValidatorMemory) GetPubKey() tmCrypto.PubKey {
return pvm.PublicKey().TendermintPubKey()
}

// TODO: consider persistence to disk/database to avoid double signing after a crash
func (pvm *privValidatorMemory) SignVote(chainID string, vote *tm_types.Vote) error {
func (pvm *privValidatorMemory) SignVote(chainID string, vote *tmTypes.Vote) error {
return pvm.lastSignedInfo.SignVote(pvm.signer, chainID, vote)
}

func (pvm *privValidatorMemory) SignProposal(chainID string, proposal *tm_types.Proposal) error {
func (pvm *privValidatorMemory) SignProposal(chainID string, proposal *tmTypes.Proposal) error {
return pvm.lastSignedInfo.SignProposal(pvm.signer, chainID, proposal)
}

func (pvm *privValidatorMemory) SignHeartbeat(chainID string, heartbeat *tm_types.Heartbeat) error {
func (pvm *privValidatorMemory) SignHeartbeat(chainID string, heartbeat *tmTypes.Heartbeat) error {
return pvm.lastSignedInfo.SignHeartbeat(pvm.signer, chainID, heartbeat)
}

func asTendermintSigner(signer crypto.Signer) goCryptoSigner {
return func(msg []byte) tm_crypto.Signature {
sig, err := signer.Sign(msg)
if err != nil {
return nil
}
tmSig := tm_crypto.SignatureEd25519{}
copy(tmSig[:], sig.RawBytes())
return tmSig

}
}
12 changes: 6 additions & 6 deletions consensus/tendermint/sign_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func NewLastSignedInfo() *LastSignedInfo {
}
}

type goCryptoSigner func(msg []byte) crypto.Signature
type tmCryptoSigner func(msg []byte) crypto.Signature

// SignVote signs a canonical representation of the vote, along with the
// chainID. Implements PrivValidator.
func (lsi *LastSignedInfo) SignVote(sign goCryptoSigner, chainID string, vote *types.Vote) error {
func (lsi *LastSignedInfo) SignVote(sign tmCryptoSigner, chainID string, vote *types.Vote) error {
lsi.Lock()
defer lsi.Unlock()
if err := lsi.signVote(sign, chainID, vote); err != nil {
Expand All @@ -64,7 +64,7 @@ func (lsi *LastSignedInfo) SignVote(sign goCryptoSigner, chainID string, vote *t

// SignProposal signs a canonical representation of the proposal, along with
// the chainID. Implements PrivValidator.
func (lsi *LastSignedInfo) SignProposal(sign goCryptoSigner, chainID string, proposal *types.Proposal) error {
func (lsi *LastSignedInfo) SignProposal(sign tmCryptoSigner, chainID string, proposal *types.Proposal) error {
lsi.Lock()
defer lsi.Unlock()
if err := lsi.signProposal(sign, chainID, proposal); err != nil {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (lsi *LastSignedInfo) checkHRS(height int64, round int, step int8) (bool, e
// signVote checks if the vote is good to sign and sets the vote signature.
// It may need to set the timestamp as well if the vote is otherwise the same as
// a previously signed vote (ie. we crashed after signing but before the vote hit the WAL).
func (lsi *LastSignedInfo) signVote(sign goCryptoSigner, chainID string, vote *types.Vote) error {
func (lsi *LastSignedInfo) signVote(sign tmCryptoSigner, chainID string, vote *types.Vote) error {
height, round, step := vote.Height, vote.Round, voteToStep(vote)
signBytes := vote.SignBytes(chainID)

Expand Down Expand Up @@ -140,7 +140,7 @@ func (lsi *LastSignedInfo) signVote(sign goCryptoSigner, chainID string, vote *t
// signProposal checks if the proposal is good to sign and sets the proposal signature.
// It may need to set the timestamp as well if the proposal is otherwise the same as
// a previously signed proposal ie. we crashed after signing but before the proposal hit the WAL).
func (lsi *LastSignedInfo) signProposal(sign goCryptoSigner, chainID string, proposal *types.Proposal) error {
func (lsi *LastSignedInfo) signProposal(sign tmCryptoSigner, chainID string, proposal *types.Proposal) error {
height, round, step := proposal.Height, proposal.Round, stepPropose
signBytes := proposal.SignBytes(chainID)

Expand Down Expand Up @@ -186,7 +186,7 @@ func (lsi *LastSignedInfo) saveSigned(height int64, round int, step int8,

// SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.
// Implements PrivValidator.
func (lsi *LastSignedInfo) SignHeartbeat(sign goCryptoSigner, chainID string, heartbeat *types.Heartbeat) error {
func (lsi *LastSignedInfo) SignHeartbeat(sign tmCryptoSigner, chainID string, heartbeat *types.Heartbeat) error {
lsi.Lock()
defer lsi.Unlock()
heartbeat.Signature = sign(heartbeat.SignBytes(chainID))
Expand Down
5 changes: 1 addition & 4 deletions consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/tendermint/tendermint/config"
tmCrypto "github.com/tendermint/tendermint/crypto"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
Expand Down Expand Up @@ -76,10 +75,8 @@ func NewNode(conf *config.Config, privValidator tmTypes.PrivValidator, genesisDo
func DeriveGenesisDoc(burrowGenesisDoc *genesis.GenesisDoc) *tmTypes.GenesisDoc {
validators := make([]tmTypes.GenesisValidator, len(burrowGenesisDoc.Validators))
for i, validator := range burrowGenesisDoc.Validators {
tm := tmCrypto.PubKeyEd25519{}
copy(tm[:], validator.PublicKey.RawBytes())
validators[i] = tmTypes.GenesisValidator{
PubKey: tm,
PubKey: validator.PublicKey.TendermintPubKey(),
Name: validator.Name,
Power: int64(validator.Amount),
}
Expand Down
12 changes: 0 additions & 12 deletions consensus/tendermint/wire.go

This file was deleted.

Loading

0 comments on commit e149519

Please sign in to comment.