-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dev/zivkovicmilos/test4-genesis
- Loading branch information
Showing
25 changed files
with
881 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
package validators | ||
|
||
import "gno.land/p/sys/validators" | ||
import ( | ||
"gno.land/p/sys/validators" | ||
) | ||
|
||
// getChanges returns the validator changes stored on the realm. | ||
// This function is unexported and intended to be called by gno.land through the GnoSDK | ||
func getChanges() []validators.Validator { | ||
// Construct the changes | ||
valsetChanges := make([]validators.Validator, len(changes)) | ||
copy(valsetChanges, changes) | ||
// GetChanges returns the validator changes stored on the realm, since the given block number. | ||
// This function is intended to be called by gno.land through the GnoSDK | ||
func GetChanges(from int64) []validators.Validator { | ||
valsetChanges := make([]validators.Validator, 0) | ||
|
||
// Reset the changes set | ||
changes = changes[:0] | ||
// Gather the changes from the specified block | ||
changes.Iterate(getBlockID(from), "", func(_ string, value interface{}) bool { | ||
chs := value.([]change) | ||
|
||
for _, ch := range chs { | ||
valsetChanges = append(valsetChanges, ch.validator) | ||
} | ||
|
||
return false | ||
}) | ||
|
||
return valsetChanges | ||
} |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package validators | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
"gno.land/p/nt/poa" | ||
"gno.land/p/sys/validators" | ||
) | ||
|
||
func init() { | ||
// The default valset protocol is PoA | ||
vp = poa.NewPoA() | ||
|
||
// No changes to apply initially | ||
changes = make([]validators.Validator, 0) | ||
changes = avl.NewTree() | ||
} |
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,102 @@ | ||
package validators | ||
|
||
import ( | ||
"testing" | ||
|
||
"std" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
"gno.land/p/demo/uassert" | ||
"gno.land/p/demo/ufmt" | ||
"gno.land/p/sys/validators" | ||
) | ||
|
||
// generateTestValidators generates a dummy validator set | ||
func generateTestValidators(count int) []validators.Validator { | ||
vals := make([]validators.Validator, 0, count) | ||
|
||
for i := 0; i < count; i++ { | ||
val := validators.Validator{ | ||
Address: testutils.TestAddress(ufmt.Sprintf("%d", i)), | ||
PubKey: "public-key", | ||
VotingPower: 10, | ||
} | ||
|
||
vals = append(vals, val) | ||
} | ||
|
||
return vals | ||
} | ||
|
||
func TestValidators_AddRemove(t *testing.T) { | ||
// Clear any changes | ||
changes = avl.NewTree() | ||
|
||
var ( | ||
vals = generateTestValidators(100) | ||
initialHeight = int64(123) | ||
) | ||
|
||
// Add in the validators | ||
for _, val := range vals { | ||
addValidator(val) | ||
|
||
// Make sure the validator is added | ||
uassert.True(t, vp.IsValidator(val.Address)) | ||
|
||
std.TestSkipHeights(1) | ||
} | ||
|
||
for i := initialHeight; i < initialHeight+int64(len(vals)); i++ { | ||
// Make sure the changes are saved | ||
chs := GetChanges(i) | ||
|
||
// We use the funky index calculation to make sure | ||
// changes are properly handled for each block span | ||
uassert.Equal(t, initialHeight+int64(len(vals))-i, int64(len(chs))) | ||
|
||
for index, val := range vals[i-initialHeight:] { | ||
// Make sure the changes are equal to the additions | ||
ch := chs[index] | ||
|
||
uassert.Equal(t, val.Address, ch.Address) | ||
uassert.Equal(t, val.PubKey, ch.PubKey) | ||
uassert.Equal(t, val.VotingPower, ch.VotingPower) | ||
} | ||
} | ||
|
||
// Save the beginning height for the removal | ||
initialRemoveHeight := std.GetHeight() | ||
|
||
// Clear any changes | ||
changes = avl.NewTree() | ||
|
||
// Remove the validators | ||
for _, val := range vals { | ||
removeValidator(val.Address) | ||
|
||
// Make sure the validator is removed | ||
uassert.False(t, vp.IsValidator(val.Address)) | ||
|
||
std.TestSkipHeights(1) | ||
} | ||
|
||
for i := initialRemoveHeight; i < initialRemoveHeight+int64(len(vals)); i++ { | ||
// Make sure the changes are saved | ||
chs := GetChanges(i) | ||
|
||
// We use the funky index calculation to make sure | ||
// changes are properly handled for each block span | ||
uassert.Equal(t, initialRemoveHeight+int64(len(vals))-i, int64(len(chs))) | ||
|
||
for index, val := range vals[i-initialRemoveHeight:] { | ||
// Make sure the changes are equal to the additions | ||
ch := chs[index] | ||
|
||
uassert.Equal(t, val.Address, ch.Address) | ||
uassert.Equal(t, val.PubKey, ch.PubKey) | ||
uassert.Equal(t, uint64(0), ch.VotingPower) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.