-
Notifications
You must be signed in to change notification settings - Fork 15
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
Resolve long time consensus stuck by network and simple refactorings #915
Changes from all commits
377d4bc
cd3c9f7
1f631c5
f8ee4e8
61ecda7
2d15378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ func NewBallotSendRecord(nodeAlias string) *BallotSendRecord { | |
return p | ||
} | ||
|
||
// SetSent sets that the ballot of this ISAACState has already been sent. | ||
// This is to prevent one node from retransmitting another result. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big big 👍 for documenting what the function is for. It's much more important than what the function do (which one can understand from the code). |
||
func (r *BallotSendRecord) SetSent(state ISAACState) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
@@ -33,6 +35,17 @@ func (r *BallotSendRecord) SetSent(state ISAACState) { | |
return | ||
} | ||
|
||
// InitSent initializes the ballot transfer record of this ISAACState.InitSent. | ||
// This function is used when an existing ballot has expired. | ||
func (r *BallotSendRecord) InitSent(state ISAACState) { | ||
r.Lock() | ||
defer r.Unlock() | ||
log.Debug("BallotSendRecord.InitSent()", "state", state) | ||
r.record[state] = false | ||
|
||
return | ||
} | ||
|
||
func (r *BallotSendRecord) Sent(state ISAACState) bool { | ||
r.RLock() | ||
defer r.RUnlock() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,41 @@ func (is *ISAAC) SelectProposer(blockHeight uint64, round uint64) string { | |
return is.proposerSelector.Select(blockHeight, round) | ||
} | ||
|
||
// GenerateExpiredBallot create an expired ballot using voting.Basis and ballot.State. | ||
// This function is used to create a ballot indicating | ||
// that a node has expired to other nodes when a timeout occurs in the state. | ||
func (is *ISAAC) GenerateExpiredBallot(basis voting.Basis, state ballot.State) (ballot.Ballot, error) { | ||
is.log.Debug("ISAAC.GenerateExpiredBallot", "basis", basis, "state", state) | ||
proposerAddr := is.SelectProposer(basis.Height, basis.Round) | ||
|
||
newExpiredBallot := ballot.NewBallot(is.Node.Address(), proposerAddr, basis, []string{}) | ||
newExpiredBallot.SetVote(state, voting.EXP) | ||
|
||
config := is.Conf | ||
var err error | ||
|
||
opc, err := ballot.NewCollectTxFeeFromBallot(*newExpiredBallot, config.CommonAccountAddress) | ||
if err != nil { | ||
return ballot.Ballot{}, err | ||
} | ||
|
||
opi, err := ballot.NewInflationFromBallot(*newExpiredBallot, config.CommonAccountAddress, config.InitialBalance) | ||
if err != nil { | ||
return ballot.Ballot{}, err | ||
} | ||
|
||
ptx, err := ballot.NewProposerTransactionFromBallot(*newExpiredBallot, opc, opi) | ||
if err != nil { | ||
return ballot.Ballot{}, err | ||
} | ||
|
||
newExpiredBallot.SetProposerTransaction(ptx) | ||
newExpiredBallot.SignByProposer(is.Node.Keypair(), config.NetworkID) | ||
newExpiredBallot.Sign(is.Node.Keypair(), config.NetworkID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we always need to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm.. already There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Geod24 How about handling it in the next pr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
return *newExpiredBallot, nil | ||
} | ||
|
||
// | ||
// Check if `basis` is a valid one for the current round of consensus | ||
// | ||
|
@@ -206,14 +241,55 @@ func (is *ISAAC) Vote(b ballot.Ballot) (isNew bool, err error) { | |
return | ||
} | ||
|
||
func (is *ISAAC) CanGetVotingResult(b ballot.Ballot) (rv RoundVoteResult, vh voting.Hole, finished bool) { | ||
// RemoveOldBallots checks that `blt` has valid confirmed and proposed time. | ||
// If it is invalid, it is removed. | ||
// And if the invalid ballot is make by itself, | ||
// return `needRenewal` = true for rebroadcasting EXP ballot | ||
func (is *ISAAC) RemoveOldBallots(blt ballot.Ballot) (needRenewal bool) { | ||
runningRound, found := is.RunningRounds[blt.VotingBasis().Index()] | ||
if !found { | ||
// if RunningRound is not found, this ballot will be stopped. | ||
return false | ||
} | ||
roundVote, err := runningRound.RoundVote(blt.Proposer()) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
var vr *RoundVoteResult | ||
|
||
switch blt.State() { | ||
case ballot.StateSIGN: | ||
vr = &roundVote.SIGN | ||
case ballot.StateACCEPT: | ||
vr = &roundVote.ACCEPT | ||
default: | ||
return false | ||
} | ||
|
||
for nodeAddr, blt := range *vr { | ||
if err = ballot.CheckHasCorrectTime(blt.ProposerConfirmed()); err == nil { | ||
if err = ballot.CheckHasCorrectTime(blt.Confirmed()); err == nil { | ||
continue | ||
} | ||
} | ||
delete(*vr, nodeAddr) | ||
if nodeAddr == is.Node.Address() { | ||
needRenewal = true | ||
} | ||
} | ||
|
||
return needRenewal | ||
} | ||
|
||
func (is *ISAAC) CanGetVotingResult(blt ballot.Ballot) (rv RoundVoteResult, vh voting.Hole, finished bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for using at least 3 char identifiers. Makes the code much more readable IMO. |
||
is.RLock() | ||
defer is.RUnlock() | ||
|
||
defer func() { | ||
is.log.Debug( | ||
"CanGetVotingResult", | ||
"ballot", b.GetHash(), | ||
"ballot", blt.GetHash(), | ||
"round-vote-result", rv, | ||
"voting-hole", vh, | ||
"finished", finished, | ||
|
@@ -224,17 +300,16 @@ func (is *ISAAC) CanGetVotingResult(b ballot.Ballot) (rv RoundVoteResult, vh vot | |
vh = voting.NOTYET | ||
finished = false | ||
|
||
runningRound, found := is.RunningRounds[b.VotingBasis().Index()] | ||
runningRound, found := is.RunningRounds[blt.VotingBasis().Index()] | ||
if !found { | ||
// if RunningRound is not found, this ballot will be stopped. | ||
finished = true | ||
return | ||
} | ||
|
||
roundVote, err := runningRound.RoundVote(b.Proposer()) | ||
roundVote, err := runningRound.RoundVote(blt.Proposer()) | ||
if err == nil { | ||
rv, vh, finished = roundVote.CanGetVotingResult(is.policy, b.State(), is.log) | ||
return | ||
rv, vh, finished = roundVote.CanGetVotingResult(is.policy, blt.State(), is.log) | ||
} | ||
|
||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this commit