|
|
@ -3,18 +3,16 @@ package evidence_test |
|
|
|
import ( |
|
|
|
"encoding/hex" |
|
|
|
"fmt" |
|
|
|
"math/rand" |
|
|
|
"sync" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/go-kit/kit/log/term" |
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
"github.com/stretchr/testify/mock" |
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
|
|
|
dbm "github.com/tendermint/tm-db" |
|
|
|
|
|
|
|
cfg "github.com/tendermint/tendermint/config" |
|
|
|
"github.com/tendermint/tendermint/crypto" |
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash" |
|
|
|
"github.com/tendermint/tendermint/evidence" |
|
|
@ -28,310 +26,565 @@ import ( |
|
|
|
|
|
|
|
var ( |
|
|
|
numEvidence = 10 |
|
|
|
timeout = 120 * time.Second // ridiculously high because CircleCI is slow
|
|
|
|
|
|
|
|
rng = rand.New(rand.NewSource(time.Now().UnixNano())) |
|
|
|
) |
|
|
|
|
|
|
|
// We have N evidence reactors connected to one another. The first reactor
|
|
|
|
// receives a number of evidence at varying heights. We test that all
|
|
|
|
// other reactors receive the evidence and add it to their own respective
|
|
|
|
// evidence pools.
|
|
|
|
type reactorTestSuite struct { |
|
|
|
reactor *evidence.Reactor |
|
|
|
pool *evidence.Pool |
|
|
|
|
|
|
|
peerID p2p.NodeID |
|
|
|
|
|
|
|
evidenceChannel *p2p.Channel |
|
|
|
evidenceInCh chan p2p.Envelope |
|
|
|
evidenceOutCh chan p2p.Envelope |
|
|
|
evidencePeerErrCh chan p2p.PeerError |
|
|
|
|
|
|
|
peerUpdatesCh chan p2p.PeerUpdate |
|
|
|
peerUpdates *p2p.PeerUpdatesCh |
|
|
|
} |
|
|
|
|
|
|
|
func setup(t *testing.T, logger log.Logger, pool *evidence.Pool, chBuf uint) *reactorTestSuite { |
|
|
|
t.Helper() |
|
|
|
|
|
|
|
pID := make([]byte, 16) |
|
|
|
_, err := rng.Read(pID) |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
peerUpdatesCh := make(chan p2p.PeerUpdate) |
|
|
|
|
|
|
|
rts := &reactorTestSuite{ |
|
|
|
pool: pool, |
|
|
|
evidenceInCh: make(chan p2p.Envelope, chBuf), |
|
|
|
evidenceOutCh: make(chan p2p.Envelope, chBuf), |
|
|
|
evidencePeerErrCh: make(chan p2p.PeerError, chBuf), |
|
|
|
peerUpdatesCh: peerUpdatesCh, |
|
|
|
peerUpdates: p2p.NewPeerUpdates(peerUpdatesCh), |
|
|
|
peerID: p2p.NodeID(fmt.Sprintf("%x", pID)), |
|
|
|
} |
|
|
|
|
|
|
|
rts.evidenceChannel = p2p.NewChannel( |
|
|
|
evidence.EvidenceChannel, |
|
|
|
new(tmproto.EvidenceList), |
|
|
|
rts.evidenceInCh, |
|
|
|
rts.evidenceOutCh, |
|
|
|
rts.evidencePeerErrCh, |
|
|
|
) |
|
|
|
|
|
|
|
rts.reactor = evidence.NewReactor( |
|
|
|
logger, |
|
|
|
rts.evidenceChannel, |
|
|
|
rts.peerUpdates, |
|
|
|
pool, |
|
|
|
) |
|
|
|
|
|
|
|
require.NoError(t, rts.reactor.Start()) |
|
|
|
require.True(t, rts.reactor.IsRunning()) |
|
|
|
|
|
|
|
t.Cleanup(func() { |
|
|
|
require.NoError(t, rts.reactor.Stop()) |
|
|
|
require.False(t, rts.reactor.IsRunning()) |
|
|
|
}) |
|
|
|
|
|
|
|
return rts |
|
|
|
} |
|
|
|
|
|
|
|
func createTestSuites(t *testing.T, stateStores []sm.Store, chBuf uint) []*reactorTestSuite { |
|
|
|
t.Helper() |
|
|
|
|
|
|
|
numSStores := len(stateStores) |
|
|
|
testSuites := make([]*reactorTestSuite, numSStores) |
|
|
|
evidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC) |
|
|
|
|
|
|
|
for i := 0; i < numSStores; i++ { |
|
|
|
logger := log.TestingLogger().With("validator", i) |
|
|
|
evidenceDB := dbm.NewMemDB() |
|
|
|
blockStore := &mocks.BlockStore{} |
|
|
|
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return( |
|
|
|
&types.BlockMeta{Header: types.Header{Time: evidenceTime}}, |
|
|
|
) |
|
|
|
|
|
|
|
pool, err := evidence.NewPool(logger, evidenceDB, stateStores[i], blockStore) |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
testSuites[i] = setup(t, logger, pool, chBuf) |
|
|
|
} |
|
|
|
|
|
|
|
return testSuites |
|
|
|
} |
|
|
|
|
|
|
|
func waitForEvidence(t *testing.T, evList types.EvidenceList, suites ...*reactorTestSuite) { |
|
|
|
t.Helper() |
|
|
|
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
|
|
|
|
for _, suite := range suites { |
|
|
|
wg.Add(1) |
|
|
|
|
|
|
|
go func(s *reactorTestSuite) { |
|
|
|
var localEvList []types.Evidence |
|
|
|
|
|
|
|
currentPoolSize := 0 |
|
|
|
for currentPoolSize != len(evList) { |
|
|
|
// each evidence should not be more than 500 bytes
|
|
|
|
localEvList, _ = s.pool.PendingEvidence(int64(len(evList) * 500)) |
|
|
|
currentPoolSize = len(localEvList) |
|
|
|
} |
|
|
|
|
|
|
|
// put the reaped evidence in a map so we can quickly check we got everything
|
|
|
|
evMap := make(map[string]types.Evidence) |
|
|
|
for _, e := range localEvList { |
|
|
|
evMap[string(e.Hash())] = e |
|
|
|
} |
|
|
|
|
|
|
|
for i, expectedEv := range evList { |
|
|
|
gotEv := evMap[string(expectedEv.Hash())] |
|
|
|
require.Equalf( |
|
|
|
t, |
|
|
|
expectedEv, |
|
|
|
gotEv, |
|
|
|
"evidence at index %d in pool does not match; got: %v, expected: %v", i, gotEv, expectedEv, |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
wg.Done() |
|
|
|
}(suite) |
|
|
|
} |
|
|
|
|
|
|
|
// wait for the evidence in all evidence pools
|
|
|
|
wg.Wait() |
|
|
|
} |
|
|
|
|
|
|
|
func createEvidenceList( |
|
|
|
t *testing.T, |
|
|
|
pool *evidence.Pool, |
|
|
|
val types.PrivValidator, |
|
|
|
numEvidence int, |
|
|
|
) types.EvidenceList { |
|
|
|
evList := make([]types.Evidence, numEvidence) |
|
|
|
for i := 0; i < numEvidence; i++ { |
|
|
|
ev := types.NewMockDuplicateVoteEvidenceWithValidator( |
|
|
|
int64(i+1), |
|
|
|
time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), |
|
|
|
val, |
|
|
|
evidenceChainID, |
|
|
|
) |
|
|
|
|
|
|
|
require.NoError(t, pool.AddEvidence(ev)) |
|
|
|
evList[i] = ev |
|
|
|
} |
|
|
|
|
|
|
|
return evList |
|
|
|
} |
|
|
|
|
|
|
|
// simulateRouter will increment the provided WaitGroup and execute a simulated
|
|
|
|
// router where, for each outbound p2p Envelope from the primary reactor, we
|
|
|
|
// proxy (send) the Envelope the relevant peer reactor. Done is invoked on the
|
|
|
|
// WaitGroup when numOut Envelopes are sent (i.e. read from the outbound channel).
|
|
|
|
func simulateRouter(wg *sync.WaitGroup, primary *reactorTestSuite, suites []*reactorTestSuite, numOut int) { |
|
|
|
wg.Add(1) |
|
|
|
|
|
|
|
// create a mapping for efficient suite lookup by peer ID
|
|
|
|
suitesByPeerID := make(map[p2p.NodeID]*reactorTestSuite) |
|
|
|
for _, suite := range suites { |
|
|
|
suitesByPeerID[suite.peerID] = suite |
|
|
|
} |
|
|
|
|
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelope to the respective peer (suite).
|
|
|
|
go func() { |
|
|
|
for i := 0; i < numOut; i++ { |
|
|
|
envelope := <-primary.evidenceOutCh |
|
|
|
other := suitesByPeerID[envelope.To] |
|
|
|
|
|
|
|
other.evidenceInCh <- p2p.Envelope{ |
|
|
|
From: primary.peerID, |
|
|
|
To: envelope.To, |
|
|
|
Message: envelope.Message, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
wg.Done() |
|
|
|
}() |
|
|
|
} |
|
|
|
|
|
|
|
func TestReactorMultiDisconnect(t *testing.T) { |
|
|
|
val := types.NewMockPV() |
|
|
|
height := int64(numEvidence) + 10 |
|
|
|
|
|
|
|
stateDB1 := initializeValidatorState(t, val, height) |
|
|
|
stateDB2 := initializeValidatorState(t, val, height) |
|
|
|
|
|
|
|
testSuites := createTestSuites(t, []sm.Store{stateDB1, stateDB2}, 20) |
|
|
|
primary := testSuites[0] |
|
|
|
secondary := testSuites[1] |
|
|
|
|
|
|
|
_ = createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure "disconnecting" the secondary peer from the primary more than once
|
|
|
|
// is handled gracefully.
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusDown, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusDown, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// TestReactorBroadcastEvidence creates an environment of multiple peers that
|
|
|
|
// are all at the same height. One peer, designated as a primary, gossips all
|
|
|
|
// evidence to the remaining peers.
|
|
|
|
func TestReactorBroadcastEvidence(t *testing.T) { |
|
|
|
config := cfg.TestConfig() |
|
|
|
N := 7 |
|
|
|
numPeers := 7 |
|
|
|
|
|
|
|
// create statedb for everyone
|
|
|
|
stateDBs := make([]sm.Store, N) |
|
|
|
// create a stateDB for all test suites (nodes)
|
|
|
|
stateDBs := make([]sm.Store, numPeers) |
|
|
|
val := types.NewMockPV() |
|
|
|
// we need validators saved for heights at least as high as we have evidence for
|
|
|
|
|
|
|
|
// We need all validators saved for heights at least as high as we have
|
|
|
|
// evidence for.
|
|
|
|
height := int64(numEvidence) + 10 |
|
|
|
for i := 0; i < N; i++ { |
|
|
|
stateDBs[i] = initializeValidatorState(val, height) |
|
|
|
for i := 0; i < numPeers; i++ { |
|
|
|
stateDBs[i] = initializeValidatorState(t, val, height) |
|
|
|
} |
|
|
|
|
|
|
|
// make reactors from statedb
|
|
|
|
reactors, pools := makeAndConnectReactorsAndPools(config, stateDBs) |
|
|
|
// Create a series of test suites where each suite contains a reactor and
|
|
|
|
// evidence pool. In addition, we mark a primary suite and the rest are
|
|
|
|
// secondaries where each secondary is added as a peer via a PeerUpdate to the
|
|
|
|
// primary. As a result, the primary will gossip all evidence to each secondary.
|
|
|
|
testSuites := createTestSuites(t, stateDBs, 0) |
|
|
|
primary := testSuites[0] |
|
|
|
secondaries := testSuites[1:] |
|
|
|
|
|
|
|
// set the peer height on each reactor
|
|
|
|
for _, r := range reactors { |
|
|
|
for _, peer := range r.Switch.Peers().List() { |
|
|
|
ps := peerState{height} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelopes to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
simulateRouter(wg, primary, testSuites, numEvidence*len(secondaries)) |
|
|
|
|
|
|
|
evList := createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
// Add each secondary suite (node) as a peer to the primary suite (node). This
|
|
|
|
// will cause the primary to gossip all evidence to the secondaries.
|
|
|
|
for _, suite := range secondaries { |
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: suite.peerID, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// send a bunch of valid evidence to the first reactor's evpool
|
|
|
|
// and wait for them all to be received in the others
|
|
|
|
evList := sendEvidence(t, pools[0], val, numEvidence) |
|
|
|
waitForEvidence(t, evList, pools) |
|
|
|
} |
|
|
|
// Wait till all secondary suites (reactor) received all evidence from the
|
|
|
|
// primary suite (node).
|
|
|
|
waitForEvidence(t, evList, secondaries...) |
|
|
|
|
|
|
|
// We have two evidence reactors connected to one another but are at different heights.
|
|
|
|
// Reactor 1 which is ahead receives a number of evidence. It should only send the evidence
|
|
|
|
// that is below the height of the peer to that peer.
|
|
|
|
func TestReactorSelectiveBroadcast(t *testing.T) { |
|
|
|
config := cfg.TestConfig() |
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Equal(t, numEvidence, int(suite.pool.Size())) |
|
|
|
} |
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
// ensure all channels are drained
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Empty(t, suite.evidenceOutCh) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// TestReactorSelectiveBroadcast tests a context where we have two reactors
|
|
|
|
// connected to one another but are at different heights. Reactor 1 which is
|
|
|
|
// ahead receives a list of evidence.
|
|
|
|
func TestReactorBroadcastEvidence_Lagging(t *testing.T) { |
|
|
|
val := types.NewMockPV() |
|
|
|
height1 := int64(numEvidence) + 10 |
|
|
|
height2 := int64(numEvidence) / 2 |
|
|
|
|
|
|
|
// DB1 is ahead of DB2
|
|
|
|
stateDB1 := initializeValidatorState(val, height1) |
|
|
|
stateDB2 := initializeValidatorState(val, height2) |
|
|
|
// stateDB1 is ahead of stateDB2, where stateDB1 has all heights (1-10) and
|
|
|
|
// stateDB2 only has heights 1-7.
|
|
|
|
stateDB1 := initializeValidatorState(t, val, height1) |
|
|
|
stateDB2 := initializeValidatorState(t, val, height2) |
|
|
|
|
|
|
|
// make reactors from statedb
|
|
|
|
reactors, pools := makeAndConnectReactorsAndPools(config, []sm.Store{stateDB1, stateDB2}) |
|
|
|
testSuites := createTestSuites(t, []sm.Store{stateDB1, stateDB2}, 0) |
|
|
|
primary := testSuites[0] |
|
|
|
secondaries := testSuites[1:] |
|
|
|
|
|
|
|
// set the peer height on each reactor
|
|
|
|
for _, r := range reactors { |
|
|
|
for _, peer := range r.Switch.Peers().List() { |
|
|
|
ps := peerState{height1} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelope to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
simulateRouter(wg, primary, testSuites, numEvidence*len(secondaries)) |
|
|
|
|
|
|
|
// Send a list of valid evidence to the first reactor's, the one that is ahead,
|
|
|
|
// evidence pool.
|
|
|
|
evList := createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
// Add each secondary suite (node) as a peer to the primary suite (node). This
|
|
|
|
// will cause the primary to gossip all evidence to the secondaries.
|
|
|
|
for _, suite := range secondaries { |
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: suite.peerID, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// update the first reactor peer's height to be very small
|
|
|
|
peer := reactors[0].Switch.Peers().List()[0] |
|
|
|
ps := peerState{height2} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// only ones less than the peers height should make it through
|
|
|
|
waitForEvidence(t, evList[:height2+2], secondaries...) |
|
|
|
|
|
|
|
// send a bunch of valid evidence to the first reactor's evpool
|
|
|
|
evList := sendEvidence(t, pools[0], val, numEvidence) |
|
|
|
require.Equal(t, numEvidence, int(primary.pool.Size())) |
|
|
|
require.Equal(t, int(height2+2), int(secondaries[0].pool.Size())) |
|
|
|
|
|
|
|
// only ones less than the peers height should make it through
|
|
|
|
waitForEvidence(t, evList[:numEvidence/2-1], []*evidence.Pool{pools[1]}) |
|
|
|
// The primary will continue to send the remaining evidence to the secondaries
|
|
|
|
// so we wait until it has sent all the envelopes.
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
// peers should still be connected
|
|
|
|
peers := reactors[1].Switch.Peers().List() |
|
|
|
assert.Equal(t, 1, len(peers)) |
|
|
|
// ensure all channels are drained
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Empty(t, suite.evidenceOutCh) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// This tests aims to ensure that reactors don't send evidence that they have committed or that ar
|
|
|
|
// not ready for the peer through three scenarios.
|
|
|
|
// First, committed evidence to a newly connected peer
|
|
|
|
// Second, evidence to a peer that is behind
|
|
|
|
// Third, evidence that was pending and became committed just before the peer caught up
|
|
|
|
func TestReactorsGossipNoCommittedEvidence(t *testing.T) { |
|
|
|
config := cfg.TestConfig() |
|
|
|
|
|
|
|
func TestReactorBroadcastEvidence_Pending(t *testing.T) { |
|
|
|
val := types.NewMockPV() |
|
|
|
var height int64 = 10 |
|
|
|
height := int64(10) |
|
|
|
|
|
|
|
// DB1 is ahead of DB2
|
|
|
|
stateDB1 := initializeValidatorState(val, height-1) |
|
|
|
stateDB2 := initializeValidatorState(val, height-2) |
|
|
|
state, err := stateDB1.Load() |
|
|
|
require.NoError(t, err) |
|
|
|
state.LastBlockHeight++ |
|
|
|
stateDB1 := initializeValidatorState(t, val, height) |
|
|
|
stateDB2 := initializeValidatorState(t, val, height) |
|
|
|
|
|
|
|
// make reactors from statedb
|
|
|
|
reactors, pools := makeAndConnectReactorsAndPools(config, []sm.Store{stateDB1, stateDB2}) |
|
|
|
testSuites := createTestSuites(t, []sm.Store{stateDB1, stateDB2}, 0) |
|
|
|
primary := testSuites[0] |
|
|
|
secondary := testSuites[1] |
|
|
|
|
|
|
|
evList := sendEvidence(t, pools[0], val, 2) |
|
|
|
pools[0].Update(state, evList) |
|
|
|
require.EqualValues(t, uint32(0), pools[0].Size()) |
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelopes to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
simulateRouter(wg, primary, testSuites, numEvidence) |
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond) |
|
|
|
// add all evidence to the primary reactor
|
|
|
|
evList := createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
peer := reactors[0].Switch.Peers().List()[0] |
|
|
|
ps := peerState{height - 2} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// Manually add half the evidence to the secondary which will mark them as
|
|
|
|
// pending.
|
|
|
|
for i := 0; i < numEvidence/2; i++ { |
|
|
|
require.NoError(t, secondary.pool.AddEvidence(evList[i])) |
|
|
|
} |
|
|
|
|
|
|
|
peer = reactors[1].Switch.Peers().List()[0] |
|
|
|
ps = peerState{height} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// the secondary should have half the evidence as pending
|
|
|
|
require.Equal(t, uint32(numEvidence/2), secondary.pool.Size()) |
|
|
|
|
|
|
|
// wait to see that no evidence comes through
|
|
|
|
time.Sleep(300 * time.Millisecond) |
|
|
|
// add the secondary reactor as a peer to the primary reactor
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
|
|
|
|
// the second pool should not have received any evidence because it has already been committed
|
|
|
|
assert.Equal(t, uint32(0), pools[1].Size(), "second reactor should not have received evidence") |
|
|
|
// The secondary reactor should have received all the evidence ignoring the
|
|
|
|
// already pending evidence.
|
|
|
|
waitForEvidence(t, evList, secondary) |
|
|
|
|
|
|
|
// the first reactor receives three more evidence
|
|
|
|
evList = make([]types.Evidence, 3) |
|
|
|
for i := 0; i < 3; i++ { |
|
|
|
ev := types.NewMockDuplicateVoteEvidenceWithValidator(height-3+int64(i), |
|
|
|
time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), val, state.ChainID) |
|
|
|
err := pools[0].AddEvidence(ev) |
|
|
|
require.NoError(t, err) |
|
|
|
evList[i] = ev |
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Equal(t, numEvidence, int(suite.pool.Size())) |
|
|
|
} |
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
// ensure all channels are drained
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Empty(t, suite.evidenceOutCh) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestReactorBroadcastEvidence_Committed(t *testing.T) { |
|
|
|
val := types.NewMockPV() |
|
|
|
height := int64(10) |
|
|
|
|
|
|
|
stateDB1 := initializeValidatorState(t, val, height) |
|
|
|
stateDB2 := initializeValidatorState(t, val, height) |
|
|
|
|
|
|
|
testSuites := createTestSuites(t, []sm.Store{stateDB1, stateDB2}, 0) |
|
|
|
primary := testSuites[0] |
|
|
|
secondary := testSuites[1] |
|
|
|
|
|
|
|
// add all evidence to the primary reactor
|
|
|
|
evList := createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
// Manually add half the evidence to the secondary which will mark them as
|
|
|
|
// pending.
|
|
|
|
for i := 0; i < numEvidence/2; i++ { |
|
|
|
require.NoError(t, secondary.pool.AddEvidence(evList[i])) |
|
|
|
} |
|
|
|
|
|
|
|
// wait to see that only one evidence is sent
|
|
|
|
time.Sleep(300 * time.Millisecond) |
|
|
|
// the secondary should have half the evidence as pending
|
|
|
|
require.Equal(t, uint32(numEvidence/2), secondary.pool.Size()) |
|
|
|
|
|
|
|
// the second pool should only have received the first evidence because it is behind
|
|
|
|
peerEv, _ := pools[1].PendingEvidence(10000) |
|
|
|
assert.EqualValues(t, []types.Evidence{evList[0]}, peerEv) |
|
|
|
state, err := stateDB2.Load() |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
// the last evidence is committed and the second reactor catches up in state to the first
|
|
|
|
// reactor. We therefore expect that the second reactor only receives one more evidence, the
|
|
|
|
// one that is still pending and not the evidence that has already been committed.
|
|
|
|
// update the secondary's pool such that all pending evidence is committed
|
|
|
|
state.LastBlockHeight++ |
|
|
|
pools[0].Update(state, []types.Evidence{evList[2]}) |
|
|
|
// the first reactor should have the two remaining pending evidence
|
|
|
|
require.EqualValues(t, uint32(2), pools[0].Size()) |
|
|
|
secondary.pool.Update(state, evList[:numEvidence/2]) |
|
|
|
|
|
|
|
// now update the state of the second reactor
|
|
|
|
pools[1].Update(state, types.EvidenceList{}) |
|
|
|
peer = reactors[0].Switch.Peers().List()[0] |
|
|
|
ps = peerState{height} |
|
|
|
peer.Set(types.PeerStateKey, ps) |
|
|
|
// the secondary should have half the evidence as committed
|
|
|
|
require.Equal(t, uint32(0), secondary.pool.Size()) |
|
|
|
|
|
|
|
// wait to see that only two evidence is sent
|
|
|
|
time.Sleep(300 * time.Millisecond) |
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelopes to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
simulateRouter(wg, primary, testSuites, numEvidence) |
|
|
|
|
|
|
|
peerEv, _ = pools[1].PendingEvidence(1000) |
|
|
|
assert.EqualValues(t, []types.Evidence{evList[0], evList[1]}, peerEv) |
|
|
|
} |
|
|
|
// add the secondary reactor as a peer to the primary reactor
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
|
|
|
|
// evidenceLogger is a TestingLogger which uses a different
|
|
|
|
// color for each validator ("validator" key must exist).
|
|
|
|
func evidenceLogger() log.Logger { |
|
|
|
return log.TestingLoggerWithColorFn(func(keyvals ...interface{}) term.FgBgColor { |
|
|
|
for i := 0; i < len(keyvals)-1; i += 2 { |
|
|
|
if keyvals[i] == "validator" { |
|
|
|
return term.FgBgColor{Fg: term.Color(uint8(keyvals[i+1].(int) + 1))} |
|
|
|
} |
|
|
|
} |
|
|
|
return term.FgBgColor{} |
|
|
|
}) |
|
|
|
// The secondary reactor should have received all the evidence ignoring the
|
|
|
|
// already committed evidence.
|
|
|
|
waitForEvidence(t, evList[numEvidence/2:], secondary) |
|
|
|
|
|
|
|
require.Equal(t, numEvidence, int(primary.pool.Size())) |
|
|
|
require.Equal(t, numEvidence/2, int(secondary.pool.Size())) |
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
// ensure all channels are drained
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Empty(t, suite.evidenceOutCh) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// connect N evidence reactors through N switches
|
|
|
|
func makeAndConnectReactorsAndPools(config *cfg.Config, stateStores []sm.Store) ([]*evidence.Reactor, |
|
|
|
[]*evidence.Pool) { |
|
|
|
N := len(stateStores) |
|
|
|
func TestReactorBroadcastEvidence_FullyConnected(t *testing.T) { |
|
|
|
numPeers := 7 |
|
|
|
|
|
|
|
reactors := make([]*evidence.Reactor, N) |
|
|
|
pools := make([]*evidence.Pool, N) |
|
|
|
logger := evidenceLogger() |
|
|
|
evidenceTime := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC) |
|
|
|
// create a stateDB for all test suites (nodes)
|
|
|
|
stateDBs := make([]sm.Store, numPeers) |
|
|
|
val := types.NewMockPV() |
|
|
|
|
|
|
|
for i := 0; i < N; i++ { |
|
|
|
evidenceDB := dbm.NewMemDB() |
|
|
|
blockStore := &mocks.BlockStore{} |
|
|
|
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return( |
|
|
|
&types.BlockMeta{Header: types.Header{Time: evidenceTime}}, |
|
|
|
) |
|
|
|
pool, err := evidence.NewPool(evidenceDB, stateStores[i], blockStore) |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
// We need all validators saved for heights at least as high as we have
|
|
|
|
// evidence for.
|
|
|
|
height := int64(numEvidence) + 10 |
|
|
|
for i := 0; i < numPeers; i++ { |
|
|
|
stateDBs[i] = initializeValidatorState(t, val, height) |
|
|
|
} |
|
|
|
|
|
|
|
testSuites := createTestSuites(t, stateDBs, 0) |
|
|
|
|
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelopes to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
for _, suite := range testSuites { |
|
|
|
simulateRouter(wg, suite, testSuites, numEvidence*(len(testSuites)-1)) |
|
|
|
} |
|
|
|
|
|
|
|
evList := createEvidenceList(t, testSuites[0].pool, val, numEvidence) |
|
|
|
|
|
|
|
// every suite (reactor) connects to every other suite (reactor)
|
|
|
|
for _, suiteI := range testSuites { |
|
|
|
for _, suiteJ := range testSuites { |
|
|
|
if suiteI.peerID != suiteJ.peerID { |
|
|
|
suiteI.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: suiteJ.peerID, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
pools[i] = pool |
|
|
|
reactors[i] = evidence.NewReactor(pool) |
|
|
|
reactors[i].SetLogger(logger.With("validator", i)) |
|
|
|
} |
|
|
|
|
|
|
|
p2p.MakeConnectedSwitches(config.P2P, N, func(i int, s *p2p.Switch) *p2p.Switch { |
|
|
|
s.AddReactor("EVIDENCE", reactors[i]) |
|
|
|
return s |
|
|
|
// wait till all suites (reactors) received all evidence from other suites (reactors)
|
|
|
|
waitForEvidence(t, evList, testSuites...) |
|
|
|
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Equal(t, numEvidence, int(suite.pool.Size())) |
|
|
|
|
|
|
|
}, p2p.Connect2Switches) |
|
|
|
// commit state so we do not continue to repeat gossiping the same evidence
|
|
|
|
state := suite.pool.State() |
|
|
|
state.LastBlockHeight++ |
|
|
|
suite.pool.Update(state, evList) |
|
|
|
} |
|
|
|
|
|
|
|
return reactors, pools |
|
|
|
wg.Wait() |
|
|
|
} |
|
|
|
|
|
|
|
// wait for all evidence on all reactors
|
|
|
|
func waitForEvidence(t *testing.T, evs types.EvidenceList, pools []*evidence.Pool) { |
|
|
|
// wait for the evidence in all evpools
|
|
|
|
func TestReactorBroadcastEvidence_RemovePeer(t *testing.T) { |
|
|
|
val := types.NewMockPV() |
|
|
|
height := int64(10) |
|
|
|
|
|
|
|
stateDB1 := initializeValidatorState(t, val, height) |
|
|
|
stateDB2 := initializeValidatorState(t, val, height) |
|
|
|
|
|
|
|
testSuites := createTestSuites(t, []sm.Store{stateDB1, stateDB2}, uint(numEvidence)) |
|
|
|
primary := testSuites[0] |
|
|
|
secondary := testSuites[1] |
|
|
|
|
|
|
|
// Simulate a router by listening for all outbound envelopes and proxying the
|
|
|
|
// envelopes to the respective peer (suite).
|
|
|
|
wg := new(sync.WaitGroup) |
|
|
|
for i := 0; i < len(pools); i++ { |
|
|
|
wg.Add(1) |
|
|
|
go _waitForEvidence(t, wg, evs, i, pools) |
|
|
|
simulateRouter(wg, primary, testSuites, numEvidence/2) |
|
|
|
|
|
|
|
// add all evidence to the primary reactor
|
|
|
|
evList := createEvidenceList(t, primary.pool, val, numEvidence) |
|
|
|
|
|
|
|
// add the secondary reactor as a peer to the primary reactor
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusUp, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
|
|
|
|
done := make(chan struct{}) |
|
|
|
go func() { |
|
|
|
wg.Wait() |
|
|
|
close(done) |
|
|
|
}() |
|
|
|
// have the secondary reactor receive only half the evidence
|
|
|
|
waitForEvidence(t, evList[:numEvidence/2], secondary) |
|
|
|
|
|
|
|
timer := time.After(timeout) |
|
|
|
select { |
|
|
|
case <-timer: |
|
|
|
t.Fatal("Timed out waiting for evidence") |
|
|
|
case <-done: |
|
|
|
// disconnect the peer
|
|
|
|
primary.peerUpdatesCh <- p2p.PeerUpdate{ |
|
|
|
Status: p2p.PeerStatusDown, |
|
|
|
PeerID: secondary.peerID, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// wait for all evidence on a single evpool
|
|
|
|
func _waitForEvidence( |
|
|
|
t *testing.T, |
|
|
|
wg *sync.WaitGroup, |
|
|
|
evs types.EvidenceList, |
|
|
|
poolIdx int, |
|
|
|
pools []*evidence.Pool, |
|
|
|
) { |
|
|
|
evpool := pools[poolIdx] |
|
|
|
var evList []types.Evidence |
|
|
|
currentPoolSize := 0 |
|
|
|
for currentPoolSize != len(evs) { |
|
|
|
evList, _ = evpool.PendingEvidence(int64(len(evs) * 500)) // each evidence should not be more than 500 bytes
|
|
|
|
currentPoolSize = len(evList) |
|
|
|
time.Sleep(time.Millisecond * 100) |
|
|
|
} |
|
|
|
|
|
|
|
// put the reaped evidence in a map so we can quickly check we got everything
|
|
|
|
evMap := make(map[string]types.Evidence) |
|
|
|
for _, e := range evList { |
|
|
|
evMap[string(e.Hash())] = e |
|
|
|
} |
|
|
|
for i, expectedEv := range evs { |
|
|
|
gotEv := evMap[string(expectedEv.Hash())] |
|
|
|
assert.Equal(t, expectedEv, gotEv, |
|
|
|
fmt.Sprintf("evidence at index %d on pool %d don't match: %v vs %v", |
|
|
|
i, poolIdx, expectedEv, gotEv)) |
|
|
|
} |
|
|
|
|
|
|
|
wg.Done() |
|
|
|
} |
|
|
|
// Ensure the secondary only received half of the evidence before being
|
|
|
|
// disconnected.
|
|
|
|
require.Equal(t, numEvidence/2, int(secondary.pool.Size())) |
|
|
|
|
|
|
|
func sendEvidence(t *testing.T, evpool *evidence.Pool, val types.PrivValidator, n int) types.EvidenceList { |
|
|
|
evList := make([]types.Evidence, n) |
|
|
|
for i := 0; i < n; i++ { |
|
|
|
ev := types.NewMockDuplicateVoteEvidenceWithValidator(int64(i+1), |
|
|
|
time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), val, evidenceChainID) |
|
|
|
err := evpool.AddEvidence(ev) |
|
|
|
require.NoError(t, err) |
|
|
|
evList[i] = ev |
|
|
|
wg.Wait() |
|
|
|
|
|
|
|
// The primary reactor should still be attempting to send the remaining half.
|
|
|
|
//
|
|
|
|
// NOTE: The channel is buffered (size numEvidence) as to ensure the primary
|
|
|
|
// reactor will send all envelopes at once before receiving the signal to stop
|
|
|
|
// gossiping.
|
|
|
|
for i := 0; i < numEvidence/2; i++ { |
|
|
|
<-primary.evidenceOutCh |
|
|
|
} |
|
|
|
return evList |
|
|
|
} |
|
|
|
|
|
|
|
type peerState struct { |
|
|
|
height int64 |
|
|
|
// ensure all channels are drained
|
|
|
|
for _, suite := range testSuites { |
|
|
|
require.Empty(t, suite.evidenceOutCh) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (ps peerState) GetHeight() int64 { |
|
|
|
return ps.height |
|
|
|
} |
|
|
|
// nolint:lll
|
|
|
|
func TestEvidenceListSerialization(t *testing.T) { |
|
|
|
exampleVote := func(msgType byte) *types.Vote { |
|
|
|
var stamp, err = time.Parse(types.TimeFormat, "2017-12-25T03:00:01.234Z") |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
func exampleVote(t byte) *types.Vote { |
|
|
|
var stamp, err = time.Parse(types.TimeFormat, "2017-12-25T03:00:01.234Z") |
|
|
|
if err != nil { |
|
|
|
panic(err) |
|
|
|
} |
|
|
|
|
|
|
|
return &types.Vote{ |
|
|
|
Type: tmproto.SignedMsgType(t), |
|
|
|
Height: 3, |
|
|
|
Round: 2, |
|
|
|
Timestamp: stamp, |
|
|
|
BlockID: types.BlockID{ |
|
|
|
Hash: tmhash.Sum([]byte("blockID_hash")), |
|
|
|
PartSetHeader: types.PartSetHeader{ |
|
|
|
Total: 1000000, |
|
|
|
Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")), |
|
|
|
return &types.Vote{ |
|
|
|
Type: tmproto.SignedMsgType(msgType), |
|
|
|
Height: 3, |
|
|
|
Round: 2, |
|
|
|
Timestamp: stamp, |
|
|
|
BlockID: types.BlockID{ |
|
|
|
Hash: tmhash.Sum([]byte("blockID_hash")), |
|
|
|
PartSetHeader: types.PartSetHeader{ |
|
|
|
Total: 1000000, |
|
|
|
Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")), |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
ValidatorAddress: crypto.AddressHash([]byte("validator_address")), |
|
|
|
ValidatorIndex: 56789, |
|
|
|
ValidatorAddress: crypto.AddressHash([]byte("validator_address")), |
|
|
|
ValidatorIndex: 56789, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// nolint:lll //ignore line length for tests
|
|
|
|
func TestEvidenceVectors(t *testing.T) { |
|
|
|
|
|
|
|
val := &types.Validator{ |
|
|
|
Address: crypto.AddressHash([]byte("validator_address")), |
|
|
@ -347,33 +600,35 @@ func TestEvidenceVectors(t *testing.T) { |
|
|
|
valSet, |
|
|
|
) |
|
|
|
|
|
|
|
testCases := []struct { |
|
|
|
testName string |
|
|
|
testCases := map[string]struct { |
|
|
|
evidenceList []types.Evidence |
|
|
|
expBytes string |
|
|
|
}{ |
|
|
|
{"DuplicateVoteEvidence", []types.Evidence{dupl}, "0a85020a82020a79080210031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb031279080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03180a200a2a060880dbaae105"}, |
|
|
|
"DuplicateVoteEvidence": { |
|
|
|
[]types.Evidence{dupl}, |
|
|
|
"0a85020a82020a79080210031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb031279080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0b08b1d381d20510809dca6f32146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03180a200a2a060880dbaae105", |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
for _, tc := range testCases { |
|
|
|
for name, tc := range testCases { |
|
|
|
tc := tc |
|
|
|
|
|
|
|
evi := make([]tmproto.Evidence, len(tc.evidenceList)) |
|
|
|
for i := 0; i < len(tc.evidenceList); i++ { |
|
|
|
ev, err := types.EvidenceToProto(tc.evidenceList[i]) |
|
|
|
require.NoError(t, err, tc.testName) |
|
|
|
evi[i] = *ev |
|
|
|
} |
|
|
|
|
|
|
|
epl := tmproto.EvidenceList{ |
|
|
|
Evidence: evi, |
|
|
|
} |
|
|
|
t.Run(name, func(t *testing.T) { |
|
|
|
protoEv := make([]tmproto.Evidence, len(tc.evidenceList)) |
|
|
|
for i := 0; i < len(tc.evidenceList); i++ { |
|
|
|
ev, err := types.EvidenceToProto(tc.evidenceList[i]) |
|
|
|
require.NoError(t, err) |
|
|
|
protoEv[i] = *ev |
|
|
|
} |
|
|
|
|
|
|
|
bz, err := epl.Marshal() |
|
|
|
require.NoError(t, err, tc.testName) |
|
|
|
epl := tmproto.EvidenceList{ |
|
|
|
Evidence: protoEv, |
|
|
|
} |
|
|
|
|
|
|
|
require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName) |
|
|
|
bz, err := epl.Marshal() |
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
require.Equal(t, tc.expBytes, hex.EncodeToString(bz)) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
} |