github.com/turingchain2020/turingchain@v1.1.21/blockchain/query.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/turingchain2020/turingchain/client"
    11  	dbm "github.com/turingchain2020/turingchain/common/db"
    12  	"github.com/turingchain2020/turingchain/queue"
    13  	"github.com/turingchain2020/turingchain/types"
    14  )
    15  
    16  //Query 检索
    17  type Query struct {
    18  	db        dbm.DB
    19  	stateHash []byte
    20  	client    queue.Client
    21  	mu        sync.Mutex
    22  	api       client.QueueProtocolAPI
    23  }
    24  
    25  //NewQuery new
    26  func NewQuery(db dbm.DB, qclient queue.Client, stateHash []byte) *Query {
    27  	var err error
    28  	query := &Query{db: db, client: qclient, stateHash: stateHash}
    29  	query.api, err = client.New(qclient, nil)
    30  	if err != nil {
    31  		panic("NewQuery client.New err")
    32  	}
    33  	return query
    34  }
    35  
    36  //Query 检索
    37  func (q *Query) Query(driver string, funcname string, param types.Message) (types.Message, error) {
    38  	query := &types.ChainExecutor{
    39  		Driver:    driver,
    40  		FuncName:  funcname,
    41  		Param:     types.Encode(param),
    42  		StateHash: q.getStateHash(),
    43  	}
    44  	return q.api.QueryChain(query)
    45  }
    46  
    47  func (q *Query) updateStateHash(stateHash []byte) {
    48  	q.mu.Lock()
    49  	defer q.mu.Unlock()
    50  	q.stateHash = stateHash
    51  }
    52  
    53  func (q *Query) getStateHash() (stateHash []byte) {
    54  	q.mu.Lock()
    55  	defer q.mu.Unlock()
    56  	return q.stateHash
    57  }