github.com/fnagchunpeng/fabric@v2.1.1+incompatible/core/endorser/state.go (about)

     1  /*
     2  Copyright IBM Corp. 2018 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package endorser
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/ledger/rwset"
    11  	endorsement "github.com/hyperledger/fabric/core/handlers/endorsement/api/state"
    12  	"github.com/hyperledger/fabric/core/ledger"
    13  	"github.com/hyperledger/fabric/core/transientstore"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  //go:generate mockery -dir . -name QueryCreator -case underscore -output mocks/
    18  // QueryCreator creates new QueryExecutors
    19  type QueryCreator interface {
    20  	NewQueryExecutor() (ledger.QueryExecutor, error)
    21  }
    22  
    23  // ChannelState defines state operations
    24  type ChannelState struct {
    25  	*transientstore.Store
    26  	QueryCreator
    27  }
    28  
    29  // FetchState fetches state
    30  func (cs *ChannelState) FetchState() (endorsement.State, error) {
    31  	qe, err := cs.NewQueryExecutor()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return &StateContext{
    37  		QueryExecutor: qe,
    38  		Store:         cs.Store,
    39  	}, nil
    40  }
    41  
    42  // StateContext defines an execution context that interacts with the state
    43  type StateContext struct {
    44  	*transientstore.Store
    45  	ledger.QueryExecutor
    46  }
    47  
    48  // GetTransientByTXID returns the private data associated with this transaction ID.
    49  func (sc *StateContext) GetTransientByTXID(txID string) ([]*rwset.TxPvtReadWriteSet, error) {
    50  	scanner, err := sc.Store.GetTxPvtRWSetByTxid(txID, nil)
    51  	if err != nil {
    52  		return nil, errors.WithStack(err)
    53  	}
    54  	defer scanner.Close()
    55  	var data []*rwset.TxPvtReadWriteSet
    56  	for {
    57  		res, err := scanner.Next()
    58  		if err != nil {
    59  			return nil, errors.WithStack(err)
    60  		}
    61  		if res == nil {
    62  			break
    63  		}
    64  		if res.PvtSimulationResultsWithConfig == nil {
    65  			continue
    66  		}
    67  		data = append(data, res.PvtSimulationResultsWithConfig.PvtRwset)
    68  	}
    69  	return data, nil
    70  }