Merge pull request #2398 from nspcc-dev/trim-micro
block/dao: simplify trimming, avoid allocations
This commit is contained in:
commit
4869049965
3 changed files with 11 additions and 24 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue