hyperledger/fabric · error

LSCC invocation is attempting to write to namespace %s

Error message

LSCC invocation is attempting to write to namespace %s

What it means

An lscc deploy/upgrade transaction is only allowed to write to two namespaces: "lscc" itself and the namespace of the chaincode being deployed/upgraded. This error means the transaction's rwset contains writes to some other namespace, so the validator rejects the transaction as a policy violation.

Source

Thrown at core/handlers/validation/builtin/v13/lscc_validation_logic.go:484

		}
		// the value must be a ChaincodeData struct
		cdRWSet := &ccprovider.ChaincodeData{}
		err = proto.Unmarshal(lsccrwset.Writes[0].Value, cdRWSet)
		if err != nil {
			return policyErr(fmt.Errorf("unmarshalling of ChaincodeData failed, error %s", err))
		}
		// the chaincode name in the lsccwriteset must match the chaincode name in the deployment spec
		if cdRWSet.Name != cdsArgs.ChaincodeSpec.ChaincodeId.Name {
			return policyErr(fmt.Errorf("expected cc name %s, found %s", cdsArgs.ChaincodeSpec.ChaincodeId.Name, cdRWSet.Name))
		}
		// the chaincode version in the lsccwriteset must match the chaincode version in the deployment spec
		if cdRWSet.Version != cdsArgs.ChaincodeSpec.ChaincodeId.Version {
			return policyErr(fmt.Errorf("expected cc version %s, found %s", cdsArgs.ChaincodeSpec.ChaincodeId.Version, cdRWSet.Version))
		}
		// it must only write to 2 namespaces: LSCC's and the cc that we are deploying/upgrading
		for _, ns := range txRWSet.NsRwSets {
			if ns.NameSpace != "lscc" && ns.NameSpace != cdRWSet.Name && len(ns.KvRwSet.Writes) > 0 {
				return policyErr(fmt.Errorf("LSCC invocation is attempting to write to namespace %s", ns.NameSpace))
			}
		}

		logger.Debugf("Validating %s for cc %s version %s", lsccFunc, cdRWSet.Name, cdRWSet.Version)

		switch lsccFunc {
		case lscc.DEPLOY:

			/******************************************************************/
			/* security check 1 - cc not in the LCCC table of instantiated cc */
			/******************************************************************/
			if ccExistsOnLedger {
				return policyErr(fmt.Errorf("Chaincode %s is already instantiated", cdsArgs.ChaincodeSpec.ChaincodeId.Name))
			}

			/****************************************************************************/
			/* security check 2 - validation of rwset (and of collections if enabled) */
			/****************************************************************************/

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Remove any putState/delState calls to namespaces other than lscc and the target chaincode from the lscc transaction path.
  2. Perform ancillary state updates in separate transactions, not in the same deploy/upgrade transaction.
  3. If running a patched lscc, review the patch — stock Fabric lscc never writes to other namespaces.
  4. Inspect the transaction rwset (e.g. with the SDK) to identify which extra namespace was written.

Example fix

// before: same tx writes other cc state
stub.PutState("othercc~key", val) // during upgrade tx
// after: separate transaction
// emit an event and do the other putState in a dedicated invoke
Defensive patterns

Strategy: validation

Validate before calling

// pre-flight: inspect the rwset namespaces before submit
for _, ns := range txRWSet.NsRwSets {
    if ns.NameSpace != "lscc" && ns.NameSpace != targetCC && len(ns.KvRwSet.Writes) > 0 {
        return fmt.Errorf("illegal write to namespace %s in lscc tx", ns.NameSpace)
    }
}

Type guard

func onlyAllowedNamespaces(rwset *rwset.TxRwSet, cc string) bool {
    for _, ns := range rwset.NsRwSets {
        if ns.NameSpace != "lscc" && ns.NameSpace != cc && len(ns.KvRwSet.Writes) > 0 { return false }
    }
    return true
}

Try / catch

if err != nil && strings.Contains(err.Error(), "attempting to write to namespace") { /* split the offending write into its own transaction */ }

Prevention

When it happens

Trigger: An lscc invocation whose transaction rwset includes writes to a third namespace — e.g. a chaincode's Invoke path that performs putState on other keys/namespaces during an lscc-driven deploy, or a maliciously composed rwset.

Common situations: Custom or modified lscc implementations writing extra state, bundling additional state changes into the same transaction as a deploy/upgrade, chaincode upgrade logic triggering writes to other chaincodes' namespaces, attempt to smuggle state changes through lifecycle transactions.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/aebef47b870fd327. Report an issue: GitHub.