Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test passed. but changed the signature of methods and rid of new file. #6

Open
wants to merge 2 commits into
base: signature-abi-encoded
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cometbft/commands/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func testnetFiles(cmd *cobra.Command, args []string) error {

pvKeyFile := filepath.Join(nodeDir, config.BaseConfig.PrivValidatorKey)
pvStateFile := filepath.Join(nodeDir, config.BaseConfig.PrivValidatorState)
pv := privval.LoadFilePV(pvKeyFile, pvStateFile, "/tmp/asd") //TODO(blas): change
pv := privval.LoadFilePV(pvKeyFile, pvStateFile) //TODO(blas): change

pubKey, err := pv.GetPubKey()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2283,8 +2283,8 @@ func (cs *State) signVote(
if err != nil {
return vote, err
}
vAbiEncoded := []byte{}
err = cs.privValidator.SignVoteAbiEncoded(cs.state.ChainID, vAbiEncoded)
// vAbiEncoded := []byte{}
// err = cs.privValidator.SignVoteAbiEncoded(cs.state.ChainID, vAbiEncoded)
vote.Signature = v.Signature
vote.Timestamp = v.Timestamp

Expand Down
105 changes: 58 additions & 47 deletions privval/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ type FilePV struct {
}

// NewFilePV generates a new validator from the given key and paths.
func NewFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string, stateFilePathAbiEncoded string) *FilePV {
func NewFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string) *FilePV {
return &FilePV{
Key: FilePVKey{
Address: privKey.PubKey().Address(),
Expand All @@ -167,32 +167,32 @@ func NewFilePV(privKey crypto.PrivKey, keyFilePath, stateFilePath string, stateF
},
LastSignStateAbiEncoded: FilePVLastSignState{
Step: stepNone,
filePath: stateFilePathAbiEncoded,
filePath: stateFilePath + "/abi",
},
}
}

// GenFilePV generates a new validator with randomly generated private key
// and sets the filePaths, but does not call Save().
func GenFilePV(keyFilePath, stateFilePath string, stateFilePathAbiEncoded string) *FilePV {
return NewFilePV(ed25519.GenPrivKey(), keyFilePath, stateFilePath, stateFilePathAbiEncoded)
func GenFilePV(keyFilePath, stateFilePath string) *FilePV {
return NewFilePV(ed25519.GenPrivKey(), keyFilePath, stateFilePath)
}

// LoadFilePV loads a FilePV from the filePaths. The FilePV handles double
// signing prevention by persisting data to the stateFilePath. If either file path
// does not exist, the program will exit.
func LoadFilePV(keyFilePath, stateFilePath string, stateFilePathAbiEncoded string) *FilePV {
return loadFilePV(keyFilePath, stateFilePath, stateFilePathAbiEncoded, true)
func LoadFilePV(keyFilePath, stateFilePath string) *FilePV {
return loadFilePV(keyFilePath, stateFilePath, true)
}

// LoadFilePVEmptyState loads a FilePV from the given keyFilePath, with an empty LastSignState.
// If the keyFilePath does not exist, the program will exit.
func LoadFilePVEmptyState(keyFilePath, stateFilePath string, stateFilePathAbiEncoded string) *FilePV {
return loadFilePV(keyFilePath, stateFilePath, stateFilePathAbiEncoded, false)
func LoadFilePVEmptyState(keyFilePath, stateFilePath string) *FilePV {
return loadFilePV(keyFilePath, stateFilePath, false)
}

// If loadState is true, we load from the stateFilePath. Otherwise, we use an empty LastSignState.
func loadFilePV(keyFilePath, stateFilePath string, stateFilePathAbiEncoded string, loadState bool) *FilePV {
func loadFilePV(keyFilePath, stateFilePath string, loadState bool) *FilePV {
keyJSONBytes, err := os.ReadFile(keyFilePath)
if err != nil {
cmtos.Exit(err.Error())
Expand Down Expand Up @@ -231,12 +231,12 @@ func loadFilePV(keyFilePath, stateFilePath string, stateFilePathAbiEncoded strin

// LoadOrGenFilePV loads a FilePV from the given filePaths
// or else generates a new one and saves it to the filePaths.
func LoadOrGenFilePV(keyFilePath, stateFilePath string, stateFilePathAbiEncoded string) *FilePV {
func LoadOrGenFilePV(keyFilePath, stateFilePath string) *FilePV {
var pv *FilePV
if cmtos.FileExists(keyFilePath) {
pv = LoadFilePV(keyFilePath, stateFilePath, stateFilePathAbiEncoded)
pv = LoadFilePV(keyFilePath, stateFilePath)
} else {
pv = GenFilePV(keyFilePath, stateFilePath, stateFilePathAbiEncoded)
pv = GenFilePV(keyFilePath, stateFilePath)
pv.Save()
}
return pv
Expand All @@ -263,6 +263,15 @@ func (pv *FilePV) SignVote(chainID string, vote *cmtproto.Vote) error {
return nil
}

// SignVote signs a canonical representation of the vote, along with the
// chainID. Implements PrivValidator.
func (pv *FilePV) SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
if err := pv.signVoteAbiEncoded(chainID, vote); err != nil {
return fmt.Errorf("error signing vote: %v", err)
}
return nil
}

// SignProposal signs a canonical representation of the proposal, along with
// the chainID. Implements PrivValidator.
func (pv *FilePV) SignProposal(chainID string, proposal *cmtproto.Proposal) error {
Expand Down Expand Up @@ -349,42 +358,44 @@ func (pv *FilePV) signVote(chainID string, vote *cmtproto.Vote) error {
// 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 (pv *FilePV) signVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
height, round, step := vote.Height, vote.Round, voteToStep(vote)

lss := pv.LastSignState

sameHRS, err := lss.CheckHRS(height, round, step)
if err != nil {
return err
}

signBytes := types.VoteSignBytesAbiEncoded(chainID, vote)

// We might crash before writing to the wal,
// causing us to try to re-sign for the same HRS.
// If signbytes are the same, use the last signature.
// If they only differ by timestamp, use last timestamp and signature
// Otherwise, return error
if sameHRS {
if bytes.Equal(signBytes, lss.SignBytes) {
vote.Signature = lss.Signature
} else if timestamp, ok := checkVotesOnlyDifferByTimestamp(lss.SignBytes, signBytes); ok {
vote.Timestamp = timestamp
vote.Signature = lss.Signature
} else {
err = fmt.Errorf("conflicting data")
}
return err
}

// It passed the checks. Sign the vote
sig, err := pv.Key.PrivKey.Sign(signBytes)
if err != nil {
return err
}
pv.saveSigned(height, round, step, signBytes, sig)
vote.Signature = sig
return nil
return pv.signVote(chainID, vote)
// height, round, step := vote.Height, vote.Round, voteToStep(vote)

// lss := pv.LastSignState

// sameHRS, err := lss.CheckHRS(height, round, step)
// if err != nil {
// return err
// }

// signBytes := types.VoteSignBytesAbiEncoded(chainID, vote)

// // We might crash before writing to the wal,
// // causing us to try to re-sign for the same HRS.
// // If signbytes are the same, use the last signature.
// // If they only differ by timestamp, use last timestamp and signature
// // Otherwise, return error
// if sameHRS {
// if bytes.Equal(signBytes, lss.SignBytes) {
// vote.Signature = lss.Signature
// } else if timestamp, ok := checkVotesOnlyDifferByTimestamp(lss.SignBytes, signBytes); ok {
// vote.Timestamp = timestamp
// vote.Signature = lss.Signature
// } else {
// err = fmt.Errorf("conflicting data")
// }
// return err
// }

// // It passed the checks. Sign the vote
// sig, err := pv.Key.PrivKey.Sign(signBytes)
// if err != nil {
// return err
// }
// pv.saveSigned(height, round, step, signBytes, sig)
// vote.Signature = sig
// return nil
}

// signProposal checks if the proposal is good to sign and sets the proposal signature.
Expand Down
26 changes: 13 additions & 13 deletions privval/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func TestGenLoadValidator(t *testing.T) {
require.Nil(t, err)
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
require.Nil(t, err)
tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
// tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
require.Nil(t, err)

privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name())

height := int64(100)
privVal.LastSignState.Height = height
privVal.Save()
addr := privVal.GetAddress()

privVal = LoadFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal = LoadFilePV(tempKeyFile.Name(), tempStateFile.Name())
assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
assert.Equal(height, privVal.LastSignState.Height, "expected privval.LastHeight to have been saved")
}
Expand All @@ -46,10 +46,10 @@ func TestResetValidator(t *testing.T) {
require.Nil(t, err)
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
require.Nil(t, err)
tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
// tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
require.Nil(t, err)

privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name())
emptyState := FilePVLastSignState{filePath: tempStateFile.Name()}

// new priv val has empty state
Expand Down Expand Up @@ -95,9 +95,9 @@ func TestLoadOrGenValidator(t *testing.T) {
t.Error(err)
}

privVal := LoadOrGenFilePV(tempKeyFilePath, tempStateFilePath, tempStateFileAbiEncodedPath)
privVal := LoadOrGenFilePV(tempKeyFilePath, tempStateFilePath)
addr := privVal.GetAddress()
privVal = LoadOrGenFilePV(tempKeyFilePath, tempStateFilePath, tempStateFileAbiEncodedPath)
privVal = LoadOrGenFilePV(tempKeyFilePath, tempStateFilePath)
assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
}

Expand Down Expand Up @@ -172,10 +172,10 @@ func TestSignVote(t *testing.T) {
require.Nil(t, err)
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
require.Nil(t, err)
tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
// tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
require.Nil(t, err)

privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name())

randbytes := cmtrand.Bytes(tmhash.Size)
randbytes2 := cmtrand.Bytes(tmhash.Size)
Expand Down Expand Up @@ -227,10 +227,10 @@ func TestSignProposal(t *testing.T) {
require.Nil(t, err)
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
require.Nil(t, err)
tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
// tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
require.Nil(t, err)

privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name())

randbytes := cmtrand.Bytes(tmhash.Size)
randbytes2 := cmtrand.Bytes(tmhash.Size)
Expand Down Expand Up @@ -277,10 +277,10 @@ func TestDifferByTimestamp(t *testing.T) {
require.Nil(t, err)
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
require.Nil(t, err)
tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
// tempStateFileAbiEncodedFile, err := os.CreateTemp("", "priv_validator_state_abi_encoded")
require.Nil(t, err)

privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), tempStateFileAbiEncodedFile.Name())
privVal := GenFilePV(tempKeyFile.Name(), tempStateFile.Name())
randbytes := cmtrand.Bytes(tmhash.Size)
block1 := types.BlockID{Hash: randbytes, PartSetHeader: types.PartSetHeader{Total: 5, Hash: randbytes}}
height, round := int64(10), int32(1)
Expand Down
3 changes: 2 additions & 1 deletion privval/retry_signer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (sc *RetrySignerClient) SignVote(chainID string, vote *cmtproto.Vote) error
return fmt.Errorf("exhausted all attempts to sign vote: %w", err)
}

func (sc *RetrySignerClient) SignVoteAbiEncoded(chainID string, vote []byte) error {
func (sc *RetrySignerClient) SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
// func (sc *RetrySignerClient) SignVoteAbiEncoded(chainID string, vote []byte) error {
var err error
for i := 0; i < sc.retries || sc.retries == 0; i++ {
err = sc.next.SignVoteAbiEncoded(chainID, vote)
Expand Down
6 changes: 4 additions & 2 deletions privval/signer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ func (sc *SignerClient) SignVote(chainID string, vote *cmtproto.Vote) error {
}

// SignVote requests a remote signer to sign a vote
func (sc *SignerClient) SignVoteAbiEncoded(chainID string, vote []byte) error {
func (sc *SignerClient) SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
// func (sc *SignerClient) SignVoteAbiEncoded(chainID string, vote []byte) error {
//TODO(blas): implement
return nil
return sc.SignVote(chainID, &cmtproto.Vote{})
// return nil
}

// SignProposal requests a remote signer to sign a proposal
Expand Down
9 changes: 5 additions & 4 deletions types/priv_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type PrivValidator interface {
GetPubKey() (crypto.PubKey, error)

SignVote(chainID string, vote *cmtproto.Vote) error
SignVoteAbiEncoded(chainID string, vote []byte) error
SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error
SignProposal(chainID string, proposal *cmtproto.Proposal) error
}

Expand Down Expand Up @@ -86,14 +86,15 @@ func (pv MockPV) SignVote(chainID string, vote *cmtproto.Vote) error {
return nil
}

func (pv *ErroringMockPV) SignVoteAbiEncoded(chainID string, []byte) error {
func (pv MockPV) SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
// func (pv MockPV) SignVoteAbiEncoded(chainID string, vote []byte) error {
useChainID := chainID
if pv.breakVoteSigning {
useChainID = "incorrect-chain-id"
}

signBytes := VoteSignBytesAbiEncoded(useChainID, vote)
sig, err := pv.PrivKey.Sign(signBytes)
_, err := pv.PrivKey.Sign(signBytes)
if err != nil {
return err
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func (pv *ErroringMockPV) SignVote(chainID string, vote *cmtproto.Vote) error {
}

// Implements PrivValidator.
func (pv *ErroringMockPV) SignVoteAbiEncoded(chainID string, vote []byte) error {
func (pv *ErroringMockPV) SignVoteAbiEncoded(chainID string, vote *cmtproto.Vote) error {
return ErroringMockPVErr
}

Expand Down
6 changes: 4 additions & 2 deletions types/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ func VoteSignBytes(chainID string, vote *cmtproto.Vote) []byte {
// devices that rely on this encoding.
//
// See CanonicalizeVote
func VoteSignBytesAbiEncoded(chainID string, vote []byte) []byte {
func VoteSignBytesAbiEncoded(chainID string, vote *cmtproto.Vote) []byte {
// func VoteSignBytesAbiEncoded(chainID string, vote []byte) []byte {
// TODO
return nil
return VoteSignBytes(chainID, vote)
// return nil
}

func (vote *Vote) Copy() *Vote {
Expand Down
Loading