Browse Source

Basic test for block_pool

pull/40/head
Jae Kwon 10 years ago
parent
commit
6c7d85c64c
3 changed files with 163 additions and 33 deletions
  1. +15
    -0
      block/block.go
  2. +74
    -33
      block/pool.go
  3. +74
    -0
      block/pool_test.go

+ 15
- 0
block/block.go View File

@ -54,6 +54,9 @@ func (b *Block) ValidateBasic(lastBlockHeight uint, lastBlockHash []byte,
}
func (b *Block) Hash() []byte {
if b.Header == nil || b.Validation == nil || b.Data == nil {
return nil
}
hashes := [][]byte{
b.Header.Hash(),
b.Validation.Hash(),
@ -81,6 +84,9 @@ func (b *Block) String() string {
}
func (b *Block) StringIndented(indent string) string {
if b == nil {
return "nil-Block"
}
return fmt.Sprintf(`Block{
%s %v
%s %v
@ -126,6 +132,9 @@ func (h *Header) Hash() []byte {
}
func (h *Header) StringIndented(indent string) string {
if h == nil {
return "nil-Header"
}
return fmt.Sprintf(`Header{
%s Network: %v
%s Height: %v
@ -212,6 +221,9 @@ func (v *Validation) Hash() []byte {
}
func (v *Validation) StringIndented(indent string) string {
if v == nil {
return "nil-Validation"
}
commitStrings := make([]string, len(v.Commits))
for i, commit := range v.Commits {
commitStrings[i] = commit.String()
@ -254,6 +266,9 @@ func (data *Data) Hash() []byte {
}
func (data *Data) StringIndented(indent string) string {
if data == nil {
return "nil-Data"
}
txStrings := make([]string, len(data.Txs))
for i, tx := range data.Txs {
txStrings[i] = fmt.Sprintf("Tx:%v", tx)


block/block_pool.go → block/pool.go View File


+ 74
- 0
block/pool_test.go View File

@ -0,0 +1,74 @@
package block
import (
"math/rand"
"testing"
. "github.com/tendermint/tendermint/common"
)
type testPeer struct {
id string
height uint
}
func makePeers(numPeers int, minHeight, maxHeight uint) map[string]testPeer {
peers := make(map[string]testPeer, numPeers)
for i := 0; i < numPeers; i++ {
peerId := RandStr(12)
height := minHeight + uint(rand.Intn(int(maxHeight-minHeight)))
peers[peerId] = testPeer{peerId, height}
}
return peers
}
func TestBasic(t *testing.T) {
// 100 peers anywhere at height 0 to 1000.
peers := makePeers(100, 0, 1000)
start := uint(42)
maxHeight := uint(300)
timeoutsCh := make(chan string)
requestsCh := make(chan BlockRequest)
blocksCh := make(chan *Block)
pool := NewBlockPool(start, timeoutsCh, requestsCh, blocksCh)
pool.Start()
// Introduce each peer.
go func() {
for _, peer := range peers {
pool.SetPeerStatus(peer.id, peer.height)
}
}()
lastSeenBlock := uint(41)
// Pull from channels
for {
select {
case peerId := <-timeoutsCh:
t.Errorf("timeout: %v", peerId)
case request := <-requestsCh:
log.Debug("Pulled new BlockRequest", "request", request)
// After a while, pretend like we got a block from the peer.
go func() {
block := &Block{Header: &Header{Height: request.Height}}
pool.AddBlock(block, request.PeerId)
log.Debug("Added block", "block", request.Height, "peer", request.PeerId)
}()
case block := <-blocksCh:
log.Debug("Pulled new Block", "height", block.Height)
if block.Height != lastSeenBlock+1 {
t.Fatalf("Wrong order of blocks seen. Expected: %v Got: %v", lastSeenBlock+1, block.Height)
}
lastSeenBlock++
if block.Height == maxHeight {
return // Done!
}
}
}
pool.Stop()
}

Loading…
Cancel
Save