From cc8eab533226d600d7e8fa47e810856161713304 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Tue, 15 Oct 2024 13:06:14 -0400 Subject: [PATCH] more on block --- ...7005bb4515d21a064c1ad13ab9746e23763da.json | 156 +++++++++++ ...c2ff75a1d9b210b99eed6f14e8a17d05530b.json} | 36 ++- ...a13b35f3ec2f47bdccd481116f663d3a5c3bc.json | 262 ------------------ sql/indexer_zkapp_commands.sql | 16 +- sql/zkapp_commands.sql | 6 +- src/api/block.rs | 50 +--- src/api/search_transactions.rs | 43 +-- src/lib.rs | 1 + src/sql_to_mesh.rs | 67 +++++ src/types.rs | 24 +- 10 files changed, 291 insertions(+), 370 deletions(-) create mode 100644 .sqlx/query-411a992540d9c8a1eaa3461adc67005bb4515d21a064c1ad13ab9746e23763da.json rename .sqlx/{query-42425d0763c334a282e699ed3a10a050798f7561d56b432d3dceb6aea2830a16.json => query-52d18e4008bd0006fafee2f64d97c2ff75a1d9b210b99eed6f14e8a17d05530b.json} (51%) delete mode 100644 .sqlx/query-56485f1d902b775a7d9702dfe3da13b35f3ec2f47bdccd481116f663d3a5c3bc.json create mode 100644 src/sql_to_mesh.rs diff --git a/.sqlx/query-411a992540d9c8a1eaa3461adc67005bb4515d21a064c1ad13ab9746e23763da.json b/.sqlx/query-411a992540d9c8a1eaa3461adc67005bb4515d21a064c1ad13ab9746e23763da.json new file mode 100644 index 0000000..8bf03bd --- /dev/null +++ b/.sqlx/query-411a992540d9c8a1eaa3461adc67005bb4515d21a064c1ad13ab9746e23763da.json @@ -0,0 +1,156 @@ +{ + "db_name": "PostgreSQL", + "query": "WITH\n canonical_blocks AS (\n SELECT\n *\n FROM\n blocks\n WHERE\n chain_status='canonical'\n ),\n max_canonical_height AS (\n SELECT\n max(HEIGHT) AS max_height\n FROM\n canonical_blocks\n ),\n pending_blocks AS (\n SELECT\n b.*\n FROM\n blocks AS b,\n max_canonical_height AS m\n WHERE\n b.height>m.max_height\n AND b.chain_status='pending'\n ),\n blocks AS (\n SELECT\n *\n FROM\n canonical_blocks\n UNION ALL\n SELECT\n *\n FROM\n pending_blocks\n ),\n zkapp_commands_info AS (\n SELECT\n zc.id,\n zc.memo,\n zc.hash,\n pk_fee_payer.value AS fee_payer,\n pk_update_body.value AS pk_update_body,\n zfpb.fee,\n zfpb.valid_until,\n zfpb.nonce,\n bzc.sequence_no,\n bzc.status AS \"status: TransactionStatus\",\n zaub.balance_change,\n bzc.block_id,\n b.state_hash,\n b.height,\n token_update_body.value AS token,\n ARRAY(\n SELECT\n unnest(zauf.failures)\n FROM\n zkapp_account_update_failures AS zauf\n WHERE\n zauf.id=ANY (bzc.failure_reasons_ids)\n ) AS failure_reasons\n FROM\n zkapp_commands AS zc\n INNER JOIN blocks_zkapp_commands AS bzc ON zc.id=bzc.zkapp_command_id\n INNER JOIN zkapp_fee_payer_body AS zfpb ON zc.zkapp_fee_payer_body_id=zfpb.id\n INNER JOIN public_keys AS pk_fee_payer ON zfpb.public_key_id=pk_fee_payer.id\n INNER JOIN blocks AS b ON bzc.block_id=b.id\n LEFT JOIN zkapp_account_update AS zau ON zau.id=ANY (zc.zkapp_account_updates_ids)\n INNER JOIN zkapp_account_update_body AS zaub ON zau.body_id=zaub.id\n INNER JOIN account_identifiers AS ai_update_body ON zaub.account_identifier_id=ai_update_body.id\n INNER JOIN public_keys AS pk_update_body ON ai_update_body.public_key_id=pk_update_body.id\n INNER JOIN tokens AS token_update_body ON ai_update_body.token_id=token_update_body.id\n WHERE\n (\n $1>=b.height\n OR $1 IS NULL\n )\n AND (\n $2=zc.hash\n OR $2 IS NULL\n )\n AND (\n (\n (\n $3=pk_fee_payer.value\n AND $4=''\n )\n OR (\n $3=pk_update_body.value\n AND $4=token_update_body.value\n )\n )\n OR (\n $3 IS NULL\n AND $4 IS NULL\n )\n )\n AND (\n $5=bzc.status\n OR $5 IS NULL\n )\n AND (\n $6=bzc.status\n OR $6 IS NULL\n )\n AND (\n (\n $7=pk_fee_payer.value\n OR $7=pk_update_body.value\n )\n OR $7 IS NULL\n )\n ),\n zkapp_commands_ids AS (\n SELECT DISTINCT\n id,\n block_id,\n sequence_no\n FROM\n zkapp_commands_info\n ),\n id_count AS (\n SELECT\n count(*) AS total_count\n FROM\n zkapp_commands_ids\n )\nSELECT\n zc.*,\n id_count.total_count\nFROM\n id_count,\n (\n SELECT\n *\n FROM\n zkapp_commands_ids\n ORDER BY\n block_id,\n id,\n sequence_no\n LIMIT\n $8\n OFFSET\n $9\n ) AS ids\n INNER JOIN zkapp_commands_info AS zc ON ids.id=zc.id\n AND ids.block_id=zc.block_id\n AND ids.sequence_no=zc.sequence_no\nORDER BY\n ids.block_id,\n ids.id,\n ids.sequence_no\n", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "memo", + "type_info": "Text" + }, + { + "ordinal": 2, + "name": "hash", + "type_info": "Text" + }, + { + "ordinal": 3, + "name": "fee_payer", + "type_info": "Text" + }, + { + "ordinal": 4, + "name": "pk_update_body", + "type_info": "Text" + }, + { + "ordinal": 5, + "name": "fee", + "type_info": "Text" + }, + { + "ordinal": 6, + "name": "valid_until", + "type_info": "Int8" + }, + { + "ordinal": 7, + "name": "nonce", + "type_info": "Int8" + }, + { + "ordinal": 8, + "name": "sequence_no", + "type_info": "Int4" + }, + { + "ordinal": 9, + "name": "status: TransactionStatus", + "type_info": { + "Custom": { + "name": "transaction_status", + "kind": { + "Enum": [ + "applied", + "failed" + ] + } + } + } + }, + { + "ordinal": 10, + "name": "balance_change", + "type_info": "Text" + }, + { + "ordinal": 11, + "name": "block_id", + "type_info": "Int4" + }, + { + "ordinal": 12, + "name": "state_hash", + "type_info": "Text" + }, + { + "ordinal": 13, + "name": "height", + "type_info": "Int8" + }, + { + "ordinal": 14, + "name": "token", + "type_info": "Text" + }, + { + "ordinal": 15, + "name": "failure_reasons", + "type_info": "TextArray" + }, + { + "ordinal": 16, + "name": "total_count", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Int8", + "Text", + "Text", + "Text", + { + "Custom": { + "name": "transaction_status", + "kind": { + "Enum": [ + "applied", + "failed" + ] + } + } + }, + { + "Custom": { + "name": "transaction_status", + "kind": { + "Enum": [ + "applied", + "failed" + ] + } + } + }, + "Text", + "Int8", + "Int8" + ] + }, + "nullable": [ + false, + false, + false, + false, + false, + false, + true, + false, + false, + false, + false, + false, + null, + null, + false, + null, + null + ] + }, + "hash": "411a992540d9c8a1eaa3461adc67005bb4515d21a064c1ad13ab9746e23763da" +} diff --git a/.sqlx/query-42425d0763c334a282e699ed3a10a050798f7561d56b432d3dceb6aea2830a16.json b/.sqlx/query-52d18e4008bd0006fafee2f64d97c2ff75a1d9b210b99eed6f14e8a17d05530b.json similarity index 51% rename from .sqlx/query-42425d0763c334a282e699ed3a10a050798f7561d56b432d3dceb6aea2830a16.json rename to .sqlx/query-52d18e4008bd0006fafee2f64d97c2ff75a1d9b210b99eed6f14e8a17d05530b.json index 5f7dc7c..0dc5cfd 100644 --- a/.sqlx/query-42425d0763c334a282e699ed3a10a050798f7561d56b432d3dceb6aea2830a16.json +++ b/.sqlx/query-52d18e4008bd0006fafee2f64d97c2ff75a1d9b210b99eed6f14e8a17d05530b.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT\n zc.id,\n zc.memo,\n zc.hash,\n pk_fee_payer.value AS fee_payer,\n zfpb.fee,\n zfpb.valid_until,\n zfpb.nonce,\n bzc.sequence_no,\n bzc.status AS \"status: TransactionStatus\",\n ARRAY(\n SELECT\n unnest(zauf.failures)\n FROM\n zkapp_account_update_failures AS zauf\n WHERE\n zauf.id=ANY (bzc.failure_reasons_ids)\n ) AS failure_reasons,\n zaub.balance_change,\n pk_update_body.value AS account,\n token_update_body.value AS token\nFROM\n blocks_zkapp_commands AS bzc\n INNER JOIN zkapp_commands AS zc ON bzc.zkapp_command_id=zc.id\n INNER JOIN zkapp_fee_payer_body AS zfpb ON zc.zkapp_fee_payer_body_id=zfpb.id\n INNER JOIN public_keys AS pk_fee_payer ON zfpb.public_key_id=pk_fee_payer.id\n INNER JOIN blocks AS b ON bzc.block_id=b.id\n LEFT JOIN zkapp_account_update AS zau ON zau.id=ANY (zc.zkapp_account_updates_ids)\n LEFT JOIN zkapp_account_update_body AS zaub ON zau.body_id=zaub.id\n LEFT JOIN account_identifiers AS ai_update_body ON zaub.account_identifier_id=ai_update_body.id\n LEFT JOIN public_keys AS pk_update_body ON ai_update_body.public_key_id=pk_update_body.id\n LEFT JOIN tokens AS token_update_body ON ai_update_body.token_id=token_update_body.id\nWHERE\n bzc.block_id=$1\n AND (\n token_update_body.value=$2\n OR token_update_body.id IS NULL\n )\nORDER BY\n zc.id,\n bzc.sequence_no\n", + "query": "SELECT\n zc.id,\n zc.memo,\n zc.hash,\n pk_fee_payer.value AS fee_payer,\n zfpb.fee,\n zfpb.valid_until,\n zfpb.nonce,\n bzc.sequence_no,\n bzc.status AS \"status: TransactionStatus\",\n b.state_hash,\n b.height,\n bzc.block_id,\n cast(0 AS BIGINT) AS total_count,\n ARRAY(\n SELECT\n unnest(zauf.failures)\n FROM\n zkapp_account_update_failures AS zauf\n WHERE\n zauf.id=ANY (bzc.failure_reasons_ids)\n ) AS failure_reasons,\n zaub.balance_change,\n pk_update_body.value AS pk_update_body,\n token_update_body.value AS token\nFROM\n blocks_zkapp_commands AS bzc\n INNER JOIN zkapp_commands AS zc ON bzc.zkapp_command_id=zc.id\n INNER JOIN zkapp_fee_payer_body AS zfpb ON zc.zkapp_fee_payer_body_id=zfpb.id\n INNER JOIN public_keys AS pk_fee_payer ON zfpb.public_key_id=pk_fee_payer.id\n INNER JOIN blocks AS b ON bzc.block_id=b.id\n LEFT JOIN zkapp_account_update AS zau ON zau.id=ANY (zc.zkapp_account_updates_ids)\n LEFT JOIN zkapp_account_update_body AS zaub ON zau.body_id=zaub.id\n LEFT JOIN account_identifiers AS ai_update_body ON zaub.account_identifier_id=ai_update_body.id\n LEFT JOIN public_keys AS pk_update_body ON ai_update_body.public_key_id=pk_update_body.id\n LEFT JOIN tokens AS token_update_body ON ai_update_body.token_id=token_update_body.id\nWHERE\n bzc.block_id=$1\n AND (\n token_update_body.value=$2\n OR token_update_body.id IS NULL\n )\nORDER BY\n zc.id,\n bzc.sequence_no\n", "describe": { "columns": [ { @@ -60,21 +60,41 @@ }, { "ordinal": 9, + "name": "state_hash", + "type_info": "Text" + }, + { + "ordinal": 10, + "name": "height", + "type_info": "Int8" + }, + { + "ordinal": 11, + "name": "block_id", + "type_info": "Int4" + }, + { + "ordinal": 12, + "name": "total_count", + "type_info": "Int8" + }, + { + "ordinal": 13, "name": "failure_reasons", "type_info": "TextArray" }, { - "ordinal": 10, + "ordinal": 14, "name": "balance_change", "type_info": "Text" }, { - "ordinal": 11, - "name": "account", + "ordinal": 15, + "name": "pk_update_body", "type_info": "Text" }, { - "ordinal": 12, + "ordinal": 16, "name": "token", "type_info": "Text" } @@ -95,11 +115,15 @@ false, false, false, + false, + false, + false, + null, null, false, false, false ] }, - "hash": "42425d0763c334a282e699ed3a10a050798f7561d56b432d3dceb6aea2830a16" + "hash": "52d18e4008bd0006fafee2f64d97c2ff75a1d9b210b99eed6f14e8a17d05530b" } diff --git a/.sqlx/query-56485f1d902b775a7d9702dfe3da13b35f3ec2f47bdccd481116f663d3a5c3bc.json b/.sqlx/query-56485f1d902b775a7d9702dfe3da13b35f3ec2f47bdccd481116f663d3a5c3bc.json deleted file mode 100644 index df58d5f..0000000 --- a/.sqlx/query-56485f1d902b775a7d9702dfe3da13b35f3ec2f47bdccd481116f663d3a5c3bc.json +++ /dev/null @@ -1,262 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "WITH\n canonical_blocks AS (\n SELECT\n *\n FROM\n blocks\n WHERE\n chain_status='canonical'\n ),\n max_canonical_height AS (\n SELECT\n max(HEIGHT) AS max_height\n FROM\n canonical_blocks\n ),\n pending_blocks AS (\n SELECT\n b.*\n FROM\n blocks AS b,\n max_canonical_height AS m\n WHERE\n b.height>m.max_height\n AND b.chain_status='pending'\n ),\n blocks AS (\n SELECT\n *\n FROM\n canonical_blocks\n UNION ALL\n SELECT\n *\n FROM\n pending_blocks\n ),\n zkapp_commands_info AS (\n SELECT\n zc.id,\n zc.memo,\n zc.hash,\n pk_fee_payer.value AS fee_payer,\n pk_update_body.value AS pk_update_body,\n zfpb.fee,\n zfpb.valid_until,\n zfpb.nonce,\n bzc.sequence_no,\n bzc.status AS \"status: TransactionStatus\",\n zaub.account_identifier_id,\n zaub.update_id,\n zaub.balance_change,\n zaub.increment_nonce,\n zaub.events_id,\n zaub.actions_id,\n zaub.call_data_id,\n zaub.call_depth,\n zaub.zkapp_network_precondition_id,\n zaub.zkapp_account_precondition_id,\n zaub.zkapp_valid_while_precondition_id,\n zaub.use_full_commitment,\n zaub.implicit_account_creation_fee,\n zaub.may_use_token AS \"may_use_token: MayUseToken\",\n zaub.authorization_kind AS \"authorization_kind: AuthorizationKindType\",\n zaub.verification_key_hash_id,\n bzc.block_id,\n b.state_hash,\n b.height,\n ARRAY(\n SELECT\n unnest(zauf.failures)\n FROM\n zkapp_account_update_failures AS zauf\n WHERE\n zauf.id=ANY (bzc.failure_reasons_ids)\n ) AS failure_reasons\n FROM\n zkapp_commands AS zc\n INNER JOIN blocks_zkapp_commands AS bzc ON zc.id=bzc.zkapp_command_id\n INNER JOIN zkapp_fee_payer_body AS zfpb ON zc.zkapp_fee_payer_body_id=zfpb.id\n INNER JOIN public_keys AS pk_fee_payer ON zfpb.public_key_id=pk_fee_payer.id\n INNER JOIN blocks AS b ON bzc.block_id=b.id\n LEFT JOIN zkapp_account_update AS zau ON zau.id=ANY (zc.zkapp_account_updates_ids)\n INNER JOIN zkapp_account_update_body AS zaub ON zau.body_id=zaub.id\n INNER JOIN account_identifiers AS ai_update_body ON zaub.account_identifier_id=ai_update_body.id\n INNER JOIN public_keys AS pk_update_body ON ai_update_body.public_key_id=pk_update_body.id\n INNER JOIN tokens AS token_update_body ON ai_update_body.token_id=token_update_body.id\n WHERE\n (\n $1>=b.height\n OR $1 IS NULL\n )\n AND (\n $2=zc.hash\n OR $2 IS NULL\n )\n AND (\n (\n (\n $3=pk_fee_payer.value\n AND $4=''\n )\n OR (\n $3=pk_update_body.value\n AND $4=token_update_body.value\n )\n )\n OR (\n $3 IS NULL\n AND $4 IS NULL\n )\n )\n AND (\n $5=bzc.status\n OR $5 IS NULL\n )\n AND (\n $6=bzc.status\n OR $6 IS NULL\n )\n AND (\n (\n $7=pk_fee_payer.value\n OR $7=pk_update_body.value\n )\n OR $7 IS NULL\n )\n ),\n zkapp_commands_ids AS (\n SELECT DISTINCT\n id,\n block_id,\n sequence_no\n FROM\n zkapp_commands_info\n ),\n id_count AS (\n SELECT\n count(*) AS total_count\n FROM\n zkapp_commands_ids\n )\nSELECT\n zc.*,\n id_count.total_count\nFROM\n id_count,\n (\n SELECT\n *\n FROM\n zkapp_commands_ids\n ORDER BY\n block_id,\n id,\n sequence_no\n LIMIT\n $8\n OFFSET\n $9\n ) AS ids\n INNER JOIN zkapp_commands_info AS zc ON ids.id=zc.id\n AND ids.block_id=zc.block_id\n AND ids.sequence_no=zc.sequence_no\nORDER BY\n ids.block_id,\n ids.id,\n ids.sequence_no\n", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int4" - }, - { - "ordinal": 1, - "name": "memo", - "type_info": "Text" - }, - { - "ordinal": 2, - "name": "hash", - "type_info": "Text" - }, - { - "ordinal": 3, - "name": "fee_payer", - "type_info": "Text" - }, - { - "ordinal": 4, - "name": "pk_update_body", - "type_info": "Text" - }, - { - "ordinal": 5, - "name": "fee", - "type_info": "Text" - }, - { - "ordinal": 6, - "name": "valid_until", - "type_info": "Int8" - }, - { - "ordinal": 7, - "name": "nonce", - "type_info": "Int8" - }, - { - "ordinal": 8, - "name": "sequence_no", - "type_info": "Int4" - }, - { - "ordinal": 9, - "name": "status: TransactionStatus", - "type_info": { - "Custom": { - "name": "transaction_status", - "kind": { - "Enum": [ - "applied", - "failed" - ] - } - } - } - }, - { - "ordinal": 10, - "name": "account_identifier_id", - "type_info": "Int4" - }, - { - "ordinal": 11, - "name": "update_id", - "type_info": "Int4" - }, - { - "ordinal": 12, - "name": "balance_change", - "type_info": "Text" - }, - { - "ordinal": 13, - "name": "increment_nonce", - "type_info": "Bool" - }, - { - "ordinal": 14, - "name": "events_id", - "type_info": "Int4" - }, - { - "ordinal": 15, - "name": "actions_id", - "type_info": "Int4" - }, - { - "ordinal": 16, - "name": "call_data_id", - "type_info": "Int4" - }, - { - "ordinal": 17, - "name": "call_depth", - "type_info": "Int4" - }, - { - "ordinal": 18, - "name": "zkapp_network_precondition_id", - "type_info": "Int4" - }, - { - "ordinal": 19, - "name": "zkapp_account_precondition_id", - "type_info": "Int4" - }, - { - "ordinal": 20, - "name": "zkapp_valid_while_precondition_id", - "type_info": "Int4" - }, - { - "ordinal": 21, - "name": "use_full_commitment", - "type_info": "Bool" - }, - { - "ordinal": 22, - "name": "implicit_account_creation_fee", - "type_info": "Bool" - }, - { - "ordinal": 23, - "name": "may_use_token: MayUseToken", - "type_info": { - "Custom": { - "name": "may_use_token", - "kind": { - "Enum": [ - "No", - "ParentsOwnToken", - "InheritFromParent" - ] - } - } - } - }, - { - "ordinal": 24, - "name": "authorization_kind: AuthorizationKindType", - "type_info": { - "Custom": { - "name": "authorization_kind_type", - "kind": { - "Enum": [ - "None_given", - "Signature", - "Proof" - ] - } - } - } - }, - { - "ordinal": 25, - "name": "verification_key_hash_id", - "type_info": "Int4" - }, - { - "ordinal": 26, - "name": "block_id", - "type_info": "Int4" - }, - { - "ordinal": 27, - "name": "state_hash", - "type_info": "Text" - }, - { - "ordinal": 28, - "name": "height", - "type_info": "Int8" - }, - { - "ordinal": 29, - "name": "failure_reasons", - "type_info": "TextArray" - }, - { - "ordinal": 30, - "name": "total_count", - "type_info": "Int8" - } - ], - "parameters": { - "Left": [ - "Int8", - "Text", - "Text", - "Text", - { - "Custom": { - "name": "transaction_status", - "kind": { - "Enum": [ - "applied", - "failed" - ] - } - } - }, - { - "Custom": { - "name": "transaction_status", - "kind": { - "Enum": [ - "applied", - "failed" - ] - } - } - }, - "Text", - "Int8", - "Int8" - ] - }, - "nullable": [ - false, - false, - false, - false, - false, - false, - true, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - true, - false, - false, - false, - false, - true, - false, - null, - null, - null, - null - ] - }, - "hash": "56485f1d902b775a7d9702dfe3da13b35f3ec2f47bdccd481116f663d3a5c3bc" -} diff --git a/sql/indexer_zkapp_commands.sql b/sql/indexer_zkapp_commands.sql index 9fd3d9e..05f89e8 100644 --- a/sql/indexer_zkapp_commands.sql +++ b/sql/indexer_zkapp_commands.sql @@ -46,25 +46,11 @@ WITH zfpb.nonce, bzc.sequence_no, bzc.status AS "status: TransactionStatus", - zaub.account_identifier_id, - zaub.update_id, zaub.balance_change, - zaub.increment_nonce, - zaub.events_id, - zaub.actions_id, - zaub.call_data_id, - zaub.call_depth, - zaub.zkapp_network_precondition_id, - zaub.zkapp_account_precondition_id, - zaub.zkapp_valid_while_precondition_id, - zaub.use_full_commitment, - zaub.implicit_account_creation_fee, - zaub.may_use_token AS "may_use_token: MayUseToken", - zaub.authorization_kind AS "authorization_kind: AuthorizationKindType", - zaub.verification_key_hash_id, bzc.block_id, b.state_hash, b.height, + token_update_body.value AS token, ARRAY( SELECT unnest(zauf.failures) diff --git a/sql/zkapp_commands.sql b/sql/zkapp_commands.sql index eed3c79..dc8d359 100644 --- a/sql/zkapp_commands.sql +++ b/sql/zkapp_commands.sql @@ -8,6 +8,10 @@ SELECT zfpb.nonce, bzc.sequence_no, bzc.status AS "status: TransactionStatus", + b.state_hash, + b.height, + bzc.block_id, + cast(0 AS BIGINT) AS total_count, ARRAY( SELECT unnest(zauf.failures) @@ -17,7 +21,7 @@ SELECT zauf.id=ANY (bzc.failure_reasons_ids) ) AS failure_reasons, zaub.balance_change, - pk_update_body.value AS account, + pk_update_body.value AS pk_update_body, token_update_body.value AS token FROM blocks_zkapp_commands AS bzc diff --git a/src/api/block.rs b/src/api/block.rs index e1fcc30..fa0cba1 100644 --- a/src/api/block.rs +++ b/src/api/block.rs @@ -7,8 +7,8 @@ use serde::Serialize; use sqlx::FromRow; use crate::{ - operation, util::DEFAULT_TOKEN_ID, ChainStatus, InternalCommandType, MinaMesh, MinaMeshError, OperationType, - TransactionStatus, UserCommandType, + operation, sql_to_mesh::zkapp_commands_to_transactions, util::DEFAULT_TOKEN_ID, ChainStatus, InternalCommandType, + MinaMesh, MinaMeshError, OperationType, TransactionStatus, UserCommandType, ZkAppCommand, }; /// https://github.com/MinaProtocol/mina/blob/985eda49bdfabc046ef9001d3c406e688bc7ec45/src/app/rosetta/lib/block.ml#L7 @@ -78,37 +78,10 @@ impl MinaMesh { } pub async fn zkapp_commands(&self, metadata: &BlockMetadata) -> Result, MinaMeshError> { - let metadata = sqlx::query_file_as!(ZkappCommandMetadata, "sql/zkapp_commands.sql", metadata.id, DEFAULT_TOKEN_ID) + let metadata = sqlx::query_file_as!(ZkAppCommand, "sql/zkapp_commands.sql", metadata.id, DEFAULT_TOKEN_ID) .fetch_all(&self.pg_pool) .await?; - let mut transactions = Vec::new(); - let mut operations = Vec::new(); - let mut current_txn_identifier = TransactionIdentifier::new(metadata[0].hash.clone()); - for item in metadata { - if current_txn_identifier.hash != item.hash { - transactions.push(Transaction::new(current_txn_identifier, operations)); - operations = Vec::new(); - current_txn_identifier = TransactionIdentifier::new(item.hash.clone()); - } - operations.push(operation( - operations.len() as i64, - Some(&item.fee), - &AccountIdentifier::new(item.fee_payer.clone()), - OperationType::ZkappFeePayerDec, - None, - None, - None, - )); - operations.push(operation( - operations.len() as i64, - Some(&item.balance_change), - &AccountIdentifier::new(item.account.clone()), - OperationType::ZkappBalanceUpdate, - None, - None, - None, - )); - } + let transactions = zkapp_commands_to_transactions(metadata); Ok(transactions) } @@ -362,18 +335,3 @@ fn internal_command_metadata_to_operation(metadata: &InternalCommandMetadata) -> } Ok(operations) } - -// TODO: implement -fn zkapp_command_metadata_to_operation(metadata: Vec) -> Result, MinaMeshError> { - // Go through each zkapp command in the vector - - // If the command id is the same as the previous, create a new operation from it - // and join it with the remaining operations of this transaction - - // If the command id is different than the previous, wrap up the previous - // transaction and start producing a new one, starting with the operation from - // this entry - - // Return a vec with the produced txns - Ok(Vec::new()) -} diff --git a/src/api/search_transactions.rs b/src/api/search_transactions.rs index e72675d..ed998d0 100644 --- a/src/api/search_transactions.rs +++ b/src/api/search_transactions.rs @@ -9,8 +9,8 @@ use serde_json::{json, Map, Value}; use sqlx::FromRow; use crate::{ - operation, util::DEFAULT_TOKEN_ID, AuthorizationKindType, ChainStatus, InternalCommandType, MayUseToken, MinaMesh, - MinaMeshError, OperationType, TransactionStatus, UserCommandType, + operation, util::DEFAULT_TOKEN_ID, ChainStatus, InternalCommandType, MinaMesh, MinaMeshError, OperationType, + TransactionStatus, UserCommandType, ZkAppCommand, }; impl MinaMesh { @@ -159,41 +159,6 @@ impl MinaMesh { } } -#[derive(FromRow)] -pub struct ZkAppCommand { - pub id: Option, - pub memo: Option, - pub hash: String, - pub fee_payer: String, - pub pk_update_body: String, - pub fee: Option, - pub valid_until: Option, - pub nonce: Option, - pub sequence_no: i32, - pub status: TransactionStatus, - pub account_identifier_id: Option, - pub update_id: Option, - pub balance_change: Option, - pub increment_nonce: bool, - pub events_id: Option, - pub actions_id: Option, - pub call_data_id: Option, - pub call_depth: Option, - pub zkapp_network_precondition_id: Option, - pub zkapp_account_precondition_id: Option, - pub zkapp_valid_while_precondition_id: Option, - pub use_full_commitment: bool, - pub implicit_account_creation_fee: bool, - pub may_use_token: MayUseToken, - pub authorization_kind: AuthorizationKindType, - pub verification_key_hash_id: Option, - pub block_id: Option, - pub state_hash: Option, - pub height: Option, - pub failure_reasons: Option>, - pub total_count: Option, -} - pub fn zkapp_commands_to_block_transactions(commands: Vec) -> Vec { let mut block_map: HashMap<(i64, String), HashMap>> = HashMap::new(); @@ -209,7 +174,7 @@ pub fn zkapp_commands_to_block_transactions(commands: Vec) -> Vec< if operations.is_empty() { operations.push(operation( 0, - Some(&format!("-{}", command.fee.unwrap_or("0".to_string()))), + Some(&format!("-{}", command.fee)), &AccountIdentifier { address: command.fee_payer.clone(), metadata: Some(json!({ "token_id": DEFAULT_TOKEN_ID })), @@ -225,7 +190,7 @@ pub fn zkapp_commands_to_block_transactions(commands: Vec) -> Vec< // Add zkapp balance update operation operations.push(operation( 0, - command.balance_change.as_ref(), + Some(&command.balance_change), &AccountIdentifier { address: command.pk_update_body.clone(), metadata: Some(json!({ "token_id": DEFAULT_TOKEN_ID })), diff --git a/src/lib.rs b/src/lib.rs index 017323c..dee15bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ mod error; mod graphql; mod operation; mod playground; +mod sql_to_mesh; mod types; mod util; diff --git a/src/sql_to_mesh.rs b/src/sql_to_mesh.rs new file mode 100644 index 0000000..39c0144 --- /dev/null +++ b/src/sql_to_mesh.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; + +use coinbase_mesh::models::{AccountIdentifier, Operation, Transaction, TransactionIdentifier}; +use serde_json::json; + +use crate::{operation, util::DEFAULT_TOKEN_ID, OperationType, TransactionStatus, ZkAppCommand}; + +pub fn zkapp_commands_to_transactions(commands: Vec) -> Vec { + let mut tx_map: HashMap> = HashMap::new(); + + for command in commands { + let tx_hash = command.hash.clone(); + + // Initialize or update the operation list for this transaction + let operations = tx_map.entry(tx_hash.clone()).or_default(); + + // Add fee operation (zkapp_fee_payer_dec) + if operations.is_empty() { + operations.push(operation( + 0, + Some(&format!("-{}", command.fee)), + &AccountIdentifier { + address: command.fee_payer.clone(), + metadata: Some(json!({ "token_id": DEFAULT_TOKEN_ID })), + sub_account: None, + }, + OperationType::ZkappFeePayerDec, + Some(&TransactionStatus::Applied), + None, + None, + )); + } + + // Add zkapp balance update operation + operations.push(operation( + 0, + Some(&command.balance_change), + &AccountIdentifier { + address: command.pk_update_body.clone(), + metadata: Some(json!({ "token_id": DEFAULT_TOKEN_ID })), + sub_account: None, + }, + OperationType::ZkappBalanceUpdate, + Some(&command.status), + None, + None, + )); + } + + let mut result = Vec::new(); + for (tx_hash, mut operations) in tx_map { + // Ensure the operations are correctly indexed + for (i, operation) in operations.iter_mut().enumerate() { + operation.operation_identifier.index = i as i64; + } + + let transaction = Transaction { + transaction_identifier: Box::new(TransactionIdentifier { hash: tx_hash.clone() }), + operations, + metadata: None, + related_transactions: None, + }; + result.push(transaction); + } + + result +} diff --git a/src/types.rs b/src/types.rs index 14a1d96..c9558fb 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ use derive_more::derive::Display; use serde::Serialize; -use sqlx::Type; +use sqlx::{FromRow, Type}; #[derive(Type, Debug, PartialEq, Eq, Serialize)] #[sqlx(type_name = "chain_status_type", rename_all = "lowercase")] @@ -83,3 +83,25 @@ pub enum AuthorizationKindType { #[sqlx(rename = "Proof")] Proof, } + +#[allow(dead_code)] +#[derive(FromRow)] +pub struct ZkAppCommand { + pub id: Option, + pub memo: Option, + pub hash: String, + pub fee_payer: String, + pub pk_update_body: String, + pub fee: String, + pub valid_until: Option, + pub nonce: Option, + pub sequence_no: i32, + pub status: TransactionStatus, + pub balance_change: String, + pub state_hash: Option, + pub failure_reasons: Option>, + pub token: Option, + pub height: Option, + pub total_count: Option, + pub block_id: Option, +}