Key activeness

The specification of the DUPLICATE_CONTRACT_KEY error says this is raised when two contracts with the same key are active. Does that mean contracts with the same key being generated by the code in the transaction, or a duplicate against a contract that was previously created and already exists on the ledger? Or either?

As a follow up on this question, can two different templates have the same key ? The same key within a transaction?

There are two types of duplicate key errors:

  1. During interpretation your transaction can have an internal duplicate key. This includes both things like doing two creates with the same key in the same transaction but also things like first doing a successful key lookup and then a create.
  2. If your transaction itself is fine, it can still produce a duplicate key error when applied to the global ledger state.

Both of these are reported as ALREADY_EXISTS: DUPLICATE_CONTRACT_KEY.

As for your follow-up question, keys are always scoped to a template so you cannot have key collisions across templates.