This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#32] Add CouchDB implementation for block storage
Signed-off-by: Firas Qutishat <[email protected]> Signed-off-by: sudesh.shetty <[email protected]>
- Loading branch information
1 parent
2f9256a
commit cbafe5d
Showing
26 changed files
with
2,298 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package cdbblkstorage | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/hyperledger/fabric/protos/common" | ||
"github.com/hyperledger/fabric/protoutil" | ||
) | ||
|
||
func extractTxIDFromEnvelope(txEnvelope *common.Envelope) (string, error) { | ||
payload, err := protoutil.GetPayload(txEnvelope) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
payloadHeader := payload.Header | ||
channelHeader, err := protoutil.UnmarshalChannelHeader(payloadHeader.ChannelHeader) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return channelHeader.TxId, nil | ||
} | ||
|
||
func extractTxnEnvelopeFromBlock(block *common.Block, txID string) (*common.Envelope, error) { | ||
blockData := block.GetData() | ||
for _, txEnvelopeBytes := range blockData.GetData() { | ||
envelope, err := protoutil.GetEnvelopeFromBlock(txEnvelopeBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
id, err := extractTxIDFromEnvelope(envelope) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if id != txID { | ||
continue | ||
} | ||
|
||
txEnvelope, err := protoutil.GetEnvelopeFromBlock(txEnvelopeBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return txEnvelope, nil | ||
} | ||
|
||
return nil, errors.Errorf("transaction not found [%s]", txID) | ||
} | ||
|
||
func extractEnvelopeFromBlock(block *common.Block, tranNum uint64) (*common.Envelope, error) { | ||
blockData := block.GetData() | ||
envelopes := blockData.GetData() | ||
envelopesLen := uint64(len(envelopes)) | ||
if envelopesLen-1 < tranNum { | ||
blockNum := block.GetHeader().GetNumber() | ||
return nil, errors.Errorf("transaction number is invalid [%d, %d, %d]", blockNum, envelopesLen, tranNum) | ||
} | ||
return protoutil.GetEnvelopeFromBlock(envelopes[tranNum]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package cdbblkstorage | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/hyperledger/fabric/common/ledger" | ||
) | ||
|
||
// blocksItr - an iterator for iterating over a sequence of blocks | ||
type blocksItr struct { | ||
cdbBlockStore *cdbBlockStore | ||
maxBlockNumAvailable uint64 | ||
blockNumToRetrieve uint64 | ||
closeMarker bool | ||
closeMarkerLock *sync.Mutex | ||
} | ||
|
||
func newBlockItr(cdbBlockStore *cdbBlockStore, startBlockNum uint64) *blocksItr { | ||
return &blocksItr{cdbBlockStore, cdbBlockStore.cpInfo.lastBlockNumber, startBlockNum, false, &sync.Mutex{}} | ||
} | ||
|
||
func (itr *blocksItr) waitForBlock(blockNum uint64) uint64 { | ||
itr.cdbBlockStore.cpInfoCond.L.Lock() | ||
defer itr.cdbBlockStore.cpInfoCond.L.Unlock() | ||
for itr.cdbBlockStore.cpInfo.lastBlockNumber < blockNum && !itr.shouldClose() { | ||
logger.Debugf("Going to wait for newer blocks. maxAvailaBlockNumber=[%d], waitForBlockNum=[%d]", | ||
itr.cdbBlockStore.cpInfo.lastBlockNumber, blockNum) | ||
itr.cdbBlockStore.cpInfoCond.Wait() | ||
logger.Debugf("Came out of wait. maxAvailaBlockNumber=[%d]", itr.cdbBlockStore.cpInfo.lastBlockNumber) | ||
} | ||
return itr.cdbBlockStore.cpInfo.lastBlockNumber | ||
} | ||
|
||
func (itr *blocksItr) shouldClose() bool { | ||
itr.closeMarkerLock.Lock() | ||
defer itr.closeMarkerLock.Unlock() | ||
return itr.closeMarker | ||
} | ||
|
||
// Next moves the cursor to next block and returns true iff the iterator is not exhausted | ||
func (itr *blocksItr) Next() (ledger.QueryResult, error) { | ||
if itr.maxBlockNumAvailable < itr.blockNumToRetrieve { | ||
itr.maxBlockNumAvailable = itr.waitForBlock(itr.blockNumToRetrieve) | ||
} | ||
itr.closeMarkerLock.Lock() | ||
defer itr.closeMarkerLock.Unlock() | ||
if itr.closeMarker { | ||
return nil, nil | ||
} | ||
|
||
nextBlock, err := itr.cdbBlockStore.RetrieveBlockByNumber(itr.blockNumToRetrieve) | ||
if err != nil { | ||
return nil, err | ||
} | ||
itr.blockNumToRetrieve++ | ||
return nextBlock, nil | ||
} | ||
|
||
// Close releases any resources held by the iterator | ||
func (itr *blocksItr) Close() { | ||
itr.cdbBlockStore.cpInfoCond.L.Lock() | ||
defer itr.cdbBlockStore.cpInfoCond.L.Unlock() | ||
itr.closeMarkerLock.Lock() | ||
defer itr.closeMarkerLock.Unlock() | ||
itr.closeMarker = true | ||
itr.cdbBlockStore.cpInfoCond.Broadcast() | ||
} |
Oops, something went wrong.