github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/lockbased_tx_simulator.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package lockbasedtxmgr 18 19 import ( 20 "errors" 21 22 "github.com/hyperledger/fabric/common/util" 23 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil" 24 ) 25 26 // LockBasedTxSimulator is a transaction simulator used in `LockBasedTxMgr` 27 type lockBasedTxSimulator struct { 28 lockBasedQueryExecutor 29 rwsetBuilder *rwsetutil.RWSetBuilder 30 } 31 32 func newLockBasedTxSimulator(txmgr *LockBasedTxMgr) *lockBasedTxSimulator { 33 rwsetBuilder := rwsetutil.NewRWSetBuilder() 34 helper := &queryHelper{txmgr: txmgr, rwsetBuilder: rwsetBuilder} 35 id := util.GenerateUUID() 36 logger.Debugf("constructing new tx simulator [%s]", id) 37 return &lockBasedTxSimulator{lockBasedQueryExecutor{helper, id}, rwsetBuilder} 38 } 39 40 // GetState implements method in interface `ledger.TxSimulator` 41 func (s *lockBasedTxSimulator) GetState(ns string, key string) ([]byte, error) { 42 return s.helper.getState(ns, key) 43 } 44 45 // SetState implements method in interface `ledger.TxSimulator` 46 func (s *lockBasedTxSimulator) SetState(ns string, key string, value []byte) error { 47 s.helper.checkDone() 48 if err := s.helper.txmgr.db.ValidateKey(key); err != nil { 49 return err 50 } 51 s.rwsetBuilder.AddToWriteSet(ns, key, value) 52 return nil 53 } 54 55 // DeleteState implements method in interface `ledger.TxSimulator` 56 func (s *lockBasedTxSimulator) DeleteState(ns string, key string) error { 57 return s.SetState(ns, key, nil) 58 } 59 60 // SetStateMultipleKeys implements method in interface `ledger.TxSimulator` 61 func (s *lockBasedTxSimulator) SetStateMultipleKeys(namespace string, kvs map[string][]byte) error { 62 for k, v := range kvs { 63 if err := s.SetState(namespace, k, v); err != nil { 64 return err 65 } 66 } 67 return nil 68 } 69 70 // GetTxSimulationResults implements method in interface `ledger.TxSimulator` 71 func (s *lockBasedTxSimulator) GetTxSimulationResults() ([]byte, error) { 72 logger.Debugf("Simulation completed, getting simulation results") 73 s.Done() 74 if s.helper.err != nil { 75 return nil, s.helper.err 76 } 77 return s.rwsetBuilder.GetTxReadWriteSet().ToProtoBytes() 78 } 79 80 // ExecuteUpdate implements method in interface `ledger.TxSimulator` 81 func (s *lockBasedTxSimulator) ExecuteUpdate(query string) error { 82 return errors.New("Not supported") 83 }