diff --git a/core/store/ledgerstore/block_store.go b/core/store/ledgerstore/block_store.go index 77dd19a0..951d4d3d 100644 --- a/core/store/ledgerstore/block_store.go +++ b/core/store/ledgerstore/block_store.go @@ -23,15 +23,16 @@ import ( "encoding/binary" "fmt" + "io" + "github.com/polynetwork/poly/common" "github.com/polynetwork/poly/common/serialization" scom "github.com/polynetwork/poly/core/store/common" "github.com/polynetwork/poly/core/store/leveldbstore" "github.com/polynetwork/poly/core/types" - "io" ) -//Block store save the data of block & transaction +// Block store save the data of block & transaction type BlockStore struct { enableCache bool //Is enable lru cache dbDir string //The path of store file @@ -39,7 +40,7 @@ type BlockStore struct { store *leveldbstore.LevelDBStore //block store handler } -//NewBlockStore return the block store instance +// NewBlockStore return the block store instance func NewBlockStore(dbDir string, enableCache bool) (*BlockStore, error) { var cache *BlockCache var err error @@ -63,12 +64,12 @@ func NewBlockStore(dbDir string, enableCache bool) (*BlockStore, error) { return blockStore, nil } -//NewBatch start a commit batch +// NewBatch start a commit batch func (this *BlockStore) NewBatch() { this.store.NewBatch() } -//SaveBlock persist block to store +// SaveBlock persist block to store func (this *BlockStore) SaveBlock(block *types.Block) error { if this.enableCache { this.cache.AddBlock(block) @@ -89,7 +90,7 @@ func (this *BlockStore) SaveBlock(block *types.Block) error { return nil } -//ContainBlock return the block specified by block hash save in store +// ContainBlock return the block specified by block hash save in store func (this *BlockStore) ContainBlock(blockHash common.Uint256) (bool, error) { if this.enableCache { if this.cache.ContainBlock(blockHash) { @@ -107,7 +108,7 @@ func (this *BlockStore) ContainBlock(blockHash common.Uint256) (bool, error) { return true, nil } -//GetBlock return block by block hash +// GetBlock return block by block hash func (this *BlockStore) GetBlock(blockHash common.Uint256) (*types.Block, error) { var block *types.Block if this.enableCache { @@ -165,7 +166,7 @@ func (this *BlockStore) loadHeaderWithTx(blockHash common.Uint256) (*types.Heade return header, txHashes, nil } -//SaveHeader persist block header to store +// SaveHeader persist block header to store func (this *BlockStore) SaveHeader(block *types.Block) error { blockHash := block.Hash() key := this.getHeaderKey(blockHash) @@ -180,7 +181,7 @@ func (this *BlockStore) SaveHeader(block *types.Block) error { return nil } -//GetHeader return the header specified by block hash +// GetHeader return the header specified by block hash func (this *BlockStore) GetHeader(blockHash common.Uint256) (*types.Header, error) { if this.enableCache { block := this.cache.GetBlock(blockHash) @@ -206,7 +207,7 @@ func (this *BlockStore) loadHeader(blockHash common.Uint256) (*types.Header, err return header, nil } -//GetCurrentBlock return the current block hash and current block height +// GetCurrentBlock return the current block hash and current block height func (this *BlockStore) GetCurrentBlock() (common.Uint256, uint32, error) { key := this.getCurrentBlockKey() data, err := this.store.Get(key) @@ -226,7 +227,7 @@ func (this *BlockStore) GetCurrentBlock() (common.Uint256, uint32, error) { return blockHash, height, nil } -//SaveCurrentBlock persist the current block height and current block hash to store +// SaveCurrentBlock persist the current block height and current block hash to store func (this *BlockStore) SaveCurrentBlock(height uint32, blockHash common.Uint256) error { key := this.getCurrentBlockKey() value := bytes.NewBuffer(nil) @@ -236,52 +237,7 @@ func (this *BlockStore) SaveCurrentBlock(height uint32, blockHash common.Uint256 return nil } -//GetHeaderIndexList return the head index store in header index list -func (this *BlockStore) GetHeaderIndexList() (map[uint32]common.Uint256, error) { - result := make(map[uint32]common.Uint256) - iter := this.store.NewIterator([]byte{byte(scom.IX_HEADER_HASH_LIST)}) - defer iter.Release() - for iter.Next() { - startCount, err := this.getStartHeightByHeaderIndexKey(iter.Key()) - if err != nil { - return nil, fmt.Errorf("getStartHeightByHeaderIndexKey error %s", err) - } - reader := bytes.NewReader(iter.Value()) - count, err := serialization.ReadUint32(reader) - if err != nil { - return nil, fmt.Errorf("serialization.ReadUint32 count error %s", err) - } - for i := uint32(0); i < count; i++ { - height := startCount + i - blockHash := common.Uint256{} - err = blockHash.Deserialize(reader) - if err != nil { - return nil, fmt.Errorf("blockHash.Deserialize error %s", err) - } - result[height] = blockHash - } - } - if err := iter.Error(); err != nil { - return nil, err - } - return result, nil -} - -//SaveHeaderIndexList persist header index list to store -func (this *BlockStore) SaveHeaderIndexList(startIndex uint32, indexList []common.Uint256) error { - indexKey := this.getHeaderIndexListKey(startIndex) - indexSize := uint32(len(indexList)) - value := bytes.NewBuffer(nil) - serialization.WriteUint32(value, indexSize) - for _, hash := range indexList { - hash.Serialize(value) - } - - this.store.BatchPut(indexKey, value.Bytes()) - return nil -} - -//GetBlockHash return block hash by block height +// GetBlockHash return block hash by block height func (this *BlockStore) GetBlockHash(height uint32) (common.Uint256, error) { key := this.getBlockHashKey(height) value, err := this.store.Get(key) @@ -295,13 +251,13 @@ func (this *BlockStore) GetBlockHash(height uint32) (common.Uint256, error) { return blockHash, nil } -//SaveBlockHash persist block height and block hash to store +// SaveBlockHash persist block height and block hash to store func (this *BlockStore) SaveBlockHash(height uint32, blockHash common.Uint256) { key := this.getBlockHashKey(height) this.store.BatchPut(key, blockHash.ToArray()) } -//SaveTransaction persist transaction to store +// SaveTransaction persist transaction to store func (this *BlockStore) SaveTransaction(tx *types.Transaction, height uint32) error { if this.enableCache { this.cache.AddTransaction(tx, height) @@ -323,7 +279,7 @@ func (this *BlockStore) putTransaction(tx *types.Transaction, height uint32) err return nil } -//GetTransaction return transaction by transaction hash +// GetTransaction return transaction by transaction hash func (this *BlockStore) GetTransaction(txHash common.Uint256) (*types.Transaction, uint32, error) { if this.enableCache { tx, height := this.cache.GetTransaction(txHash) @@ -364,7 +320,7 @@ func (this *BlockStore) loadTransaction(txHash common.Uint256) (*types.Transacti return tx, height, nil } -//IsContainTransaction return whether the transaction is in store +// IsContainTransaction return whether the transaction is in store func (this *BlockStore) ContainTransaction(txHash common.Uint256) (bool, error) { key := this.getTransactionKey(txHash) @@ -383,7 +339,7 @@ func (this *BlockStore) ContainTransaction(txHash common.Uint256) (bool, error) return true, nil } -//GetVersion return the version of store +// GetVersion return the version of store func (this *BlockStore) GetVersion() (byte, error) { key := this.getVersionKey() value, err := this.store.Get(key) @@ -394,13 +350,13 @@ func (this *BlockStore) GetVersion() (byte, error) { return reader.ReadByte() } -//SaveVersion persist version to store +// SaveVersion persist version to store func (this *BlockStore) SaveVersion(ver byte) error { key := this.getVersionKey() return this.store.Put(key, []byte{ver}) } -//ClearAll clear all the data of block store +// ClearAll clear all the data of block store func (this *BlockStore) ClearAll() error { this.NewBatch() iter := this.store.NewIterator(nil) @@ -414,12 +370,12 @@ func (this *BlockStore) ClearAll() error { return this.CommitTo() } -//CommitTo commit the batch to store +// CommitTo commit the batch to store func (this *BlockStore) CommitTo() error { return this.store.BatchCommit() } -//Close block store +// Close block store func (this *BlockStore) Close() error { return this.store.Close() } @@ -457,19 +413,3 @@ func (this *BlockStore) getBlockMerkleTreeKey() []byte { func (this *BlockStore) getVersionKey() []byte { return []byte{byte(scom.SYS_VERSION)} } - -func (this *BlockStore) getHeaderIndexListKey(startHeight uint32) []byte { - key := bytes.NewBuffer(nil) - key.WriteByte(byte(scom.IX_HEADER_HASH_LIST)) - serialization.WriteUint32(key, startHeight) - return key.Bytes() -} - -func (this *BlockStore) getStartHeightByHeaderIndexKey(key []byte) (uint32, error) { - reader := bytes.NewReader(key[1:]) - height, err := serialization.ReadUint32(reader) - if err != nil { - return 0, err - } - return height, nil -} diff --git a/core/store/ledgerstore/block_store_test.go b/core/store/ledgerstore/block_store_test.go index f46490b9..6b7972ec 100644 --- a/core/store/ledgerstore/block_store_test.go +++ b/core/store/ledgerstore/block_store_test.go @@ -21,13 +21,14 @@ package ledgerstore import ( "crypto/sha256" "fmt" + "testing" + "time" + "github.com/ontio/ontology-crypto/keypair" "github.com/polynetwork/poly/account" "github.com/polynetwork/poly/common" "github.com/polynetwork/poly/core/payload" "github.com/polynetwork/poly/core/types" - "testing" - "time" ) func TestVersion(t *testing.T) { @@ -165,55 +166,6 @@ func TestSaveTransaction(t *testing.T) { } } -func TestHeaderIndexList(t *testing.T) { - testBlockStore.NewBatch() - startHeight := uint32(0) - size := uint32(100) - indexMap := make(map[uint32]common.Uint256, size) - indexList := make([]common.Uint256, 0) - for i := startHeight; i < size; i++ { - hash := common.Uint256(sha256.Sum256([]byte(fmt.Sprintf("%v", i)))) - indexMap[i] = hash - indexList = append(indexList, hash) - } - err := testBlockStore.SaveHeaderIndexList(startHeight, indexList) - if err != nil { - t.Errorf("SaveHeaderIndexList error %s", err) - return - } - startHeight = uint32(100) - size = uint32(100) - indexMap = make(map[uint32]common.Uint256, size) - for i := startHeight; i < size; i++ { - hash := common.Uint256(sha256.Sum256([]byte(fmt.Sprintf("%v", i)))) - indexMap[i] = hash - indexList = append(indexList, hash) - } - err = testBlockStore.CommitTo() - if err != nil { - t.Errorf("CommitTo error %s", err) - return - } - - totalMap, err := testBlockStore.GetHeaderIndexList() - if err != nil { - t.Errorf("GetHeaderIndexList error %s", err) - return - } - - for height, hash := range indexList { - h, ok := totalMap[uint32(height)] - if !ok { - t.Errorf("TestHeaderIndexList failed height:%d hash not exist", height) - return - } - if hash != h { - t.Errorf("TestHeaderIndexList failed height:%d hash %x != %x", height, hash, h) - return - } - } -} - func TestSaveHeader(t *testing.T) { acc1 := account.NewAccount("") acc2 := account.NewAccount("") diff --git a/core/store/ledgerstore/header_Index_cache.go b/core/store/ledgerstore/header_Index_cache.go new file mode 100644 index 00000000..0bd5de8b --- /dev/null +++ b/core/store/ledgerstore/header_Index_cache.go @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2021 The poly network Authors + * This file is part of The poly network library. + * + * The poly network is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The poly network is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the poly network. If not, see . + */ + +// Header index cache +package ledgerstore + +import "github.com/polynetwork/poly/common" + +const ( + HEADER_INDEX_MAX_SIZE = uint32(2000) //Max size of saving header index to cache +) + +type HeaderIndexCache struct { + headerIndex map[uint32]common.Uint256 //Header index, Mapping header height => block hash + firstIndex uint32 //First index of cache + lastIndex uint32 //Last index of cache +} + +func NewHeaderIndexCache() *HeaderIndexCache { + return &HeaderIndexCache{ + headerIndex: make(map[uint32]common.Uint256), + } +} + +func (this *HeaderIndexCache) setHeaderIndex(curBlockHeight uint32, curHeaderHeight uint32, blockHash common.Uint256) { + this.headerIndex[curHeaderHeight] = blockHash + if this.getLastIndex() < curHeaderHeight { + this.lastIndex = curHeaderHeight + } + if this.getFirstIndex() < curBlockHeight { + cacheSize := curBlockHeight - this.getFirstIndex() + 1 + for height := this.getFirstIndex(); cacheSize > HEADER_INDEX_MAX_SIZE; cacheSize-- { + this.delHeaderIndex(height) + height++ + this.firstIndex = height + } + } +} + +func (this *HeaderIndexCache) delHeaderIndex(height uint32) { + delete(this.headerIndex, height) +} + +func (this *HeaderIndexCache) getHeaderIndex(height uint32) common.Uint256 { + blockHash, ok := this.headerIndex[height] + if !ok { + return common.Uint256{} + } + return blockHash +} + +func (this *HeaderIndexCache) setLastIndex(lastIndex uint32) { + this.lastIndex = lastIndex +} + +func (this *HeaderIndexCache) getLastIndex() uint32 { + return this.lastIndex +} + +func (this *HeaderIndexCache) setFirstIndex(firstIndex uint32) { + this.firstIndex = firstIndex +} + +func (this *HeaderIndexCache) getFirstIndex() uint32 { + return this.firstIndex +} + +func (this *HeaderIndexCache) getCurrentHeaderHash() common.Uint256 { + blockHash, ok := this.headerIndex[this.getLastIndex()] + if !ok { + return common.Uint256{} + } + return blockHash +} diff --git a/core/store/ledgerstore/ledger_store.go b/core/store/ledgerstore/ledger_store.go index 3a5afd11..d0a08ba1 100644 --- a/core/store/ledgerstore/ledger_store.go +++ b/core/store/ledgerstore/ledger_store.go @@ -62,7 +62,7 @@ var ( MerkleTreeStorePath = "merkle_tree.db" ) -//LedgerStoreImp is main store struct fo ledger +// LedgerStoreImp is main store struct fo ledger type LedgerStoreImp struct { blockStore *BlockStore //BlockStore for saving block & transaction data stateStore *StateStore //StateStore for saving state data, like balance, smart contract execution result, and so on. @@ -71,17 +71,16 @@ type LedgerStoreImp struct { currBlockHeight uint32 //Current block height currBlockHash common.Uint256 //Current block hash headerCache map[common.Uint256]*types.Header //BlockHash => Header - headerIndex map[uint32]common.Uint256 //Header index, Mapping header height => block hash + headerIndexCache *HeaderIndexCache //Header index cache, Mapping header height => block hash savingBlockSemaphore chan bool vbftPeerInfoheader map[string]uint32 //pubInfo save pubkey,peerindex vbftPeerInfoblock map[string]uint32 //pubInfo save pubkey,peerindex lock sync.RWMutex } -//NewLedgerStore return LedgerStoreImp instance +// NewLedgerStore return LedgerStoreImp instance func NewLedgerStore(dataDir string) (*LedgerStoreImp, error) { ledgerStore := &LedgerStoreImp{ - headerIndex: make(map[uint32]common.Uint256), headerCache: make(map[common.Uint256]*types.Header, 0), vbftPeerInfoheader: make(map[string]uint32), vbftPeerInfoblock: make(map[string]uint32), @@ -107,11 +106,12 @@ func NewLedgerStore(dataDir string) (*LedgerStoreImp, error) { return nil, fmt.Errorf("NewEventStore error %s", err) } ledgerStore.eventStore = eventState + ledgerStore.headerIndexCache = NewHeaderIndexCache() return ledgerStore, nil } -//InitLedgerStoreWithGenesisBlock init the ledger store with genesis block. It's the first operation after NewLedgerStore. +// InitLedgerStoreWithGenesisBlock init the ledger store with genesis block. It's the first operation after NewLedgerStore. func (this *LedgerStoreImp) InitLedgerStoreWithGenesisBlock(genesisBlock *types.Block, defaultBookkeeper []keypair.PublicKey) error { hasInit, err := this.hasAlreadyInitGenesisBlock() if err != nil { @@ -249,24 +249,21 @@ func (this *LedgerStoreImp) loadCurrentBlock() error { func (this *LedgerStoreImp) loadHeaderIndexList() error { currBlockHeight := this.GetCurrentBlockHeight() - headerIndex, err := this.blockStore.GetHeaderIndexList() - if err != nil { - return fmt.Errorf("LoadHeaderIndexList error %s", err) + var height uint32 + if currBlockHeight+1 > HEADER_INDEX_MAX_SIZE { + height = currBlockHeight - HEADER_INDEX_MAX_SIZE + 1 } - storeIndexCount := uint32(len(headerIndex)) - this.headerIndex = headerIndex - this.storedIndexCount = storeIndexCount - - for i := storeIndexCount; i <= currBlockHeight; i++ { - height := i - blockHash, err := this.blockStore.GetBlockHash(height) + this.headerIndexCache.setFirstIndex(height) + this.headerIndexCache.setLastIndex(height) + for i := height; i <= currBlockHeight; i++ { + blockHash, err := this.blockStore.GetBlockHash(i) if err != nil { - return fmt.Errorf("LoadBlockHash height %d error %s", height, err) + return fmt.Errorf("LoadBlockHash height %d error %s", i, err) } if blockHash == common.UINT256_EMPTY { - return fmt.Errorf("LoadBlockHash height %d hash nil", height) + return fmt.Errorf("LoadBlockHash height %d hash nil", i) } - this.headerIndex[height] = blockHash + this.headerIndexCache.setHeaderIndex(currBlockHeight, i, blockHash) } return nil } @@ -316,40 +313,43 @@ func (this *LedgerStoreImp) recoverStore() error { func (this *LedgerStoreImp) setHeaderIndex(height uint32, blockHash common.Uint256) { this.lock.Lock() defer this.lock.Unlock() - this.headerIndex[height] = blockHash + this.headerIndexCache.setHeaderIndex(this.currBlockHeight, height, blockHash) } func (this *LedgerStoreImp) getHeaderIndex(height uint32) common.Uint256 { this.lock.RLock() defer this.lock.RUnlock() - blockHash, ok := this.headerIndex[height] - if !ok { - return common.Uint256{} + var blockHash common.Uint256 + if blockHash = this.headerIndexCache.getHeaderIndex(height); blockHash == common.UINT256_EMPTY { + var err error + if blockHash, err = this.blockStore.GetBlockHash(height); err != nil { + return common.Uint256{} + } } return blockHash } -//GetCurrentHeaderHeight return the current header height. -//In block sync states, Header height is usually higher than block height that is has already committed to storage +// GetCurrentHeaderHeight return the current header height. +// In block sync states, Header height is usually higher than block height that is has already committed to storage func (this *LedgerStoreImp) GetCurrentHeaderHeight() uint32 { this.lock.RLock() defer this.lock.RUnlock() - size := len(this.headerIndex) - if size == 0 { - return 0 + idx := this.headerIndexCache.getLastIndex() + if idx == 0 { + return this.currBlockHeight } - return uint32(size) - 1 + return idx } -//GetCurrentHeaderHash return the current header hash. The current header means the latest header. +// GetCurrentHeaderHash return the current header hash. The current header means the latest header. func (this *LedgerStoreImp) GetCurrentHeaderHash() common.Uint256 { this.lock.RLock() defer this.lock.RUnlock() - size := len(this.headerIndex) - if size == 0 { - return common.Uint256{} + var currentHeaderHash common.Uint256 + if currentHeaderHash = this.headerIndexCache.getCurrentHeaderHash(); currentHeaderHash == common.UINT256_EMPTY { + return this.currBlockHash } - return this.headerIndex[uint32(size)-1] + return currentHeaderHash } func (this *LedgerStoreImp) setCurrentBlock(height uint32, blockHash common.Uint256) { @@ -360,22 +360,22 @@ func (this *LedgerStoreImp) setCurrentBlock(height uint32, blockHash common.Uint return } -//GetCurrentBlock return the current block height, and block hash. -//Current block means the latest block in store. +// GetCurrentBlock return the current block height, and block hash. +// Current block means the latest block in store. func (this *LedgerStoreImp) GetCurrentBlock() (uint32, common.Uint256) { this.lock.RLock() defer this.lock.RUnlock() return this.currBlockHeight, this.currBlockHash } -//GetCurrentBlockHash return the current block hash +// GetCurrentBlockHash return the current block hash func (this *LedgerStoreImp) GetCurrentBlockHash() common.Uint256 { this.lock.RLock() defer this.lock.RUnlock() return this.currBlockHash } -//GetCurrentBlockHeight return the current block height +// GetCurrentBlockHeight return the current block height func (this *LedgerStoreImp) GetCurrentBlockHeight() uint32 { this.lock.RLock() defer this.lock.RUnlock() @@ -483,7 +483,7 @@ func (this *LedgerStoreImp) verifyHeader(header *types.Header, vbftPeerInfo map[ return vbftPeerInfo, nil } -//AddHeader add header to cache, and add the mapping of block height to block hash. Using in block sync +// AddHeader add header to cache, and add the mapping of block height to block hash. Using in block sync func (this *LedgerStoreImp) AddHeader(header *types.Header) error { nextHeaderHeight := this.GetCurrentHeaderHeight() + 1 if header.Height != nextHeaderHeight { @@ -499,7 +499,7 @@ func (this *LedgerStoreImp) AddHeader(header *types.Header) error { return nil } -//AddHeaders bath add header. +// AddHeaders bath add header. func (this *LedgerStoreImp) AddHeaders(headers []*types.Header) error { sort.Slice(headers, func(i, j int) bool { return headers[i].Height < headers[j].Height @@ -566,8 +566,8 @@ func (this *LedgerStoreImp) SubmitBlock(block *types.Block, result store.Execute return nil } -//AddBlock add the block to store. -//When the block is not the next block, it will be cache. until the missing block arrived +// AddBlock add the block to store. +// When the block is not the next block, it will be cache. until the missing block arrived func (this *LedgerStoreImp) AddBlock(block *types.Block, stateMerkleRoot common.Uint256) error { currBlockHeight := this.GetCurrentBlockHeight() blockHeight := block.Header.Height @@ -613,11 +613,7 @@ func (this *LedgerStoreImp) saveBlockToBlockStore(block *types.Block) error { blockHeight := block.Header.Height this.setHeaderIndex(blockHeight, blockHash) - err := this.saveHeaderIndexList() - if err != nil { - return fmt.Errorf("saveHeaderIndexList error %s", err) - } - err = this.blockStore.SaveCurrentBlock(blockHeight, blockHash) + err := this.blockStore.SaveCurrentBlock(blockHeight, blockHash) if err != nil { return fmt.Errorf("SaveCurrentBlock error %s", err) } @@ -741,7 +737,7 @@ func (this *LedgerStoreImp) releaseSavingBlockLock() { } } -//saveBlock do the job of execution samrt contract and commit block to store. +// saveBlock do the job of execution samrt contract and commit block to store. func (this *LedgerStoreImp) submitBlock(block *types.Block, result store.ExecuteResult) error { blockHash := block.Hash() blockHeight := block.Header.Height @@ -791,7 +787,7 @@ func (this *LedgerStoreImp) submitBlock(block *types.Block, result store.Execute return nil } -//saveBlock do the job of execution samrt contract and commit block to store. +// saveBlock do the job of execution samrt contract and commit block to store. func (this *LedgerStoreImp) saveBlock(block *types.Block, stateMerkleRoot common.Uint256) error { blockHeight := block.Header.Height if this.tryGetSavingBlockLock() { @@ -832,33 +828,6 @@ func (this *LedgerStoreImp) handleTransaction(overlay *overlaydb.OverlayDB, cach } } -func (this *LedgerStoreImp) saveHeaderIndexList() error { - this.lock.RLock() - storeCount := this.storedIndexCount - currHeight := this.currBlockHeight - if currHeight-storeCount < HEADER_INDEX_BATCH_SIZE { - this.lock.RUnlock() - return nil - } - - headerList := make([]common.Uint256, HEADER_INDEX_BATCH_SIZE) - for i := uint32(0); i < HEADER_INDEX_BATCH_SIZE; i++ { - height := storeCount + i - headerList[i] = this.headerIndex[height] - } - this.lock.RUnlock() - - err := this.blockStore.SaveHeaderIndexList(storeCount, headerList) - if err != nil { - return fmt.Errorf("SaveHeaderIndexList start %d error %s", storeCount, err) - } - - this.lock.Lock() - this.storedIndexCount += HEADER_INDEX_BATCH_SIZE - this.lock.Unlock() - return nil -} - func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*cstates.PreExecResult, error) { result := &sstate.PreExecResult{State: event.CONTRACT_STATE_FAIL, Result: nil} if _, ok := tx.Payload.(*payload.InvokeCode); !ok { @@ -884,12 +853,12 @@ func (this *LedgerStoreImp) PreExecuteContract(tx *types.Transaction) (*cstates. return &sstate.PreExecResult{State: event.CONTRACT_STATE_SUCCESS, Result: common.ToHexString(res.([]byte)), Notify: service.GetNotify()}, nil } -//IsContainBlock return whether the block is in store +// IsContainBlock return whether the block is in store func (this *LedgerStoreImp) IsContainBlock(blockHash common.Uint256) (bool, error) { return this.blockStore.ContainBlock(blockHash) } -//IsContainTransaction return whether the transaction is in store. Wrap function of BlockStore.ContainTransaction +// IsContainTransaction return whether the transaction is in store. Wrap function of BlockStore.ContainTransaction func (this *LedgerStoreImp) IsContainTransaction(txHash common.Uint256) (bool, error) { return this.blockStore.ContainTransaction(txHash) } @@ -909,12 +878,12 @@ func (this *LedgerStoreImp) GetBlockRootWithPreBlockHashes(startHeight uint32, p return this.stateStore.GetBlockRootWithPreBlockHashes(needs) } -//GetBlockHash return the block hash by block height +// GetBlockHash return the block hash by block height func (this *LedgerStoreImp) GetBlockHash(height uint32) common.Uint256 { return this.getHeaderIndex(height) } -//GetHeaderByHash return the block header by block hash +// GetHeaderByHash return the block header by block hash func (this *LedgerStoreImp) GetHeaderByHash(blockHash common.Uint256) (*types.Header, error) { header := this.getHeaderCache(blockHash) if header != nil { @@ -923,7 +892,7 @@ func (this *LedgerStoreImp) GetHeaderByHash(blockHash common.Uint256) (*types.He return this.blockStore.GetHeader(blockHash) } -//GetHeaderByHash return the block header by block height +// GetHeaderByHash return the block header by block height func (this *LedgerStoreImp) GetHeaderByHeight(height uint32) (*types.Header, error) { blockHash := this.GetBlockHash(height) var empty common.Uint256 @@ -933,17 +902,17 @@ func (this *LedgerStoreImp) GetHeaderByHeight(height uint32) (*types.Header, err return this.GetHeaderByHash(blockHash) } -//GetTransaction return transaction by transaction hash. Wrap function of BlockStore.GetTransaction +// GetTransaction return transaction by transaction hash. Wrap function of BlockStore.GetTransaction func (this *LedgerStoreImp) GetTransaction(txHash common.Uint256) (*types.Transaction, uint32, error) { return this.blockStore.GetTransaction(txHash) } -//GetBlockByHash return block by block hash. Wrap function of BlockStore.GetBlockByHash +// GetBlockByHash return block by block hash. Wrap function of BlockStore.GetBlockByHash func (this *LedgerStoreImp) GetBlockByHash(blockHash common.Uint256) (*types.Block, error) { return this.blockStore.GetBlock(blockHash) } -//GetBlockByHeight return block by height. +// GetBlockByHeight return block by height. func (this *LedgerStoreImp) GetBlockByHeight(height uint32) (*types.Block, error) { blockHash := this.GetBlockHash(height) var empty common.Uint256 @@ -953,32 +922,32 @@ func (this *LedgerStoreImp) GetBlockByHeight(height uint32) (*types.Block, error return this.GetBlockByHash(blockHash) } -//GetBookkeeperState return the bookkeeper state. Wrap function of StateStore.GetBookkeeperState +// GetBookkeeperState return the bookkeeper state. Wrap function of StateStore.GetBookkeeperState func (this *LedgerStoreImp) GetBookkeeperState() (*states.BookkeeperState, error) { return this.stateStore.GetBookkeeperState() } -//GetMerkleProof return the block merkle proof. Wrap function of StateStore.GetMerkleProof +// GetMerkleProof return the block merkle proof. Wrap function of StateStore.GetMerkleProof func (this *LedgerStoreImp) GetMerkleProof(raw []byte, proofHeight, rootHeight uint32) ([]byte, error) { return this.stateStore.GetMerkleProof(raw, proofHeight, rootHeight) } -//GetStorageItem return the storage value of the key in smart contract. Wrap function of StateStore.GetStorageState +// GetStorageItem return the storage value of the key in smart contract. Wrap function of StateStore.GetStorageState func (this *LedgerStoreImp) GetStorageItem(key *states.StorageKey) (*states.StorageItem, error) { return this.stateStore.GetStorageState(key) } -//GetEventNotifyByTx return the events notify gen by executing of smart contract. Wrap function of EventStore.GetEventNotifyByTx +// GetEventNotifyByTx return the events notify gen by executing of smart contract. Wrap function of EventStore.GetEventNotifyByTx func (this *LedgerStoreImp) GetEventNotifyByTx(tx common.Uint256) (*event.ExecuteNotify, error) { return this.eventStore.GetEventNotifyByTx(tx) } -//GetEventNotifyByBlock return the transaction hash which have event notice after execution of smart contract. Wrap function of EventStore.GetEventNotifyByBlock +// GetEventNotifyByBlock return the transaction hash which have event notice after execution of smart contract. Wrap function of EventStore.GetEventNotifyByBlock func (this *LedgerStoreImp) GetEventNotifyByBlock(height uint32) ([]*event.ExecuteNotify, error) { return this.eventStore.GetEventNotifyByBlock(height) } -//Close ledger store. +// Close ledger store. func (this *LedgerStoreImp) Close() error { err := this.blockStore.Close() if err != nil {