-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package score | ||
|
||
import "github.com/pactus-project/pactus/types/certificate" | ||
|
||
type scoreData struct { | ||
inCommittee int // Number of times a validator was in the committee | ||
absent int // Number of times a validator was absent from the committee (not voted) | ||
} | ||
|
||
type Manager struct { | ||
certs map[uint32]*certificate.Certificate | ||
vals map[int32]*scoreData | ||
maxCert uint32 | ||
} | ||
|
||
func NewScoreManager(maxCert uint32) *Manager { | ||
return &Manager{ | ||
certs: make(map[uint32]*certificate.Certificate), | ||
vals: make(map[int32]*scoreData), | ||
maxCert: maxCert, | ||
} | ||
} | ||
|
||
func (sm *Manager) SetCertificate(cert *certificate.Certificate) { | ||
lastHeight := cert.Height() | ||
sm.certs[lastHeight] = cert | ||
|
||
for _, num := range cert.Committers() { | ||
data, ok := sm.vals[num] | ||
if !ok { | ||
data = new(scoreData) | ||
sm.vals[num] = data | ||
} | ||
|
||
data.inCommittee++ | ||
} | ||
|
||
for _, num := range cert.Absentees() { | ||
data := sm.vals[num] | ||
sm.vals[num] = data | ||
|
||
data.absent++ | ||
} | ||
|
||
oldHeight := lastHeight - sm.maxCert | ||
oldCert, ok := sm.certs[oldHeight] | ||
if ok { | ||
for _, num := range oldCert.Committers() { | ||
data := sm.vals[num] | ||
data.inCommittee-- | ||
} | ||
|
||
for _, num := range oldCert.Absentees() { | ||
data := sm.vals[num] | ||
data.absent-- | ||
} | ||
|
||
delete(sm.certs, oldHeight) | ||
} | ||
} | ||
|
||
func (sm *Manager) AvailabilityScore(valNum int32) float64 { | ||
data, ok := sm.vals[valNum] | ||
if ok { | ||
if data.inCommittee == 0 { | ||
return 1.0 | ||
} | ||
|
||
return 1 - (float64(data.absent) / float64(data.inCommittee)) | ||
} | ||
|
||
return 1.0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package score | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pactus-project/pactus/types/certificate" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestScoreManager(t *testing.T) { | ||
maxCert := uint32(3) | ||
sm := NewScoreManager(maxCert) | ||
|
||
cert1 := certificate.NewCertificate(1, 0, []int32{0, 1, 2, 3}, []int32{0}, nil) | ||
cert2 := certificate.NewCertificate(2, 0, []int32{0, 1, 2, 3}, []int32{3}, nil) | ||
cert3 := certificate.NewCertificate(3, 0, []int32{1, 2, 3, 4}, []int32{2}, nil) | ||
cert4 := certificate.NewCertificate(4, 0, []int32{1, 2, 3, 4}, []int32{2}, nil) | ||
cert5 := certificate.NewCertificate(5, 0, []int32{1, 2, 3, 4}, []int32{2}, nil) | ||
|
||
tests := []struct { | ||
cert *certificate.Certificate | ||
score0 float64 | ||
score1 float64 | ||
score2 float64 | ||
score3 float64 | ||
score4 float64 | ||
}{ | ||
{cert1, 0, 1, 1, 1, 1}, | ||
{cert2, 0.5, 1, 1, 0.5, 1}, | ||
{cert3, 0.5, 1, 1 - (float64(1) / float64(3)), 1 - (float64(1) / float64(3)), 1}, | ||
{cert4, 1, 1, 1 - (float64(2) / float64(3)), 1 - (float64(1) / float64(3)), 1}, | ||
{cert5, 1, 1, 0, 1, 1}, | ||
} | ||
|
||
for i, test := range tests { | ||
sm.SetCertificate(test.cert) | ||
|
||
score0 := sm.AvailabilityScore(0) | ||
assert.Equal(t, test.score0, score0, "#%v: invalid score0, expected %v, got %v", | ||
i, test.score0, score0) | ||
|
||
score1 := sm.AvailabilityScore(1) | ||
assert.Equal(t, test.score1, score1, "#%v: invalid score1, expected %v, got %v", | ||
i, test.score1, score1) | ||
|
||
score2 := sm.AvailabilityScore(2) | ||
assert.Equal(t, test.score2, score2, "#%v: invalid score2, expected %v, got %v", | ||
i, test.score2, score2) | ||
|
||
score3 := sm.AvailabilityScore(3) | ||
assert.Equal(t, test.score3, score3, "#%v: invalid score3, expected %v, got %v", | ||
i, test.score3, score3) | ||
|
||
score4 := sm.AvailabilityScore(4) | ||
assert.Equal(t, test.score4, score4, "#%v: invalid score4, expected %v, got %v", | ||
i, test.score4, score4) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters