Playground
doc.gno
// v0 - Unaudited: This is an initial version that has not yet been formally audited. // A fully audited version will be published as a subsequent release. // Use in production at your own risk. // // Package poa implements a Proof of Authority validator set management system. package poa
gnomod.toml
module = "gno.land/p/nt/poa/v0" gno = "0.9" [addpkg] creator = "g15ge0ae9077eh40erwrn2eq0xw6wupwqthpv34l"
option.gno
package poa
import "gno.land/p/sys/validators/v0"
type Option func(*PoA)
// WithInitialSet sets the initial PoA validator set
func WithInitialSet(validators []validators.Validator) Option {
return func(p *PoA) {
for _, validator := range validators {
p.validators.Set(validator.Address.String(), validator)
}
}
}
poa.gno
package poa
import (
"errors"
"gno.land/p/nt/bptree/v0"
"gno.land/p/sys/validators/v0"
)
var ErrInvalidVotingPower = errors.New("invalid voting power")
// PoA specifies the Proof of Authority validator set, with simple add / remove constraints.
//
// To add:
// - proposed validator must not be part of the set already
// - proposed validator voting power must be > 0
//
// To remove:
// - proposed validator must be part of the set already
type PoA struct {
validators *bptree.BPTree // address -> validators.Validator
}
// NewPoA creates a new empty Proof of Authority validator set
func NewPoA(opts ...Option) *PoA {
// Create the empty set
p := &PoA{
validators: bptree.NewBPTree32(),
}
// Apply the options
for _, opt := range opts {
opt(p)
}
return p
}
func (p *PoA) AddValidator(address_XXX address, pubKey string, power uint64) (validators.Validator, error) {
// Validate that the operation is a valid call.
// Check if the validator is already in the set
if p.IsValidator(address_XXX) {
return validators.Validator{}, validators.ErrValidatorExists
}
// Make sure the voting power > 0
if power == 0 {
return validators.Validator{}, ErrInvalidVotingPower
}
v := validators.Validator{
Address: address_XXX,
PubKey: pubKey, // TODO: in the future, verify the public key
VotingPower: power,
}
// Add the validator to the set
p.validators.Set(address_XXX.String(), v)
return v, nil
}
func (p *PoA) RemoveValidator(address_XXX address) (validators.Validator, error) {
// Validate that the operation is a valid call
// Fetch the validator
validator, err := p.GetValidator(address_XXX)
if err != nil {
return validators.Validator{}, err
}
// Remove the validator from the set
p.validators.Remove(address_XXX.String())
return validator, nil
}
func (p *PoA) IsValidator(address_XXX address) bool {
return p.validators.Has(address_XXX.String())
}
func (p *PoA) GetValidator(address_XXX address) (validators.Validator, error) {
validatorRaw := p.validators.Get(address_XXX.String())
if validatorRaw == nil {
return validators.Validator{}, validators.ErrValidatorMissing
}
validator := validatorRaw.(validators.Validator)
return validator, nil
}
func (p *PoA) GetValidators() []validators.Validator {
vals := make([]validators.Validator, 0, p.validators.Size())
p.validators.Iterate("", "", func(_ string, value any) bool {
validator := value.(validators.Validator)
vals = append(vals, validator)
return false
})
return vals
}
poa_test.gno
package poa
import (
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
"gno.land/p/nt/urequire/v0"
"gno.land/p/sys/validators/v0"
"gno.land/p/nt/ufmt/v0"
)
// 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: 1,
}
vals = append(vals, val)
}
return vals
}
func TestPoA_AddValidator_Invalid(t *testing.T) {
t.Parallel()
t.Run("validator already in set", func(t *testing.T) {
t.Parallel()
var (
proposalAddress = testutils.TestAddress("caller")
proposalKey = "public-key"
initialSet = generateTestValidators(1)
)
initialSet[0].Address = proposalAddress
initialSet[0].PubKey = proposalKey
// Create the protocol with an initial set
p := NewPoA(WithInitialSet(initialSet))
// Attempt to add the validator
_, err := p.AddValidator(proposalAddress, proposalKey, 1)
uassert.ErrorIs(t, err, validators.ErrValidatorExists)
})
t.Run("invalid voting power", func(t *testing.T) {
t.Parallel()
var (
proposalAddress = testutils.TestAddress("caller")
proposalKey = "public-key"
)
// Create the protocol with no initial set
p := NewPoA()
// Attempt to add the validator
_, err := p.AddValidator(proposalAddress, proposalKey, 0)
uassert.ErrorIs(t, err, ErrInvalidVotingPower)
})
}
func TestPoA_AddValidator(t *testing.T) {
t.Parallel()
var (
proposalAddress = testutils.TestAddress("caller")
proposalKey = "public-key"
)
// Create the protocol with no initial set
p := NewPoA()
// Attempt to add the validator
_, err := p.AddValidator(proposalAddress, proposalKey, 1)
uassert.NoError(t, err)
// Make sure the validator is added
if !p.IsValidator(proposalAddress) || p.validators.Size() != 1 {
t.Fatal("address is not validator")
}
}
func TestPoA_RemoveValidator_Invalid(t *testing.T) {
t.Parallel()
t.Run("proposed removal not in set", func(t *testing.T) {
t.Parallel()
var (
proposalAddress = testutils.TestAddress("caller")
initialSet = generateTestValidators(1)
)
initialSet[0].Address = proposalAddress
// Create the protocol with an initial set
p := NewPoA(WithInitialSet(initialSet))
// Attempt to remove the validator
_, err := p.RemoveValidator(testutils.TestAddress("totally random"))
uassert.ErrorIs(t, err, validators.ErrValidatorMissing)
})
}
func TestPoA_RemoveValidator(t *testing.T) {
t.Parallel()
var (
proposalAddress = testutils.TestAddress("caller")
initialSet = generateTestValidators(1)
)
initialSet[0].Address = proposalAddress
// Create the protocol with an initial set
p := NewPoA(WithInitialSet(initialSet))
// Attempt to remove the validator
_, err := p.RemoveValidator(proposalAddress)
urequire.NoError(t, err)
// Make sure the validator is removed
if p.IsValidator(proposalAddress) || p.validators.Size() != 0 {
t.Fatal("address is validator")
}
}
func TestPoA_GetValidator(t *testing.T) {
t.Parallel()
t.Run("validator not in set", func(t *testing.T) {
t.Parallel()
// Create the protocol with no initial set
p := NewPoA()
// Attempt to get the voting power
_, err := p.GetValidator(testutils.TestAddress("caller"))
uassert.ErrorIs(t, err, validators.ErrValidatorMissing)
})
t.Run("validator fetched", func(t *testing.T) {
t.Parallel()
var (
address_XXX = testutils.TestAddress("caller")
pubKey = "public-key"
votingPower = uint64(10)
initialSet = generateTestValidators(1)
)
initialSet[0].Address = address_XXX
initialSet[0].PubKey = pubKey
initialSet[0].VotingPower = votingPower
// Create the protocol with an initial set
p := NewPoA(WithInitialSet(initialSet))
// Get the validator
val, err := p.GetValidator(address_XXX)
urequire.NoError(t, err)
// Validate the address
if val.Address != address_XXX {
t.Fatal("invalid address")
}
// Validate the voting power
if val.VotingPower != votingPower {
t.Fatal("invalid voting power")
}
// Validate the public key
if val.PubKey != pubKey {
t.Fatal("invalid public key")
}
})
}
func TestPoA_GetValidators(t *testing.T) {
t.Parallel()
t.Run("empty set", func(t *testing.T) {
t.Parallel()
// Create the protocol with no initial set
p := NewPoA()
// Attempt to get the voting power
vals := p.GetValidators()
if len(vals) != 0 {
t.Fatal("validator set is not empty")
}
})
t.Run("validator set fetched", func(t *testing.T) {
t.Parallel()
initialSet := generateTestValidators(10)
// Create the protocol with an initial set
p := NewPoA(WithInitialSet(initialSet))
// Get the validator set
vals := p.GetValidators()
if len(vals) != len(initialSet) {
t.Fatal("returned validator set mismatch")
}
for _, val := range vals {
for _, initialVal := range initialSet {
if val.Address != initialVal.Address {
continue
}
// Validate the voting power
uassert.Equal(t, val.VotingPower, initialVal.VotingPower)
// Validate the public key
uassert.Equal(t, val.PubKey, initialVal.PubKey)
}
}
})
}