From c5ed3b788b22b847aae7010a88010af364d39c7b Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 12 Dec 2019 21:04:55 +0300 Subject: [PATCH] core: simplify dao creation with newDao() --- pkg/core/blockchain.go | 6 ++--- pkg/core/dao.go | 4 ++++ pkg/core/dao_test.go | 54 +++++++++++++++++++++--------------------- pkg/core/interops.go | 2 +- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index ab72791fc..f574a3631 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -86,7 +86,7 @@ type headersOpFunc func(headerList *HeaderHashList) func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration) (*Blockchain, error) { bc := &Blockchain{ config: cfg, - dao: &dao{store: storage.NewMemCachedStore(s)}, + dao: newDao(s), headersOp: make(chan headersOpFunc), headersOpDone: make(chan struct{}), stopCh: make(chan struct{}), @@ -344,7 +344,7 @@ func (bc *Blockchain) processHeader(h *Header, batch storage.Batch, headerList * // is happening here, quite allot as you can see :). If things are wired together // and all tests are in place, we can make a more optimized and cleaner implementation. func (bc *Blockchain) storeBlock(block *Block) error { - cache := &dao{store: storage.NewMemCachedStore(bc.dao.store)} + cache := newDao(bc.dao.store) if err := cache.StoreAsBlock(block, 0); err != nil { return err } @@ -1156,7 +1156,7 @@ func (bc *Blockchain) GetStandByValidators() (keys.PublicKeys, error) { // GetValidators returns validators. // Golang implementation of GetValidators method in C# (https://github.com/neo-project/neo/blob/c64748ecbac3baeb8045b16af0d518398a6ced24/neo/Persistence/Snapshot.cs#L182) func (bc *Blockchain) GetValidators(txes ...*transaction.Transaction) ([]*keys.PublicKey, error) { - cache := &dao{store: storage.NewMemCachedStore(bc.dao.store)} + cache := newDao(bc.dao.store) if len(txes) > 0 { for _, tx := range txes { // iterate through outputs diff --git a/pkg/core/dao.go b/pkg/core/dao.go index 1a0c8cea3..c6bbd2ddc 100644 --- a/pkg/core/dao.go +++ b/pkg/core/dao.go @@ -19,6 +19,10 @@ type dao struct { store *storage.MemCachedStore } +func newDao(backend storage.Store) *dao { + return &dao{store: storage.NewMemCachedStore(backend)} +} + // GetAndDecode performs get operation and decoding with serializable structures. func (dao *dao) GetAndDecode(entity io.Serializable, key []byte) error { entityBytes, err := dao.store.Get(key) diff --git a/pkg/core/dao_test.go b/pkg/core/dao_test.go index 5aa44fe9f..5ae92c694 100644 --- a/pkg/core/dao_test.go +++ b/pkg/core/dao_test.go @@ -15,7 +15,7 @@ import ( ) func TestPutGetAndDecode(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) serializable := &TestSerializable{field: random.String(4)} hash := []byte{1} err := dao.Put(serializable, hash) @@ -40,7 +40,7 @@ func (t *TestSerializable) DecodeBinary(reader *io.BinReader) { } func TestGetAccountStateOrNew_New(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint160() createdAccount, err := dao.GetAccountStateOrNew(hash) require.NoError(t, err) @@ -51,7 +51,7 @@ func TestGetAccountStateOrNew_New(t *testing.T) { } func TestPutAndGetAccountStateOrNew(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint160() accountState := &state.Account{ScriptHash: hash} err := dao.PutAccountState(accountState) @@ -62,7 +62,7 @@ func TestPutAndGetAccountStateOrNew(t *testing.T) { } func TestPutAndGetAssetState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) id := random.Uint256() assetState := &state.Asset{ID: id, Owner: keys.PublicKey{}} err := dao.PutAssetState(assetState) @@ -73,7 +73,7 @@ func TestPutAndGetAssetState(t *testing.T) { } func TestPutAndGetContractState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) contractState := &state.Contract{Script: []byte{}, ParamList:[]smartcontract.ParamType{}} hash := contractState.ScriptHash() err := dao.PutContractState(contractState) @@ -84,7 +84,7 @@ func TestPutAndGetContractState(t *testing.T) { } func TestDeleteContractState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) contractState := &state.Contract{Script: []byte{}, ParamList:[]smartcontract.ParamType{}} hash := contractState.ScriptHash() err := dao.PutContractState(contractState) @@ -97,7 +97,7 @@ func TestDeleteContractState(t *testing.T) { } func TestGetUnspentCoinStateOrNew_New(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() unspentCoinState, err := dao.GetUnspentCoinStateOrNew(hash) require.NoError(t, err) @@ -108,7 +108,7 @@ func TestGetUnspentCoinStateOrNew_New(t *testing.T) { } func TestGetUnspentCoinState_Err(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() gotUnspentCoinState, err := dao.GetUnspentCoinState(hash) require.Error(t, err) @@ -116,7 +116,7 @@ func TestGetUnspentCoinState_Err(t *testing.T) { } func TestPutGetUnspentCoinState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() unspentCoinState := &UnspentCoinState{states:[]state.Coin{}} err := dao.PutUnspentCoinState(hash, unspentCoinState) @@ -127,7 +127,7 @@ func TestPutGetUnspentCoinState(t *testing.T) { } func TestGetSpentCoinStateOrNew_New(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() spentCoinState, err := dao.GetSpentCoinsOrNew(hash) require.NoError(t, err) @@ -138,7 +138,7 @@ func TestGetSpentCoinStateOrNew_New(t *testing.T) { } func TestPutAndGetSpentCoinState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} err := dao.PutSpentCoinState(hash, spentCoinState) @@ -149,7 +149,7 @@ func TestPutAndGetSpentCoinState(t *testing.T) { } func TestGetSpentCoinState_Err(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() spentCoinState, err := dao.GetSpentCoinState(hash) require.Error(t, err) @@ -157,7 +157,7 @@ func TestGetSpentCoinState_Err(t *testing.T) { } func TestDeleteSpentCoinState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() spentCoinState := &SpentCoinState{items:make(map[uint16]uint32)} err := dao.PutSpentCoinState(hash, spentCoinState) @@ -170,7 +170,7 @@ func TestDeleteSpentCoinState(t *testing.T) { } func TestGetValidatorStateOrNew_New(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) publicKey := &keys.PublicKey{} validatorState, err := dao.GetValidatorStateOrNew(publicKey) require.NoError(t, err) @@ -181,7 +181,7 @@ func TestGetValidatorStateOrNew_New(t *testing.T) { } func TestPutGetValidatorState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) publicKey := &keys.PublicKey{} validatorState := &state.Validator{ PublicKey: publicKey, @@ -196,7 +196,7 @@ func TestPutGetValidatorState(t *testing.T) { } func TestDeleteValidatorState(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) publicKey := &keys.PublicKey{} validatorState := &state.Validator{ PublicKey: publicKey, @@ -213,7 +213,7 @@ func TestDeleteValidatorState(t *testing.T) { } func TestGetValidators(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) publicKey := &keys.PublicKey{} validatorState := &state.Validator{ PublicKey: publicKey, @@ -228,7 +228,7 @@ func TestGetValidators(t *testing.T) { } func TestPutGetAppExecResult(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() appExecResult := &state.AppExecResult{TxHash: hash, Events:[]state.NotificationEvent{}} err := dao.PutAppExecResult(appExecResult) @@ -239,7 +239,7 @@ func TestPutGetAppExecResult(t *testing.T) { } func TestPutGetStorageItem(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint160() key := []byte{0} storageItem := &state.StorageItem{Value: []uint8{}} @@ -250,7 +250,7 @@ func TestPutGetStorageItem(t *testing.T) { } func TestDeleteStorageItem(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint160() key := []byte{0} storageItem := &state.StorageItem{Value: []uint8{}} @@ -263,7 +263,7 @@ func TestDeleteStorageItem(t *testing.T) { } func TestGetBlock_NotExists(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) hash := random.Uint256() block, err := dao.GetBlock(hash) require.Error(t, err) @@ -271,7 +271,7 @@ func TestGetBlock_NotExists(t *testing.T) { } func TestPutGetBlock(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) block := &Block{ BlockBase: BlockBase{ Script: transaction.Witness{ @@ -289,14 +289,14 @@ func TestPutGetBlock(t *testing.T) { } func TestGetVersion_NoVersion(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) version, err := dao.GetVersion() require.Error(t, err) require.Equal(t, "", version) } func TestGetVersion(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) err := dao.PutVersion("testVersion") require.NoError(t, err) version, err := dao.GetVersion() @@ -305,14 +305,14 @@ func TestGetVersion(t *testing.T) { } func TestGetCurrentHeaderHeight_NoHeader(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) height, err := dao.GetCurrentBlockHeight() require.Error(t, err) require.Equal(t, uint32(0), height) } func TestGetCurrentHeaderHeight_Store(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) block := &Block{ BlockBase: BlockBase{ Script: transaction.Witness{ @@ -329,7 +329,7 @@ func TestGetCurrentHeaderHeight_Store(t *testing.T) { } func TestStoreAsTransaction(t *testing.T) { - dao := &dao{store: storage.NewMemCachedStore(storage.NewMemoryStore())} + dao := newDao(storage.NewMemoryStore()) tx := &transaction.Transaction{} hash := tx.Hash() err := dao.StoreAsTransaction(tx, 0) diff --git a/pkg/core/interops.go b/pkg/core/interops.go index 33fac36c6..def5fda15 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -24,7 +24,7 @@ type interopContext struct { } func newInteropContext(trigger byte, bc Blockchainer, s storage.Store, block *Block, tx *transaction.Transaction) *interopContext { - dao := &dao{store: storage.NewMemCachedStore(s)} + dao := newDao(s) nes := make([]state.NotificationEvent, 0) return &interopContext{bc, trigger, block, tx, dao, nes} }