core: do not return block if only header is present

This commit is contained in:
Evgeniy Stratonikov 2021-03-10 12:29:56 +03:00
parent f83b376181
commit ac95510402
2 changed files with 33 additions and 2 deletions

View file

@ -1122,6 +1122,9 @@ func (bc *Blockchain) GetBlock(hash util.Uint256) (*block.Block, error) {
if err != nil {
return nil, err
}
if !block.MerkleRoot.Equals(util.Uint256{}) && len(block.Transactions) == 0 {
return nil, errors.New("only header is found")
}
for _, tx := range block.Transactions {
stx, _, err := bc.dao.GetTransaction(tx.Hash())
if err != nil {

View file

@ -230,6 +230,33 @@ func TestGetBlock(t *testing.T) {
}
assert.NoError(t, bc.persist())
}
t.Run("store only header", func(t *testing.T) {
t.Run("non-empty block", func(t *testing.T) {
tx, err := testchain.NewTransferFromOwner(bc, bc.contracts.NEO.Hash,
random.Uint160(), 1, 1, 1000)
b := bc.newBlock(tx)
require.NoError(t, bc.AddHeaders(&b.Header))
_, err = bc.GetBlock(b.Hash())
require.Error(t, err)
_, err = bc.GetHeader(b.Hash())
require.NoError(t, err)
require.NoError(t, bc.AddBlock(b))
_, err = bc.GetBlock(b.Hash())
require.NoError(t, err)
})
t.Run("empty block", func(t *testing.T) {
b := bc.newBlock()
require.NoError(t, bc.AddHeaders(&b.Header))
_, err = bc.GetBlock(b.Hash())
require.NoError(t, err)
})
})
}
func (bc *Blockchain) newTestTx(h util.Uint160, script []byte) *transaction.Transaction {
@ -1558,9 +1585,10 @@ func TestRemoveUntraceable(t *testing.T) {
require.Error(t, err)
_, err = bc.GetAppExecResults(tx1.Hash(), trigger.Application)
require.Error(t, err)
b, err := bc.GetBlock(b1.Hash())
_, err = bc.GetBlock(b1.Hash())
require.Error(t, err)
_, err = bc.GetHeader(b1.Hash())
require.NoError(t, err)
require.Len(t, b.Transactions, 0)
}
func TestInvalidNotification(t *testing.T) {