diff options
Diffstat (limited to 'core/negative-ack.go')
-rw-r--r-- | core/negative-ack.go | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/core/negative-ack.go b/core/negative-ack.go index 13a4832..8957152 100644 --- a/core/negative-ack.go +++ b/core/negative-ack.go @@ -25,67 +25,67 @@ import ( type negativeAck struct { // owner is the ID of proposer itself, this is used when deciding - // a validator to be restricted or not. - owner types.ValidatorID + // a node to be restricted or not. + owner types.NodeID - numOfValidators int + numOfNodes int // timeDelay and timeExpire are for nack timeout. timeDelay time.Duration timeExpire time.Duration - // restricteds stores validators which has been restricted and the time it's + // restricteds stores nodes which has been restricted and the time it's // restricted. - restricteds map[types.ValidatorID]time.Time + restricteds map[types.NodeID]time.Time - // lastVotes and lockedVotes store the votes for nack. lastVotes[vid1][vid2] - // and lockedVotes[vid1][vid2] both mean that vid2 votes vid1. The difference - // is lockedVotes works only when vid1 is restricted, so that the votes are + // lastVotes and lockedVotes store the votes for nack. lastVotes[nid1][nid2] + // and lockedVotes[nid1][nid2] both mean that nid2 votes nid1. The difference + // is lockedVotes works only when nid1 is restricted, so that the votes are // needed to be locked. - lastVotes map[types.ValidatorID]map[types.ValidatorID]struct{} - lockedVotes map[types.ValidatorID]map[types.ValidatorID]struct{} + lastVotes map[types.NodeID]map[types.NodeID]struct{} + lockedVotes map[types.NodeID]map[types.NodeID]struct{} - // timeDiffs is the cache for last time stamps. timeDiffs[vid1][vid2] means - // the last updated timestamps vid1 sees vid2. - timeDiffs map[types.ValidatorID]map[types.ValidatorID]map[types.ValidatorID]time.Time + // timeDiffs is the cache for last time stamps. timeDiffs[nid1][nid2] means + // the last updated timestamps nid1 sees nid2. + timeDiffs map[types.NodeID]map[types.NodeID]map[types.NodeID]time.Time } // newNegativeAck creates a new negaticeAck instance. -func newNegativeAck(vid types.ValidatorID) *negativeAck { +func newNegativeAck(nid types.NodeID) *negativeAck { n := &negativeAck{ - owner: vid, - numOfValidators: 0, - restricteds: make(map[types.ValidatorID]time.Time), - lastVotes: make(map[types.ValidatorID]map[types.ValidatorID]struct{}), - lockedVotes: make(map[types.ValidatorID]map[types.ValidatorID]struct{}), - timeDiffs: make(map[types.ValidatorID]map[types.ValidatorID]map[types.ValidatorID]time.Time), + owner: nid, + numOfNodes: 0, + restricteds: make(map[types.NodeID]time.Time), + lastVotes: make(map[types.NodeID]map[types.NodeID]struct{}), + lockedVotes: make(map[types.NodeID]map[types.NodeID]struct{}), + timeDiffs: make(map[types.NodeID]map[types.NodeID]map[types.NodeID]time.Time), } - n.addValidator(vid) + n.addNode(nid) return n } -// processNewVote is called when a new "vote" occurs, that is, a validator -// sees that other 2f + 1 validators think a validator is slow. "vid" is the -// validator which propesed the block which the timestamps votes and "h" is -// the validator been voted to be nacked. +// processNewVote is called when a new "vote" occurs, that is, a node +// sees that other 2f + 1 nodes think a node is slow. "nid" is the +// node which propesed the block which the timestamps votes and "h" is +// the node been voted to be nacked. func (n *negativeAck) processNewVote( - vid types.ValidatorID, - h types.ValidatorID, -) []types.ValidatorID { + nid types.NodeID, + h types.NodeID, +) []types.NodeID { - nackeds := []types.ValidatorID{} + nackeds := []types.NodeID{} if _, exist := n.restricteds[h]; exist { - n.lockedVotes[h][vid] = struct{}{} - if len(n.lockedVotes[h]) > 2*(n.numOfValidators-1)/3 { + n.lockedVotes[h][nid] = struct{}{} + if len(n.lockedVotes[h]) > 2*(n.numOfNodes-1)/3 { nackeds = append(nackeds, h) delete(n.restricteds, h) } } else { - if n.owner == vid { + if n.owner == nid { n.restrict(h) } else { - n.lastVotes[h][vid] = struct{}{} - if len(n.lastVotes[h]) > (n.numOfValidators-1)/3 { + n.lastVotes[h][nid] = struct{}{} + if len(n.lastVotes[h]) > (n.numOfNodes-1)/3 { n.restrict(h) } } @@ -94,44 +94,44 @@ func (n *negativeAck) processNewVote( } // processTimestamps process new timestamps of a block which is proposed by -// validator vid, and returns the validators being nacked. +// node nid, and returns the nodes being nacked. func (n *negativeAck) processTimestamps( - vid types.ValidatorID, - ts map[types.ValidatorID]time.Time, -) []types.ValidatorID { + nid types.NodeID, + ts map[types.NodeID]time.Time, +) []types.NodeID { n.checkRestrictExpire() - nackeds := []types.ValidatorID{} + nackeds := []types.NodeID{} for h := range n.timeDiffs { - if n.timeDiffs[vid][h][h].Equal(ts[h]) { + if n.timeDiffs[nid][h][h].Equal(ts[h]) { votes := 0 for hh := range n.timeDiffs { - if ts[hh].Sub(n.timeDiffs[vid][h][hh]) >= n.timeDelay { + if ts[hh].Sub(n.timeDiffs[nid][h][hh]) >= n.timeDelay { votes++ } } - if votes > 2*((n.numOfValidators-1)/3) { - n.lastVotes[h][vid] = struct{}{} - nack := n.processNewVote(vid, h) + if votes > 2*((n.numOfNodes-1)/3) { + n.lastVotes[h][nid] = struct{}{} + nack := n.processNewVote(nid, h) for _, i := range nack { nackeds = append(nackeds, i) } } else { - delete(n.lastVotes[h], vid) + delete(n.lastVotes[h], nid) } } else { for hh := range n.timeDiffs { - n.timeDiffs[vid][h][hh] = ts[hh] + n.timeDiffs[nid][h][hh] = ts[hh] } - delete(n.lastVotes[h], vid) + delete(n.lastVotes[h], nid) } } return nackeds } func (n *negativeAck) checkRestrictExpire() { - expired := []types.ValidatorID{} + expired := []types.NodeID{} now := time.Now() for h, t := range n.restricteds { if now.Sub(t) >= n.timeExpire { @@ -143,19 +143,19 @@ func (n *negativeAck) checkRestrictExpire() { } } -func (n *negativeAck) restrict(vid types.ValidatorID) { - if _, exist := n.restricteds[vid]; !exist { - n.restricteds[vid] = time.Now().UTC() - n.lockedVotes[vid] = map[types.ValidatorID]struct{}{} - for h := range n.lastVotes[vid] { - n.lockedVotes[vid][h] = struct{}{} +func (n *negativeAck) restrict(nid types.NodeID) { + if _, exist := n.restricteds[nid]; !exist { + n.restricteds[nid] = time.Now().UTC() + n.lockedVotes[nid] = map[types.NodeID]struct{}{} + for h := range n.lastVotes[nid] { + n.lockedVotes[nid][h] = struct{}{} } } } -func (n *negativeAck) getRestrictedValidators() map[types.ValidatorID]struct{} { +func (n *negativeAck) getRestrictedNodes() map[types.NodeID]struct{} { n.checkRestrictExpire() - ret := map[types.ValidatorID]struct{}{} + ret := map[types.NodeID]struct{}{} for h := range n.restricteds { ret[h] = struct{}{} } @@ -170,42 +170,42 @@ func (n *negativeAck) setTimeExpire(t time.Duration) { n.timeExpire = t } -func (n *negativeAck) addValidator(vid types.ValidatorID) { - n.numOfValidators++ - n.lastVotes[vid] = make(map[types.ValidatorID]struct{}) - n.lockedVotes[vid] = make(map[types.ValidatorID]struct{}) +func (n *negativeAck) addNode(nid types.NodeID) { + n.numOfNodes++ + n.lastVotes[nid] = make(map[types.NodeID]struct{}) + n.lockedVotes[nid] = make(map[types.NodeID]struct{}) - newTimeDiff := make(map[types.ValidatorID]map[types.ValidatorID]time.Time) + newTimeDiff := make(map[types.NodeID]map[types.NodeID]time.Time) for h := range n.timeDiffs { - newTimeDiff2 := make(map[types.ValidatorID]time.Time) + newTimeDiff2 := make(map[types.NodeID]time.Time) for hh := range n.timeDiffs { newTimeDiff2[hh] = time.Time{} } newTimeDiff[h] = newTimeDiff2 } - n.timeDiffs[vid] = newTimeDiff + n.timeDiffs[nid] = newTimeDiff for h := range n.timeDiffs { - n.timeDiffs[h][vid] = make(map[types.ValidatorID]time.Time) + n.timeDiffs[h][nid] = make(map[types.NodeID]time.Time) } } -func (n *negativeAck) deleteValidator(vid types.ValidatorID) { - n.numOfValidators-- +func (n *negativeAck) deleteNode(nid types.NodeID) { + n.numOfNodes-- - delete(n.timeDiffs, vid) + delete(n.timeDiffs, nid) for h := range n.lastVotes { - delete(n.lastVotes[h], vid) + delete(n.lastVotes[h], nid) } - delete(n.lastVotes, vid) - delete(n.lockedVotes, vid) + delete(n.lastVotes, nid) + delete(n.lockedVotes, nid) for h := range n.timeDiffs { - delete(n.timeDiffs[h], vid) + delete(n.timeDiffs[h], nid) for hh := range n.timeDiffs[h] { - delete(n.timeDiffs[h][hh], vid) + delete(n.timeDiffs[h][hh], nid) } } - delete(n.restricteds, vid) + delete(n.restricteds, nid) } |