本文整理匯總了Golang中github.com/hyperledger/fabric/core/ledger/statemgmt.NewStateDelta函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewStateDelta函數的具體用法?Golang NewStateDelta怎麽用?Golang NewStateDelta使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewStateDelta函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestStateSnapshotIterator
func TestStateSnapshotIterator(t *testing.T) {
testDBWrapper.CreateFreshDB(t)
stateTrieTestWrapper := newStateTrieTestWrapper(t)
stateTrie := stateTrieTestWrapper.stateTrie
stateDelta := statemgmt.NewStateDelta()
// insert keys
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
stateDelta.Set("chaincodeID3", "key3", []byte("value3"), nil)
stateDelta.Set("chaincodeID4", "key4", []byte("value4"), nil)
stateDelta.Set("chaincodeID5", "key5", []byte("value5"), nil)
stateDelta.Set("chaincodeID6", "key6", []byte("value6"), nil)
stateTrie.PrepareWorkingSet(stateDelta)
stateTrieTestWrapper.PersistChangesAndResetInMemoryChanges()
//check that the key is persisted
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID1", "key1"), []byte("value1"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID2", "key2"), []byte("value2"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID3", "key3"), []byte("value3"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID4", "key4"), []byte("value4"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID5", "key5"), []byte("value5"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID6", "key6"), []byte("value6"))
// take db snapeshot
dbSnapshot := db.GetDBHandle().GetSnapshot()
stateDelta1 := statemgmt.NewStateDelta()
// delete a few keys
stateDelta1.Delete("chaincodeID1", "key1", nil)
stateDelta1.Delete("chaincodeID3", "key3", nil)
stateDelta1.Delete("chaincodeID4", "key4", nil)
stateDelta1.Delete("chaincodeID6", "key6", nil)
// update remaining keys
stateDelta1.Set("chaincodeID2", "key2", []byte("value2_new"), nil)
stateDelta1.Set("chaincodeID5", "key5", []byte("value5_new"), nil)
stateTrie.PrepareWorkingSet(stateDelta1)
stateTrieTestWrapper.PersistChangesAndResetInMemoryChanges()
//check that the keys are updated
testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID1", "key1"))
testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID3", "key3"))
testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID4", "key4"))
testutil.AssertNil(t, stateTrieTestWrapper.Get("chaincodeID6", "key6"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID2", "key2"), []byte("value2_new"))
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID5", "key5"), []byte("value5_new"))
itr, err := newStateSnapshotIterator(dbSnapshot)
testutil.AssertNoError(t, err, "Error while getting state snapeshot iterator")
stateDeltaFromSnapshot := statemgmt.NewStateDelta()
for itr.Next() {
keyBytes, valueBytes := itr.GetRawKeyValue()
t.Logf("key=[%s], value=[%s]", string(keyBytes), string(valueBytes))
chaincodeID, key := statemgmt.DecodeCompositeKey(keyBytes)
stateDeltaFromSnapshot.Set(chaincodeID, key, valueBytes, nil)
}
testutil.AssertEquals(t, stateDelta, stateDeltaFromSnapshot)
}
示例2: TestRangeScanIteratorEmptyArray
func TestRangeScanIteratorEmptyArray(t *testing.T) {
testDBWrapper.CreateFreshDB(t)
stateImplTestWrapper := newStateImplTestWrapper(t)
stateDelta := statemgmt.NewStateDelta()
// insert keys
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID1", "key2", []byte{}, nil)
stateDelta.Set("chaincodeID1", "key3", []byte{}, nil)
stateImplTestWrapper.prepareWorkingSet(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
// test range scan for chaincodeID2
rangeScanItr := stateImplTestWrapper.getRangeScanIterator("chaincodeID1", "key1", "key3")
var results = make(map[string][]byte)
for rangeScanItr.Next() {
key, value := rangeScanItr.GetKeyValue()
results[key] = value
}
t.Logf("Results = %s", results)
testutil.AssertEquals(t, len(results), 3)
testutil.AssertEquals(t, results["key3"], []byte{})
rangeScanItr.Close()
}
示例3: TestStateTrie_ComputeHash_AllInMemory_NoContents
func TestStateTrie_ComputeHash_AllInMemory_NoContents(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
hash := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(statemgmt.NewStateDelta())
testutil.AssertEquals(t, hash, nil)
}
示例4: NewState
// NewState constructs a new State. This Initializes encapsulated state implementation
func NewState() *State {
initConfig()
logger.Infof("Initializing state implementation [%s]", stateImplName)
switch stateImplName {
case buckettreeType:
stateImpl = buckettree.NewStateImpl()
case trieType:
stateImpl = trie.NewStateImpl()
case rawType:
stateImpl = raw.NewStateImpl()
default:
panic("Should not reach here. Configs should have checked for the stateImplName being a valid names ")
}
err := stateImpl.Initialize(stateImplConfigs)
if err != nil {
panic(fmt.Errorf("Error during initialization of state implementation: %s", err))
}
return &State{stateImpl, statemgmt.NewStateDelta(), statemgmt.NewStateDelta(), "", make(map[string][]byte),
false, uint64(deltaHistorySize)}
}
示例5: FetchStateDeltaFromDB
// FetchStateDeltaFromDB fetches the StateDelta corrsponding to given blockNumber
func (state *State) FetchStateDeltaFromDB(blockNumber uint64) (*statemgmt.StateDelta, error) {
stateDeltaBytes, err := db.GetDBHandle().GetFromStateDeltaCF(encodeStateDeltaKey(blockNumber))
if err != nil {
return nil, err
}
if stateDeltaBytes == nil {
return nil, nil
}
stateDelta := statemgmt.NewStateDelta()
stateDelta.Unmarshal(stateDeltaBytes)
return stateDelta, nil
}
示例6: TestStateTrie_GetSet_WithDB
func TestStateTrie_GetSet_WithDB(t *testing.T) {
testDBWrapper.CreateFreshDB(t)
stateTrie := NewStateTrie()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
stateDelta := statemgmt.NewStateDelta()
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID1", "key2", []byte("value2"), nil)
stateDelta.Set("chaincodeID2", "key3", []byte("value3"), nil)
stateDelta.Set("chaincodeID2", "key4", []byte("value4"), nil)
stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(stateDelta)
stateTrieTestWrapper.PersistChangesAndResetInMemoryChanges()
testutil.AssertEquals(t, stateTrieTestWrapper.Get("chaincodeID1", "key1"), []byte("value1"))
}
示例7: testGetRootHashes
func testGetRootHashes(t *testing.T, enableBlockCache bool) ([]byte, []byte, []byte, []byte) {
// number of buckets at each level 26,9,3,1
testHasher, stateImplTestWrapper, stateDelta := createFreshDBAndInitTestStateImplWithCustomHasher(t, 26, 3)
// populate hash fucntion such that they intersect at higher level buckets
testHasher.populate("chaincodeID1", "key1", 1)
testHasher.populate("chaincodeID2", "key2", 15)
testHasher.populate("chaincodeID3", "key3", 26)
if !enableBlockCache {
stateImplTestWrapper.stateImpl.bucketCache = newBucketCache(0)
}
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
stateDelta.Set("chaincodeID3", "key3", []byte("value3"), nil)
rootHash1 := stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
stateDelta = statemgmt.NewStateDelta()
stateDelta.Set("chaincodeID1", "key1", []byte("value1_new"), nil)
rootHash2 := stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
stateDelta = statemgmt.NewStateDelta()
stateDelta.Delete("chaincodeID2", "key2", nil)
rootHash3 := stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
if enableBlockCache {
stateImplTestWrapper.stateImpl.bucketCache = newBucketCache(20)
stateImplTestWrapper.stateImpl.bucketCache.loadAllBucketNodesFromDB()
}
stateDelta = statemgmt.NewStateDelta()
stateDelta.Set("chaincodeID3", "key3", []byte("value3_new"), nil)
rootHash4 := stateImplTestWrapper.prepareWorkingSetAndComputeCryptoHash(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
return rootHash1, rootHash2, rootHash3, rootHash4
}
示例8: sendStateSnapshot
// sendBlocks sends the blocks based upon the supplied SyncBlockRange over the stream.
func (d *Handler) sendStateSnapshot(syncStateSnapshotRequest *pb.SyncStateSnapshotRequest) {
peerLogger.Debugf("Sending state snapshot with correlationId = %d", syncStateSnapshotRequest.CorrelationId)
snapshot, err := d.Coordinator.GetStateSnapshot()
if err != nil {
peerLogger.Errorf("Error getting snapshot: %s", err)
return
}
defer snapshot.Release()
// Iterate over the state deltas and send to requestor
currBlockNumber := snapshot.GetBlockNumber()
var sequence uint64
// Loop through and send the Deltas
for i := 0; snapshot.Next(); i++ {
delta := statemgmt.NewStateDelta()
k, v := snapshot.GetRawKeyValue()
cID, kID := statemgmt.DecodeCompositeKey(k)
delta.Set(cID, kID, v, nil)
deltaAsBytes := delta.Marshal()
// Encode a SyncStateSnapsot into the payload
sequence = uint64(i)
syncStateSnapshot := &pb.SyncStateSnapshot{Delta: deltaAsBytes, Sequence: sequence, BlockNumber: currBlockNumber, Request: syncStateSnapshotRequest}
syncStateSnapshotBytes, err := proto.Marshal(syncStateSnapshot)
if err != nil {
peerLogger.Errorf("Error marshalling syncStateSnapsot for BlockNum = %d: %s", currBlockNumber, err)
break
}
if err := d.SendMessage(&pb.Message{Type: pb.Message_SYNC_STATE_SNAPSHOT, Payload: syncStateSnapshotBytes}); err != nil {
peerLogger.Errorf("Error sending syncStateSnapsot for BlockNum = %d: %s", currBlockNumber, err)
break
}
}
// Now send the terminating message
syncStateSnapshot := &pb.SyncStateSnapshot{Delta: []byte{}, Sequence: sequence + 1, BlockNumber: currBlockNumber, Request: syncStateSnapshotRequest}
syncStateSnapshotBytes, err := proto.Marshal(syncStateSnapshot)
if err != nil {
peerLogger.Errorf("Error marshalling terminating syncStateSnapsot message for correlationId = %d, BlockNum = %d: %s", syncStateSnapshotRequest.CorrelationId, currBlockNumber, err)
return
}
if err := d.SendMessage(&pb.Message{Type: pb.Message_SYNC_STATE_SNAPSHOT, Payload: syncStateSnapshotBytes}); err != nil {
peerLogger.Errorf("Error sending terminating syncStateSnapsot for correlationId = %d, BlockNum = %d: %s", syncStateSnapshotRequest.CorrelationId, currBlockNumber, err)
return
}
}
示例9: createFreshDBAndInitTestStateImplWithCustomHasher
func createFreshDBAndInitTestStateImplWithCustomHasher(t testing.TB, numBuckets int, maxGroupingAtEachLevel int) (*testHasher, *stateImplTestWrapper, *statemgmt.StateDelta) {
testHasher := newTestHasher()
configMap := map[string]interface{}{
ConfigNumBuckets: numBuckets,
ConfigMaxGroupingAtEachLevel: maxGroupingAtEachLevel,
ConfigHashFunction: testHasher.getHashFunction(),
}
testDBWrapper.CleanDB(t)
stateImpl := NewStateImpl()
stateImpl.Initialize(configMap)
stateImplTestWrapper := &stateImplTestWrapper{configMap, stateImpl, t}
stateDelta := statemgmt.NewStateDelta()
return testHasher, stateImplTestWrapper, stateDelta
}
示例10: TestStateImpl_ComputeHash_DB_3
func TestStateImpl_ComputeHash_DB_3(t *testing.T) {
// simple test... not using custom hasher
conf = newConfig(DefaultNumBuckets, DefaultMaxGroupingAtEachLevel, fnvHash)
testDBWrapper.CreateFreshDB(t)
stateImplTestWrapper := newStateImplTestWrapper(t)
stateImpl := stateImplTestWrapper.stateImpl
stateDelta := statemgmt.NewStateDelta()
stateDelta.Set("chaincode1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincode2", "key2", []byte("value2"), nil)
stateDelta.Set("chaincode3", "key3", []byte("value3"), nil)
stateImpl.PrepareWorkingSet(stateDelta)
hash1 := stateImplTestWrapper.computeCryptoHash()
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
stateDelta = statemgmt.NewStateDelta()
stateDelta.Delete("chaincode1", "key1", nil)
stateDelta.Delete("chaincode2", "key2", nil)
stateDelta.Delete("chaincode3", "key3", nil)
stateImpl.PrepareWorkingSet(stateDelta)
hash2 := stateImplTestWrapper.computeCryptoHash()
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
testutil.AssertNotEquals(t, hash1, hash2)
testutil.AssertNil(t, hash2)
}
示例11: TestDataNodesDelta
func TestDataNodesDelta(t *testing.T) {
conf = newConfig(26, 3, fnvHash)
stateDelta := statemgmt.NewStateDelta()
stateDelta.Set("chaincodeID1", "key1", []byte("value1_1"), nil)
stateDelta.Set("chaincodeID1", "key2", []byte("value1_2"), nil)
stateDelta.Set("chaincodeID2", "key1", []byte("value2_1"), nil)
stateDelta.Set("chaincodeID2", "key2", []byte("value2_2"), nil)
dataNodesDelta := newDataNodesDelta(stateDelta)
affectedBuckets := dataNodesDelta.getAffectedBuckets()
testutil.AssertContains(t, affectedBuckets, newDataKey("chaincodeID1", "key1").getBucketKey())
testutil.AssertContains(t, affectedBuckets, newDataKey("chaincodeID1", "key2").getBucketKey())
testutil.AssertContains(t, affectedBuckets, newDataKey("chaincodeID2", "key1").getBucketKey())
testutil.AssertContains(t, affectedBuckets, newDataKey("chaincodeID2", "key2").getBucketKey())
}
示例12: TestStateImpl_DB_EmptyArrayValues
func TestStateImpl_DB_EmptyArrayValues(t *testing.T) {
testDBWrapper.CreateFreshDB(t)
stateImplTestWrapper := newStateImplTestWrapper(t)
stateImpl := stateImplTestWrapper.stateImpl
stateDelta := statemgmt.NewStateDelta()
stateDelta.Set("chaincode1", "key1", []byte{}, nil)
stateImpl.PrepareWorkingSet(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
emptyBytes := stateImplTestWrapper.get("chaincode1", "key1")
if emptyBytes == nil || len(emptyBytes) != 0 {
t.Fatalf("Expected an empty byte array. found = %#v", emptyBytes)
}
nilVal := stateImplTestWrapper.get("chaincodeID3", "non-existing-key")
if nilVal != nil {
t.Fatalf("Expected a nil. found = %#v", nilVal)
}
}
示例13: TxFinish
// TxFinish marks the completion of on-going tx. If txID is not same as of the on-going tx, this call panics
func (state *State) TxFinish(txID string, txSuccessful bool) {
logger.Debugf("txFinish() for txId [%s], txSuccessful=[%t]", txID, txSuccessful)
if state.currentTxID != txID {
panic(fmt.Errorf("Different txId in tx-begin [%s] and tx-finish [%s]", state.currentTxID, txID))
}
if txSuccessful {
if !state.currentTxStateDelta.IsEmpty() {
logger.Debugf("txFinish() for txId [%s] merging state changes", txID)
state.stateDelta.ApplyChanges(state.currentTxStateDelta)
state.txStateDeltaHash[txID] = state.currentTxStateDelta.ComputeCryptoHash()
state.updateStateImpl = true
} else {
state.txStateDeltaHash[txID] = nil
}
}
state.currentTxStateDelta = statemgmt.NewStateDelta()
state.currentTxID = ""
}
示例14: TestStateSnapshotIterator
func TestStateSnapshotIterator(t *testing.T) {
testDBWrapper.CleanDB(t)
stateImplTestWrapper := newStateImplTestWrapper(t)
stateDelta := statemgmt.NewStateDelta()
// insert keys
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID2", "key2", []byte("value2"), nil)
stateDelta.Set("chaincodeID3", "key3", []byte("value3"), nil)
stateDelta.Set("chaincodeID4", "key4", []byte("value4"), nil)
stateDelta.Set("chaincodeID5", "key5", []byte("value5"), nil)
stateDelta.Set("chaincodeID6", "key6", []byte("value6"), nil)
stateImplTestWrapper.prepareWorkingSet(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
//check that the key is persisted
testutil.AssertEquals(t, stateImplTestWrapper.get("chaincodeID5", "key5"), []byte("value5"))
// take db snapeshot
dbSnapshot := db.GetDBHandle().GetSnapshot()
// delete keys
stateDelta.Delete("chaincodeID1", "key1", nil)
stateDelta.Delete("chaincodeID2", "key2", nil)
stateDelta.Delete("chaincodeID3", "key3", nil)
stateDelta.Delete("chaincodeID4", "key4", nil)
stateDelta.Delete("chaincodeID5", "key5", nil)
stateDelta.Delete("chaincodeID6", "key6", nil)
stateImplTestWrapper.prepareWorkingSet(stateDelta)
stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
//check that the key is deleted
testutil.AssertNil(t, stateImplTestWrapper.get("chaincodeID5", "key5"))
itr, err := newStateSnapshotIterator(dbSnapshot)
testutil.AssertNoError(t, err, "Error while getting state snapeshot iterator")
numKeys := 0
for itr.Next() {
key, value := itr.GetRawKeyValue()
t.Logf("key=[%s], value=[%s]", string(key), string(value))
numKeys++
}
testutil.AssertEquals(t, numKeys, 6)
}
示例15: TestStateTrie_ComputeHash_AllInMemory
func TestStateTrie_ComputeHash_AllInMemory(t *testing.T) {
testDBWrapper.CleanDB(t)
stateTrie := NewStateTrie()
stateTrieTestWrapper := &stateTrieTestWrapper{stateTrie, t}
stateDelta := statemgmt.NewStateDelta()
// Test1 - Add a few keys
stateDelta.Set("chaincodeID1", "key1", []byte("value1"), nil)
stateDelta.Set("chaincodeID1", "key2", []byte("value2"), nil)
stateDelta.Set("chaincodeID2", "key3", []byte("value3"), nil)
stateDelta.Set("chaincodeID2", "key4", []byte("value4"), nil)
stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(stateDelta)
rootHash1 := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(stateDelta)
hash1 := expectedCryptoHashForTest(newTrieKey("chaincodeID1", "key1"), []byte("value1"))
hash2 := expectedCryptoHashForTest(newTrieKey("chaincodeID1", "key2"), []byte("value2"))
hash3 := expectedCryptoHashForTest(newTrieKey("chaincodeID2", "key3"), []byte("value3"))
hash4 := expectedCryptoHashForTest(newTrieKey("chaincodeID2", "key4"), []byte("value4"))
hash1Hash2 := expectedCryptoHashForTest(nil, nil, hash1, hash2)
hash3Hash4 := expectedCryptoHashForTest(nil, nil, hash3, hash4)
expectedRootHash1 := expectedCryptoHashForTest(nil, nil, hash1Hash2, hash3Hash4)
testutil.AssertEquals(t, rootHash1, expectedRootHash1)
stateTrie.ClearWorkingSet(true)
//Test2 - Add one more key
t.Logf("-- Add one more key exiting key --- ")
stateDelta.Set("chaincodeID3", "key5", []byte("value5"), nil)
rootHash2 := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(stateDelta)
hash5 := expectedCryptoHashForTest(newTrieKey("chaincodeID3", "key5"), []byte("value5"))
expectedRootHash2 := expectedCryptoHashForTest(nil, nil, hash1Hash2, hash3Hash4, hash5)
testutil.AssertEquals(t, rootHash2, expectedRootHash2)
stateTrie.ClearWorkingSet(true)
// Test3 - Remove one of the existing keys
t.Logf("-- Remove an exiting key --- ")
stateDelta.Delete("chaincodeID2", "key4", nil)
rootHash3 := stateTrieTestWrapper.PrepareWorkingSetAndComputeCryptoHash(stateDelta)
expectedRootHash3 := expectedCryptoHashForTest(nil, nil, hash1Hash2, hash3, hash5)
testutil.AssertEquals(t, rootHash3, expectedRootHash3)
stateTrie.ClearWorkingSet(true)
}