Skip to content

Commit

Permalink
fix(gnostats): get correct isValidator value
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Aug 28, 2024
1 parent 17c3169 commit 86278f5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
23 changes: 22 additions & 1 deletion contribs/gnostats/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,28 @@ func getRandomBatchResults(t *testing.T, random *mrand.Rand) []any {
peers[i] = ctypes.Peer{NodeInfo: randomNodeInfo(t, random)}
}

// Generate random validators
validators := make([]*types.Validator, randomIntInRange(t, random, 3, 32))
for i := range validators {
validators[i], _ = types.RandValidator(false, 42)
}

// Get node validator info from validators list or create a new one
var validator *types.Validator
if random.Intn(2) == 0 {
validator = validators[randomIntInRange(t, random, 0, len(validators)-1)]
} else {
validator, _ = types.RandValidator(false, 42)
}
validatorInfo := ctypes.ValidatorInfo{
Address: validator.Address,
PubKey: validator.PubKey,
VotingPower: validator.VotingPower,
}

return []any{
&ctypes.ResultStatus{NodeInfo: randomNodeInfo(t, random)},
&ctypes.ResultStatus{NodeInfo: randomNodeInfo(t, random), ValidatorInfo: validatorInfo},
&ctypes.ResultValidators{Validators: validators},
&ctypes.ResultNetInfo{Peers: peers},
&ctypes.ResultUnconfirmedTxs{Total: randomIntInRange(t, random, 0, 100)},

Expand Down Expand Up @@ -158,6 +178,7 @@ func TestAgent_E2E(t *testing.T) {

mockCaller.On("NewBatch").Return(mockBatch)
mockBatch.On("Status").Return(nil)
mockBatch.On("Validators").Return(nil)
mockBatch.On("NetInfo").Return(nil)
mockBatch.On("NumUnconfirmedTxs").Return(nil)
mockBatch.On("Block", (*uint64)(nil)).Return(nil)
Expand Down
25 changes: 17 additions & 8 deletions contribs/gnostats/agent/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type rpcClient interface {
}
type rpcBatch interface {
Status() error
Validators() error
NetInfo() error
NumUnconfirmedTxs() error
Block(*uint64) error
Expand All @@ -38,6 +39,8 @@ func (c *collector) CollectDynamic(ctx context.Context) (*proto.DynamicInfo, err
for _, request := range [](func() error){
// Request Status to get address, moniker and validator info
batch.Status,
// Request Validators to get the list of validators
batch.Validators,
// Request NetInfo to get peers info
batch.NetInfo,
// Request NumUnconfirmedTxs to get pending txs
Expand All @@ -60,11 +63,12 @@ func (c *collector) CollectDynamic(ctx context.Context) (*proto.DynamicInfo, err

// Cast responses to the appropriate types
var (
status = results[0].(*ctypes.ResultStatus)
netInfo = results[1].(*ctypes.ResultNetInfo)
uncTxs = results[2].(*ctypes.ResultUnconfirmedTxs)
blk = results[3].(*ctypes.ResultBlock)
blkRes = results[4].(*ctypes.ResultBlockResults)
status = results[0].(*ctypes.ResultStatus)
validators = results[1].(*ctypes.ResultValidators)
netInfo = results[2].(*ctypes.ResultNetInfo)
uncTxs = results[3].(*ctypes.ResultUnconfirmedTxs)
blk = results[4].(*ctypes.ResultBlock)
blkRes = results[5].(*ctypes.ResultBlockResults)
)

// Convert the list of peers from NetInfo to proto type
Expand All @@ -78,9 +82,14 @@ func (c *collector) CollectDynamic(ctx context.Context) (*proto.DynamicInfo, err
}
}

// Determine if the node is a validator using validatorInfo
// @TODO: Check if this is right with @zivkovicmilos
isValidator := status.ValidatorInfo.Address.ID().String() != ""
// Determine if the node is a validator for the last block by searching for
// own validatorInfo address in validators list
isValidator := false
for _, validator := range validators.Validators {
if validator.Address.Compare(status.ValidatorInfo.Address) == 0 {
isValidator = true
}
}

// Get gas used / wanted in DeliverTxs (if any)
var gasUsed, gasWanted uint64
Expand Down
47 changes: 41 additions & 6 deletions contribs/gnostats/agent/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/bft/state"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/crypto/ed25519"
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/gnolang/gnostats/proto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,6 +41,11 @@ func (m *MockRPCBatch) Status() error {
return args.Error(0)
}

func (m *MockRPCBatch) Validators() error {
args := m.Called()
return args.Error(0)
}

func (m *MockRPCBatch) NetInfo() error {
args := m.Called()
return args.Error(0)
Expand Down Expand Up @@ -102,6 +108,16 @@ func getBatchResults(t *testing.T) []any {
)
}

// Generate validators for Validators request
validators := make([]*types.Validator, 3)
for i, pubKey := range []crypto.PubKey{
ed25519.GenPrivKeyFromSecret([]byte("validator1")).PubKey(),
ed25519.GenPrivKeyFromSecret([]byte("validator2")).PubKey(),
ed25519.GenPrivKeyFromSecret([]byte("validator3")).PubKey(),
} {
validators[i] = types.NewValidator(pubKey, 42)
}

return []any{
&ctypes.ResultStatus{
NodeInfo: p2p.NodeInfo{
Expand All @@ -114,6 +130,15 @@ func getBatchResults(t *testing.T) []any {
},
),
},
ValidatorInfo: ctypes.ValidatorInfo{
Address: validators[2].Address,
PubKey: validators[2].PubKey,
VotingPower: validators[2].VotingPower,
},
},

&ctypes.ResultValidators{
Validators: validators,
},

&ctypes.ResultNetInfo{
Expand Down Expand Up @@ -151,16 +176,24 @@ func compareBatchResultToDynamicInfo(t *testing.T, results []any, dynamicInfo *p
t.Helper()

var (
status = results[0].(*ctypes.ResultStatus)
netInfo = results[1].(*ctypes.ResultNetInfo)
uncTxs = results[2].(*ctypes.ResultUnconfirmedTxs)
blk = results[3].(*ctypes.ResultBlock)
blkRes = results[4].(*ctypes.ResultBlockResults)
status = results[0].(*ctypes.ResultStatus)
validators = results[1].(*ctypes.ResultValidators)
netInfo = results[2].(*ctypes.ResultNetInfo)
uncTxs = results[3].(*ctypes.ResultUnconfirmedTxs)
blk = results[4].(*ctypes.ResultBlock)
blkRes = results[5].(*ctypes.ResultBlockResults)
)

assert.Equal(t, dynamicInfo.Address, status.NodeInfo.ID().String())
assert.Equal(t, dynamicInfo.Moniker, status.NodeInfo.Moniker)
assert.Equal(t, dynamicInfo.IsValidator, status.ValidatorInfo.Address.ID().String() != "")

isValidator := false
for _, validator := range validators.Validators {
if validator.Address.Compare(status.ValidatorInfo.Address) == 0 {
isValidator = true
}
}
assert.Equal(t, dynamicInfo.IsValidator, isValidator)

assert.NotNil(t, dynamicInfo.NetInfo)
assert.Equal(t, dynamicInfo.NetInfo.P2PAddress, status.NodeInfo.NetAddress.String())
Expand Down Expand Up @@ -190,6 +223,7 @@ func TestCollector_DynamicSuccess(t *testing.T) {

mockCaller.On("NewBatch").Return(mockBatch)
mockBatch.On("Status").Return(nil)
mockBatch.On("Validators").Return(nil)
mockBatch.On("NetInfo").Return(nil)
mockBatch.On("NumUnconfirmedTxs").Return(nil)
mockBatch.On("Block", (*uint64)(nil)).Return(nil)
Expand Down Expand Up @@ -238,6 +272,7 @@ func TestCollector_DynamicTimeout(t *testing.T) {

mockCaller.On("NewBatch").Return(mockBatch)
mockBatch.On("Status").Return(nil)
mockBatch.On("Validators").Return(nil)
mockBatch.On("NetInfo").Return(nil)
mockBatch.On("NumUnconfirmedTxs").Return(nil)
mockBatch.On("Block", (*uint64)(nil)).Return(nil)
Expand Down

0 comments on commit 86278f5

Please sign in to comment.