mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
c531dc0bde
This one will replace blockCache in Blockchain itself as it can and should be external from it. The idea is that we only feed successive blocks into the Blockchain and it only stores valid proper Blockchain and nothing else.
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package network
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBlockQueue(t *testing.T) {
|
|
chain := &testChain{}
|
|
// notice, it's not yet running
|
|
bq := newBlockQueue(0, chain)
|
|
blocks := make([]*core.Block, 11)
|
|
for i := 1; i < 11; i++ {
|
|
blocks[i] = &core.Block{BlockBase: core.BlockBase{Index: uint32(i)}}
|
|
}
|
|
// not the ones expected currently
|
|
for i := 3; i < 5; i++ {
|
|
assert.NoError(t, bq.putBlock(blocks[i]))
|
|
}
|
|
// nothing should be put into the blockchain
|
|
assert.Equal(t, uint32(0), chain.BlockHeight())
|
|
assert.Equal(t, 2, bq.length())
|
|
// now added expected ones (with duplicates)
|
|
for i := 1; i < 5; i++ {
|
|
assert.NoError(t, bq.putBlock(blocks[i]))
|
|
}
|
|
// but they're still not put into the blockchain, because bq isn't running
|
|
assert.Equal(t, uint32(0), chain.BlockHeight())
|
|
assert.Equal(t, 4, bq.length())
|
|
go bq.run()
|
|
// run() is asynchronous, so we need some kind of timeout anyway and this is the simplest one
|
|
for i := 0; i < 5; i++ {
|
|
if chain.BlockHeight() != 4 {
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|
|
assert.Equal(t, 0, bq.length())
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
// put some old blocks
|
|
for i := 1; i < 5; i++ {
|
|
assert.NoError(t, bq.putBlock(blocks[i]))
|
|
}
|
|
assert.Equal(t, 0, bq.length())
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
// unexpected blocks with run() active
|
|
assert.NoError(t, bq.putBlock(blocks[8]))
|
|
assert.Equal(t, 1, bq.length())
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
assert.NoError(t, bq.putBlock(blocks[7]))
|
|
assert.Equal(t, 2, bq.length())
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
// sparse put
|
|
assert.NoError(t, bq.putBlock(blocks[10]))
|
|
assert.Equal(t, 3, bq.length())
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
assert.NoError(t, bq.putBlock(blocks[6]))
|
|
assert.NoError(t, bq.putBlock(blocks[5]))
|
|
// run() is asynchronous, so we need some kind of timeout anyway and this is the simplest one
|
|
for i := 0; i < 5; i++ {
|
|
if chain.BlockHeight() != 8 {
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|
|
assert.Equal(t, 1, bq.length())
|
|
assert.Equal(t, uint32(8), chain.BlockHeight())
|
|
bq.discard()
|
|
assert.Equal(t, 0, bq.length())
|
|
}
|