diff --git a/pkg/core/block/block.go b/pkg/core/block/block.go index 028b278d2..97b041d0b 100644 --- a/pkg/core/block/block.go +++ b/pkg/core/block/block.go @@ -100,26 +100,16 @@ func New(stateRootEnabled bool) *Block { } } -// Trim returns a subset of the block data to save up space -// in storage. -// Notice that only the hashes of the transactions are stored. -func (b *Block) Trim() ([]byte, error) { - buf := io.NewBufBinWriter() - numTx := len(b.Transactions) - buf.Grow(b.GetExpectedBlockSizeWithoutTransactions(numTx) + util.Uint256Size*numTx) - b.Header.EncodeBinary(buf.BinWriter) +// EncodeTrimmed writes trimmed representation of the block data into w. Trimmed blocks +// do not store complete transactions, instead they only store their hashes. +func (b *Block) EncodeTrimmed(w *io.BinWriter) { + b.Header.EncodeBinary(w) - buf.WriteVarUint(uint64(numTx)) + w.WriteVarUint(uint64(len(b.Transactions))) for _, tx := range b.Transactions { h := tx.Hash() - h.EncodeBinary(buf.BinWriter) + h.EncodeBinary(w) } - - if buf.Err != nil { - return nil, buf.Err - } - - return buf.Bytes(), nil } // DecodeBinary decodes the block from the given BinReader, implementing diff --git a/pkg/core/block/block_test.go b/pkg/core/block/block_test.go index e4995dfe2..dd9df7ed1 100644 --- a/pkg/core/block/block_test.go +++ b/pkg/core/block/block_test.go @@ -55,10 +55,11 @@ func TestDecodeBlock1(t *testing.T) { func TestTrimmedBlock(t *testing.T) { block := getDecodedBlock(t, 1) - b, err := block.Trim() - require.NoError(t, err) + buf := io.NewBufBinWriter() + block.EncodeTrimmed(buf.BinWriter) + require.NoError(t, buf.Err) - r := io.NewBinReaderFromBuf(b) + r := io.NewBinReaderFromBuf(buf.Bytes()) trimmedBlock, err := NewTrimmedFromReader(false, r) require.NoError(t, err) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index 04eb31cf4..c85e1084b 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -648,11 +648,7 @@ func (dao *Simple) StoreAsBlock(block *block.Block, aer1 *state.AppExecResult, a buf = dao.getDataBuf() ) buf.WriteB(storage.ExecBlock) - b, err := block.Trim() - if err != nil { - return err - } - buf.WriteBytes(b) + block.EncodeTrimmed(buf.BinWriter) if aer1 != nil { aer1.EncodeBinary(buf.BinWriter) }