Skip to content

Commit

Permalink
use struct's context
Browse files Browse the repository at this point in the history
Signed-off-by: Angelo De Caro <[email protected]>
  • Loading branch information
adecaro committed Dec 12, 2024
1 parent c69666f commit 67ea104
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions platform/fabric/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (i *ChaincodeDiscover) WithFilterByMSPIDs(mspIDs ...string) *ChaincodeDisco
return i
}

func (i *ChaincodeDiscover) WithContext(context context.Context) *ChaincodeDiscover {
i.ChaincodeDiscover.WithContext(context)
return i
}

type ChaincodeInvocation struct {
driver.ChaincodeInvocation
}
Expand Down
8 changes: 7 additions & 1 deletion platform/fabric/core/generic/chaincode/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ type Discovery struct {
QueryForPeers bool

DefaultTTL time.Duration
Context context.Context
}

func NewDiscovery(chaincode *Chaincode) *Discovery {
// set key to the concatenation of chaincode name and version
return &Discovery{
chaincode: chaincode,
DefaultTTL: chaincode.ChannelConfig.DiscoveryDefaultTTLS(),
Context: context.Background(),
}
}

Expand Down Expand Up @@ -230,7 +232,7 @@ func (d *Discovery) query(req *discovery.Request) (discovery.Response, error) {
ClientIdentity: signerRaw,
ClientTlsCertHash: ClientTLSCertHash,
}
timeout, cancel := context.WithTimeout(context.Background(), d.chaincode.ChannelConfig.DiscoveryTimeout())
timeout, cancel := context.WithTimeout(d.Context, d.chaincode.ChannelConfig.DiscoveryTimeout())
defer cancel()
cl, err := pc.DiscoveryClient()
if err != nil {
Expand Down Expand Up @@ -318,6 +320,10 @@ func (d *Discovery) ChaincodeVersion() (string, error) {
return "", errors.Errorf("chaincode [%s] not found", d.chaincode.name)
}

func (d *Discovery) WithContext(context context.Context) {
d.Context = context
}

func ccCall(ccNames ...string) []*peer.ChaincodeCall {
var call []*peer.ChaincodeCall
for _, ccName := range ccNames {
Expand Down
8 changes: 6 additions & 2 deletions platform/fabric/core/generic/chaincode/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewInvoke(chaincode *Chaincode, newChaincodeDiscover NewChaincodeDiscoverFu
NumRetries: int(chaincode.NumRetries),
RetrySleep: chaincode.RetrySleep,
NewChaincodeDiscover: newChaincodeDiscover,
Context: context.Background(),
}
}

Expand Down Expand Up @@ -316,6 +317,8 @@ func (i *Invoke) prepare(query bool) (string, *pb.Proposal, []*pb.ProposalRespon
i.EndorsersMSPIDs...,
).WithImplicitCollections(
i.ImplicitCollectionMSPIDs...,
).WithContext(
i.Context,
)
if query {
discovery.WithForQuery()
Expand Down Expand Up @@ -473,7 +476,8 @@ func (i *Invoke) collectResponses(endorserClients []pb.EndorserClient, signedPro
for _, endorser := range endorserClients {
go func(endorser pb.EndorserClient) {
defer wg.Done()
proposalResp, err := endorser.ProcessProposal(context.Background(), signedProposal)
// TODO: we could evaluate the policy already here after we get a result to see if still need more answers
proposalResp, err := endorser.ProcessProposal(i.Context, signedProposal)
if err != nil {
errorCh <- err
return
Expand Down Expand Up @@ -553,7 +557,7 @@ func (i *Invoke) broadcast(txID string, env *common.Envelope) error {
if err := i.Chaincode.Broadcaster.Broadcast(i.Context, env); err != nil {
return err
}
return i.Chaincode.Finality.IsFinal(context.Background(), txID)
return i.Chaincode.Finality.IsFinal(i.Context, txID)
}

func (i *Invoke) checkQueryPolicy(errs []error, successes int, n int) error {
Expand Down
1 change: 1 addition & 0 deletions platform/fabric/driver/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type ChaincodeDiscover interface {
WithImplicitCollections(mspIDs ...string) ChaincodeDiscover
WithForQuery() ChaincodeDiscover
ChaincodeVersion() (string, error)
WithContext(ctx context.Context)
}

// Chaincode exposes chaincode-related functions
Expand Down

0 comments on commit 67ea104

Please sign in to comment.