Skip to content

PrivValidator Structure #673

Closed
Closed

Description

@ebuchman

Continue refactoring of PrivValidator started here: #637

TODO: write a proper ADR for this. Some things to think about:

  1. move internal fields into substructs, eg. ID and LastSignedInfo
  2. change interface to be Address/PubKey instead of GetAddress/GetPubKey
  3. move signBytesHRS to be LastSignedInfo.SignBytesHRS
  4. generalize persistence so not stuck with a file

Activity

  1. self-assigned this
    on Sep 25, 2017
  2. silasdavis commented on Oct 3, 2017

    @silasdavis
    Contributor

    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     = -1
    	StepNone      = 0 // Used to distinguish the initial state
    	StepPropose   = 1
    	StepPrevote   = 2
    	StepPrecommit = 3
    )
    
    func VoteToStep(vote *tm_types.Vote) int8 {
    	switch vote.Type {
    	case tm_types.VoteTypePrevote:
    		return StepPrevote
    	case tm_types.VoteTypePrecommit:
    		return StepPrecommit
    	default:
    		return StepError
    	}
    }
    
    type privValidatorMemory struct {
    	privateAccount acm.PrivateAccount
    	lastSignedInfo *LastSignedInfo
    }
    
    var _ tm_types.PrivValidator = &privValidatorMemory{}
    
    func NewPrivValidatorMemory(privateAccount acm.PrivateAccount) *privValidatorMemory {
    	return &privValidatorMemory{
    		privateAccount: privateAccount,
    		lastSignedInfo: new(LastSignedInfo),
    	}
    }
    
    func (pvm *privValidatorMemory) GetAddress() data.Bytes {
    	return pvm.privateAccount.Address().Bytes()
    }
    
    func (pvm *privValidatorMemory) GetPubKey() crypto.PubKey {
    	return pvm.privateAccount.PubKey()
    
    }
    
    func (pvm *privValidatorMemory) SignVote(chainID string, vote *tm_types.Vote) error {
    	return pvm.lastSignedInfo.SignVote(pvm.privateAccount, chainID, vote)
    }
    
    func (pvm *privValidatorMemory) SignProposal(chainID string, proposal *tm_types.Proposal) error {
    	return pvm.lastSignedInfo.SignProposal(pvm.privateAccount, chainID, proposal)
    
    }
    
    func (pvm *privValidatorMemory) SignHeartbeat(chainID string, heartbeat *tm_types.Heartbeat) error {
    	return pvm.lastSignedInfo.SignHeartbeat(pvm.privateAccount, chainID, heartbeat)
    }
    
    type Verifier interface {
    	SignVote(signer tm_types.Signer, chainID string, vote *tm_types.Vote) error
    	SignProposal(signer tm_types.Signer, chainID string, proposal *tm_types.Proposal) error
    	SignHeartbeat(signer tm_types.Signer, chainID string, heartbeat *tm_types.Heartbeat) error
    }
    
    type LastSignedInfo struct {
    	mtx           sync.Mutex
    	LastHeight    int              `json:"last_height"`
    	LastRound     int              `json:"last_round"`
    	LastStep      int8             `json:"last_step"`
    	LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
    	LastSignBytes 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, chainID string, vote *tm_types.Vote) error {
    	lsi.mtx.Lock()
    	defer lsi.mtx.Unlock()
    	signature, err := lsi.signBytesHRS(signer, vote.Height, vote.Round, VoteToStep(vote), tm_types.SignBytes(chainID, vote))
    	if err != nil {
    		return fmt.Errorf("error signing vote: %v", err)
    	}
    	vote.Signature = signature
    	return nil
    }
    
    // SignProposal signs a canonical representation of the proposal, along with the chainID.
    // Implements PrivValidator.
    func (lsi *LastSignedInfo) SignProposal(signer tm_types.Signer, chainID string, proposal *tm_types.Proposal) error {
    	lsi.mtx.Lock()
    	defer lsi.mtx.Unlock()
    	signature, err := lsi.signBytesHRS(signer, proposal.Height, proposal.Round, StepPropose, tm_types.SignBytes(chainID, proposal))
    	if err != nil {
    		return fmt.Errorf("error signing proposal: %v", err)
    	}
    	proposal.Signature = signature
    	return nil
    }
    
    // SignHeartbeat signs a canonical representation of the heartbeat, along with the chainID.
    // Implements PrivValidator.
    func (lsi *LastSignedInfo) SignHeartbeat(signer tm_types.Signer, chainID string, heartbeat *tm_types.Heartbeat) error {
    	lsi.mtx.Lock()
    	defer lsi.mtx.Unlock()
    	var err error
    	heartbeat.Signature, err = signer.Sign(tm_types.SignBytes(chainID, heartbeat))
    	return err
    }
    
    // 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, round int, step int8, signBytes []byte) (crypto.Signature, error) {
    
    	sig := crypto.Signature{}
    	// If height regression, err
    	if lsi.LastHeight > height {
    		return sig, errors.New("height regression")
    	}
    	// More cases for when the height matches
    	if lsi.LastHeight == height {
    		// If round regression, err
    		if lsi.LastRound > round {
    			return sig, errors.New("round regression")
    		}
    		// If step regression, err
    		if lsi.LastRound == round {
    			if lsi.LastStep > step {
    				return sig, errors.New("step regression")
    			} else if lsi.LastStep == step {
    				if lsi.LastSignBytes != nil {
    					if lsi.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 ones
    					if bytes.Equal(lsi.LastSignBytes, signBytes) {
    						// log.Notice("Using lsi.LastSignature", "sig", lsi.LastSignature)
    						return lsi.LastSignature, nil
    					}
    				}
    				return sig, errors.New("step regression")
    			}
    		}
    	}
    
    	// Sign
    	sig, err := signer.Sign(signBytes)
    	if err != nil {
    		return sig, err
    	}
    
    	// Persist height/round/step
    	lsi.LastHeight = height
    	lsi.LastRound = round
    	lsi.LastStep = step
    	lsi.LastSignature = sig
    	lsi.LastSignBytes = signBytes
    
    	return sig, nil
    }
    
    func (lsi *LastSignedInfo) String() string {
    	return fmt.Sprintf("LastSignedInfo{LastHeight:%v, LastRound:%v, LastStep:%v}",
    		lsi.LastHeight, lsi.LastRound, lsi.LastStep)
    }
  3. ebuchman commented on Oct 3, 2017

    @ebuchman
    ContributorAuthor

    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 ?

  4. silasdavis commented on Oct 3, 2017

    @silasdavis
    Contributor

    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:

    type LastSignedInfo struct {
    	mtx           sync.Mutex
    	LastHeight    int              `json:"last_height"`
    	LastRound     int              `json:"last_round"`
    	LastStep      int8             `json:"last_step"`
    	LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
    	LastSignBytes 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?

  5. silasdavis commented on Oct 3, 2017

    @silasdavis
    Contributor

    Also the point of the Verifier interface is so that can compose verification and signing logic independently:

    type Verifier interface {
    	SignVote(signer tm_types.Signer, chainID string, vote *tm_types.Vote) error
    	SignProposal(signer tm_types.Signer, chainID string, proposal *tm_types.Proposal) error
    	SignHeartbeat(signer tm_types.Signer, chainID string, heartbeat *tm_types.Heartbeat) error
    }

    And Tendermint provides the canonical implementation of Verifier in the form of *LastSignedInfo.

    Then PrivValidator is there to implemented from scratch by those wanting to do it all in an enclave.

  6. ebuchman commented on Oct 3, 2017

    @ebuchman
    ContributorAuthor

    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:

    type privValidatorMemory struct {
    	privateAccount acm.PrivateAccount
    	lastSignedInfo *LastSignedInfo
    }
    
    var _ tm_types.PrivValidator = &privValidatorMemory{}
    
    func NewPrivValidatorMemory(privateAccount acm.PrivateAccount) *privValidatorMemory {
    	return &privValidatorMemory{
    		privateAccount: privateAccount,
    		lastSignedInfo: new(LastSignedInfo),
    	}
    }
    
    func (pvm *privValidatorMemory) GetAddress() data.Bytes {
    	return pvm.privateAccount.Address().Bytes()
    }
    
    func (pvm *privValidatorMemory) GetPubKey() crypto.PubKey {
    	return pvm.privateAccount.PubKey()
    
    }
    
    func (pvm *privValidatorMemory) SignVote(chainID string, vote *tm_types.Vote) error {
    	return pvm.lastSignedInfo.SignVote(pvm.privateAccount, chainID, vote)
    }
    
    func (pvm *privValidatorMemory) SignProposal(chainID string, proposal *tm_types.Proposal) error {
    	return pvm.lastSignedInfo.SignProposal(pvm.privateAccount, chainID, proposal)
    
    }
    
    func (pvm *privValidatorMemory) SignHeartbeat(chainID string, heartbeat *tm_types.Heartbeat) error {
    	return pvm.lastSignedInfo.SignHeartbeat(pvm.privateAccount, chainID, heartbeat)
    }
    
    

    Correct?

  7. silasdavis commented on Oct 3, 2017

    @silasdavis
    Contributor

    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:

    type LastSignedInfo struct {
    	mtx           sync.Mutex
    	LastHeight    int              `json:"last_height"`
    	LastRound     int              `json:"last_round"`
    	LastStep      int8             `json:"last_step"`
    	LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
    	LastSignBytes data.Bytes       `json:"last_signbytes,omitempty"` // so we dont lose signatures
    	// Save callback to whatever persistence layer (or none)
    	committer func(lsi LastSignedInfo) error 
    }
    
    func NewDefaultVerifier(committer func(lsi LastSignedInfo) error) Verifier {
    	return &LastSignedInfo {
    		committer: committer
    	}
    }

    Then add:

    // Make sure to copy value onto the stack not pointer
    lsi.committer(*lsi)

    Back to signBytesHRS on *LastSignedInfo

  8. added this to the 0.12.0 milestone on Oct 3, 2017
  9. removed their assignment
    on Jan 22, 2018
  10. zramsay commented on Feb 19, 2018

    @zramsay
    Contributor
  11. removed this from the 0.12.0 milestone on Feb 19, 2018
  12. ebuchman commented on Mar 20, 2018

    @ebuchman
    ContributorAuthor

    Theres an ADR and a lot of recent work on this. Closing for more specific issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions