Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort RewardDelegators when marshaling a validator #1591

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions x/nodes/keeper/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package keeper

import (
"encoding/hex"
"fmt"
"math/rand"
"reflect"
"strconv"
"testing"
"time"

"github.com/pokt-network/pocket-core/crypto"
sdk "github.com/pokt-network/pocket-core/types"
"github.com/pokt-network/pocket-core/x/nodes/types"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -150,6 +155,60 @@ func Test_sortNoLongerStakedValidators(t *testing.T) {
}
}

// Verifies the marshaled product of a validator is the same regardless of
// the order of RewardDelegators. If it is not, a key of the merkle tree in
// application.db is not deterministic, that will cause consensus failure.
func Test_Marshal_RewardDelegators(t *testing.T) {
numOfIterations := 100
numOfDelegators := 100

chains := []string{"0001", "0002", "005A", "005B", "005C", "005D"}
url := "https://test.pokt.network:443"
stake := int64(18000000000)
pubKey, _ := crypto.NewPublicKey("0b787e54e66b3db3a3396c2322d8314287e08990f7696c490153b078bada7e94")
nodeAddr := sdk.Address(pubKey.PubKey().Address())
outputAddr, _ := sdk.AddressFromHex("42846261e1798fc08e1dfd97325af7b280f815b0")

randNums := make([]int, numOfDelegators)
for j := 0; j < numOfDelegators; j++ {
randNums[j] = rand.Int()
}

var valHash string

for i := 0; i < numOfIterations; i++ {
rand.Shuffle(len(randNums), func(i, j int) {
randNums[i], randNums[j] = randNums[j], randNums[i]
})
delegatorMap := map[string]uint32{}
for randNum := range randNums {
delegatorMap[strconv.Itoa(randNum)] = uint32(randNum % 10)
}

val := types.Validator{
Address: nodeAddr,
PublicKey: pubKey,
Jailed: false,
Status: sdk.Staked,
Chains: chains,
UnstakingCompletionTime: time.Time{},
ServiceURL: url,
StakedTokens: sdk.NewInt(stake),
OutputAddress: outputAddr,
RewardDelegators: delegatorMap,
}
valBytes, err := val.Marshal()
assert.Nil(t, err)

// Make sure the hash is always the same regardless of the order of randNums
if len(valHash) == 0 {
valHash = hex.EncodeToString(valBytes)
} else {
assert.Equal(t, hex.EncodeToString(valBytes), valHash)
}
}
}

// There are two versions of structs to represent a validator.
// - LegacyValidator - the original version
// - Validator - LegacyValidator + OutputAddress + Delegators (since 0.11)
Expand Down
8 changes: 8 additions & 0 deletions x/nodes/types/nodes.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.