digital-asset / daml

The Daml smart contract language
https://www.digitalasset.com/developers
797 stars 199 forks source link

Archived contracts showing up on JSON-API query from a ledger migrated from pre-1.0.0 #5659

Closed lima-da closed 4 years ago

lima-da commented 4 years ago

Scenario:

Ledger was running on sandbox classic with postgres, upgraded from 0.13.55 to 1.0.0 on sandbox classic. /v1/query returning contract that has been archived before the upgrade. Using navigator these contract are shown inside include archived.

leo-da commented 4 years ago

@lima-da are you running JSON-API with PostreSQL index enabled? If you are running with PostgreSQL, can you wipe out the entire DB and recreate the schema from scratch, starting the JSON API once with createSchema=true

--query-store-jdbc-config "driver=<JDBC driver class name>,url=<JDBC connection url>,user=<user>,password=<password>,createSchema=<true|false>"
        Optional query store JDBC configuration string. Query store is a search index, use it if you need to query large active contract sets. Contains comma-separated key-value pairs. Where:
        driver -- JDBC driver class name, only org.postgresql.Driver supported right now,
        url -- JDBC connection URL, only jdbc:postgresql supported right now,
        user -- database user name,
        password -- database user password,
        createSchema -- boolean flag, if set to true, the process will re-create database schema and terminate immediately.
        Example: "driver=org.postgresql.Driver,url=jdbc:postgresql://localhost:5432/test?&ssl=true,user=postgres,password=password,createSchema=false"
leo-da commented 4 years ago

@lima-da

  1. what about the contracts that were archived after the upgrade? Are you also receiving those?
  2. does this affect only one specific template or you see multiple archived contracts from different templates?
  3. can you also attach the DAML for the contract template that was archived, but returned by /v1/query after the upgrade.
lima-da commented 4 years ago

@leo-da

  1. after upgrade new contracts are acting fine, create/archive are normal for new contract with new contractId
  2. I only tried asset.dar from quickstart, but I think it will reproduce with other contracts
  3. it's from quickstart Main:Asset
lima-da commented 4 years ago

@leo-da It's running without json api postgres index enabled

leo-da commented 4 years ago

this issue is blocking DABL upgrade, bumping up the priority.

leo-da commented 4 years ago
  1. DABL sandbox DB has 3 contracts that were created and archived on SDK 0.13.55

  2. Switching to sandbox-classic on SDK 1.0.0

select * from contracts where id in ('#39:0', '#42:0', '#43:0')

idtransaction_idworkflow_idpackage_idnamecreate_offsetarchive_offsetkeycreate_event_id
#43:043<null>b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbeMain:Asset00 00 00 00 00 00 00 2b 00 00 00 00 00 00 00 32 <null>#43:0
#39:039<null>b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbeMain:Asset00 00 00 00 00 00 00 27 00 00 00 00 00 00 00 39 <null>#39:0
#42:042<null>b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbeMain:Asset00 00 00 00 00 00 00 2a 00 00 00 00 00 00 00 35 <null>#42:0

Note that archive_offset is not null, so the contracts have been archived.

  1. JSON API calls com.daml.ledger.client.services.acs.ActiveContractSetClient#getActiveContracts and it gets the following GetActiveContractsResponses in the stream:

    15:46:22.456 [http-json-ledger-api-akka.actor.default-dispatcher-6] DEBUG com.daml.http.LedgerClientJwt$ - getActiveContracts: GetActiveContractsResponse(0000000000000027,,Vector(CreatedEvent(#39:0,#39:0,Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),None,Some(Record(Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),Vector(RecordField(issuer,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(owner,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(name,Some(Value(Text(AAA))))))),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(),Some())),None)
    15:46:22.457 [http-json-ledger-api-akka.actor.default-dispatcher-6] DEBUG com.daml.http.LedgerClientJwt$ - getActiveContracts: GetActiveContractsResponse(000000000000002a,,Vector(CreatedEvent(#42:0,#42:0,Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),None,Some(Record(Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),Vector(RecordField(issuer,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(owner,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(name,Some(Value(Text(AAA))))))),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(),Some())),None)
    15:46:22.458 [http-json-ledger-api-akka.actor.default-dispatcher-6] DEBUG com.daml.http.LedgerClientJwt$ - getActiveContracts: GetActiveContractsResponse(000000000000002b,,Vector(CreatedEvent(#43:0,#43:0,Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),None,Some(Record(Some(Identifier(b3488a9041bb7994eb9ec629cb700e78c1c619e9be7d711ff22b0cadfcce3fbe,Main,Asset)),Vector(RecordField(issuer,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(owner,Some(Value(Party(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e)))), RecordField(name,Some(Value(Text(AAA))))))),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(ledger-party-56068fe1-e476-4c3c-b288-2b03bf6b360e),Vector(),Some())),None)
    15:46:22.458 [http-json-ledger-api-akka.actor.default-dispatcher-6] DEBUG com.daml.http.LedgerClientJwt$ - getActiveContracts: GetActiveContractsResponse(0000000000000047,,Vector(),None)
  2. Note that the 2nd field in the CreatedEvent is contractId:

    final case class CreatedEvent(val eventId : scala.Predef.String, val contractId : scala.Predef.String...
  3. Question: why are we receiving archived contracts in the ACS stream: #39:0, #42:0 and #43:0. Is this sandbox-classic 1.0.0 DB migration issue?

cc @stefanobaghino-da @gerolf-da

stefanobaghino-da commented 4 years ago

The active contract service is served by the participant_events table: the queries collects create events for contract that have not been archived and serves them. This is a recent addition and was first introduced by this commit and released as part of version v0.13.56-snapshot.20200404.3816.0.30f2c742. This seems to be a possible issue with the migration.

@lima-da what's the status of the flyway_schema_history table after the migration?

EDIT: as a further check, can you have a look inside participant_events and check the value of create_consumed_at for the contracts that were archived before the migration? That value should not bu null.

leo-da commented 4 years ago

after migraiton

select * from flyway_schema_history

installed_rank version description type script checksum installed_by installed_on execution_time success
1 1 Init SQL V1__Init.sql -72965523 postgres 2020-04-22 20:53:16.479029 297 true
2 2.0 Contract divulgence SQL V2_0__Contract_divulgence.sql 1461208192 postgres 2020-04-22 20:53:16.82314 49 true
3 2.1 Rebuild Acs JDBC db.migration.postgres.V2_1__Rebuild_Acs postgres 2020-04-22 20:53:16.912273 52 true
4 3 Recompute Key Hash JDBC db.migration.postgres.V3__Recompute_Key_Hash postgres 2020-04-22 20:53:16.993835 25 true
5 4.0 Add parties SQL V4_0__Add_parties.sql 2091746815 postgres 2020-04-22 20:53:17.043624 44 true
6 4.1 Collect Parties JDBC db.migration.postgres.V4_1__Collect_Parties postgres 2020-04-22 20:53:17.110682 18 true
7 5 Add packages SQL V5__Add_packages.sql 1089030748 postgres 2020-04-22 20:53:17.165935 32 true
8 6 External Ledger Offset SQL V6__External_Ledger_Offset.sql 1642074148 postgres 2020-04-22 20:53:17.221672 7 true
9 7 Command deduplication SQL V7__Command_deduplication.sql 478192496 postgres 2020-04-22 20:53:17.251705 45 true
10 8 Contract Divulgence SQL V8__Contract_Divulgence.sql -1984400579 postgres 2020-04-22 20:53:17.329792 47 true
11 9 Contract Divulgence SQL V9__Contract_Divulgence.sql -39708021 postgres 2020-04-22 20:53:17.399061 46 true
12 10.0 Extract Event Data SQL V10_0__Extract_Event_Data.sql 1741793687 postgres 2020-04-22 20:53:17.493105 72 true
13 10.1 Populate Event Data JDBC db.migration.postgres.V10_1__Populate_Event_Data postgres 2020-04-22 20:53:17.597843 22 true
14 10.2 Extract Event Data SQL V10_2__Extract_Event_Data.sql -1727099174 postgres 2020-04-22 20:53:17.643328 10 true
15 11 Disclosures index SQL V11__Disclosures_index.sql 2049922513 postgres 2020-04-22 20:53:17.694984 20 true
16 12 Add configuration SQL V12__Add_configuration.sql 1064393329 postgres 2020-04-22 20:53:17.738093 61 true
17 13 Party entries SQL V13__Party_entries.sql 1719257450 postgres 2020-04-22 20:53:17.821485 41 true
18 14 Package entries SQL V14__Package_entries.sql 2024306888 postgres 2020-04-22 20:53:17.907928 61 true
19 15 Loosen transaction check SQL V15__Loosen_transaction_check.sql -955389528 postgres 2020-04-22 20:53:18.015604 32 true
20 16 Create command completions table SQL V16__Create_command_completions_table.sql 1970730105 postgres 2020-04-22 20:53:18.090811 45 true
21 17 Command deduplication SQL V17__Command_deduplication.sql 1107112150 postgres 2020-04-22 20:53:18.172541 51 true
22 18 Backfill completions SQL V18__Backfill_completions.sql -1664109353 postgres 2020-04-22 20:53:18.273411 48 true
23 19 Fix Completions SQL V19__Fix_Completions.sql 904991759 postgres 2020-04-23 12:58:15.943361 4 true
24 20 Events new schema SQL V20__Events_new_schema.sql 1928761158 postgres 2020-04-23 12:58:15.967546 72 true
25 21 Stable offsets SQL V21__Stable_offsets.sql 1237032852 postgres 2020-04-23 12:58:16.075983 266 true
26 22 Remove maximum record time SQL V22__Remove_maximum_record_time.sql 915980718 postgres 2020-04-23 12:58:16.372577 5 true
27 23 Delete checkpoints SQL V23__Delete_checkpoints.sql 1235716342 postgres 2020-04-23 12:58:16.396607 9 true
28 24 Stable offsets archival SQL V24__Stable_offsets_archival.sql -1078040150 postgres 2020-04-23 12:58:16.412403 34 true
29 25 Backfill Participant Events JDBC db.migration.postgres.V25__Backfill_Participant_Events postgres 2020-04-23 12:58:16.464352 568 true
30 26.0 Contracts new schema SQL V26_0__Contracts_new_schema.sql -1230616752 postgres 2020-04-23 12:58:17.041823 28 true
31 26.1 Fill create argument JDBC db.migration.postgres.V26_1__Fill_create_argument postgres 2020-04-23 12:58:17.074826 31 true
32 26.2 Contract create arg not null SQL V26_2__Contract_create_arg_not_null.sql -1144271682 postgres 2020-04-23 12:58:17.112385 1 true
33 27 Events table fixes SQL V27__Events_table_fixes.sql 1347117250 postgres 2020-04-23 12:58:17.118872 9 true
leo-da commented 4 years ago

after migration

select event_id, contract_id, create_consumed_at from participant_events where contract_id in ('#39:0', '#42:0', '#43:0')

event_idcontract_idcreate_consumed_at
#50:0#43:0<null>
#53:0#42:0<null>
#57:0#39:0<null>
#39:0#39:0<null>
#43:0#43:0<null>
#42:0#42:0<null>