You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is my copypasta of PrivValidatorFS to use the verification logic in my own PrivValidator, in case it helps with this ticket. I've refactored the signing state into LastSignedInfo and then I've made that implement an interface I've called Verifier (perhaps there is a better name) that rather than enclosing a signer takes one statically for each of its methods. So Verifier still checks, signs, and updates state atomically. There might be an argument for making the logical verification purely static on non-pointer value type of LastSignedInfo.
I've also removed a panic from signBytesHRS because I don't see why an error won't do.
p.s. What is 'ADR' in this context... abstract data representation, aloof dusting reaction?
import (
"bytes""errors""fmt""sync"
acm "github.com/hyperledger/burrow/account""github.com/tendermint/go-crypto""github.com/tendermint/go-wire/data"
tm_types "github.com/tendermint/tendermint/types"
)
const (
StepError=-1StepNone=0// Used to distinguish the initial stateStepPropose=1StepPrevote=2StepPrecommit=3
)
funcVoteToStep(vote*tm_types.Vote) int8 {
switchvote.Type {
casetm_types.VoteTypePrevote:
returnStepPrevotecasetm_types.VoteTypePrecommit:
returnStepPrecommitdefault:
returnStepError
}
}
typeprivValidatorMemorystruct {
privateAccount acm.PrivateAccountlastSignedInfo*LastSignedInfo
}
var_ tm_types.PrivValidator=&privValidatorMemory{}
funcNewPrivValidatorMemory(privateAccount acm.PrivateAccount) *privValidatorMemory {
return&privValidatorMemory{
privateAccount: privateAccount,
lastSignedInfo: new(LastSignedInfo),
}
}
func (pvm*privValidatorMemory) GetAddress() data.Bytes {
returnpvm.privateAccount.Address().Bytes()
}
func (pvm*privValidatorMemory) GetPubKey() crypto.PubKey {
returnpvm.privateAccount.PubKey()
}
func (pvm*privValidatorMemory) SignVote(chainIDstring, vote*tm_types.Vote) error {
returnpvm.lastSignedInfo.SignVote(pvm.privateAccount, chainID, vote)
}
func (pvm*privValidatorMemory) SignProposal(chainIDstring, proposal*tm_types.Proposal) error {
returnpvm.lastSignedInfo.SignProposal(pvm.privateAccount, chainID, proposal)
}
func (pvm*privValidatorMemory) SignHeartbeat(chainIDstring, heartbeat*tm_types.Heartbeat) error {
returnpvm.lastSignedInfo.SignHeartbeat(pvm.privateAccount, chainID, heartbeat)
}
typeVerifierinterface {
SignVote(signer tm_types.Signer, chainIDstring, vote*tm_types.Vote) errorSignProposal(signer tm_types.Signer, chainIDstring, proposal*tm_types.Proposal) errorSignHeartbeat(signer tm_types.Signer, chainIDstring, heartbeat*tm_types.Heartbeat) error
}
typeLastSignedInfostruct {
mtx sync.MutexLastHeightint`json:"last_height"`LastRoundint`json:"last_round"`LastStepint8`json:"last_step"`LastSignature crypto.Signature`json:"last_signature,omitempty"`// so we dont lose signaturesLastSignBytes data.Bytes`json:"last_signbytes,omitempty"`// so we dont lose signatures
}
var_Verifier=&LastSignedInfo{}
// SignVote signs a canonical representation of the vote, along with the chainID.// Implements PrivValidator.func (lsi*LastSignedInfo) SignVote(signer tm_types.Signer, chainIDstring, vote*tm_types.Vote) error {
lsi.mtx.Lock()
deferlsi.mtx.Unlock()
signature, err:=lsi.signBytesHRS(signer, vote.Height, vote.Round, VoteToStep(vote), tm_types.SignBytes(chainID, vote))
iferr!=nil {
returnfmt.Errorf("error signing vote: %v", err)
}
vote.Signature=signaturereturnnil
}
// SignProposal signs a canonical representation of the proposal, along with the chainID.// Implements PrivValidator.func (lsi*LastSignedInfo) SignProposal(signer tm_types.Signer, chainIDstring, proposal*tm_types.Proposal) error {
lsi.mtx.Lock()
deferlsi.mtx.Unlock()
signature, err:=lsi.signBytesHRS(signer, proposal.Height, proposal.Round, StepPropose, tm_types.SignBytes(chainID, proposal))
iferr!=nil {
returnfmt.Errorf("error signing proposal: %v", err)
}
proposal.Signature=signaturereturnnil
}
// SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.// Implements PrivValidator.func (lsi*LastSignedInfo) SignHeartbeat(signer tm_types.Signer, chainIDstring, heartbeat*tm_types.Heartbeat) error {
lsi.mtx.Lock()
deferlsi.mtx.Unlock()
varerrerrorheartbeat.Signature, err=signer.Sign(tm_types.SignBytes(chainID, heartbeat))
returnerr
}
// signBytesHRS signs the given signBytes if the height/round/step (HRS)// are greater than the latest state. If the HRS are equal,// it returns the privValidator.LastSignature.func (lsi*LastSignedInfo) signBytesHRS(signer tm_types.Signer, height, roundint, stepint8, signBytes []byte) (crypto.Signature, error) {
sig:= crypto.Signature{}
// If height regression, erriflsi.LastHeight>height {
returnsig, errors.New("height regression")
}
// More cases for when the height matchesiflsi.LastHeight==height {
// If round regression, erriflsi.LastRound>round {
returnsig, errors.New("round regression")
}
// If step regression, erriflsi.LastRound==round {
iflsi.LastStep>step {
returnsig, errors.New("step regression")
} elseiflsi.LastStep==step {
iflsi.LastSignBytes!=nil {
iflsi.LastSignature.Empty() {
return crypto.Signature{}, errors.New("lsi: LastSignature is nil but LastSignBytes is not")
}
// so we dont sign a conflicting vote or proposal// NOTE: proposals are non-deterministic (include time),// so we can actually lose them, but will still never sign conflicting onesifbytes.Equal(lsi.LastSignBytes, signBytes) {
// log.Notice("Using lsi.LastSignature", "sig", lsi.LastSignature)returnlsi.LastSignature, nil
}
}
returnsig, errors.New("step regression")
}
}
}
// Signsig, err:=signer.Sign(signBytes)
iferr!=nil {
returnsig, err
}
// Persist height/round/steplsi.LastHeight=heightlsi.LastRound=roundlsi.LastStep=steplsi.LastSignature=siglsi.LastSignBytes=signBytesreturnsig, nil
}
func (lsi*LastSignedInfo) String() string {
returnfmt.Sprintf("LastSignedInfo{LastHeight:%v, LastRound:%v, LastStep:%v}",
lsi.LastHeight, lsi.LastRound, lsi.LastStep)
}
I think if we refactor along the lines I was going in before, ie with independent LastSignedInfo struct, you wouldn't need all this code - right? Is this just so you don't need to depend on a file ?
The top half is my implementation of a PrivValidator, but the bottom half is a LastSignedInfo object lifted from PrivValidatorFS since there is no easy way to use the same logic without doing it. See:
typeLastSignedInfostruct {
mtx sync.MutexLastHeightint`json:"last_height"`LastRoundint`json:"last_round"`LastStepint8`json:"last_step"`LastSignature crypto.Signature`json:"last_signature,omitempty"`// so we dont lose signaturesLastSignBytes data.Bytes`json:"last_signbytes,omitempty"`// so we dont lose signatures
}
Not fully sure what you mean by independent LastSignedInfo struct unless it is something like this. Are you suggesting a way to do this with less code? In what way?
Not fully sure what you mean by independent LastSignedInfo struct unless it is this
Yes that's what I want.
Also the point of the Verifier interface is so that can compose verification and signing logic independently:
Seems like good idea.
Basically the goal is you should write as little code as possible to get a PrivValidator the way you want it. Ie. after we make the changes, your impl should look like:
Ah right yeah exactly. The reason I have copied in from PrivValidatorFS is that I am working against develop and various things aren't exported.
You also said wanted to be able to call Save() on LastSignedInfo to persist it. It seems like is something a Verifier can handle internally. In the case of LastSignedInfo (our default Verifier) I'd also like to have that persistence decoupled so if we have:
typeLastSignedInfostruct {
mtx sync.MutexLastHeightint`json:"last_height"`LastRoundint`json:"last_round"`LastStepint8`json:"last_step"`LastSignature crypto.Signature`json:"last_signature,omitempty"`// so we dont lose signaturesLastSignBytes data.Bytes`json:"last_signbytes,omitempty"`// so we dont lose signatures// Save callback to whatever persistence layer (or none)committerfunc(lsiLastSignedInfo) error
}
funcNewDefaultVerifier(committerfunc(lsiLastSignedInfo) error) Verifier {
return&LastSignedInfo {
committer: committer
}
}
Then add:
// Make sure to copy value onto the stack not pointerlsi.committer(*lsi)
Activity
silasdavis commentedon Oct 3, 2017
Here is my copypasta of
PrivValidatorFSto use the verification logic in my ownPrivValidator, in case it helps with this ticket. I've refactored the signing state intoLastSignedInfoand then I've made that implement an interface I've calledVerifier(perhaps there is a better name) that rather than enclosing a signer takes one statically for each of its methods. SoVerifierstill checks, signs, and updates state atomically. There might be an argument for making the logical verification purely static on non-pointer value type ofLastSignedInfo.I've also removed a panic from
signBytesHRSbecause I don't see why an error won't do.p.s. What is 'ADR' in this context... abstract data representation, aloof dusting reaction?
ebuchman commentedon Oct 3, 2017
Lol - Architecture Decision Record. I guess similar to what people call RFC.
Thanks Silas.
I think if we refactor along the lines I was going in before, ie with independent LastSignedInfo struct, you wouldn't need all this code - right? Is this just so you don't need to depend on a file ?
silasdavis commentedon Oct 3, 2017
The top half is my implementation of a
PrivValidator, but the bottom half is aLastSignedInfoobject lifted fromPrivValidatorFSsince there is no easy way to use the same logic without doing it. See:Not fully sure what you mean by independent
LastSignedInfostruct unless it is something like this. Are you suggesting a way to do this with less code? In what way?silasdavis commentedon Oct 3, 2017
Also the point of the
Verifierinterface is so that can compose verification and signing logic independently:And Tendermint provides the canonical implementation of
Verifierin the form of*LastSignedInfo.Then
PrivValidatoris there to implemented from scratch by those wanting to do it all in an enclave.ebuchman commentedon Oct 3, 2017
Yes that's what I want.
Seems like good idea.
Basically the goal is you should write as little code as possible to get a PrivValidator the way you want it. Ie. after we make the changes, your impl should look like:
Correct?
silasdavis commentedon Oct 3, 2017
Ah right yeah exactly. The reason I have copied in from
PrivValidatorFSis that I am working against develop and various things aren't exported.You also said wanted to be able to call
Save()onLastSignedInfoto persist it. It seems like is something aVerifiercan handle internally. In the case ofLastSignedInfo(our default Verifier) I'd also like to have that persistence decoupled so if we have:Then add:
Back to
signBytesHRSon*LastSignedInfozramsay commentedon Feb 19, 2018
#1203
ebuchman commentedon Mar 20, 2018
Theres an ADR and a lot of recent work on this. Closing for more specific issues.
Fix race condition in consensus.State code (#673) (#691)
Fix race condition in consensus.State code (tendermint#673) (tendermi…
Fix race condition in consensus.State code (tendermint#673) (tendermi…
Fix race condition in consensus.State code (tendermint#673)