mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 19:42:23 +00:00
core: store system fee together with block
Recalculating system fee can be rather costly if done frequently.
This commit is contained in:
parent
907d599edb
commit
f69c8a763b
3 changed files with 15 additions and 13 deletions
|
@ -29,7 +29,7 @@ import (
|
|||
// Tuning parameters.
|
||||
const (
|
||||
headerBatchCount = 2000
|
||||
version = "0.0.4"
|
||||
version = "0.0.5"
|
||||
|
||||
// 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
|
||||
|
@ -414,6 +414,7 @@ func (bc *Blockchain) processHeader(h *block.Header, batch storage.Batch, header
|
|||
}
|
||||
|
||||
buf.Reset()
|
||||
buf.BinWriter.WriteU32LE(0) // sys fee is yet to be calculated
|
||||
h.EncodeBinary(buf.BinWriter)
|
||||
if buf.Err != nil {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -974,7 +975,7 @@ func (bc *Blockchain) GetHeader(hash util.Uint256) (*block.Header, error) {
|
|||
return tb.Header(), nil
|
||||
}
|
||||
}
|
||||
block, err := bc.dao.GetBlock(hash)
|
||||
block, _, err := bc.dao.GetBlock(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -375,17 +375,18 @@ func makeStorageItemKey(scripthash util.Uint160, key []byte) []byte {
|
|||
// -- other.
|
||||
|
||||
// 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())
|
||||
b, err := dao.store.Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
block, err := block.NewBlockFromTrimmedBytes(b)
|
||||
|
||||
block, err := block.NewBlockFromTrimmedBytes(b[4:])
|
||||
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
|
||||
|
@ -508,8 +509,7 @@ func (dao *dao) StoreAsBlock(block *block.Block, sysFee uint32) error {
|
|||
key = storage.AppendPrefix(storage.DataBlock, block.Hash().BytesLE())
|
||||
buf = io.NewBufBinWriter()
|
||||
)
|
||||
// sysFee needs to be handled somehow
|
||||
// buf.WriteU32LE(sysFee)
|
||||
buf.WriteU32LE(sysFee)
|
||||
b, err := block.Trim()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -254,7 +254,7 @@ func TestDeleteStorageItem(t *testing.T) {
|
|||
func TestGetBlock_NotExists(t *testing.T) {
|
||||
dao := newDao(storage.NewMemoryStore())
|
||||
hash := random.Uint256()
|
||||
block, err := dao.GetBlock(hash)
|
||||
block, _, err := dao.GetBlock(hash)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, block)
|
||||
}
|
||||
|
@ -270,11 +270,12 @@ func TestPutGetBlock(t *testing.T) {
|
|||
},
|
||||
}
|
||||
hash := b.Hash()
|
||||
err := dao.StoreAsBlock(b, 0)
|
||||
err := dao.StoreAsBlock(b, 42)
|
||||
require.NoError(t, err)
|
||||
gotBlock, err := dao.GetBlock(hash)
|
||||
gotBlock, sysfee, err := dao.GetBlock(hash)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, gotBlock)
|
||||
require.EqualValues(t, 42, sysfee)
|
||||
}
|
||||
|
||||
func TestGetVersion_NoVersion(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue