-
Notifications
You must be signed in to change notification settings - Fork 158
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
3 changed files
with
237 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package consensus |
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,229 @@ | ||
package election | ||
|
||
import ( | ||
"encoding/hex" | ||
"github.com/nknorg/nkn/v2/common" | ||
"github.com/nknorg/nkn/v2/config" | ||
"github.com/nknorg/nkn/v2/util" | ||
"math/rand" | ||
"os" | ||
"reflect" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var neighbors []*Election | ||
|
||
func TestMain(m *testing.M) { | ||
neighbors = make([]*Election, 20) | ||
for i := 0; i < len(neighbors); i++ { | ||
elc, _ := newElection() | ||
neighbors[i] = elc | ||
} | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestOneVoteElection(t *testing.T) { | ||
check := func(f string, got, want interface{}) { | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("%s mismatch: got %v, want %v", f, got, want) | ||
} | ||
} | ||
elc, err := newElection() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
elc.Start() | ||
|
||
neighborID := "123" | ||
err = elc.ReceiveVote(neighborID, common.EmptyUint256) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
elc.Stop() | ||
result, u, f, err := elc.GetResult() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
check("result", result, common.EmptyUint256) | ||
check("abs weight", u, uint32(1)) | ||
check("relative weight", f, float32(1)) | ||
} | ||
|
||
func TestElection(t *testing.T) { | ||
check := func(f string, got, want interface{}) { | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("%s mismatch: got %v, want %v", f, got, want) | ||
} | ||
} | ||
elc, err := newElection() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = elc.SetInitialVote(common.EmptyUint256) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
elc.Start() | ||
|
||
neighborIDs := make([]interface{}, 10) | ||
for i := range neighborIDs { | ||
neighborIDs[i] = hex.EncodeToString(util.RandomBytes(16)) | ||
} | ||
err = elc.PrefillNeighborVotes(neighborIDs, common.MaxUint256) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = elc.ReceiveVote("123", common.EmptyUint256) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
nids := elc.GetNeighborIDsByVote(common.MaxUint256) | ||
check("neighbors id", len(nids), len(neighborIDs)) | ||
|
||
nid := elc.GetNeighborIDsByVote(common.EmptyUint256) | ||
check("neighbor id", len(nid), 1) | ||
|
||
count := elc.NeighborVoteCount() | ||
check("neighbor vote count", count, uint32(11)) | ||
|
||
elc.Stop() | ||
result, u, f, err := elc.GetResult() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
check("result", result, common.MaxUint256) | ||
check("abs weight", u, uint32(10)) | ||
check("relative weight", f, float32(0.8333333)) | ||
} | ||
|
||
func TestMultiElection(t *testing.T) { | ||
check := func(f string, got, want interface{}) { | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("%s mismatch: got %v, want %v", f, got, want) | ||
} | ||
} | ||
var wg sync.WaitGroup | ||
for i := 0; i < len(neighbors); i++ { | ||
i := i | ||
wg.Add(1) | ||
go func(int) { | ||
defer wg.Done() | ||
neighbors[i].Start() | ||
}(i) | ||
} | ||
for _, e := range neighbors { | ||
go e.run() | ||
} | ||
|
||
h := common.EmptyUint256 | ||
h[0] = 1 | ||
|
||
neighbors[0].txVoteChan <- h | ||
neighbors[1].txVoteChan <- h | ||
neighbors[2].txVoteChan <- common.MaxUint256 | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
wg.Wait() | ||
for _, e := range neighbors { | ||
for { | ||
select { | ||
case <-e.txVoteChan: | ||
continue | ||
default: | ||
} | ||
break | ||
} | ||
e.Stop() | ||
result, _, _, err := e.GetResult() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
check("result", result, h) | ||
} | ||
|
||
} | ||
|
||
func newElection() (*Election, error) { | ||
getWeight := func(neighborID interface{}) uint32 { | ||
return 1 | ||
} | ||
c := &Config{ | ||
Duration: config.ConsensusDuration / 2, | ||
MinVotingInterval: 200 * time.Millisecond, | ||
MaxVotingInterval: 2 * time.Second, | ||
ChangeVoteMinRelativeWeight: 0.5, | ||
ConsensusMinRelativeWeight: 2.0 / 3.0, | ||
GetWeight: getWeight, | ||
} | ||
|
||
elc, err := NewElection(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return elc, nil | ||
} | ||
|
||
func getRandomElemFromSlice(n []string) string { | ||
rand.NewSource(time.Now().Unix()) | ||
rand.Shuffle(len(n), func(i, j int) { | ||
n[i], n[j] = n[j], n[i] | ||
}) | ||
return n[0] | ||
} | ||
|
||
func (election *Election) run() { | ||
n1 := []string{"1", "2", "3", "6", "7", "8", "9", "10"} | ||
n2 := []string{"4", "5", "11"} | ||
|
||
for { | ||
i := 0 | ||
election.neighborVotes.Range(func(key, value any) bool { | ||
i++ | ||
return true | ||
}) | ||
if i == len(n1)+len(n2) { | ||
return | ||
} | ||
select { | ||
case val := <-election.txVoteChan: | ||
hash, ok := val.(common.Uint256) | ||
if !ok { | ||
return | ||
} | ||
|
||
var neighborID string | ||
if hash == common.MaxUint256 { | ||
neighborID = getRandomElemFromSlice(n2) | ||
} else { | ||
neighborID = getRandomElemFromSlice(n1) | ||
} | ||
err := election.ReceiveVote(neighborID, hash) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, n := range neighbors { | ||
nn := n | ||
go func() { | ||
if nn == election { | ||
return | ||
} | ||
if !nn.IsStopped() { | ||
nn.txVoteChan <- hash | ||
} | ||
}() | ||
} | ||
} | ||
} | ||
} |