core: store system fee together with block

Recalculating system fee can be rather costly if done
frequently.
This commit is contained in:
Evgenii Stratonikov 2020-02-27 16:31:28 +03:00
parent 907d599edb
commit f69c8a763b
3 changed files with 15 additions and 13 deletions

View file

@ -29,7 +29,7 @@ import (
// Tuning parameters. // Tuning parameters.
const ( const (
headerBatchCount = 2000 headerBatchCount = 2000
version = "0.0.4" version = "0.0.5"
// This one comes from C# code and it's different from the constant used // This one comes from C# code and it's different from the constant used
// when creating an asset with Neo.Asset.Create interop call. It looks // when creating an asset with Neo.Asset.Create interop call. It looks
@ -414,6 +414,7 @@ func (bc *Blockchain) processHeader(h *block.Header, batch storage.Batch, header
} }
buf.Reset() buf.Reset()
buf.BinWriter.WriteU32LE(0) // sys fee is yet to be calculated
h.EncodeBinary(buf.BinWriter) h.EncodeBinary(buf.BinWriter)
if buf.Err != nil { if buf.Err != nil {
return buf.Err return buf.Err
@ -949,7 +950,7 @@ func (bc *Blockchain) GetBlock(hash util.Uint256) (*block.Block, error) {
} }
} }
block, err := bc.dao.GetBlock(hash) block, _, err := bc.dao.GetBlock(hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -974,7 +975,7 @@ func (bc *Blockchain) GetHeader(hash util.Uint256) (*block.Header, error) {
return tb.Header(), nil return tb.Header(), nil
} }
} }
block, err := bc.dao.GetBlock(hash) block, _, err := bc.dao.GetBlock(hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -375,17 +375,18 @@ func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte {
// -- other. // -- other.
// GetBlock returns Block by the given hash if it exists in the store. // GetBlock returns Block by the given hash if it exists in the store.
func (dao *dao) GetBlock(hash util.Uint256) (*block.Block, error) { func (dao *dao) GetBlock(hash util.Uint256) (*block.Block, uint32, error) {
key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE()) key := storage.AppendPrefix(storage.DataBlock, hash.BytesLE())
b, err := dao.store.Get(key) b, err := dao.store.Get(key)
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
block, err := block.NewBlockFromTrimmedBytes(b)
block, err := block.NewBlockFromTrimmedBytes(b[4:])
if err != nil { if err != nil {
return nil, err return nil, 0, err
} }
return block, err return block, binary.LittleEndian.Uint32(b[:4]), nil
} }
// GetVersion attempts to get the current version stored in the // GetVersion attempts to get the current version stored in the
@ -508,8 +509,7 @@ func (dao *dao) StoreAsBlock(block *block.Block, sysFee uint32) error {
key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE()) key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
buf = io.NewBufBinWriter() buf = io.NewBufBinWriter()
) )
// sysFee needs to be handled somehow buf.WriteU32LE(sysFee)
// buf.WriteU32LE(sysFee)
b, err := block.Trim() b, err := block.Trim()
if err != nil { if err != nil {
return err return err

View file

@ -254,7 +254,7 @@ func TestDeleteStorageItem(t *testing.T) {
func TestGetBlock_NotExists(t *testing.T) { func TestGetBlock_NotExists(t *testing.T) {
dao := newDao(storage.NewMemoryStore()) dao := newDao(storage.NewMemoryStore())
hash := random.Uint256() hash := random.Uint256()
block, err := dao.GetBlock(hash) block, _, err := dao.GetBlock(hash)
require.Error(t, err) require.Error(t, err)
require.Nil(t, block) require.Nil(t, block)
} }
@ -270,11 +270,12 @@ func TestPutGetBlock(t *testing.T) {
}, },
} }
hash := b.Hash() hash := b.Hash()
err := dao.StoreAsBlock(b, 0) err := dao.StoreAsBlock(b, 42)
require.NoError(t, err) require.NoError(t, err)
gotBlock, err := dao.GetBlock(hash) gotBlock, sysfee, err := dao.GetBlock(hash)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, gotBlock) require.NotNil(t, gotBlock)
require.EqualValues(t, 42, sysfee)
} }
func TestGetVersion_NoVersion(t *testing.T) { func TestGetVersion_NoVersion(t *testing.T) {