forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_contexts.go
123 lines (101 loc) · 3.53 KB
/
transaction_contexts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package chaincode
import (
"context"
"sync"
commonledger "github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/ledger"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)
type key string
const (
// TXSimulatorKey is the context key used to provide a ledger.TxSimulator
// from the endorser to the chaincode.
TXSimulatorKey key = "txsimulatorkey"
// HistoryQueryExecutorKey is the context key used to provide a
// ledger.HistoryQueryExecutor from the endorser to the chaincode.
HistoryQueryExecutorKey key = "historyqueryexecutorkey"
)
// TransactionContexts maintains active transaction contexts for a Handler.
type TransactionContexts struct {
mutex sync.Mutex
contexts map[string]*TransactionContext
}
// NewTransactionContexts creates a registry for active transaction contexts.
func NewTransactionContexts() *TransactionContexts {
return &TransactionContexts{
contexts: map[string]*TransactionContext{},
}
}
// contextID creates a transaction identifier that is scoped to a chain.
func contextID(chainID, txID string) string {
return chainID + txID
}
// Create creates a new TransactionContext for the specified chain and
// transaction ID. An error is returned when a transaction context has already
// been created for the specified chain and transaction ID.
func (c *TransactionContexts) Create(txParams *ccprovider.TransactionParams) (*TransactionContext, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
ctxID := contextID(txParams.ChannelID, txParams.TxID)
if c.contexts[ctxID] != nil {
return nil, errors.Errorf("txid: %s(%s) exists", txParams.TxID, txParams.ChannelID)
}
txctx := &TransactionContext{
ChainID: txParams.ChannelID,
SignedProp: txParams.SignedProp,
Proposal: txParams.Proposal,
ResponseNotifier: make(chan *pb.ChaincodeMessage, 1),
TXSimulator: txParams.TXSimulator,
HistoryQueryExecutor: txParams.HistoryQueryExecutor,
CollectionStore: txParams.CollectionStore,
IsInitTransaction: txParams.IsInitTransaction,
queryIteratorMap: map[string]commonledger.ResultsIterator{},
pendingQueryResults: map[string]*PendingQueryResult{},
AllowedCollectionAccess: make(map[string]bool),
}
c.contexts[ctxID] = txctx
return txctx, nil
}
func getTxSimulator(ctx context.Context) ledger.TxSimulator {
if txsim, ok := ctx.Value(TXSimulatorKey).(ledger.TxSimulator); ok {
return txsim
}
return nil
}
func getHistoryQueryExecutor(ctx context.Context) ledger.HistoryQueryExecutor {
if historyQueryExecutor, ok := ctx.Value(HistoryQueryExecutorKey).(ledger.HistoryQueryExecutor); ok {
return historyQueryExecutor
}
return nil
}
// Get retrieves the transaction context associated with the chain and
// transaction ID.
func (c *TransactionContexts) Get(chainID, txID string) *TransactionContext {
ctxID := contextID(chainID, txID)
c.mutex.Lock()
tc := c.contexts[ctxID]
c.mutex.Unlock()
return tc
}
// Delete removes the transaction context associated with the specified chain
// and transaction ID.
func (c *TransactionContexts) Delete(chainID, txID string) {
ctxID := contextID(chainID, txID)
c.mutex.Lock()
delete(c.contexts, ctxID)
c.mutex.Unlock()
}
// Close closes all query iterators assocated with the context.
func (c *TransactionContexts) Close() {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, txctx := range c.contexts {
txctx.CloseQueryIterators()
}
}