2023-03-07 08:36:42 +00:00
|
|
|
package bqueue
|
2019-09-25 16:54:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-01 10:50:08 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/internal/fakechain"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2019-09-25 16:54:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-09-25 16:54:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlockQueue(t *testing.T) {
|
2021-02-01 10:50:08 +00:00
|
|
|
chain := fakechain.NewFakeChain()
|
2019-09-25 16:54:31 +00:00
|
|
|
// notice, it's not yet running
|
2023-03-07 08:36:42 +00:00
|
|
|
bq := New(chain, zaptest.NewLogger(t), nil, nil)
|
2020-01-14 12:32:07 +00:00
|
|
|
blocks := make([]*block.Block, 11)
|
2019-09-25 16:54:31 +00:00
|
|
|
for i := 1; i < 11; i++ {
|
2021-03-01 13:44:47 +00:00
|
|
|
blocks[i] = &block.Block{Header: block.Header{Index: uint32(i)}}
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
|
|
|
// not the ones expected currently
|
|
|
|
for i := 3; i < 5; i++ {
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[i]))
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
2023-03-07 08:36:42 +00:00
|
|
|
last, capLeft := bq.LastQueued()
|
network: do not allow to request invalid block count
The problem is in peer disconnection due to invalid GetBlockByIndex
payload (the logs are from some patched neo-go version):
```
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO new peer connected {"addr": "10.78.69.115:50846", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z WARN peer disconnected {"addr": "10.78.69.115:50846", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO started protocol {"addr": "10.78.69.115:50846", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.491Z INFO new peer connected {"addr": "10.78.69.115:50856", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z WARN peer disconnected {"addr": "10.78.69.115:50856", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO started protocol {"addr": "10.78.69.115:50856", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO new peer connected {"addr": "10.78.69.115:50858", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z INFO started protocol {"addr": "10.78.69.115:50858", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z WARN peer disconnected {"addr": "10.78.69.115:50858", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO new peer connected {"addr": "10.78.69.115:50874", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO started protocol {"addr": "10.78.69.115:50874", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z WARN peer disconnected {"addr": "10.78.69.115:50874", "error": "invalid block count", "peerCount": 2}
```
GetBlockByIndex payload can't be decoded, and the only possible cause
is zero (or <-1, but it's probably not the case) block count requested.
Error is improved as far.
2022-12-15 17:10:09 +00:00
|
|
|
assert.Equal(t, uint32(0), last)
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.Equal(t, CacheSize-2, capLeft)
|
2019-09-25 16:54:31 +00:00
|
|
|
// nothing should be put into the blockchain
|
|
|
|
assert.Equal(t, uint32(0), chain.BlockHeight())
|
|
|
|
assert.Equal(t, 2, bq.length())
|
2022-04-20 18:30:09 +00:00
|
|
|
// now added the expected ones (with duplicates)
|
2019-09-25 16:54:31 +00:00
|
|
|
for i := 1; i < 5; i++ {
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[i]))
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
|
|
|
// but they're still not put into the blockchain, because bq isn't running
|
2023-03-07 08:36:42 +00:00
|
|
|
last, capLeft = bq.LastQueued()
|
network: do not allow to request invalid block count
The problem is in peer disconnection due to invalid GetBlockByIndex
payload (the logs are from some patched neo-go version):
```
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO new peer connected {"addr": "10.78.69.115:50846", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z WARN peer disconnected {"addr": "10.78.69.115:50846", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO started protocol {"addr": "10.78.69.115:50846", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.491Z INFO new peer connected {"addr": "10.78.69.115:50856", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z WARN peer disconnected {"addr": "10.78.69.115:50856", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO started protocol {"addr": "10.78.69.115:50856", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO new peer connected {"addr": "10.78.69.115:50858", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z INFO started protocol {"addr": "10.78.69.115:50858", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z WARN peer disconnected {"addr": "10.78.69.115:50858", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO new peer connected {"addr": "10.78.69.115:50874", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO started protocol {"addr": "10.78.69.115:50874", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z WARN peer disconnected {"addr": "10.78.69.115:50874", "error": "invalid block count", "peerCount": 2}
```
GetBlockByIndex payload can't be decoded, and the only possible cause
is zero (or <-1, but it's probably not the case) block count requested.
Error is improved as far.
2022-12-15 17:10:09 +00:00
|
|
|
assert.Equal(t, uint32(4), last)
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.Equal(t, CacheSize-4, capLeft)
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, uint32(0), chain.BlockHeight())
|
|
|
|
assert.Equal(t, 4, bq.length())
|
2020-09-02 12:00:53 +00:00
|
|
|
// block with too big index is dropped
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(&block.Block{Header: block.Header{Index: bq.chain.BlockHeight() + CacheSize + 1}}))
|
2020-09-02 12:00:53 +00:00
|
|
|
assert.Equal(t, 4, bq.length())
|
2023-03-07 08:36:42 +00:00
|
|
|
go bq.Run()
|
2019-09-25 16:54:31 +00:00
|
|
|
// run() is asynchronous, so we need some kind of timeout anyway and this is the simplest one
|
2022-01-17 21:04:29 +00:00
|
|
|
assert.Eventually(t, func() bool { return chain.BlockHeight() == 4 }, 4*time.Second, 100*time.Millisecond)
|
2023-03-07 08:36:42 +00:00
|
|
|
last, capLeft = bq.LastQueued()
|
network: do not allow to request invalid block count
The problem is in peer disconnection due to invalid GetBlockByIndex
payload (the logs are from some patched neo-go version):
```
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO new peer connected {"addr": "10.78.69.115:50846", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z WARN peer disconnected {"addr": "10.78.69.115:50846", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO started protocol {"addr": "10.78.69.115:50846", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.491Z INFO new peer connected {"addr": "10.78.69.115:50856", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z WARN peer disconnected {"addr": "10.78.69.115:50856", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO started protocol {"addr": "10.78.69.115:50856", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO new peer connected {"addr": "10.78.69.115:50858", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z INFO started protocol {"addr": "10.78.69.115:50858", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z WARN peer disconnected {"addr": "10.78.69.115:50858", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO new peer connected {"addr": "10.78.69.115:50874", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO started protocol {"addr": "10.78.69.115:50874", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z WARN peer disconnected {"addr": "10.78.69.115:50874", "error": "invalid block count", "peerCount": 2}
```
GetBlockByIndex payload can't be decoded, and the only possible cause
is zero (or <-1, but it's probably not the case) block count requested.
Error is improved as far.
2022-12-15 17:10:09 +00:00
|
|
|
assert.Equal(t, uint32(4), last)
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.Equal(t, CacheSize, capLeft)
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 0, bq.length())
|
|
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
|
|
// put some old blocks
|
|
|
|
for i := 1; i < 5; i++ {
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[i]))
|
2019-09-25 16:54:31 +00:00
|
|
|
}
|
2023-03-07 08:36:42 +00:00
|
|
|
last, capLeft = bq.LastQueued()
|
network: do not allow to request invalid block count
The problem is in peer disconnection due to invalid GetBlockByIndex
payload (the logs are from some patched neo-go version):
```
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO new peer connected {"addr": "10.78.69.115:50846", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z WARN peer disconnected {"addr": "10.78.69.115:50846", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO started protocol {"addr": "10.78.69.115:50846", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.491Z INFO new peer connected {"addr": "10.78.69.115:50856", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z WARN peer disconnected {"addr": "10.78.69.115:50856", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO started protocol {"addr": "10.78.69.115:50856", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO new peer connected {"addr": "10.78.69.115:50858", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z INFO started protocol {"addr": "10.78.69.115:50858", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z WARN peer disconnected {"addr": "10.78.69.115:50858", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO new peer connected {"addr": "10.78.69.115:50874", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO started protocol {"addr": "10.78.69.115:50874", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z WARN peer disconnected {"addr": "10.78.69.115:50874", "error": "invalid block count", "peerCount": 2}
```
GetBlockByIndex payload can't be decoded, and the only possible cause
is zero (or <-1, but it's probably not the case) block count requested.
Error is improved as far.
2022-12-15 17:10:09 +00:00
|
|
|
assert.Equal(t, uint32(4), last)
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.Equal(t, CacheSize, capLeft)
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 0, bq.length())
|
|
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
|
|
// unexpected blocks with run() active
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[8]))
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 1, bq.length())
|
|
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[7]))
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 2, bq.length())
|
|
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
|
|
|
// sparse put
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[10]))
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 3, bq.length())
|
|
|
|
assert.Equal(t, uint32(4), chain.BlockHeight())
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.NoError(t, bq.PutBlock(blocks[6]))
|
|
|
|
assert.NoError(t, bq.PutBlock(blocks[5]))
|
2019-09-25 16:54:31 +00:00
|
|
|
// run() is asynchronous, so we need some kind of timeout anyway and this is the simplest one
|
2022-01-17 21:04:29 +00:00
|
|
|
assert.Eventually(t, func() bool { return chain.BlockHeight() == 8 }, 4*time.Second, 100*time.Millisecond)
|
2023-03-07 08:36:42 +00:00
|
|
|
last, capLeft = bq.LastQueued()
|
network: do not allow to request invalid block count
The problem is in peer disconnection due to invalid GetBlockByIndex
payload (the logs are from some patched neo-go version):
```
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO new peer connected {"addr": "10.78.69.115:50846", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z WARN peer disconnected {"addr": "10.78.69.115:50846", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.490Z INFO started protocol {"addr": "10.78.69.115:50846", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.491Z INFO new peer connected {"addr": "10.78.69.115:50856", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z WARN peer disconnected {"addr": "10.78.69.115:50856", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO started protocol {"addr": "10.78.69.115:50856", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.492Z INFO new peer connected {"addr": "10.78.69.115:50858", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z INFO started protocol {"addr": "10.78.69.115:50858", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.493Z WARN peer disconnected {"addr": "10.78.69.115:50858", "error": "invalid block count", "peerCount": 2}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO new peer connected {"addr": "10.78.69.115:50874", "peerCount": 3}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z INFO started protocol {"addr": "10.78.69.115:50874", "userAgent": "/NEO-GO:1.0.0/", "startHeight": 0, "id": 1339571820}
дек 15 16:02:39 glagoli neo-go[928530]: 2022-12-15T16:02:39.494Z WARN peer disconnected {"addr": "10.78.69.115:50874", "error": "invalid block count", "peerCount": 2}
```
GetBlockByIndex payload can't be decoded, and the only possible cause
is zero (or <-1, but it's probably not the case) block count requested.
Error is improved as far.
2022-12-15 17:10:09 +00:00
|
|
|
assert.Equal(t, uint32(8), last)
|
2023-03-07 08:36:42 +00:00
|
|
|
assert.Equal(t, CacheSize-1, capLeft)
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 1, bq.length())
|
|
|
|
assert.Equal(t, uint32(8), chain.BlockHeight())
|
2023-03-07 08:36:42 +00:00
|
|
|
bq.Discard()
|
2019-09-25 16:54:31 +00:00
|
|
|
assert.Equal(t, 0, bq.length())
|
|
|
|
}
|
2021-10-31 09:23:07 +00:00
|
|
|
|
|
|
|
// length wraps len access for tests to make them thread-safe.
|
2023-03-07 08:36:42 +00:00
|
|
|
func (bq *Queue) length() int {
|
2021-10-31 09:23:07 +00:00
|
|
|
bq.queueLock.Lock()
|
|
|
|
defer bq.queueLock.Unlock()
|
|
|
|
return bq.len
|
|
|
|
}
|