Is the transaction context the same for any contract? #4198
-
We have an issue on our ledger where we have two types of contract entities whose keys can cross (be the same) in the world state. And creating an entity with the same key after another, overwrites the first one. Our contracts have a structure similar to the following. package chaincode
import (
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type CredentialContract struct {
contractapi.Contract
}
func (s *CredentialContract) GetCredentialById(ctx contractapi.TransactionContextInterface, credentialId string) ([]byte, error) {
credentialJSON, err := ctx.GetStub().GetState(credentialId)
if err != nil {
return nil, err
}
return credentialJSON, nil
} And in the main package, we initialize them in the following way: package main
import (
"log"
"ledger-service/chaincode"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
func main() {
// Initialize Smart Contracts
contracts, err := contractapi.NewChaincode(
&chaincode.CredentialContract{},
if err != nil {
log.Panicf("Error creating smart contracts: %v", err)
}
if err := contracts.Start(); err != nil {
log.Panicf("Error starting smart contracts : %v", err)
}
} Was not the context (a.k.a: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Ledger keys written by a smart contract are scoped (or namespaced) within the channel's ledger by the name of their containing chaincode. Smart contracts packaged within a single chaincode share the same chaincode namespace. You need to either ensure that smart contracts within the same chaincode package don't inadvertently use the same ledger keys, or split your smart contracts into separate chaincode packages so their keys are contained within separate namespaces. |
Beta Was this translation helpful? Give feedback.
Ledger keys written by a smart contract are scoped (or namespaced) within the channel's ledger by the name of their containing chaincode. Smart contracts packaged within a single chaincode share the same chaincode namespace. You need to either ensure that smart contracts within the same chaincode package don't inadvertently use the same ledger keys, or split your smart contracts into separate chaincode packages so their keys are contained within separate namespaces.