Skip to content

Commit bebafec

Browse files
committed
[FAB-10038] history query executor on x-chan call
Change-Id: I088138ff65bfade39b233ac02a678a14387e9f8f Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 02963f0 commit bebafec

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

core/chaincode/handler.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -832,14 +832,19 @@ func (h *Handler) HandleInvokeChaincode(msg *pb.ChaincodeMessage, txContext *Tra
832832
return nil, errors.Errorf("failed to find ledger for channel: %s", targetInstance.ChainID)
833833
}
834834

835-
txsim2, err := lgr.NewTxSimulator(msg.Txid)
835+
sim, err := lgr.NewTxSimulator(msg.Txid)
836836
if err != nil {
837837
return nil, errors.WithStack(err)
838838
}
839-
defer txsim2.Done()
839+
defer sim.Done()
840840

841-
// TODO: Is the history query executor supposed to be updated here too?
842-
txsim = txsim2
841+
hqe, err := lgr.NewHistoryQueryExecutor()
842+
if err != nil {
843+
return nil, errors.WithStack(err)
844+
}
845+
846+
txsim = sim
847+
historyQueryExecutor = hqe
843848
}
844849
ctxt = context.WithValue(ctxt, TXSimulatorKey, txsim)
845850
ctxt = context.WithValue(ctxt, HistoryQueryExecutorKey, historyQueryExecutor)

core/chaincode/handler_test.go

+39-8
Original file line numberDiff line numberDiff line change
@@ -1349,14 +1349,15 @@ var _ = Describe("Handler", func() {
13491349

13501350
Describe("HandleInvokeChaincode", func() {
13511351
var (
1352-
expectedSignedProp *pb.SignedProposal
1353-
expectedProposal *pb.Proposal
1354-
targetDefinition *ccprovider.ChaincodeData
1355-
fakePeerLedger *mock.PeerLedger
1356-
newTxSimulator *mock.TxSimulator
1357-
request *pb.ChaincodeSpec
1358-
incomingMessage *pb.ChaincodeMessage
1359-
response *pb.Response
1352+
expectedSignedProp *pb.SignedProposal
1353+
expectedProposal *pb.Proposal
1354+
targetDefinition *ccprovider.ChaincodeData
1355+
fakePeerLedger *mock.PeerLedger
1356+
newTxSimulator *mock.TxSimulator
1357+
newHistoryQueryExecutor *mock.HistoryQueryExecutor
1358+
request *pb.ChaincodeSpec
1359+
incomingMessage *pb.ChaincodeMessage
1360+
response *pb.Response
13601361
)
13611362

13621363
BeforeEach(func() {
@@ -1372,9 +1373,11 @@ var _ = Describe("Handler", func() {
13721373
txContext.SignedProp = expectedSignedProp
13731374

13741375
newTxSimulator = &mock.TxSimulator{}
1376+
newHistoryQueryExecutor = &mock.HistoryQueryExecutor{}
13751377
fakePeerLedger = &mock.PeerLedger{}
13761378
fakePeerLedger.NewTxSimulatorReturns(newTxSimulator, nil)
13771379
fakeLedgerGetter.GetLedgerReturns(fakePeerLedger)
1380+
fakePeerLedger.NewHistoryQueryExecutorReturns(newHistoryQueryExecutor, nil)
13781381

13791382
targetDefinition = &ccprovider.ChaincodeData{
13801383
Name: "target-chaincode-data-name",
@@ -1481,6 +1484,23 @@ var _ = Describe("Handler", func() {
14811484
Expect(sim).To(BeIdenticalTo(newTxSimulator)) // same instance, not just equal
14821485
})
14831486

1487+
It("creates a new history query executor for target execution", func() {
1488+
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
1489+
Expect(err).NotTo(HaveOccurred())
1490+
1491+
Expect(fakePeerLedger.NewHistoryQueryExecutorCallCount()).To(Equal(1))
1492+
})
1493+
1494+
It("provides the new history query executor in the context used for execution", func() {
1495+
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
1496+
Expect(err).NotTo(HaveOccurred())
1497+
1498+
Expect(fakeExecutor.ExecuteCallCount()).To(Equal(1))
1499+
ctx, _, _ := fakeExecutor.ExecuteArgsForCall(0)
1500+
hqe := ctx.Value(chaincode.HistoryQueryExecutorKey)
1501+
Expect(hqe).To(BeIdenticalTo(newHistoryQueryExecutor)) // same instance, not just equal
1502+
})
1503+
14841504
It("marks the new transaction simulator as done after execute", func() {
14851505
fakeExecutor.ExecuteStub = func(context.Context, *ccprovider.CCContext, ccprovider.ChaincodeSpecGetter) (*pb.Response, *pb.ChaincodeEvent, error) {
14861506
Expect(newTxSimulator.DoneCallCount()).To(Equal(0))
@@ -1514,6 +1534,17 @@ var _ = Describe("Handler", func() {
15141534
Expect(err).To(MatchError("bonkers"))
15151535
})
15161536
})
1537+
1538+
Context("when creating the new history query executor fails", func() {
1539+
BeforeEach(func() {
1540+
fakePeerLedger.NewHistoryQueryExecutorReturns(nil, errors.New("razzies"))
1541+
})
1542+
1543+
It("returns an error", func() {
1544+
_, err := handler.HandleInvokeChaincode(incomingMessage, txContext)
1545+
Expect(err).To(MatchError("razzies"))
1546+
})
1547+
})
15171548
})
15181549

15191550
Context("when the target is a system chaincode", func() {

0 commit comments

Comments
 (0)