Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from consensus-shipyard/fix18
Browse files Browse the repository at this point in the history
More tests and validation
  • Loading branch information
dnkolegov authored Aug 2, 2023
2 parents 49fa8a0 + d5ff449 commit d59f63e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
7 changes: 5 additions & 2 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ func NewValidatorFromString(s string) (*Validator, error) {
idAndWeight := parts[0]
netAddr := parts[1]
parts = strings.Split(idAndWeight, ":")
if len(parts) > 2 {
if len(parts) != 2 {
return nil, fmt.Errorf("weight or ID are incorrect")
}

id := parts[0]
if len(parts) == 2 {
if parts[1] == "" {
return nil, fmt.Errorf("empty weight")
}
n, err := big.FromString(parts[1])
if err != nil {
return nil, fmt.Errorf("failed to parse weight: %w", err)
Expand Down Expand Up @@ -113,7 +116,7 @@ func SplitAndTrimEmpty(s, sep, cutset string) []string {
return nonEmptyStrings
}

// OnchainValidators information stored in the gateway actor
// OnChainValidators information stored in the gateway actor
type OnChainValidators struct {
Validators Set
TotalWeight abi.TokenAmount
Expand Down
60 changes: 43 additions & 17 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

func TestHashesAreEqualForEqualMemberships(t *testing.T) {
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)

vs1 := NewValidatorSet(0, []*Validator{v1, v2})
Expand Down Expand Up @@ -47,6 +47,12 @@ var validatorStrTests = []validatorStrTest{
"1",
true,
},
{
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy",
"/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
true,
},
{
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy",
"/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
Expand Down Expand Up @@ -97,10 +103,25 @@ var incorrectValidatorStrings = []string{
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy::/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:8",
"t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
"8:/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ",
"8:@",
}

func TestValidatorInvalidStringErr(t *testing.T) {
_, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.Error(t, err)
require.ErrorContains(t, err, "weight or ID are incorrect")

_, err = NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.Error(t, err)
require.ErrorContains(t, err, "empty weight")

_, err = NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:test@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.Error(t, err)
require.ErrorContains(t, err, "failed to parse weight")
}

func TestValidatorFromString(t *testing.T) {
for _, test := range validatorStrTests {
v, err := NewValidatorFromString(fmt.Sprintf("%v:%v@%v", test.addr, test.weight, test.netAddr))
Expand Down Expand Up @@ -128,7 +149,7 @@ func TestValidatorSetFromEnv(t *testing.T) {
addr := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy"
netAddr := "/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"

err := os.Setenv("TEST_VALIDATORS_FROM_ENV", fmt.Sprintf("%v;%v@%v", nonce, addr, netAddr))
err := os.Setenv("TEST_VALIDATORS_FROM_ENV", fmt.Sprintf("%v;%v:10@%v", nonce, addr, netAddr))
require.NoError(t, err)
defer func() {
err := os.Unsetenv("TEST_VALIDATORS_FROM_ENV")
Expand All @@ -149,11 +170,11 @@ func TestValidatorSetFromEnv(t *testing.T) {
}

func TestMembership(t *testing.T) {
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:3@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)

v := []*Validator{v1, v2, v3}
Expand All @@ -169,11 +190,11 @@ func TestValidatorSetFromFile(t *testing.T) {
require.NoError(t, err)
})

v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:10000000000000000000000000000000000000000000000000000000000@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)

vs1 := NewValidatorSet(0, []*Validator{v1, v2, v3})
Expand All @@ -193,16 +214,21 @@ func TestValidatorSetFromFile(t *testing.T) {
}

func TestValidatorSetFromString(t *testing.T) {
s1 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s2 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s3 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s1 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s2 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s3 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:3@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"

v1, err := NewValidatorFromString(s1)
require.NoError(t, err)
require.Equal(t, "1", v1.Weight.String())

v2, err := NewValidatorFromString(s2)
require.NoError(t, err)
require.Equal(t, "2", v2.Weight.String())

v3, err := NewValidatorFromString(s3)
require.NoError(t, err)
require.Equal(t, "3", v3.Weight.String())

vs1 := NewValidatorSet(3, []*Validator{v1, v2, v3})
require.Equal(t, 3, vs1.Size())
Expand All @@ -214,8 +240,8 @@ func TestValidatorSetFromString(t *testing.T) {
}

func TestValidatorSetFromJson(t *testing.T) {
s1 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s2 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s1 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"
s2 := "t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ"

v1, err := NewValidatorFromString(s1)
require.NoError(t, err)
Expand All @@ -239,7 +265,7 @@ func TestAddValidatorToFile(t *testing.T) {
require.NoError(t, err)
})

v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v1, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:1@/ip4/127.0.0.1/tcp/10000/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
err = AddValidatorToFile(fileName, v1)
require.NoError(t, err)
Expand All @@ -249,12 +275,12 @@ func TestAddValidatorToFile(t *testing.T) {
require.NoError(t, err)
require.Equal(t, true, vs1.Equal(vs))

v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v2, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:2@/ip4/127.0.0.1/tcp/10001/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
err = AddValidatorToFile(fileName, v2)
require.NoError(t, err)

v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
v3, err := NewValidatorFromString("t1wpixt5mihkj75lfhrnaa6v56n27epvlgwparujy:3@/ip4/127.0.0.1/tcp/10002/p2p/12D3KooWJhKBXvytYgPCAaiRtiNLJNSFG5jreKDu2jiVpJetzvVJ")
require.NoError(t, err)
err = AddValidatorToFile(fileName, v3)
require.NoError(t, err)
Expand Down

0 comments on commit d59f63e

Please sign in to comment.