Skip to content

Commit

Permalink
Fixup module tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Nov 17, 2024
1 parent a179026 commit d4ff457
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 222 deletions.
111 changes: 84 additions & 27 deletions tm2/pkg/bft/blockchain/reactor_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package blockchain

import (
"context"
"log/slog"
"os"
"sort"
"testing"
"time"

"github.com/stretchr/testify/assert"

abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/appconn"
cfg "github.com/gnolang/gno/tm2/pkg/bft/config"
Expand All @@ -20,9 +19,12 @@ import (
tmtime "github.com/gnolang/gno/tm2/pkg/bft/types/time"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
"github.com/gnolang/gno/tm2/pkg/errors"
p2pTesting "github.com/gnolang/gno/tm2/pkg/internal/p2p"
"github.com/gnolang/gno/tm2/pkg/log"
"github.com/gnolang/gno/tm2/pkg/p2p"
p2pTypes "github.com/gnolang/gno/tm2/pkg/p2p/types"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/assert"
)

var config *cfg.Config
Expand Down Expand Up @@ -125,15 +127,35 @@ func TestNoBlockResponse(t *testing.T) {

maxBlockHeight := int64(65)

reactorPairs := make([]BlockchainReactorPair, 2)
var (
reactorPairs = make([]BlockchainReactorPair, 2)
options = make(map[int][]p2p.SwitchOption)
)

reactorPairs[0] = newBlockchainReactor(log.NewTestingLogger(t), genDoc, privVals, maxBlockHeight)
reactorPairs[1] = newBlockchainReactor(log.NewTestingLogger(t), genDoc, privVals, 0)
for i := range reactorPairs {
height := int64(0)
if i == 0 {
height = maxBlockHeight
}

p2p.MakeConnectedSwitches(config.P2P, 2, func(i int, s *p2p.MultiplexSwitch) *p2p.MultiplexSwitch {
s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
return s
}, p2p.Connect2Switches)
reactorPairs[i] = newBlockchainReactor(log.NewTestingLogger(t), genDoc, privVals, height)

options[i] = []p2p.SwitchOption{
p2p.WithReactor("BLOCKCHAIN", reactorPairs[i].reactor),
}
}

ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

testingCfg := p2pTesting.TestingConfig{
Count: 2,
P2PCfg: config.P2P,
SwitchOptions: options,
Channels: []byte{BlockchainChannel},
}

p2pTesting.MakeConnectedPeers(t, ctx, testingCfg)

defer func() {
for _, r := range reactorPairs {
Expand Down Expand Up @@ -194,17 +216,35 @@ func TestFlappyBadBlockStopsPeer(t *testing.T) {
otherChain.app.Stop()
}()

reactorPairs := make([]BlockchainReactorPair, 4)
var (
reactorPairs = make([]BlockchainReactorPair, 4)
options = make(map[int][]p2p.SwitchOption)
)

for i := range reactorPairs {
height := int64(0)
if i == 0 {
height = maxBlockHeight
}

reactorPairs[i] = newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, height)

reactorPairs[0] = newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, maxBlockHeight)
reactorPairs[1] = newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, 0)
reactorPairs[2] = newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, 0)
reactorPairs[3] = newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, 0)
options[i] = []p2p.SwitchOption{
p2p.WithReactor("BLOCKCHAIN", reactorPairs[i].reactor),
}
}

switches := p2p.MakeConnectedSwitches(config.P2P, 4, func(i int, s *p2p.MultiplexSwitch) *p2p.MultiplexSwitch {
s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
return s
}, p2p.Connect2Switches)
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

testingCfg := p2pTesting.TestingConfig{
Count: 4,
P2PCfg: config.P2P,
SwitchOptions: options,
Channels: []byte{BlockchainChannel},
}

switches, transports := p2pTesting.MakeConnectedPeers(t, ctx, testingCfg)

defer func() {
for _, r := range reactorPairs {
Expand All @@ -222,32 +262,49 @@ func TestFlappyBadBlockStopsPeer(t *testing.T) {
}

// at this time, reactors[0-3] is the newest
assert.Equal(t, 3, reactorPairs[1].reactor.Switch.Peers().Size())
assert.Equal(t, 3, len(reactorPairs[1].reactor.Switch.Peers().List()))

// mark reactorPairs[3] is an invalid peer
reactorPairs[3].reactor.store = otherChain.reactor.store

lastReactorPair := newBlockchainReactor(log.NewNoopLogger(), genDoc, privVals, 0)
reactorPairs = append(reactorPairs, lastReactorPair)

switches = append(switches, p2p.MakeConnectedSwitches(config.P2P, 1, func(i int, s *p2p.MultiplexSwitch) *p2p.MultiplexSwitch {
s.AddReactor("BLOCKCHAIN", reactorPairs[len(reactorPairs)-1].reactor)
return s
}, p2p.Connect2Switches)...)
persistentPeers := make([]*p2pTypes.NetAddress, 0, len(transports))

for i := 0; i < len(reactorPairs)-1; i++ {
p2p.Connect2Switches(switches, i, len(reactorPairs)-1)
for _, tr := range transports {
addr := tr.NetAddress()
persistentPeers = append(persistentPeers, &addr)
}

for i, opt := range options {
opt = append(opt, p2p.WithPersistentPeers(persistentPeers))

options[i] = opt
}

ctx, cancelFn = context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

testingCfg = p2pTesting.TestingConfig{
Count: 1,
P2PCfg: config.P2P,
SwitchOptions: options,
Channels: []byte{BlockchainChannel},
}

sw, _ := p2pTesting.MakeConnectedPeers(t, ctx, testingCfg)
switches = append(switches, sw...)

for {
if lastReactorPair.reactor.pool.IsCaughtUp() || lastReactorPair.reactor.Switch.Peers().Size() == 0 {
if lastReactorPair.reactor.pool.IsCaughtUp() || len(lastReactorPair.reactor.Switch.Peers().List()) == 0 {
break
}

time.Sleep(1 * time.Second)
}

assert.True(t, lastReactorPair.reactor.Switch.Peers().Size() < len(reactorPairs)-1)
assert.True(t, len(lastReactorPair.reactor.Switch.Peers().List()) < len(reactorPairs)-1)
}

func TestBcBlockRequestMessageValidateBasic(t *testing.T) {
Expand Down
70 changes: 48 additions & 22 deletions tm2/pkg/bft/consensus/reactor_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package consensus

import (
"context"
"fmt"
"log/slog"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/bft/abci/example/kvstore"
cfg "github.com/gnolang/gno/tm2/pkg/bft/config"
Expand All @@ -18,27 +17,39 @@ import (
"github.com/gnolang/gno/tm2/pkg/bitarray"
"github.com/gnolang/gno/tm2/pkg/crypto/tmhash"
"github.com/gnolang/gno/tm2/pkg/events"
p2pTesting "github.com/gnolang/gno/tm2/pkg/internal/p2p"
"github.com/gnolang/gno/tm2/pkg/log"
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/gnolang/gno/tm2/pkg/p2p/mock"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/assert"
)

// ----------------------------------------------
// in-process testnets

func startConsensusNet(css []*ConsensusState, n int) ([]*ConsensusReactor, []<-chan events.Event, []events.EventSwitch, []*p2p.MultiplexSwitch) {
func startConsensusNet(
t *testing.T,
css []*ConsensusState,
n int,
) ([]*ConsensusReactor, []<-chan events.Event, []events.EventSwitch, []*p2p.MultiplexSwitch) {
t.Helper()

reactors := make([]*ConsensusReactor, n)
blocksSubs := make([]<-chan events.Event, 0)
eventSwitches := make([]events.EventSwitch, n)
p2pSwitches := ([]*p2p.MultiplexSwitch)(nil)
options := make(map[int][]p2p.SwitchOption)
for i := 0; i < n; i++ {
/*logger, err := tmflags.ParseLogLevel("consensus:info,*:error", logger, "info")
if err != nil { t.Fatal(err)}*/
reactors[i] = NewConsensusReactor(css[i], true) // so we dont start the consensus states
reactors[i].SetLogger(css[i].Logger)

options[i] = []p2p.SwitchOption{
p2p.WithReactor("CONSENSUS", reactors[i]),
}

// evsw is already started with the cs
eventSwitches[i] = css[i].evsw
reactors[i].SetEventSwitch(eventSwitches[i])
Expand All @@ -51,11 +62,22 @@ func startConsensusNet(css []*ConsensusState, n int) ([]*ConsensusReactor, []<-c
}
}
// make connected switches and start all reactors
p2pSwitches = p2p.MakeConnectedSwitches(config.P2P, n, func(i int, s *p2p.MultiplexSwitch) *p2p.MultiplexSwitch {
s.AddReactor("CONSENSUS", reactors[i])
s.SetLogger(reactors[i].conS.Logger.With("module", "p2p"))
return s
}, p2p.Connect2Switches)
ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second)
defer cancelFn()

testingCfg := p2pTesting.TestingConfig{
P2PCfg: config.P2P,
Count: n,
SwitchOptions: options,
Channels: []byte{
StateChannel,
DataChannel,
VoteChannel,
VoteSetBitsChannel,
},
}

p2pSwitches, _ = p2pTesting.MakeConnectedPeers(t, ctx, testingCfg)

// now that everyone is connected, start the state machines
// If we started the state machines before everyone was connected,
Expand All @@ -68,11 +90,15 @@ func startConsensusNet(css []*ConsensusState, n int) ([]*ConsensusReactor, []<-c
return reactors, blocksSubs, eventSwitches, p2pSwitches
}

func stopConsensusNet(logger *slog.Logger, reactors []*ConsensusReactor, eventSwitches []events.EventSwitch, p2pSwitches []*p2p.MultiplexSwitch) {
func stopConsensusNet(
logger *slog.Logger,
reactors []*ConsensusReactor,
eventSwitches []events.EventSwitch,
p2pSwitches []*p2p.MultiplexSwitch,
) {
logger.Info("stopConsensusNet", "n", len(reactors))
for i, r := range reactors {
for i, _ := range reactors {
logger.Info("stopConsensusNet: Stopping ConsensusReactor", "i", i)
r.Switch.Stop()
}
for i, b := range eventSwitches {
logger.Info("stopConsensusNet: Stopping evsw", "i", i)
Expand All @@ -92,7 +118,7 @@ func TestReactorBasic(t *testing.T) {
N := 4
css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
defer cleanup()
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, N)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, N)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)
// wait till everyone makes the first new block
timeoutWaitGroup(t, N, func(j int) {
Expand All @@ -112,7 +138,7 @@ func TestReactorCreatesBlockWhenEmptyBlocksFalse(t *testing.T) {
c.Consensus.CreateEmptyBlocks = false
})
defer cleanup()
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, N)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, N)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)

// send a tx
Expand All @@ -132,12 +158,12 @@ func TestReactorReceiveDoesNotPanicIfAddPeerHasntBeenCalledYet(t *testing.T) {
N := 1
css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
defer cleanup()
reactors, _, eventSwitches, p2pSwitches := startConsensusNet(css, N)
reactors, _, eventSwitches, p2pSwitches := startConsensusNet(t, css, N)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)

var (
reactor = reactors[0]
peer = mock.NewPeer(nil)
peer = p2pTesting.NewPeer(t)
msg = amino.MustMarshalAny(&HasVoteMessage{Height: 1, Round: 1, Index: 1, Type: types.PrevoteType})
)

Expand All @@ -156,12 +182,12 @@ func TestReactorReceivePanicsIfInitPeerHasntBeenCalledYet(t *testing.T) {
N := 1
css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
defer cleanup()
reactors, _, eventSwitches, p2pSwitches := startConsensusNet(css, N)
reactors, _, eventSwitches, p2pSwitches := startConsensusNet(t, css, N)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)

var (
reactor = reactors[0]
peer = mock.NewPeer(nil)
peer = p2pTesting.NewPeer(t)
msg = amino.MustMarshalAny(&HasVoteMessage{Height: 1, Round: 1, Index: 1, Type: types.PrevoteType})
)

Expand All @@ -182,7 +208,7 @@ func TestFlappyReactorRecordsVotesAndBlockParts(t *testing.T) {
N := 4
css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter)
defer cleanup()
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, N)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, N)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)

// wait till everyone makes the first new block
Expand Down Expand Up @@ -210,7 +236,7 @@ func TestReactorVotingPowerChange(t *testing.T) {
css, cleanup := randConsensusNet(nVals, "consensus_voting_power_changes_test", newMockTickerFunc(true), newPersistentKVStore)
defer cleanup()

reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, nVals)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, nVals)
defer stopConsensusNet(logger, reactors, eventSwitches, p2pSwitches)

// map of active validators
Expand Down Expand Up @@ -276,7 +302,7 @@ func TestReactorValidatorSetChanges(t *testing.T) {

logger := log.NewTestingLogger(t)

reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, nPeers)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, nPeers)
defer stopConsensusNet(logger, reactors, eventSwitches, p2pSwitches)

// map of active validators
Expand Down Expand Up @@ -375,7 +401,7 @@ func TestReactorWithTimeoutCommit(t *testing.T) {
css[i].config.SkipTimeoutCommit = false
}

reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(css, N-1)
reactors, blocksSubs, eventSwitches, p2pSwitches := startConsensusNet(t, css, N-1)
defer stopConsensusNet(log.NewTestingLogger(t), reactors, eventSwitches, p2pSwitches)

// wait till everyone makes the first new block
Expand Down
4 changes: 2 additions & 2 deletions tm2/pkg/bft/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ func TestStateOutputsBlockPartsStats(t *testing.T) {

// create dummy peer
cs, _ := randConsensusState(1)
peer := p2pmock.NewPeer(nil)
peer := p2pmock.Peer{}

// 1) new block part
parts := types.NewPartSetFromData(random.RandBytes(100), 10)
Expand Down Expand Up @@ -1777,7 +1777,7 @@ func TestStateOutputVoteStats(t *testing.T) {

cs, vss := randConsensusState(2)
// create dummy peer
peer := p2pmock.NewPeer(nil)
peer := &p2pmock.Peer{}

vote := signVote(vss[1], types.PrecommitType, []byte("test"), types.PartSetHeader{})

Expand Down
Loading

0 comments on commit d4ff457

Please sign in to comment.