2019-07-14 21:45:16 +03:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-15 22:17:25 +03:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2019-07-14 21:45:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-15 22:17:25 +03:00
|
|
|
initHeight int64 = 5
|
|
|
|
peerID p2p.ID = "1"
|
|
|
|
peerIDTwo p2p.ID = "2"
|
|
|
|
peerHeight int64 = 20
|
|
|
|
blockSize int64 = 10
|
2019-07-14 21:45:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestScheduleInit(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, sc.getStateAtHeight(initHeight), blockStateNew)
|
|
|
|
assert.Equal(t, sc.getStateAtHeight(initHeight+1), blockStateProcessed)
|
|
|
|
assert.Equal(t, sc.getStateAtHeight(initHeight-1), blockStateUnknown)
|
2019-07-14 21:45:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddPeer(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.addPeer(peerID))
|
|
|
|
assert.Nil(t, sc.addPeer(peerIDTwo))
|
2019-07-14 21:45:16 +03:00
|
|
|
assert.Equal(t, sc.addPeer(peerID), errDuplicatePeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTouchPeer(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
2019-07-14 21:45:16 +03:00
|
|
|
now := time.Now()
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, errPeerNotFound, sc.touchPeer(peerID, now),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Touching an unknown peer should return errPeerNotFound")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.addPeer(peerID),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Adding a peer should return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.touchPeer(peerID, now),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Touching a peer should return no error")
|
|
|
|
|
|
|
|
threshold := 10 * time.Second
|
|
|
|
assert.Equal(t, peerID, len(sc.peersSince(threshold, now.Add(9*time.Second))), 0,
|
|
|
|
"Expected no peers to have been touched over 9 seconds")
|
|
|
|
assert.Equal(t, peerID, sc.peersSince(threshold, now.Add(11*time.Second))[0].peerID, peerID,
|
|
|
|
"Expected one peer to have been touched over 10 seconds ago")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerHeight(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight))
|
|
|
|
for i := initHeight; i <= peerHeight; i++ {
|
2019-07-14 21:45:16 +03:00
|
|
|
assert.Equal(t, sc.getStateAtHeight(i), blockStateNew,
|
|
|
|
"Expected all blocks to be in blockStateNew")
|
|
|
|
assert.Equal(t, sc.getPeersAtHeight(i)[0].peerID, peerID,
|
|
|
|
"Expected the block to be registered to the correct peer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeightFSM(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
|
|
|
now := time.Now()
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.addPeer(peerID),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Adding a peer should return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, errPeerNotFound, sc.markPending(peerID, peerHeight, now),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected markingPending on an unknown peer to return errPeerNotFound")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected setPeerHeight to return no error")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, errBadSchedule, sc.markReceived(peerID, peerHeight, blockSize, now.Add(1*time.Second)),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expecting transitioning from blockStateNew to blockStateReceived to fail")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, errBadSchedule, sc.markProcessed(peerHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expecting transitioning from blockStateNew to blockStateReceived to fail")
|
|
|
|
|
|
|
|
assert.Equal(t, blockStateUnknown, sc.getStateAtHeight(peerHeight+10),
|
|
|
|
"Expected the maximum height seen + 10 to be in blockStateUnknown")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, errBadSchedule, sc.markPending(peerID, peerHeight+10, now.Add(1*time.Second)),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected markPending on block in blockStateUnknown height to fail")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markPending(peerID, initHeight, now.Add(1*time.Second)),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected markPending on a known height with a known peer to return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, blockStatePending, sc.getStateAtHeight(initHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected a the markedBlock to be in blockStatePending")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markReceived(peerID, initHeight, blockSize, now.Add(2*time.Second)),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected a marking a pending block received to return no error")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.resetBlocks(peerID),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected resetBlocks to return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, blockStateNew, sc.getStateAtHeight(initHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected blocks to be in blockStateNew after being reset")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markPending(peerID, initHeight, now),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected marking a reset block to pending to return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, blockStatePending, sc.getStateAtHeight(initHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected block to be in blockStatePending")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markReceived(peerID, blockSize, initHeight, now.Add(2*time.Second)),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected marking a pending block as received to return no error")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, blockStateReceived, sc.getStateAtHeight(initHeight))
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markProcessed(initHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected marking a block as processed to success")
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, blockStateProcessed, sc.getStateAtHeight(initHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected the block to in blockStateProcessed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMinMaxHeight(t *testing.T) {
|
2019-07-15 22:17:25 +03:00
|
|
|
sc := newSchedule(initHeight)
|
|
|
|
now := time.Now()
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, initHeight, sc.minHeight(),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected min height to be the initialized height")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, initHeight, sc.maxHeight(),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected max height to be the initialized height")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Equal(t, initHeight, sc.maxHeight(),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected max height to be the initialized height")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.addPeer(peerID),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Adding a peer should return no error")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.setPeerHeight(peerID, peerHeight),
|
2019-07-14 21:45:16 +03:00
|
|
|
"Expected setPeerHeight to return no error")
|
|
|
|
|
|
|
|
assert.Equal(t, peerHeight, sc.maxHeight(),
|
|
|
|
"Expected max height to increase to peerHeight")
|
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
assert.Nil(t, sc.markPending(peerID, initHeight, now.Add(1*time.Second)),
|
|
|
|
"Expected marking initHeight as pending to return no error")
|
|
|
|
|
|
|
|
assert.Equal(t, initHeight+1, sc.minHeight(),
|
|
|
|
"Expected marking initHeight as pending to move minHeight forward")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLatePeers(t *testing.T) {
|
|
|
|
}
|
2019-07-14 21:45:16 +03:00
|
|
|
|
2019-07-15 22:17:25 +03:00
|
|
|
func TestSlowPeers(t *testing.T) {
|
2019-07-14 21:45:16 +03:00
|
|
|
}
|