hyperledger/fabric · error

chaincode argument error

Error message

chaincode argument error

What it means

Wrapping error in getChaincodeSpec: the -ctor (constructor) JSON supplied on the command line could not be unmarshaled into a chaincodeInput. The user's --ctor argument is malformed JSON or does not match the expected shape {"Args":[...]} / {"Function":"f","Args":[...]}; the wrapped json error gives the exact syntax problem.

Source

Thrown at internal/peer/chaincode/common.go:47

	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"google.golang.org/protobuf/proto"
)

// getChaincodeSpec get chaincode spec from the cli cmd parameters
func getChaincodeSpec(cmd *cobra.Command) (*pb.ChaincodeSpec, error) {
	spec := &pb.ChaincodeSpec{}
	if err := checkChaincodeCmdParams(cmd); err != nil {
		// unset usage silence because it's a command line usage error
		cmd.SilenceUsage = false
		return spec, err
	}

	// Build the spec
	input := chaincodeInput{}
	if err := json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
		return spec, errors.Wrap(err, "chaincode argument error")
	}
	input.IsInit = isInit

	chaincodeLang = strings.ToUpper(chaincodeLang)
	spec = &pb.ChaincodeSpec{
		Type:        pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
		ChaincodeId: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName, Version: chaincodeVersion},
		Input:       &input.ChaincodeInput,
	}
	return spec, nil
}

// chaincodeInput is wrapper around the proto defined ChaincodeInput message that
// is decorated with a custom JSON unmarshaller.
type chaincodeInput struct {
	pb.ChaincodeInput
}

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Validate the --ctor JSON, e.g. '{"function":"Invoke","Args":["a","b","1"]}'
  2. Use single quotes around the whole JSON in bash so inner double quotes survive
  3. Use peer chaincode invoke --peerAddresses ... with --ctor written via a file or echo test to confirm it parses with jq
  4. If using go-based chaincodeArgs helper (peer CLI code), prefer the structured API instead of hand-written JSON

Example fix

// before
peer chaincode invoke -C mychannel -n mycc --ctor '{Args:["put","a","1"]}'
// after
peer chaincode invoke -C mychannel -n mycc --ctor '{"Args":["put","a","1"]}'
Defensive patterns

Strategy: validation

Validate before calling

// Validate ctor JSON before invoking the CLI
const ctor = JSON.parse(process.argv[2]);
if (typeof ctor !== 'object' || !(Array.isArray(ctor.Args) || typeof ctor.function === 'string')) {
  throw new Error('ctor must be {"function":... ,"Args":[...] }');
}

Try / catch

try {
  JSON.parse(ctorJSON);
} catch (e) {
  console.error('invalid --ctor JSON, quote inner strings with double quotes inside single quotes:', e.message);
  process.exit(1);
}

Prevention

When it happens

Trigger: Calling peer chaincode invoke/query with a --ctor string that is not valid JSON, e.g. mismatched braces, single quotes instead of double quotes, or missing the Args/function structure.

Common situations: Shell quoting stripping double quotes around Args arrays; copy-pasted function names without the '{"function":"x","Args":[...]}' wrapper; using old '{"Args":[...]}' style where init is required.

Related errors


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