Skip to content

Add GetTotalItems in CacheStorageInterface & GetBlockByHeightCacheFormat in Blocks Service #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/storage/blockState.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (bs *BlockStateStorage) GetAllItems(item interface{}) error {
return nil
}

func (bs *BlockStateStorage) GetTotalItems() int {
// this storage only have 1 item
return 1
}

func (bs *BlockStateStorage) RemoveItem(key interface{}) error {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions common/storage/mempoolBackupStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func (m *MempoolBackupStorage) GetAllItems(item interface{}) error {
return nil
}

func (m *MempoolBackupStorage) GetTotalItems() int {
m.RLock()
var totalItems = len(m.mempools)
m.RUnlock()
return totalItems
}

// RemoveItem remove specific item by key
func (m *MempoolBackupStorage) RemoveItem(key interface{}) error {
m.Lock()
Expand Down
7 changes: 7 additions & 0 deletions common/storage/mempoolStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func (m *MempoolCacheStorage) GetAllItems(item interface{}) error {
return nil
}

func (m *MempoolCacheStorage) GetTotalItems() int {
m.RLock()
var totalItems = len(m.mempoolMap)
m.RUnlock()
return totalItems
}

func (m *MempoolCacheStorage) RemoveItem(keys interface{}) error {
m.Lock()
defer m.Unlock()
Expand Down
10 changes: 10 additions & 0 deletions common/storage/nodeAddressInfoStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func (nas *NodeAddressInfoStorage) GetAllItems(item interface{}) error {
return nil
}

func (nas *NodeAddressInfoStorage) GetTotalItems() int {
nas.RLock()
defer nas.RUnlock()
var totalItems int
for _, nodeIDs := range nas.nodeAddressInfoMapByAddressPort {
totalItems += len(nodeIDs)
}
return totalItems
}

func (nas *NodeAddressInfoStorage) RemoveItem(key interface{}) error {
nas.Lock()
defer nas.Unlock()
Expand Down
5 changes: 5 additions & 0 deletions common/storage/nodeAdmissionTimestampStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (ns *NodeAdmissionTimestampStorage) GetAllItems(item interface{}) error {
return nil
}

func (ns *NodeAdmissionTimestampStorage) GetTotalItems() int {
// this storage only have 1 item
return 1
}

func (ns *NodeAdmissionTimestampStorage) RemoveItem(key interface{}) error {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions common/storage/nodeRegistration.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ func (n *NodeRegistryCacheStorage) GetAllItems(item interface{}) error {
return nil
}

func (n *NodeRegistryCacheStorage) GetTotalItems() int {
n.RLock()
var totalItems = len(n.transactionalNodeRegistries)
n.RUnlock()
return totalItems
}

func (n *NodeRegistryCacheStorage) RemoveItem(idx interface{}) error {
n.Lock()
defer n.Unlock()
Expand Down
10 changes: 10 additions & 0 deletions common/storage/nodeShardCacheStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func (n *NodeShardCacheStorage) GetAllItems(item interface{}) error {
return nil
}

func (n *NodeShardCacheStorage) GetTotalItems() int {
n.RLock()
defer n.RUnlock()
var totalItems int
for _, IDs := range n.shardMap.NodeShards {
totalItems += len(IDs)
}
return totalItems
}

func (n *NodeShardCacheStorage) RemoveItem(key interface{}) error {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions common/storage/receiptPoolCacheStorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (brs *ReceiptPoolCacheStorage) GetAllItems(items interface{}) error {
return nil
}

func (brs *ReceiptPoolCacheStorage) GetTotalItems() int {
brs.Lock()
var totalItems = len(brs.receipts)
brs.Unlock()
return totalItems
}

func (brs *ReceiptPoolCacheStorage) RemoveItem(_ interface{}) error {
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions common/storage/receiptReminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ func (rs *ReceiptReminderStorage) GetAllItems(key interface{}) error {
}
return blocker.NewBlocker(blocker.ValidationErr, "WrongType key")
}

func (rs *ReceiptReminderStorage) GetTotalItems() int {
rs.Lock()
var totalItems = len(rs.reminders)
rs.Unlock()
return totalItems
}

func (rs *ReceiptReminderStorage) RemoveItem(key interface{}) error {
rs.Lock()
defer rs.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions common/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type (
GetItem(key, item interface{}) error
// GetAllItems fetch all cached items
GetAllItems(item interface{}) error
// GetTotalItems fetch the number of total cached items
GetTotalItems() int
// RemoveItem remove item by providing the key(s)
RemoveItem(key interface{}) error
// GetSize return the size of storage in number of `byte`
Expand Down
1 change: 1 addition & 0 deletions core/service/blockCoreService.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type (
PushBlock(previousBlock, block *model.Block, broadcast, persist bool) error
GetBlockByID(id int64, withAttachedData bool) (*model.Block, error)
GetBlockByHeight(uint32) (*model.Block, error)
GetBlockByHeightCacheFormat(uint32) (*storage.BlockCacheObject, error)
GetBlocksFromHeight(startHeight, limit uint32, withAttachedData bool) ([]*model.Block, error)
GetLastBlock() (*model.Block, error)
GetLastBlockCacheFormat() (*storage.BlockCacheObject, error)
Expand Down
17 changes: 15 additions & 2 deletions core/service/blockMainService.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,15 @@ func (bs *BlockService) GetBlockByHeight(height uint32) (*model.Block, error) {
return block, nil
}

func (bs *BlockService) GetBlockByHeightCacheFormat(height uint32) (*storage.BlockCacheObject, error) {
return commonUtils.GetBlockByHeightUseBlocksCache(
height,
bs.QueryExecutor,
bs.BlockQuery,
bs.BlocksStorage,
)
}

// GetGenesisBlock return the last pushed block
func (bs *BlockService) GetGenesisBlock() (*model.Block, error) {
var (
Expand Down Expand Up @@ -1459,11 +1468,15 @@ func (bs *BlockService) ReceiveBlock(
if err != nil {
return nil, err
}

lastBlockCacheFormat := &storage.BlockCacheObject{
ID: lastBlock.ID,
Height: lastBlock.Height,
BlockHash: lastBlock.BlockHash,
}
// generate receipt and return as response
receipt, err := bs.ReceiptService.GenerateReceiptWithReminder(
bs.Chaintype, block.GetBlockHash(),
lastBlock,
lastBlockCacheFormat,
senderPublicKey,
nodeSecretPhrase,
constant.ReceiptDatumTypeBlock,
Expand Down
2 changes: 1 addition & 1 deletion core/service/blockMainService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2967,7 +2967,7 @@ type (
)

func (*mockReceiptServiceSuccess) GenerateReceiptWithReminder(
chaintype.ChainType, []byte, *model.Block, []byte, string, uint32,
chaintype.ChainType, []byte, *storage.BlockCacheObject, []byte, string, uint32,
) (*model.Receipt, error) {
return nil, nil
}
Expand Down
17 changes: 10 additions & 7 deletions core/service/blockSpineService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"bytes"
"database/sql"
"fmt"
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/signaturetype"
"math/big"
"sort"
"sync"

"github.com/zoobc/zoobc-core/common/accounttype"

log "github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/common/accounttype"
"github.com/zoobc/zoobc-core/common/blocker"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/monitoring"
"github.com/zoobc/zoobc-core/common/query"
"github.com/zoobc/zoobc-core/common/signaturetype"
"github.com/zoobc/zoobc-core/common/storage"
commonUtils "github.com/zoobc/zoobc-core/common/util"
"github.com/zoobc/zoobc-core/core/smith/strategy"
Expand Down Expand Up @@ -487,6 +486,10 @@ func (bs *BlockSpineService) GetBlockByHeight(height uint32) (*model.Block, erro
return block, nil
}

func (bs *BlockSpineService) GetBlockByHeightCacheFormat(height uint32) (*storage.BlockCacheObject, error) {
return nil, blocker.NewBlocker(blocker.AppErr, "GetBlockByHeightCacheFormat-NotImplementedYet")
}

// GetGenesisBlock return the genesis block
func (bs *BlockSpineService) GetGenesisBlock() (*model.Block, error) {
var (
Expand Down Expand Up @@ -615,7 +618,7 @@ func (bs *BlockSpineService) GenerateBlock(
newReferenceBlockHeight uint32
)
// select main block to be include in spine block
lastMainBlock, err := bs.MainBlockService.GetLastBlock()
lastMainBlock, err := bs.MainBlockService.GetLastBlockCacheFormat()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -918,7 +921,7 @@ func (bs *BlockSpineService) validateIncludedMainBlock(lastBlock, incomingBlock
if incomingBlock.ReferenceBlockHeight <= lastBlock.ReferenceBlockHeight {
return blocker.NewBlocker(blocker.ValidationErr, "InvalidReferenceBlockHeight")
}
var mainLastBlock, err = bs.MainBlockService.GetLastBlock()
var mainLastBlock, err = bs.MainBlockService.GetLastBlockCacheFormat()
if err != nil {
return err
}
Expand All @@ -928,7 +931,7 @@ func (bs *BlockSpineService) validateIncludedMainBlock(lastBlock, incomingBlock
}
var referenceBlock = mainLastBlock
if mainLastBlock.Height != incomingBlock.ReferenceBlockHeight {
referenceBlock, err = bs.MainBlockService.GetBlockByHeight(incomingBlock.ReferenceBlockHeight)
referenceBlock, err = bs.MainBlockService.GetBlockByHeightCacheFormat(incomingBlock.ReferenceBlockHeight)
if err != nil {
return err
}
Expand Down
26 changes: 15 additions & 11 deletions core/service/blockSpineService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/zoobc/zoobc-core/common/crypto"
"math/big"
"reflect"
"regexp"
Expand All @@ -15,6 +14,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/crypto"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
"github.com/zoobc/zoobc-core/common/storage"
Expand Down Expand Up @@ -1433,8 +1433,12 @@ var (
}
)

func (*mockSpineGenerateblockMainBlockServiceSuccess) GetLastBlock() (*model.Block, error) {
return &mockGenerateBlockMainBlock, nil
func (*mockSpineGenerateblockMainBlockServiceSuccess) GetLastBlockCacheFormat() (*storage.BlockCacheObject, error) {
return &storage.BlockCacheObject{
ID: mockGenerateBlockMainBlock.ID,
Height: mockGenerateBlockMainBlock.Height,
BlockHash: mockGenerateBlockMainBlock.BlockHash,
}, nil
}

func (*mockSpineGenerateblockMainBlockServiceSuccess) GetBlocksFromHeight(startHeight, limit uint32, withAttachedData bool) ([]*model.Block, error) {
Expand Down Expand Up @@ -2345,12 +2349,12 @@ type (
}
)

func (*mockReceiveBlockMainBlockServiceSuccess) GetBlockByHeight(uint32) (*model.Block, error) {
return &model.Block{}, nil
func (*mockReceiveBlockMainBlockServiceSuccess) GetBlockByHeightCacheFormat(uint32) (*storage.BlockCacheObject, error) {
return &storage.BlockCacheObject{}, nil
}

func (*mockReceiveBlockMainBlockServiceSuccess) GetLastBlock() (*model.Block, error) {
return &model.Block{}, nil
func (*mockReceiveBlockMainBlockServiceSuccess) GetLastBlockCacheFormat() (*storage.BlockCacheObject, error) {
return &storage.BlockCacheObject{}, nil
}

func TestBlockSpineService_ReceiveBlock(t *testing.T) {
Expand Down Expand Up @@ -2886,12 +2890,12 @@ type (
}
)

func (*mockValidateBlockMainBlockServiceSuccess) GetBlockByHeight(uint32) (*model.Block, error) {
return &model.Block{}, nil
func (*mockValidateBlockMainBlockServiceSuccess) GetBlockByHeightCacheFormat(uint32) (*storage.BlockCacheObject, error) {
return &storage.BlockCacheObject{}, nil
}

func (*mockValidateBlockMainBlockServiceSuccess) GetLastBlock() (*model.Block, error) {
return &model.Block{}, nil
func (*mockValidateBlockMainBlockServiceSuccess) GetLastBlockCacheFormat() (*storage.BlockCacheObject, error) {
return &storage.BlockCacheObject{}, nil
}

func (*mockSpineBlocksmithServiceValidateBlockSuccess) GetSortedBlocksmithsMap(*model.Block) map[string]*int64 {
Expand Down
Loading