Skip to content

Commit

Permalink
Add blockstats test to check for event details (#26)
Browse files Browse the repository at this point in the history
* Delete extraneous function makeTestBlock inline

* Add test for blockstats event details

* Delete another comment
  • Loading branch information
algochoi authored Nov 2, 2022
1 parent 5ccca0d commit 37a0812
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions cmd/algoh/blockstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/logging/telemetryspec"
"github.com/algorand/go-algorand/rpcs"
"github.com/algorand/go-algorand/test/partitiontest"
Expand All @@ -33,15 +34,15 @@ type event struct {

identifier telemetryspec.Event

details interface{}
details telemetryspec.BlockStatsEventDetails
}

type MockEventSender struct {
events []event
}

func (mes *MockEventSender) EventWithDetails(category telemetryspec.Category, identifier telemetryspec.Event, details interface{}) {
mes.events = append(mes.events, event{category: category, identifier: identifier, details: details})
mes.events = append(mes.events, event{category: category, identifier: identifier, details: details.(telemetryspec.BlockStatsEventDetails)})
}

// Helper method to create an EncodedBlockCert for the block handler.
Expand All @@ -54,10 +55,6 @@ func TestConsecutiveBlocks(t *testing.T) {
sender := MockEventSender{}
bs := blockstats{log: &sender}

makeTestBlock := func(round uint64) rpcs.EncodedBlockCert {
return rpcs.EncodedBlockCert{Block: bookkeeping.Block{BlockHeader: bookkeeping.BlockHeader{Round: basics.Round(round)}}}
}

bs.onBlock(makeTestBlock(300))
// first consecutive block
bs.onBlock(makeTestBlock(301))
Expand All @@ -69,6 +66,46 @@ func TestConsecutiveBlocks(t *testing.T) {
require.Equal(t, 2, len(sender.events))
}

func TestEventWithDetails(t *testing.T) {
partitiontest.PartitionTest(t)
sender := MockEventSender{}
bs := blockstats{log: &sender}

// Create blocks with some senders in the payload.
makeStxnWithAddr := func(addr basics.Address) transactions.SignedTxnInBlock {
return transactions.SignedTxnInBlock{SignedTxnWithAD: transactions.SignedTxnWithAD{SignedTxn: transactions.SignedTxn{Txn: transactions.Transaction{Header: transactions.Header{Sender: addr}}}}}
}
addr := basics.Address{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
otherAddr := basics.Address{0x7, 0xda, 0xcb, 0x4b, 0x6d, 0x9e, 0xd1, 0x41, 0xb1, 0x75, 0x76, 0xbd, 0x45, 0x9a, 0xe6, 0x42, 0x1d, 0x48, 0x6d, 0xa3, 0xd4, 0xef, 0x22, 0x47, 0xc4, 0x9, 0xa3, 0x96, 0xb8, 0x2e, 0xa2, 0x21}
// Check that only unique addrs are returned by ActiveUsers.
stxn1 := makeStxnWithAddr(addr)
stxn2 := makeStxnWithAddr(otherAddr)
stxn3 := makeStxnWithAddr(addr)
// Make block with some transactions.
testBlock := makeTestBlock(300)
testBlock.Block.Payset = transactions.Payset{stxn1, stxn2, stxn3}

bs.onBlock(makeTestBlock(299))
bs.onBlock(testBlock)
bs.onBlock(makeTestBlock(301))

testCases := []struct {
round uint64
activeUsers uint64
txns uint64
}{
{uint64(300), uint64(2), uint64(3)},
{uint64(301), uint64(0), uint64(0)},
}

require.Equal(t, 2, len(sender.events))
for i, event := range sender.events {
require.Equal(t, testCases[i].round, event.details.Round)
require.Equal(t, testCases[i].activeUsers, event.details.ActiveUsers)
require.Equal(t, testCases[i].txns, event.details.Transactions)
}
}

func TestAgreementTime(t *testing.T) {
partitiontest.PartitionTest(t)
sleepTime := 50 * time.Millisecond
Expand All @@ -87,7 +124,7 @@ func TestAgreementTime(t *testing.T) {
end := time.Now()

require.Equal(t, 1, len(sender.events))
details := sender.events[0].details.(telemetryspec.BlockStatsEventDetails)
details := sender.events[0].details

// Test to see that the wait duration is at least the amount of time we slept
require.True(t, int(details.AgreementDurationMs) >= int(sleepTime)/int(time.Millisecond))
Expand Down

0 comments on commit 37a0812

Please sign in to comment.