From 0e3b9c48a2c65e363814ebadc434b0500d7703aa Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 3 Aug 2021 09:56:39 +0300 Subject: [PATCH] core: add API to store StateSyncPoint and StateSyncCurrentBlockHeight We need it in order not to mess up the blockchain which has its own CurrentBlockHeight. --- pkg/core/blockchain.go | 2 +- pkg/core/dao/dao.go | 37 +++++++++++++++++++++++++++++++++++++ pkg/core/dao/dao_test.go | 30 ++++++++++++++++++++++++++++++ pkg/core/storage/store.go | 28 +++++++++++++++------------- 4 files changed, 83 insertions(+), 14 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 58936ac71..bca60b94b 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -43,7 +43,7 @@ import ( // Tuning parameters. const ( headerBatchCount = 2000 - version = "0.1.1" + version = "0.1.2" defaultInitialGAS = 52000000_00000000 defaultMemPoolSize = 50000 diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index 6ac54c190..b895d1033 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -45,6 +45,8 @@ type DAO interface { GetHeaderHashes() ([]util.Uint256, error) GetNEP17TransferInfo(acc util.Uint160) (*state.NEP17TransferInfo, error) GetNEP17TransferLog(acc util.Uint160, index uint32) (*state.NEP17TransferLog, error) + GetStateSyncPoint() (uint32, error) + GetStateSyncCurrentBlockHeight() (uint32, error) GetStorageItem(id int32, key []byte) state.StorageItem GetStorageItems(id int32) (map[string]state.StorageItem, error) GetStorageItemsWithPrefix(id int32, prefix []byte) (map[string]state.StorageItem, error) @@ -58,6 +60,8 @@ type DAO interface { PutCurrentHeader(hashAndIndex []byte) error PutNEP17TransferInfo(acc util.Uint160, bs *state.NEP17TransferInfo) error PutNEP17TransferLog(acc util.Uint160, index uint32, lg *state.NEP17TransferLog) error + PutStateSyncPoint(p uint32) error + PutStateSyncCurrentBlockHeight(h uint32) error PutStorageItem(id int32, key []byte, si state.StorageItem) error PutVersion(v string) error Seek(id int32, prefix []byte, f func(k, v []byte)) @@ -399,6 +403,25 @@ func (dao *Simple) GetCurrentHeaderHeight() (i uint32, h util.Uint256, err error return } +// GetStateSyncPoint returns current state synchronisation point P. +func (dao *Simple) GetStateSyncPoint() (uint32, error) { + b, err := dao.Store.Get(storage.SYSStateSyncPoint.Bytes()) + if err != nil { + return 0, err + } + return binary.LittleEndian.Uint32(b), nil +} + +// GetStateSyncCurrentBlockHeight returns current block height stored during state +// synchronisation process. +func (dao *Simple) GetStateSyncCurrentBlockHeight() (uint32, error) { + b, err := dao.Store.Get(storage.SYSStateSyncCurrentBlockHeight.Bytes()) + if err != nil { + return 0, err + } + return binary.LittleEndian.Uint32(b), nil +} + // GetHeaderHashes returns a sorted list of header hashes retrieved from // the given underlying store. func (dao *Simple) GetHeaderHashes() ([]util.Uint256, error) { @@ -466,6 +489,20 @@ func (dao *Simple) PutCurrentHeader(hashAndIndex []byte) error { return dao.Store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndex) } +// PutStateSyncPoint stores current state synchronisation point P. +func (dao *Simple) PutStateSyncPoint(p uint32) error { + buf := make([]byte, 4) + binary.LittleEndian.PutUint32(buf, p) + return dao.Store.Put(storage.SYSStateSyncPoint.Bytes(), buf) +} + +// PutStateSyncCurrentBlockHeight stores current block height during state synchronisation process. +func (dao *Simple) PutStateSyncCurrentBlockHeight(h uint32) error { + buf := make([]byte, 4) + binary.LittleEndian.PutUint32(buf, h) + return dao.Store.Put(storage.SYSStateSyncCurrentBlockHeight.Bytes(), buf) +} + // read2000Uint256Hashes attempts to read 2000 Uint256 hashes from // the given byte array. func read2000Uint256Hashes(b []byte) ([]util.Uint256, error) { diff --git a/pkg/core/dao/dao_test.go b/pkg/core/dao/dao_test.go index fb8bb0960..581b0e521 100644 --- a/pkg/core/dao/dao_test.go +++ b/pkg/core/dao/dao_test.go @@ -171,3 +171,33 @@ func TestMakeStorageItemKey(t *testing.T) { actual = makeStorageItemKey(id, nil) require.Equal(t, expected, actual) } + +func TestPutGetStateSyncPoint(t *testing.T) { + dao := NewSimple(storage.NewMemoryStore(), true) + + // empty store + _, err := dao.GetStateSyncPoint() + require.Error(t, err) + + // non-empty store + var expected uint32 = 5 + require.NoError(t, dao.PutStateSyncPoint(expected)) + actual, err := dao.GetStateSyncPoint() + require.NoError(t, err) + require.Equal(t, expected, actual) +} + +func TestPutGetStateSyncCurrentBlockHeight(t *testing.T) { + dao := NewSimple(storage.NewMemoryStore(), true) + + // empty store + _, err := dao.GetStateSyncCurrentBlockHeight() + require.Error(t, err) + + // non-empty store + var expected uint32 = 5 + require.NoError(t, dao.PutStateSyncCurrentBlockHeight(expected)) + actual, err := dao.GetStateSyncCurrentBlockHeight() + require.NoError(t, err) + require.Equal(t, expected, actual) +} diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index 540fdcc39..b475e54d5 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -8,19 +8,21 @@ import ( // KeyPrefix constants. const ( - DataBlock KeyPrefix = 0x01 - DataTransaction KeyPrefix = 0x02 - DataMPT KeyPrefix = 0x03 - STAccount KeyPrefix = 0x40 - STNotification KeyPrefix = 0x4d - STContractID KeyPrefix = 0x51 - STStorage KeyPrefix = 0x70 - STNEP17Transfers KeyPrefix = 0x72 - STNEP17TransferInfo KeyPrefix = 0x73 - IXHeaderHashList KeyPrefix = 0x80 - SYSCurrentBlock KeyPrefix = 0xc0 - SYSCurrentHeader KeyPrefix = 0xc1 - SYSVersion KeyPrefix = 0xf0 + DataBlock KeyPrefix = 0x01 + DataTransaction KeyPrefix = 0x02 + DataMPT KeyPrefix = 0x03 + STAccount KeyPrefix = 0x40 + STNotification KeyPrefix = 0x4d + STContractID KeyPrefix = 0x51 + STStorage KeyPrefix = 0x70 + STNEP17Transfers KeyPrefix = 0x72 + STNEP17TransferInfo KeyPrefix = 0x73 + IXHeaderHashList KeyPrefix = 0x80 + SYSCurrentBlock KeyPrefix = 0xc0 + SYSCurrentHeader KeyPrefix = 0xc1 + SYSStateSyncCurrentBlockHeight KeyPrefix = 0xc2 + SYSStateSyncPoint KeyPrefix = 0xc3 + SYSVersion KeyPrefix = 0xf0 ) const (