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

refactor(privval): reverse conditional + more idiomatic Go code with early returns #2156

Merged
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
56 changes: 32 additions & 24 deletions privval/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,40 @@ func (lss *FilePVLastSignState) CheckHRS(height int64, round int32, step int8) (
return false, fmt.Errorf("height regression. Got %v, last height %v", height, lss.Height)
}

if lss.Height == height {
if lss.Round > round {
return false, fmt.Errorf("round regression at height %v. Got %v, last round %v", height, round, lss.Round)
}
if lss.Height != height {
return false, nil
}

if lss.Round == round {
if lss.Step > step {
return false, fmt.Errorf(
"step regression at height %v round %v. Got %v, last step %v",
height,
round,
step,
lss.Step,
)
} else if lss.Step == step {
if lss.SignBytes != nil {
if lss.Signature == nil {
panic("pv: Signature is nil but SignBytes is not!")
}
return true, nil
}
return false, errors.New("no SignBytes found")
}
}
if lss.Round > round {
return false, fmt.Errorf("round regression at height %v. Got %v, last round %v", height, round, lss.Round)
}

if lss.Round != round {
return false, nil
}

if lss.Step > step {
return false, fmt.Errorf(
"step regression at height %v round %v. Got %v, last step %v",
height,
round,
step,
lss.Step,
)
}

if lss.Step < step {
return false, nil
}

if lss.SignBytes == nil {
return false, errors.New("no SignBytes found")
}

if lss.Signature == nil {
panic("pv: Signature is nil but SignBytes is not!")
}
return false, nil
return true, nil
}

// Save persists the FilePvLastSignState to its filePath.
Expand Down
Loading