diff --git a/.codecov.yml b/.codecov.yml index a648e2f4ac8..c391b1c4802 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -56,6 +56,7 @@ ignore: - "packages/app-connect-contract/src/**" - "packages/moderation-charters-contract/src/**" - "packages/decision-proposals-contract/src/**" + - "packages/token-market-contract/src/**" - "packages/masternode-reward-shares-contract/src/**" - "packages/token-history-contract/src/**" - "packages/wallet-utils-contract/src/**" diff --git a/.github/package-filters/js-packages-direct.yml b/.github/package-filters/js-packages-direct.yml index 9b5ccfcbb8e..80400143ce2 100644 --- a/.github/package-filters/js-packages-direct.yml +++ b/.github/package-filters/js-packages-direct.yml @@ -19,6 +19,9 @@ '@dashevo/decision-proposals-contract': - packages/decision-proposals-contract/** +'@dashevo/token-market-contract': + - packages/token-market-contract/** + '@dashevo/dashpay-contract': - packages/dashpay-contract/** diff --git a/.github/package-filters/js-packages-no-workflows.yml b/.github/package-filters/js-packages-no-workflows.yml index 77e36fe7755..88cba158adc 100644 --- a/.github/package-filters/js-packages-no-workflows.yml +++ b/.github/package-filters/js-packages-no-workflows.yml @@ -19,6 +19,9 @@ '@dashevo/decision-proposals-contract': &decision-proposals-contract - packages/decision-proposals-contract/** +'@dashevo/token-market-contract': &token-market-contract + - packages/token-market-contract/** + '@dashevo/dashpay-contract': &dashpay-contract - packages/dashpay-contract/** @@ -47,6 +50,7 @@ - *app-connect-contract - *moderation-charters-contract - *decision-proposals-contract + - *token-market-contract - packages/rs-platform-serialization/** - packages/rs-platform-serialization-derive/** - packages/rs-platform-value/** diff --git a/.github/package-filters/js-packages.yml b/.github/package-filters/js-packages.yml index 65bcfadfe1d..ed324859ce6 100644 --- a/.github/package-filters/js-packages.yml +++ b/.github/package-filters/js-packages.yml @@ -26,6 +26,10 @@ - .github/workflows/tests* - packages/decision-proposals-contract/** +'@dashevo/token-market-contract': &token-market-contract + - .github/workflows/tests* + - packages/token-market-contract/** + '@dashevo/dashpay-contract': &dashpay-contract - .github/workflows/tests* - packages/dashpay-contract/** @@ -60,6 +64,7 @@ - *app-connect-contract - *moderation-charters-contract - *decision-proposals-contract + - *token-market-contract - packages/rs-platform-serialization/** - packages/rs-platform-serialization-derive/** - packages/rs-platform-value/** diff --git a/.github/package-filters/rs-packages-direct.yml b/.github/package-filters/rs-packages-direct.yml index 7393b64bf2f..9631dce5dd0 100644 --- a/.github/package-filters/rs-packages-direct.yml +++ b/.github/package-filters/rs-packages-direct.yml @@ -33,6 +33,11 @@ decision-proposals-contract: - packages/decision-proposals-contract/schema/** - packages/decision-proposals-contract/Cargo.toml +token-market-contract: + - packages/token-market-contract/src/** + - packages/token-market-contract/schema/** + - packages/token-market-contract/Cargo.toml + dashpay-contract: - packages/dashpay-contract/src/** - packages/dashpay-contract/schema/** diff --git a/.github/package-filters/rs-packages-no-workflows.yml b/.github/package-filters/rs-packages-no-workflows.yml index b219a429573..13da21762bc 100644 --- a/.github/package-filters/rs-packages-no-workflows.yml +++ b/.github/package-filters/rs-packages-no-workflows.yml @@ -33,6 +33,11 @@ decision-proposals-contract: &decision-proposals-contract - packages/decision-proposals-contract/schema/** - packages/decision-proposals-contract/Cargo.toml +token-market-contract: &token-market-contract + - packages/token-market-contract/src/** + - packages/token-market-contract/schema/** + - packages/token-market-contract/Cargo.toml + dashpay-contract: &dashpay-contract - packages/dashpay-contract/src/** - packages/dashpay-contract/schema/** @@ -69,6 +74,7 @@ data-contracts: &data-contracts - *app-connect-contract - *moderation-charters-contract - *decision-proposals-contract + - *token-market-contract dpp: &dpp - packages/rs-dpp/** diff --git a/.github/package-filters/rs-packages.yml b/.github/package-filters/rs-packages.yml index 058394be9df..bcc49765fa4 100644 --- a/.github/package-filters/rs-packages.yml +++ b/.github/package-filters/rs-packages.yml @@ -40,6 +40,12 @@ decision-proposals-contract: &decision-proposals-contract - packages/decision-proposals-contract/schema/** - packages/decision-proposals-contract/Cargo.toml +token-market-contract: &token-market-contract + - .github/workflows/tests* + - packages/token-market-contract/src/** + - packages/token-market-contract/schema/** + - packages/token-market-contract/Cargo.toml + dashpay-contract: &dashpay-contract - .github/workflows/tests* - packages/dashpay-contract/src/** @@ -82,6 +88,7 @@ data-contracts: &data-contracts - *app-connect-contract - *moderation-charters-contract - *decision-proposals-contract + - *token-market-contract dpp: &dpp - .github/workflows/tests* diff --git a/.github/package-filters/test-suite-triggers.yml b/.github/package-filters/test-suite-triggers.yml index 769afeb55d4..bb695a59865 100644 --- a/.github/package-filters/test-suite-triggers.yml +++ b/.github/package-filters/test-suite-triggers.yml @@ -33,6 +33,7 @@ run: - packages/app-connect-contract/** - packages/moderation-charters-contract/** - packages/decision-proposals-contract/** + - packages/token-market-contract/** - packages/wallet-utils-contract/** # Local network scripts and action - .github/actions/local-network/** diff --git a/.github/workflows/tests-rs-workspace.yml b/.github/workflows/tests-rs-workspace.yml index 43b784d099c..7bd8b6b0e56 100644 --- a/.github/workflows/tests-rs-workspace.yml +++ b/.github/workflows/tests-rs-workspace.yml @@ -507,6 +507,7 @@ jobs: --package app-connect-contract \ --package moderation-charters-contract \ --package decision-proposals-contract \ + --package token-market-contract \ --all-features \ --locked \ -E '(not test(~shield) or (package(=platform-wallet-storage) and not test(~wallet::shielded::))) and (not binary_id(=drive-abci::strategy_tests) or test(~comprehensive_mixed_operations) or test(~process_proposal_collision))' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f380da6a3d8..13d5424f8db 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -353,6 +353,7 @@ jobs: - packages/app-connect-contract/** - packages/moderation-charters-contract/** - packages/decision-proposals-contract/** + - packages/token-market-contract/** - packages/dapi-grpc/** - packages/dashpay-contract/** - packages/data-contracts/** diff --git a/.pnp.cjs b/.pnp.cjs index 1fbbf7dcfd6..17c6642412d 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -90,6 +90,10 @@ const RAW_RUNTIME_STATE = "name": "@dashevo/token-history-contract",\ "reference": "workspace:packages/token-history-contract"\ },\ + {\ + "name": "@dashevo/token-market-contract",\ + "reference": "workspace:packages/token-market-contract"\ + },\ {\ "name": "@dashevo/wallet-lib",\ "reference": "workspace:packages/wallet-lib"\ @@ -141,6 +145,7 @@ const RAW_RUNTIME_STATE = ["@dashevo/platform", ["workspace:."]],\ ["@dashevo/platform-test-suite", ["workspace:packages/platform-test-suite"]],\ ["@dashevo/token-history-contract", ["workspace:packages/token-history-contract"]],\ + ["@dashevo/token-market-contract", ["workspace:packages/token-market-contract"]],\ ["@dashevo/wallet-lib", ["workspace:packages/wallet-lib"]],\ ["@dashevo/wallet-utils-contract", ["workspace:packages/wallet-utils-contract"]],\ ["@dashevo/wasm-dpp", ["workspace:packages/wasm-dpp"]],\ @@ -3080,6 +3085,22 @@ const RAW_RUNTIME_STATE = "linkType": "SOFT"\ }]\ ]],\ + ["@dashevo/token-market-contract", [\ + ["workspace:packages/token-market-contract", {\ + "packageLocation": "./packages/token-market-contract/",\ + "packageDependencies": [\ + ["@dashevo/token-market-contract", "workspace:packages/token-market-contract"],\ + ["@dashevo/wasm-dpp", "workspace:packages/wasm-dpp"],\ + ["chai", "npm:4.3.10"],\ + ["dirty-chai", "virtual:5066f1efd4c78a5ddf1dc175fd2039811919d09bb6f7aa5f2b46141ac45f2e6a675ff6260802f91c4f0e827a9565804d3931db690e7aa741774d17536ffb79fb#npm:2.0.1"],\ + ["eslint", "virtual:de32c10d523830f1843784ae863166d6ef2e074b6da9615f2b3296a1f90385ed3f59e274e3957326ba7cf3442d82470d9e1ec01e6720989a570c075c95d90dbc#npm:9.39.2"],\ + ["mocha", "npm:11.1.0"],\ + ["sinon", "npm:18.0.1"],\ + ["sinon-chai", "virtual:5066f1efd4c78a5ddf1dc175fd2039811919d09bb6f7aa5f2b46141ac45f2e6a675ff6260802f91c4f0e827a9565804d3931db690e7aa741774d17536ffb79fb#npm:3.7.0"]\ + ],\ + "linkType": "SOFT"\ + }]\ + ]],\ ["@dashevo/wallet-lib", [\ ["workspace:packages/wallet-lib", {\ "packageLocation": "./packages/wallet-lib/",\ diff --git a/Cargo.lock b/Cargo.lock index 3a7c7a14f48..0a62fe3a87c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2104,6 +2104,7 @@ dependencies = [ "serde_json", "thiserror 2.0.18", "token-history-contract", + "token-market-contract", "wallet-utils-contract", "withdrawals-contract", ] @@ -6623,6 +6624,7 @@ dependencies = [ "dash-async", "dash-context-provider", "dash-core-proof", + "data-contracts", "dpp", "futures", "gloo-timers", @@ -7914,6 +7916,17 @@ dependencies = [ "thiserror 2.0.18", ] +[[package]] +name = "token-market-contract" +version = "5.0.1" +dependencies = [ + "base58", + "platform-value", + "platform-version", + "serde_json", + "thiserror 2.0.18", +] + [[package]] name = "tokio" version = "1.52.3" diff --git a/Cargo.toml b/Cargo.toml index 4a553221f13..be7e350e517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ members = [ "packages/app-connect-contract", "packages/moderation-charters-contract", "packages/decision-proposals-contract", + "packages/token-market-contract", "packages/rs-sdk-ffi", "packages/rs-sdk-cxx", "packages/wasm-drive-verify", diff --git a/Dockerfile b/Dockerfile index 25c3010b9be..b857a35ce89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -409,6 +409,7 @@ COPY --parents \ packages/app-connect-contract \ packages/moderation-charters-contract \ packages/decision-proposals-contract \ + packages/token-market-contract \ packages/data-contracts \ packages/strategy-tests \ packages/simple-signer \ @@ -536,6 +537,7 @@ COPY --parents \ packages/app-connect-contract \ packages/moderation-charters-contract \ packages/decision-proposals-contract \ + packages/token-market-contract \ packages/withdrawals-contract \ packages/masternode-reward-shares-contract \ packages/dpns-contract \ @@ -724,6 +726,7 @@ COPY --parents \ packages/app-connect-contract \ packages/moderation-charters-contract \ packages/decision-proposals-contract \ + packages/token-market-contract \ packages/masternode-reward-shares-contract \ packages/dpns-contract \ packages/data-contracts \ @@ -872,6 +875,7 @@ COPY --from=build-dashmate-helper /platform/packages/keyword-search-contract pac COPY --from=build-dashmate-helper /platform/packages/app-connect-contract packages/app-connect-contract COPY --from=build-dashmate-helper /platform/packages/moderation-charters-contract packages/moderation-charters-contract COPY --from=build-dashmate-helper /platform/packages/decision-proposals-contract packages/decision-proposals-contract +COPY --from=build-dashmate-helper /platform/packages/token-market-contract packages/token-market-contract COPY --from=build-dashmate-helper /platform/packages/withdrawals-contract packages/withdrawals-contract COPY --from=build-dashmate-helper /platform/packages/masternode-reward-shares-contract packages/masternode-reward-shares-contract COPY --from=build-dashmate-helper /platform/packages/dpns-contract packages/dpns-contract @@ -978,6 +982,7 @@ COPY --parents \ packages/app-connect-contract \ packages/moderation-charters-contract \ packages/decision-proposals-contract \ + packages/token-market-contract \ packages/withdrawals-contract \ packages/masternode-reward-shares-contract \ packages/dpns-contract \ diff --git a/book/src/architecture/overview.md b/book/src/architecture/overview.md index a6c05c9b5a5..42e24dd3a8d 100644 --- a/book/src/architecture/overview.md +++ b/book/src/architecture/overview.md @@ -199,12 +199,12 @@ Several smaller crates provide cross-cutting infrastructure: The versioning backbone. Defines `PlatformVersion`, `ProtocolVersion`, and the version tables for every consensus-critical method across DPP, Drive, and -Drive-ABCI. Currently tracks 14 protocol versions (v1 through v14). +Drive-ABCI. Currently tracks 15 protocol versions (v1 through v15). ```rust // From packages/rs-platform-version/src/version/mod.rs pub type ProtocolVersion = u32; -pub const LATEST_VERSION: ProtocolVersion = PROTOCOL_VERSION_14; +pub const LATEST_VERSION: ProtocolVersion = PROTOCOL_VERSION_15; pub const INITIAL_PROTOCOL_VERSION: ProtocolVersion = 1; ``` @@ -281,7 +281,7 @@ Here is a simplified view of every Rust workspace member, grouped by role: | **gRPC definitions** | `dapi-grpc` | | **WASM bindings** | `wasm-dpp`, `wasm-dpp2`, `wasm-sdk`, `wasm-drive-verify` | | **iOS/FFI** | `rs-sdk-ffi` | -| **System contracts** | `dpns-contract`, `dashpay-contract`, `withdrawals-contract`, `masternode-reward-shares-contract`, `wallet-utils-contract`, `token-history-contract`, `keyword-search-contract`, `document-history-contract`, `app-connect-contract`, `moderation-charters-contract`, `decision-proposals-contract`, `data-contracts` | +| **System contracts** | `dpns-contract`, `dashpay-contract`, `withdrawals-contract`, `masternode-reward-shares-contract`, `wallet-utils-contract`, `token-history-contract`, `keyword-search-contract`, `document-history-contract`, `app-connect-contract`, `moderation-charters-contract`, `decision-proposals-contract`, `token-market-contract`, `data-contracts` | | **Tooling** | `dashmate` (JS), `strategy-tests`, `simple-signer`, `check-features`, `json-schema-compatibility-validator` | | **Other** | `dash-platform-macros`, `rs-dash-event-bus`, `rs-platform-wallet`, `dash-platform-balance-checker`, `rs-dapi` | diff --git a/book/src/data-model/token-marketplace.md b/book/src/data-model/token-marketplace.md index c1743a09bfb..25d52b69cae 100644 --- a/book/src/data-model/token-marketplace.md +++ b/book/src/data-model/token-marketplace.md @@ -49,3 +49,44 @@ created tradeable becomes tradeable only if its issuer kept the right to change A non-transferable token cannot be tradeable because filling a listing or an offer moves the token from the seller to the buyer, which is the transfer the token refuses. `transferable` is fixed when the token is created, so the trade mode is the only half of the rule an update can change. + +## Where orders and escrow live + +Open orders are documents of the token market system contract +(`AE6yYq65VdfGCg2MSyV9vV7cWGiFWjbwBuy7THesDh4e`), registered at genesis from protocol version 15 +and inserted by the upgrade to 15. It has two document types, `ask` (a listing) and `bid` (an +offer), each with three indexes: + +| Index | Properties | Serves | +|---|---|---| +| `book` | `tokenId`, `decimals`, `price`, `$createdAt` | A token's order book, by price and then by age | +| `depth` | `tokenId`, `decimals`, `price`, summed | The total open at each price, or over a price range, in one proven sum query: the base units of the asks (`remaining`) and the credits of the bids (`escrow`) | +| `byOwner` | `$ownerId`, `tokenId`, `$createdAt` | An identity's open orders | + +Each side sums what it holds in escrow, so the sums cannot overflow: the tokens listed never exceed +the token's supply, and the credits offered never exceed the credits on Platform. Every integer of +an order is a signed 8-byte value, so a partial fill rewrites an order without changing its stored +size. + +An order's price is in credits per whole token, and each order records the token's decimals when +it was placed (`decimals`). Every fill prices the order at that scale, so a later change to the +token's decimals (a `Conventions` update) changes neither what an open listing costs nor what an +open offer pays. The `book` and `depth` indexes keep each scale apart (`tokenId`, then `decimals`, +then `price`), so a book query and a depth sum name the token's decimals and never mix prices of +different scales. After a token changes its decimals, the orders placed before the change stay +under the old decimals until they close; a client showing them alongside the new ones queries both +and scales each by its `decimals`. + +Only the marketplace transitions write orders. A document transition cannot create one +(`creationRestrictionMode` 2), and a replace or a delete of one is refused by a data trigger. The +types stay mutable and deletable so the documents carry a revision and storage flags: the order's +owner pays its storage and gets it back when the order closes. + +What an order holds is kept outside the contract: + +- The tokens of a listing sit in `[Tokens, TOKEN_ORDER_ESCROW_KEY]`, a BigSumTree with one SumTree + per token, keyed by order id. The token conservation check counts it beside the identity + balances and the shielded pools. +- The credits of an offer sit in `[PreFundedSpecializedBalances, PREFUNDED_BALANCES_FOR_TOKEN_ORDERS]`, + a SumTree keyed by order id, which the credit conservation check already counts. + diff --git a/package.json b/package.json index d8a6a5ca3aa..8f78006250e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "test:app-connect-contract": "ultra -r --filter \"packages/@(app-connect-contract|js-dash-sdk|js-drive|js-dapi-client|wasm-dpp|wallet-lib|dapi|platform-test-suite)\" test", "test:moderation-charters-contract": "ultra -r --filter \"packages/@(moderation-charters-contract|js-dash-sdk|js-drive|js-dapi-client|wasm-dpp|wallet-lib|dapi|platform-test-suite)\" test", "test:decision-proposals-contract": "ultra -r --filter \"packages/@(decision-proposals-contract|js-dash-sdk|js-drive|js-dapi-client|wasm-dpp|wallet-lib|dapi|platform-test-suite)\" test", + "test:token-market-contract": "ultra -r --filter \"packages/@(token-market-contract|js-dash-sdk|js-drive|js-dapi-client|wasm-dpp|wallet-lib|dapi|platform-test-suite)\" test", "test:dapi-client": "ultra -r --filter \"packages/@(js-dapi-client|wallet-lib|js-dash-sdk|platform-test-suite)\" test", "test:sdk": "ultra -r --filter \"packages/@(js-dash-sdk|platform-test-suite)\" test", "test:spv": "ultra -r --filter \"packages/@(dash-spv|js-dapi-client)\" test", @@ -83,6 +84,7 @@ "packages/app-connect-contract", "packages/moderation-charters-contract", "packages/decision-proposals-contract", + "packages/token-market-contract", "packages/wasm-drive-verify", "packages/wasm-sdk", "packages/js-evo-sdk" diff --git a/packages/data-contracts/Cargo.toml b/packages/data-contracts/Cargo.toml index 940c043b195..a269ce985ef 100644 --- a/packages/data-contracts/Cargo.toml +++ b/packages/data-contracts/Cargo.toml @@ -21,6 +21,7 @@ all-contracts = [ "app-connect", "moderation-charters", "decision-proposals", + "token-market", ] # Individual contract features @@ -35,6 +36,7 @@ document-history = ["dep:document-history-contract"] app-connect = ["dep:app-connect-contract"] moderation-charters = ["dep:moderation-charters-contract"] decision-proposals = ["dep:decision-proposals-contract"] +token-market = ["dep:token-market-contract"] [dependencies] thiserror = "2.0.12" @@ -52,6 +54,7 @@ document-history-contract = { path = "../document-history-contract", optional = app-connect-contract = { path = "../app-connect-contract", optional = true } moderation-charters-contract = { path = "../moderation-charters-contract", optional = true } decision-proposals-contract = { path = "../decision-proposals-contract", optional = true } +token-market-contract = { path = "../token-market-contract", optional = true } [dev-dependencies] base58 = "0.2.0" diff --git a/packages/data-contracts/src/error.rs b/packages/data-contracts/src/error.rs index ae66a5feb0d..b9174eb3d1f 100644 --- a/packages/data-contracts/src/error.rs +++ b/packages/data-contracts/src/error.rs @@ -221,3 +221,21 @@ impl From for Error { } } } + +#[cfg(feature = "token-market")] +impl From for Error { + fn from(e: token_market_contract::Error) -> Self { + match e { + token_market_contract::Error::UnknownVersionMismatch { + method, + known_versions, + received, + } => Error::UnknownVersionMismatch { + method, + known_versions, + received, + }, + token_market_contract::Error::InvalidSchemaJson(e) => Error::InvalidSchemaJson(e), + } + } +} diff --git a/packages/data-contracts/src/lib.rs b/packages/data-contracts/src/lib.rs index 76d3c242b74..7ee3f71f725 100644 --- a/packages/data-contracts/src/lib.rs +++ b/packages/data-contracts/src/lib.rs @@ -34,6 +34,9 @@ pub use document_history_contract; #[cfg(feature = "token-history")] pub use token_history_contract; +#[cfg(feature = "token-market")] +pub use token_market_contract; + #[cfg(feature = "wallet-utils")] pub use wallet_utils_contract; @@ -64,6 +67,10 @@ pub enum SystemDataContract { /// vote poll it funds (protocol version 15). Registered from protocol version 15 on: /// registered at genesis by chains born at 15 and inserted by the upgrade to 15. DecisionProposals = 11, + /// The open listings and offers of the token marketplace (protocol version 15). Registered + /// from protocol version 15 on: registered at genesis by chains born at 15 and inserted by + /// the upgrade to 15. + TokenMarket = 12, } pub struct DataContractSource { @@ -80,7 +87,7 @@ impl SystemDataContract { /// Deliberately kept beside the enum so that adding a variant and adding it here are the /// same edit. `assert_every_variant_is_listed` below makes that mechanical rather than /// remembered: a new variant makes its match non-exhaustive and the crate stops compiling. - pub const ALL: [SystemDataContract; 12] = [ + pub const ALL: [SystemDataContract; 13] = [ SystemDataContract::Withdrawals, SystemDataContract::MasternodeRewards, SystemDataContract::FeatureFlags, @@ -93,6 +100,7 @@ impl SystemDataContract { SystemDataContract::AppConnect, SystemDataContract::ModerationCharters, SystemDataContract::DecisionProposals, + SystemDataContract::TokenMarket, ]; /// A new variant must also be added to [`SystemDataContract::ALL`]; this match is where the @@ -194,6 +202,13 @@ impl SystemDataContract { 40, 81, 10, 2, 166, 107, 62, 218, 6, 184, 111, 202, 230, 218, 170, 157, 181, 119, 102, 211, 195, 114, 92, 200, 90, 88, 205, 30, 174, 49, 203, 200, ], + #[cfg(feature = "token-market")] + SystemDataContract::TokenMarket => token_market_contract::ID_BYTES, + #[cfg(not(feature = "token-market"))] + SystemDataContract::TokenMarket => [ + 137, 20, 125, 33, 9, 113, 160, 140, 158, 199, 2, 183, 75, 230, 71, 38, 213, 195, + 65, 13, 129, 32, 10, 91, 226, 116, 230, 164, 77, 101, 0, 147, + ], }; Identifier::new(bytes) } @@ -344,6 +359,16 @@ impl SystemDataContract { SystemDataContract::DecisionProposals => { Err(Error::ContractNotIncluded("decision-proposals")) } + #[cfg(feature = "token-market")] + SystemDataContract::TokenMarket => Ok(DataContractSource { + id_bytes: token_market_contract::ID_BYTES, + owner_id_bytes: token_market_contract::OWNER_ID_BYTES, + version: platform_version.system_data_contracts.token_market as u32, + definitions: token_market_contract::load_definitions(platform_version)?, + document_schemas: token_market_contract::load_documents_schemas(platform_version)?, + }), + #[cfg(not(feature = "token-market"))] + SystemDataContract::TokenMarket => Err(Error::ContractNotIncluded("token-market")), } } } @@ -399,6 +424,9 @@ mod tests { SystemDataContract::DecisionProposals => { published("3iNyoNwjCTkb7XS3RHyWUtLx7pgC6AFAEQNNDHzxqngF") } + SystemDataContract::TokenMarket => { + published("AE6yYq65VdfGCg2MSyV9vV7cWGiFWjbwBuy7THesDh4e") + } }; assert_eq!( diff --git a/packages/rs-dpp/Cargo.toml b/packages/rs-dpp/Cargo.toml index e67bbc6cd5c..26c7e1b7a6d 100644 --- a/packages/rs-dpp/Cargo.toml +++ b/packages/rs-dpp/Cargo.toml @@ -309,6 +309,7 @@ all-system_contracts = [ "app-connect-contract", "moderation-charters-contract", "decision-proposals-contract", + "token-market-contract", ] # Individual data contract features @@ -326,6 +327,7 @@ document-history-contract = ["data-contracts", "data-contracts/document-history" app-connect-contract = ["data-contracts", "data-contracts/app-connect"] moderation-charters-contract = ["data-contracts", "data-contracts/moderation-charters"] decision-proposals-contract = ["data-contracts", "data-contracts/decision-proposals"] +token-market-contract = ["data-contracts", "data-contracts/token-market"] fixtures-and-mocks = ["all-system_contracts", "platform-value/json"] random-public-keys = ["bls-signatures", "ed25519-dalek"] random-identities = ["random-public-keys"] diff --git a/packages/rs-dpp/src/balances/total_tokens_balance/mod.rs b/packages/rs-dpp/src/balances/total_tokens_balance/mod.rs index 7f920242e54..d29cc435c1b 100644 --- a/packages/rs-dpp/src/balances/total_tokens_balance/mod.rs +++ b/packages/rs-dpp/src/balances/total_tokens_balance/mod.rs @@ -11,6 +11,9 @@ pub struct TotalTokensBalance { pub total_identity_token_balances: SumTokenAmount, /// all the tokens held in token shielded pools (0 before protocol version 14) pub total_token_shielded_pool_balances: SumTokenAmount, + /// all the tokens the token marketplace holds in escrow for open listings (0 before + /// protocol version 15) + pub total_token_order_escrow_balances: SumTokenAmount, } impl fmt::Display for TotalTokensBalance { @@ -28,9 +31,14 @@ impl fmt::Display for TotalTokensBalance { )?; writeln!( f, - " total_token_shielded_pool_balances: {}", + " total_token_shielded_pool_balances: {},", self.total_token_shielded_pool_balances )?; + writeln!( + f, + " total_token_order_escrow_balances: {}", + self.total_token_order_escrow_balances + )?; write!(f, "}}") } } @@ -42,6 +50,7 @@ impl TotalTokensBalance { total_tokens_in_platform, total_identity_token_balances, total_token_shielded_pool_balances, + total_token_order_escrow_balances, } = *self; if total_tokens_in_platform < 0 { @@ -62,26 +71,64 @@ impl TotalTokensBalance { )); } - let total_balances = total_identity_token_balances - .checked_add(total_token_shielded_pool_balances) - .ok_or_else(|| { - ProtocolError::CriticalCorruptedCreditsCodeExecution( - "Overflow adding identity and shielded pool token balances".to_string(), - ) - })?; + // Every protocol version runs this check. The escrow term is zero below protocol + // version 15, where `calculate_total_tokens_balance` 0 and 1 report none, so this + // refusal and its share of the sum below cannot change their verdict. + if total_token_order_escrow_balances < 0 { + return Err(ProtocolError::CriticalCorruptedCreditsCodeExecution( + "Tokens in order escrow are less than 0".to_string(), + )); + } - Ok(total_tokens_in_platform == total_balances) + Ok(total_tokens_in_platform == self.total_balances()?) } /// The balance side of the conservation equation: identity balances plus shielded pool - /// balances. Errors on overflow. + /// balances plus the tokens held in escrow for open listings. Errors on overflow. pub fn total_balances(&self) -> Result { self.total_identity_token_balances .checked_add(self.total_token_shielded_pool_balances) + .and_then(|balances| balances.checked_add(self.total_token_order_escrow_balances)) .ok_or_else(|| { ProtocolError::CriticalCorruptedCreditsCodeExecution( - "Overflow adding identity and shielded pool token balances".to_string(), + "Overflow adding identity, shielded pool and order escrow token balances" + .to_string(), ) }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_count_the_tokens_held_in_order_escrow_on_the_balance_side() { + let balance = TotalTokensBalance { + total_tokens_in_platform: 100, + total_identity_token_balances: 60, + total_token_shielded_pool_balances: 15, + total_token_order_escrow_balances: 25, + }; + assert!(balance.ok().expect("expected no overflow")); + assert_eq!(balance.total_balances().expect("expected no overflow"), 100); + + // Leaving the escrow out of the balance side is a broken conservation check. + let without_escrow = TotalTokensBalance { + total_token_order_escrow_balances: 0, + ..balance + }; + assert!(!without_escrow.ok().expect("expected no overflow")); + } + + #[test] + fn should_refuse_a_negative_order_escrow_total() { + let balance = TotalTokensBalance { + total_tokens_in_platform: 0, + total_identity_token_balances: 0, + total_token_shielded_pool_balances: 0, + total_token_order_escrow_balances: -1, + }; + assert!(balance.ok().is_err()); + } +} diff --git a/packages/rs-dpp/src/system_data_contracts.rs b/packages/rs-dpp/src/system_data_contracts.rs index df426efcf30..2360eb394b6 100644 --- a/packages/rs-dpp/src/system_data_contracts.rs +++ b/packages/rs-dpp/src/system_data_contracts.rs @@ -84,6 +84,15 @@ impl ConfigurationForSystemContract for SystemDataContract { config.set_sized_integer_types_enabled(true); Ok(config) } + // Every integer of an order is a signed 8-byte value: the `depth` index of each + // order type sums one of them, which a summed property must be, and a partial fill + // rewrites `remaining` (and a bid's `escrow`) without changing the stored size of + // the order. + SystemDataContract::TokenMarket => { + let mut config = DataContractConfig::default_for_version(platform_version)?; + config.set_sized_integer_types_enabled(false); + Ok(config) + } } } } @@ -183,6 +192,7 @@ mod tests { SystemDataContract::DecisionProposals => { contract_versions.decision_proposals == 0 } + SystemDataContract::TokenMarket => contract_versions.token_market == 0, SystemDataContract::Withdrawals | SystemDataContract::MasternodeRewards | SystemDataContract::DPNS @@ -1459,3 +1469,114 @@ mod dpns_v3_tests { ); } } + +#[cfg(all(test, feature = "token-market-contract", feature = "validation"))] +mod token_market_tests { + use super::*; + use crate::data_contract::accessors::v0::DataContractV0Getters; + use crate::data_contract::document_type::accessors::{ + DocumentTypeV0Getters, DocumentTypeV2Getters, + }; + use crate::data_contract::document_type::restricted_creation::CreationRestrictionMode; + use crate::data_contract::document_type::DocumentPropertyType; + use crate::document::transfer::Transferable; + use crate::nft::TradeMode; + use crate::system_data_contracts::token_market_contract::v1::document_types::{ask, bid}; + + /// The order types are what the marketplace transitions rely on: only they create orders, + /// orders stay mutable and deletable so a partial fill can rewrite them and a close can + /// delete them with their storage flags, every integer is a signed 8-byte value, and the + /// `depth` index sums what the order holds in escrow. + #[test] + fn should_load_the_order_types_the_marketplace_writes() { + let contract = + load_system_data_contract(SystemDataContract::TokenMarket, PlatformVersion::latest()) + .expect("the token market contract loads with full validation"); + assert_eq!(contract.id(), token_market_contract::ID); + assert_eq!(contract.owner_id(), token_market_contract::OWNER_ID); + assert_eq!(contract.document_types().len(), 2); + + for (name, summed) in [ + (ask::NAME, ask::properties::REMAINING), + (bid::NAME, bid::properties::ESCROW), + ] { + let document_type = contract + .document_type_for_name(name) + .unwrap_or_else(|_| panic!("the {name} type")); + assert!(document_type.documents_mutable(), "{name} is mutable"); + assert!( + document_type.documents_can_be_deleted(), + "{name} is deletable" + ); + assert_eq!( + document_type.creation_restriction_mode(), + CreationRestrictionMode::NoCreationAllowed, + "only the marketplace transitions create {name} orders" + ); + // The data trigger bindings refuse a user's replace and delete; these defaults + // refuse the remaining document transitions (transfer, purchase, price update and + // index-only delete), so a schema edit that enables one fails here. + assert_eq!( + document_type.documents_transferable(), + Transferable::Never, + "{name} orders cannot be transferred" + ); + assert_eq!( + document_type.trade_mode(), + TradeMode::None, + "{name} orders cannot be priced or purchased as documents" + ); + assert!( + !document_type.index_only(), + "{name} orders are not index-only" + ); + for property in document_type.properties().values() { + if matches!( + property.property_type, + DocumentPropertyType::U8 + | DocumentPropertyType::U16 + | DocumentPropertyType::U32 + | DocumentPropertyType::U64 + | DocumentPropertyType::I8 + | DocumentPropertyType::I16 + | DocumentPropertyType::I32 + ) { + panic!("{name} stores an integer narrower than i64 or unsigned"); + } + } + let depth = document_type + .indexes() + .get(ask::indexes::DEPTH) + .unwrap_or_else(|| panic!("{name} has a depth index")); + assert_eq!(depth.summable.as_deref(), Some(summed)); + // Prices are per whole token at each order's decimals, so the book and the depth + // sums are kept apart per scale: a query names the token and the decimals. + let index_properties = |index_name: &str| -> Vec { + document_type + .indexes() + .get(index_name) + .unwrap_or_else(|| panic!("{name} has a {index_name} index")) + .properties + .iter() + .map(|property| property.name.clone()) + .collect() + }; + assert_eq!( + index_properties(ask::indexes::DEPTH), + ["tokenId", "decimals", "price"] + ); + assert_eq!( + index_properties(ask::indexes::BOOK), + ["tokenId", "decimals", "price", "$createdAt"] + ); + } + } + + #[test] + fn should_not_exist_before_protocol_version_15() { + let platform_version = PlatformVersion::get(14).expect("protocol version 14"); + assert!( + load_system_data_contract(SystemDataContract::TokenMarket, platform_version).is_err() + ); + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v1/mod.rs index 3c58125521b..55c0ff2f344 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/initialization/create_genesis_state/v1/mod.rs @@ -6,6 +6,7 @@ use drive::dpp::identity::TimestampMillis; use dpp::block::block_info::BlockInfo; use dpp::prelude::CoreBlockHeight; use dpp::system_data_contracts::load_system_data_contract; +use dpp::version::feature_initial_protocol_versions::TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION; use dpp::version::PlatformVersion; use drive::dpp::system_data_contracts::SystemDataContract; use drive::query::TransactionArg; @@ -98,6 +99,15 @@ impl Platform { ); } + // The token market contract activates with protocol version 15: a chain born at 15 + // registers it at genesis, an older chain gets it from `transition_to_version_15`. + if platform_version.protocol_version >= TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION { + system_data_contract_types.insert( + SystemDataContract::TokenMarket, + system_data_contracts.load_token_market(platform_version)?, + ); + } + for data_contract in system_data_contract_types.values() { self.register_system_data_contract_operations( data_contract, @@ -315,5 +325,48 @@ mod tests { } } } + + /// The token market contract joins the genesis state at protocol version 15. + #[test] + pub fn should_register_the_token_market_contract_only_from_protocol_version_15() { + let token_market_id = SystemDataContract::TokenMarket.id(); + + for (platform_version, expected) in [ + (PlatformVersion::get(14).expect("protocol 14"), false), + (PlatformVersion::latest(), true), + ] { + let initial_protocol_version = platform_version.protocol_version; + let platform = TestPlatformBuilder::new() + .with_initial_protocol_version(initial_protocol_version) + .build_with_mock_rpc() + .set_genesis_state(); + + let stored = platform + .drive + .fetch_contract( + token_market_id.to_buffer(), + None, + None, + None, + platform_version, + ) + .value + .expect("expected to query the token market contract"); + + assert_eq!( + stored.is_some(), + expected, + "token market contract presence in a genesis state born at protocol version {initial_protocol_version}" + ); + + if let Some(stored) = stored { + assert_eq!(stored.contract.id(), token_market_id); + assert_eq!(stored.contract.owner_id(), Identifier::from([0u8; 32])); + assert!(stored.contract.document_type_for_name("ask").is_ok()); + assert!(stored.contract.document_type_for_name("bid").is_ok()); + assert_eq!(stored.contract.document_types().len(), 2); + } + } + } } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs index 78094eaa3b1..f211141e529 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/protocol_upgrade/perform_events_on_first_block_of_protocol_change/v0/mod.rs @@ -915,8 +915,9 @@ impl Platform { /// When transitioning to version 15 we re-store the DPNS contract whose v3 /// schema checks a domain create with keywords instead of most of its data - /// trigger, create the vote decisions trees that yes/no polls live in, and - /// insert the decision proposals contract whose proposals open those polls. + /// trigger, create the vote decisions trees that yes/no polls live in, + /// insert the decision proposals contract whose proposals open those polls, and add + /// the token marketplace: its escrow trees and the token market contract. fn transition_to_version_15( &self, block_info: &BlockInfo, @@ -959,6 +960,30 @@ impl Platform { platform_version, )?; + // Token marketplace escrow trees (v15 note 17): the BigSumTree under the Tokens tree of + // the tokens open listings hold, and the SumTree under the prefunded specialized + // balances of the credits open offers hold. The same helper builds them at genesis + // (`create_initial_state_structure_v5`), last, so both node populations build the same + // `[Tokens]` and `[PreFundedSpecializedBalances]` subtrees. + self.drive + .insert_token_order_escrow_trees(Some(transaction), platform_version)?; + + // Token market contract: the open listings and offers of the token marketplace, one + // system contract id on every network from this version. Fresh chains register it at + // genesis (`create_genesis_state` v1). Stored without storage flags, as genesis stores + // system contracts. + let token_market_contract = + load_system_data_contract(SystemDataContract::TokenMarket, platform_version)?; + + self.drive.apply_contract( + &token_market_contract, + *block_info, + true, + None, + Some(transaction), + platform_version, + )?; + Ok(()) } } @@ -4164,6 +4189,206 @@ mod tests { ); } + /// The token marketplace escrow trees are built by two paths that must coincide byte for + /// byte: `Drive::create_initial_state_structure_v5` at a v15 genesis and + /// `transition_to_version_15` on a chain upgraded from v14. + #[test] + fn test_genesis_v15_and_upgrade_to_v15_build_identical_token_order_escrow_trees() { + use drive::drive::prefunded_specialized_balances::PREFUNDED_BALANCES_FOR_TOKEN_ORDERS; + use drive::drive::tokens::paths::TOKEN_ORDER_ESCROW_KEY; + + let platform_version_15 = PlatformVersion::get(15).expect("expected v15"); + + let platform_a = TestPlatformBuilder::new() + .with_initial_protocol_version(15) + .build_with_mock_rpc() + .set_genesis_state(); + + let platform_b = TestPlatformBuilder::new() + .with_initial_protocol_version(14) + .build_with_mock_rpc() + .set_genesis_state(); + + // A genuine v14 genesis has neither escrow tree yet. + { + let txn = platform_b.drive.grove.start_transaction(); + for (root, key) in [ + (RootTree::Tokens as u8, TOKEN_ORDER_ESCROW_KEY), + ( + RootTree::PreFundedSpecializedBalances as u8, + PREFUNDED_BALANCES_FOR_TOKEN_ORDERS, + ), + ] { + let root_path: [&[u8]; 1] = [&[root]]; + let escrow_root = platform_b.drive.grove.get( + SubtreePath::from(&root_path[..]), + &[key], + Some(&txn), + &platform_version_15.drive.grove_version, + ); + assert!( + escrow_root.value.is_err(), + "v14 genesis must not contain the escrow tree {key} under root {root}; got {:?}", + escrow_root.value + ); + } + } + + let txn_b = platform_b.drive.grove.start_transaction(); + platform_b + .transition_to_version_15(&BlockInfo::default(), &txn_b, platform_version_15) + .expect("upgrade: transition_to_version_15 should succeed"); + + let roots = [ + RootTree::Tokens as u8, + RootTree::PreFundedSpecializedBalances as u8, + ]; + // `collect_subtree_diffs` walks one subtree from its path, so each root is compared on + // its own. + let mut diffs = Vec::new(); + for root in roots { + diffs.extend(collect_subtree_diffs( + &platform_a, + &platform_b, + &txn_b, + vec![vec![root]], + )); + } + assert!( + diffs.is_empty(), + "CONSENSUS FORK: the [Tokens] or [PreFundedSpecializedBalances] subtree differs \ + between a fresh genesis-v15 node and an in-place-upgraded v15 node.\n{}", + diffs.join("\n"), + ); + + // The children agree; the Merk of each root holds the new key itself, so compare the + // root elements too, which carry its root key and sum. + let root_element = |platform: &crate::platform_types::platform::Platform< + crate::rpc::core::MockCoreRPCLike, + >, + root: u8, + txn: drive::grovedb::TransactionArg| { + platform + .drive + .grove + .get_raw( + drive::grovedb_path::SubtreePath::empty(), + &[root], + txn, + &platform_version_15.drive.grove_version, + ) + .unwrap() + .expect("expected to read the root element") + }; + for root in roots { + assert_eq!( + root_element(&platform_a, root, None), + root_element(&platform_b, root, Some(&txn_b)), + "CONSENSUS FORK: the Merk of root {root} is shaped differently on a fresh \ + genesis-v15 node and an in-place-upgraded one" + ); + } + + // The walks above compare decoded keys and elements, which carry no Merk hash: two + // subtrees holding the same entries in a differently balanced tree would pass them. + // A tree element's value hash in its parent Merk commits to the root hash of the Merk + // below it, and so to everything that subtree holds and how it is arranged; a raw proof + // of the root element yields it. GroveDB proves only committed state, so the upgrade is + // committed first. + platform_b + .drive + .grove + .commit_transaction(txn_b) + .unwrap() + .expect("expected to commit the upgrade"); + let subtree_hash = |platform: &crate::platform_types::platform::Platform< + crate::rpc::core::MockCoreRPCLike, + >, + root: u8| { + let grove_version = &platform_version_15.drive.grove_version; + let query = drive::grovedb::PathQuery::new_single_key(vec![], vec![root]); + let proof = platform + .drive + .grove + .get_proved_path_query(&query, None, None, grove_version) + .unwrap() + .expect("expected to prove the root element"); + let (_, proved) = + drive::grovedb::GroveDb::verify_query_raw(&proof, &query, grove_version) + .expect("expected the proof of the root element to verify"); + assert_eq!(proved.len(), 1, "expected the root element alone"); + proved[0].proof + }; + for root in roots { + assert_eq!( + subtree_hash(&platform_a, root), + subtree_hash(&platform_b, root), + "CONSENSUS FORK: root {root} authenticates to a different hash on a fresh \ + genesis-v15 node and an in-place-upgraded one" + ); + } + } + + #[test] + fn should_insert_the_token_market_contract_on_transition_to_version_15() { + use dpp::data_contract::accessors::v0::DataContractV0Getters; + + let platform = TestPlatformBuilder::new() + .with_initial_protocol_version(14) + .build_with_mock_rpc() + .set_genesis_state(); + + let platform_version_14 = PlatformVersion::get(14).expect("expected platform version 14"); + let platform_version = PlatformVersion::get(15).expect("expected platform version 15"); + let token_market_id = SystemDataContract::TokenMarket.id(); + + let transaction = platform.drive.grove.start_transaction(); + + assert!( + platform + .drive + .fetch_contract( + token_market_id.to_buffer(), + None, + None, + Some(&transaction), + platform_version_14, + ) + .value + .expect("expected to query the token market contract") + .is_none(), + "the token market contract must not exist before transition_to_version_15" + ); + + platform + .transition_to_version_15(&BlockInfo::default(), &transaction, platform_version) + .expect("expected the transition to succeed"); + + let stored = platform + .drive + .fetch_contract( + token_market_id.to_buffer(), + None, + None, + Some(&transaction), + platform_version, + ) + .value + .expect("expected to fetch the token market contract") + .expect("the token market contract must exist after transition_to_version_15"); + + assert_eq!(stored.contract.id(), token_market_id); + assert_eq!(stored.contract.owner_id(), Identifier::from([0u8; 32])); + assert_eq!(stored.contract.document_types().len(), 2); + assert!(platform + .drive + .cache + .system_data_contracts + .find_by_id(token_market_id, platform_version) + .expect("expected the post-activation lookup to succeed") + .is_some()); + } + /// CONSENSUS-CRITICAL equivalence guard for the v11→v12 boundary. /// /// The `[ShieldedBalances]` subtree is built two ways that MUST be diff --git a/packages/rs-drive-abci/src/execution/platform_events/tokens/validate_token_aggregated_balance/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/tokens/validate_token_aggregated_balance/v0/mod.rs index 6408070828f..d2b02d5b4a0 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/tokens/validate_token_aggregated_balance/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/tokens/validate_token_aggregated_balance/v0/mod.rs @@ -21,10 +21,11 @@ impl Platform { if !token_balance.ok()? { // The balance side of the conservation equation includes the token shielded - // pools. Every protocol version selects this generation, and that is inert at - // the ones that predate those pools: below the token pool protocol version - // `calculate_total_tokens_balance` reports a pool total of zero, so - // `total_balances` is the identity balance total it always was. The line is on + // pools and the token order escrow. Every protocol version selects this + // generation, and that is inert at the ones that predate them: below the token + // pool protocol version `calculate_total_tokens_balance` reports a pool total of + // zero, and below 15 an escrow total of zero, so `total_balances` is the + // identity balance total it always was. The line is on // the diagnostic of an already-failed invariant in any case; the accept or // reject decision is `TotalTokensBalance::ok`. return Err(Error::Execution( diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/mod.rs index e7e67702fd8..7a34659aa3e 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/mod.rs @@ -5,6 +5,7 @@ use crate::execution::validation::state_transition::batch::data_triggers::bindin mod v0; mod v1; mod v2; +mod v3; pub fn data_trigger_bindings_list( platform_version: &PlatformVersion, @@ -29,10 +30,177 @@ pub fn data_trigger_bindings_list( .into_iter() .map(|binding| binding.into()) .collect()), + 3 => Ok(v3::data_trigger_bindings_list_v3()? + .into_iter() + .map(|binding| binding.into()) + .collect()), version => Err(ProtocolError::UnknownVersionMismatch { method: "data_trigger_bindings".to_string(), - known_versions: vec![0, 1, 2], + known_versions: vec![0, 1, 2, 3], received: version, }), } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::execution::types::state_transition_execution_context::v0::StateTransitionExecutionContextV0; + use crate::execution::types::state_transition_execution_context::StateTransitionExecutionContext; + use crate::execution::validation::state_transition::batch::data_triggers::bindings::data_trigger_binding::DataTriggerBindingV0Getters; + use crate::execution::validation::state_transition::batch::data_triggers::DataTriggerExecutionContext; + use crate::platform_types::platform::PlatformStateRef; + use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::block::block_info::BlockInfo; + use dpp::consensus::state::data_trigger::DataTriggerError; + use dpp::identifier::Identifier; + use dpp::system_data_contracts::token_market_contract; + use dpp::system_data_contracts::token_market_contract::v1::document_types::{ask, bid}; + use dpp::tokens::gas_fees_paid_by::GasFeesPaidBy; + use drive::drive::contract::DataContractFetchInfo; + use drive::state_transition_action::batch::batched_transition::document_transition::document_base_transition_action::{DocumentBaseTransitionAction, DocumentBaseTransitionActionV0}; + use drive::state_transition_action::batch::batched_transition::document_transition::document_delete_transition_action::v0::DocumentDeleteTransitionActionV0; + use drive::state_transition_action::batch::batched_transition::document_transition::document_delete_transition_action::DocumentDeleteTransitionAction; + use drive::state_transition_action::batch::batched_transition::document_transition::document_replace_transition_action::{ + DocumentReplaceTransitionAction, DocumentReplaceTransitionActionV0, + }; + use drive::state_transition_action::batch::batched_transition::document_transition::{ + DocumentTransitionAction, DocumentTransitionActionType, + }; + use std::sync::Arc; + + /// A replace or a delete of the order `document_type_name` sent as a document transition. + fn order_write( + document_type_name: &str, + action_type: DocumentTransitionActionType, + contract: Arc, + ) -> DocumentTransitionAction { + let base = DocumentBaseTransitionAction::V0(DocumentBaseTransitionActionV0 { + id: Identifier::from([9; 32]), + identity_contract_nonce: 1, + document_type_name: document_type_name.to_string(), + data_contract: contract, + token_cost: None, + gas_fees_paid_by: GasFeesPaidBy::DocumentOwner, + contract_gas_fees_paid_by: GasFeesPaidBy::default(), + declared_action_fee: None, + shielded_token_payment: None, + }); + match action_type { + DocumentTransitionActionType::Replace => DocumentTransitionAction::ReplaceAction( + DocumentReplaceTransitionAction::V0(DocumentReplaceTransitionActionV0 { + base, + revision: 2, + created_at: None, + updated_at: None, + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + data: Default::default(), + changed_data_fields: Default::default(), + removed_identifier_fields: Default::default(), + stored_changed_values: Default::default(), + creator_id: None, + moderated_at: None, + moderated_by: None, + property_constraint_reads: None, + }), + ), + DocumentTransitionActionType::Delete => DocumentTransitionAction::DeleteAction( + DocumentDeleteTransitionAction::V0(DocumentDeleteTransitionActionV0 { base }), + ), + other => panic!("not an order write: {other:?}"), + } + } + + /// Through the version dispatch: the latest protocol version binds exactly one trigger to + /// each replace and delete of an `ask` and a `bid`, and running it refuses the write with + /// `DataTriggerConditionError`. Protocol version 14 binds none of them. + #[test] + fn should_refuse_order_writes_through_the_latest_bindings_and_bind_none_before_15() { + let platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + let state = platform.state.load(); + let platform_ref = PlatformStateRef { + drive: &platform.drive, + state: &state, + config: &platform.config, + }; + let latest = PlatformVersion::latest(); + let contract = platform + .drive + .get_contract_with_fetch_info( + token_market_contract::ID.to_buffer(), + false, + None, + latest, + ) + .expect("expected to fetch the token market contract") + .expect("expected the token market contract at genesis"); + let order_writes = [ask::NAME, bid::NAME].into_iter().flat_map(|name| { + [ + DocumentTransitionActionType::Replace, + DocumentTransitionActionType::Delete, + ] + .into_iter() + .map(move |action_type| (name, action_type)) + }); + + let bindings = data_trigger_bindings_list(latest).expect("expected the bindings"); + for (name, action_type) in order_writes.clone() { + let matching: Vec<&DataTriggerBinding> = bindings + .iter() + .filter(|binding| { + binding.is_matching(&token_market_contract::ID, name, action_type) + }) + .collect(); + assert_eq!(matching.len(), 1, "{name} {action_type:?}"); + let mut execution_context = + StateTransitionExecutionContext::V0(StateTransitionExecutionContextV0::default()); + let block_info = BlockInfo::default(); + let owner_id = Identifier::from([7; 32]); + let mut context = DataTriggerExecutionContext { + platform: &platform_ref, + block_info: &block_info, + owner_id: &owner_id, + state_transition_execution_context: &mut execution_context, + transaction: None, + }; + let result = matching[0] + .execute( + &order_write(name, action_type, Arc::clone(&contract)), + &mut context, + latest, + ) + .expect("expected the trigger to run"); + assert!( + matches!( + result.errors.as_slice(), + [DataTriggerError::DataTriggerConditionError(_)] + ), + "{name} {action_type:?}: {:?}", + result.errors + ); + } + + let before = data_trigger_bindings_list( + PlatformVersion::get(14).expect("expected protocol version 14"), + ) + .expect("expected the bindings"); + for (name, action_type) in order_writes { + assert!( + !before.iter().any(|binding| binding.is_matching( + &token_market_contract::ID, + name, + action_type + )), + "{name} {action_type:?} is bound before 15" + ); + } + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/v3/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/v3/mod.rs new file mode 100644 index 00000000000..d4d53e6885e --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/data_triggers/bindings/list/v3/mod.rs @@ -0,0 +1,66 @@ +use crate::execution::validation::state_transition::batch::data_triggers::bindings::data_trigger_binding::DataTriggerBindingV0; +use crate::execution::validation::state_transition::batch::data_triggers::bindings::list::v2::data_trigger_bindings_list_v2; +use crate::execution::validation::state_transition::batch::data_triggers::triggers::reject::reject_data_trigger; + +use dpp::errors::ProtocolError; +use dpp::system_data_contracts::token_market_contract; +use dpp::system_data_contracts::token_market_contract::v1::document_types::{ask, bid}; +use drive::state_transition_action::batch::batched_transition::document_transition::DocumentTransitionActionType; + +/// Retrieves a list of data triggers binding with matching params. +/// +/// v3 (PROTOCOL_VERSION_15): v2 plus a reject of every replace and delete of a token market +/// contract order (`ask` and `bid`) sent as a document transition. Only the token marketplace +/// transitions write orders: creating one as a document is refused by +/// `creationRestrictionMode` 2, and the types stay mutable and deletable, so documents carry a +/// revision and storage flags for the partial fills and closes the platform writes. +#[inline(always)] +pub(super) fn data_trigger_bindings_list_v3() -> Result, ProtocolError> { + let mut data_triggers = data_trigger_bindings_list_v2()?; + for document_type in [ask::NAME, bid::NAME] { + for transition_action_type in [ + DocumentTransitionActionType::Replace, + DocumentTransitionActionType::Delete, + ] { + data_triggers.push(DataTriggerBindingV0 { + data_contract_id: token_market_contract::ID, + document_type: document_type.to_string(), + transition_action_type, + data_trigger: reject_data_trigger, + }); + } + } + Ok(data_triggers) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_reject_every_replace_and_delete_of_an_order_and_keep_the_v2_bindings() { + let v2 = data_trigger_bindings_list_v2().expect("expected the v2 bindings"); + let v3 = data_trigger_bindings_list_v3().expect("expected the v3 bindings"); + assert_eq!(v3.len(), v2.len() + 4); + + let token_market_bindings: Vec<(&str, DocumentTransitionActionType)> = v3 + .iter() + .filter(|binding| binding.data_contract_id == token_market_contract::ID) + .map(|binding| { + ( + binding.document_type.as_str(), + binding.transition_action_type, + ) + }) + .collect(); + assert_eq!( + token_market_bindings, + vec![ + (ask::NAME, DocumentTransitionActionType::Replace), + (ask::NAME, DocumentTransitionActionType::Delete), + (bid::NAME, DocumentTransitionActionType::Replace), + (bid::NAME, DocumentTransitionActionType::Delete), + ] + ); + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/commit_reveal_lookup.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/commit_reveal_lookup.rs index 016cb66294e..f1d12b2d17c 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/commit_reveal_lookup.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/commit_reveal_lookup.rs @@ -1763,15 +1763,13 @@ mod commit_reveal_lookup_tests { actual_fees.processing_fee, )); } - // A chain born at 15 also holds the decision proposals contract, one more sibling - // registered at genesis in the contracts tree, which reshapes it so the written post - // reads its contract for fewer bytes (-45_920). The lookups cost the same. - let ((found_at_14, found_storage_at_14), missing_at_14) = fees[0]; + // A chain born at 15 also holds the decision proposals and token market contracts, + // two more siblings registered at genesis in the contracts tree; with both, the + // written post reads its contract for as many bytes as at 14. The lookups cost the + // same. assert_eq!( - ((found_at_14 - 45_920, found_storage_at_14), missing_at_14), - fees[1], - "keyed by owner {keyed_by_owner}: the fees at 14 less the genesis shift, then at \ - 15" + fees[0], fees[1], + "keyed by owner {keyed_by_owner}: the fees at 14, then at 15" ); } } @@ -3007,10 +3005,10 @@ mod commit_reveal_lookup_tests { /// it consumes from what its lookup read, without reading it again. #[tokio::test] async fn should_pin_the_fees_of_a_reveal_choosing_the_first_of_its_preorders() { - // At 15 the contracts tree also holds the decision proposals contract, one more sibling - // registered at genesis, which reshapes it so the fixture contract's node is billed - // fewer bytes to reach (-45_920) - for (protocol_version, accepted_processing_fee) in [(14, 3_574_720), (15, 3_516_780)] { + // At 15 the contracts tree also holds the decision proposals and token market contracts, + // two more siblings registered at genesis; with both, the fixture contract's node is + // billed as many bytes to reach as at 14 + for (protocol_version, accepted_processing_fee) in [(14, 3_574_720), (15, 3_562_700)] { let platform_version = PlatformVersion::get(protocol_version).expect("expected the platform version"); let mut fixture = CommitRevealFixture::new_at(platform_version, |contract| { diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/deletion.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/deletion.rs index e34a17c55b8..cceb7a0d9b5 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/deletion.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/deletion.rs @@ -17,9 +17,8 @@ mod deletion_tests { // increases byte-billed contract-tree reads. // The app-connect and moderation charters contracts each add one sibling to the // genesis contracts tree, increasing the bytes billed when reading that tree - // (protocol 14 only). The decision proposals contract adds another at protocol - // 15, which reshapes the tree so the contract's node is billed fewer bytes to - // reach (-47_280). + // (protocol 14 only). The decision proposals and token market contracts add two more + // at protocol 15, which reshape the tree so the contract read is billed fewer bytes (-47_280). 1770160, ) .await; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns.rs index 28382d96db3..16a61100e25 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns.rs @@ -37,9 +37,9 @@ mod dpns_tests { // increasing the bytes billed when reading that tree (protocol 14 only). The // moderation charters contract adds another, which reshapes the tree so the // DPNS contract's node is billed fewer bytes to reach (-287_760). The decision - // proposals contract adds a third at protocol 15, which reshapes it back to as - // many bytes as with the app-connect contract alone (+287_760, 95_920 a create). - 6_049_360, + // proposals and token market contracts add two more at protocol 15, and the DPNS + // node is billed more bytes to reach again (+137_760). + 5_899_360, ) .await; } diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns_v3.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns_v3.rs index b70dcd7332e..6ce0903d90b 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns_v3.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/dpns_v3.rs @@ -1566,8 +1566,9 @@ mod dpns_v3_tests { } } // At 15 each create also reads the DPNS contract through a contracts tree holding the - // decision proposals contract, one more sibling registered at genesis, which reshapes - // the tree so the DPNS contract's node is billed more bytes to reach (+95_920 a create). + // decision proposals and token market contracts, two more siblings registered at + // genesis, which reshape the tree so the DPNS contract's node is billed more bytes to + // reach (+95_920 a create). assert_eq!( measured, vec![ diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/mod.rs index 0099827d5fc..a3750ecbcdb 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/mod.rs @@ -41,6 +41,7 @@ mod replacement; mod required_since; mod system_agreement; mod token_cost_policy; +mod token_market_orders; mod transfer; mod typed_array_references; mod typed_array_where; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs index 7ea76a4008f..6c0f9ef12a2 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs @@ -525,9 +525,8 @@ mod replacement_tests { // increases byte-billed contract-tree reads. // The app-connect and moderation charters contracts each add one sibling to the // genesis contracts tree, increasing the bytes billed when reading that tree - // (protocol 14 only). The decision proposals contract adds another at protocol - // 15, which reshapes the tree so the contract's node is billed fewer bytes to - // reach (-47_280). + // (protocol 14 only). The decision proposals and token market contracts add two more + // at protocol 15, which reshape the tree so the contract read is billed fewer bytes (-47_280). 1498860, ) .await; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/token_market_orders.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/token_market_orders.rs new file mode 100644 index 00000000000..46c90e1da40 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/token_market_orders.rs @@ -0,0 +1,224 @@ +//! The token market contract's orders (`ask` and `bid`, protocol version 15) through block +//! processing. Only the token marketplace transitions write an order, so a document +//! transition replacing or deleting a stored order is refused by the reject data trigger, +//! even when its owner signs it, and the order stays as stored. + +use super::*; + +mod token_market_orders_tests { + use super::*; + use crate::execution::validation::state_transition::tests::create_token_contract_with_owner_identity; + use dpp::consensus::state::data_trigger::DataTriggerError; + use dpp::data_contract::associated_token::token_configuration::accessors::v0::TokenConfigurationV0Getters; + use dpp::data_contract::associated_token::token_configuration::TokenConfiguration; + use dpp::data_contract::associated_token::token_configuration_convention::accessors::v0::TokenConfigurationConventionV0Setters; + use dpp::document::{Document, DocumentV0}; + use dpp::prelude::DataContract; + use dpp::state_transition::StateTransition; + use dpp::system_data_contracts::token_market_contract; + use dpp::system_data_contracts::token_market_contract::v1::document_types::{ask, bid}; + use drive::util::object_size_info::{ + DocumentAndContractInfo, DocumentInfo::DocumentRefInfo, OwnedDocumentInfo, + }; + use std::collections::BTreeMap; + + /// An order of `document_type_name` owned by `owner_id`, for 10 units of `token_id` (a + /// token of 6 decimals) at a price of 5, as the marketplace stores it. + fn order( + document_type_name: &str, + id: u8, + owner_id: Identifier, + token_id: Identifier, + ) -> Document { + let mut properties = BTreeMap::from([ + ( + "tokenId".to_string(), + Value::Identifier(token_id.to_buffer()), + ), + ("price".to_string(), Value::U64(5)), + ("amount".to_string(), Value::U64(10)), + ("remaining".to_string(), Value::U64(10)), + ("decimals".to_string(), Value::U8(6)), + ]); + if document_type_name == bid::NAME { + properties.insert("escrow".to_string(), Value::U64(50)); + } + Document::V0(DocumentV0 { + id: Identifier::from([id; 32]), + owner_id, + properties, + revision: Some(1), + created_at: Some(0), + updated_at: Some(0), + ..Default::default() + }) + } + + fn stored_orders( + platform: &TempPlatform, + market: &DataContract, + document_type_name: &str, + ) -> Vec { + let query = DriveDocumentQuery::from_sql_expr( + &format!("select * from {document_type_name}"), + market, + Some(&platform.config.drive), + PlatformVersion::latest(), + ) + .expect("expected an order query"); + platform + .drive + .query_documents(query, None, false, None, None) + .expect("expected the stored orders") + .documents() + .to_vec() + } + + fn process( + platform: &TempPlatform, + transition: &StateTransition, + ) -> StateTransitionExecutionResult { + let serialized = transition + .serialize_to_bytes() + .expect("expected the transition to serialize"); + let transaction = platform.drive.grove.start_transaction(); + let processing_result = platform + .platform + .process_raw_state_transitions( + &[serialized], + &platform.state.load(), + &BlockInfo::default(), + &transaction, + PlatformVersion::latest(), + false, + None, + ) + .expect("expected to process the state transition"); + platform + .drive + .grove + .commit_transaction(transaction) + .unwrap() + .expect("expected to commit the transaction"); + processing_result.into_execution_results().remove(0) + } + + /// The owner of a stored ask and a stored bid signs a replace and a delete of each: block + /// processing refuses every one with the reject trigger's `DataTriggerConditionError` and + /// leaves both orders as stored. + #[tokio::test] + async fn should_refuse_a_signed_replace_or_delete_of_a_stored_order() { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + let (owner, signer, key) = setup_identity(&mut platform, 961, dash_to_credits!(0.5)); + let (issuer, _, _) = setup_identity(&mut platform, 962, dash_to_credits!(0.5)); + let (_, token_id) = create_token_contract_with_owner_identity( + &mut platform, + issuer.id(), + Some(|configuration: &mut TokenConfiguration| { + configuration.conventions_mut().set_decimals(6) + }), + None, + None, + None, + platform_version, + ); + let market = platform + .drive + .get_contract_with_fetch_info( + token_market_contract::ID.to_buffer(), + false, + None, + platform_version, + ) + .expect("expected to fetch the token market contract") + .expect("expected the token market contract at genesis") + .contract + .clone(); + + let mut nonce = 1; + for (id, name) in [(1, ask::NAME), (2, bid::NAME)] { + let document_type = market + .document_type_for_name(name) + .expect("expected the order type"); + let stored = order(name, id, owner.id(), token_id); + platform + .drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo((&stored, None)), + owner_id: None, + }, + contract: &market, + document_type, + }, + false, + BlockInfo::default(), + true, + None, + platform_version, + None, + ) + .expect("expected to store the order"); + let before = stored_orders(&platform, &market, name); + assert_eq!( + before + .iter() + .map(|document| document.id()) + .collect::>(), + vec![stored.id()] + ); + + let mut replacement = stored.clone(); + replacement.set("remaining", Value::U64(4)); + replacement + .increment_revision() + .expect("expected the revision to increment"); + let replace = BatchTransition::new_document_replacement_transition_from_document( + replacement, + document_type, + &key, + nonce, + 0, + None, + &signer, + platform_version, + None, + ) + .await + .expect("expected the replace transition"); + let delete = BatchTransition::new_document_deletion_transition_from_document( + stored, + document_type, + &key, + nonce + 1, + 0, + None, + &signer, + platform_version, + None, + ) + .await + .expect("expected the delete transition"); + nonce += 2; + + for (action, transition) in [("replace", replace), ("delete", delete)] { + let result = process(&platform, &transition); + assert_matches!( + result, + PaidConsensusError { + error: ConsensusError::StateError(StateError::DataTriggerError( + DataTriggerError::DataTriggerConditionError(_) + )), + .. + }, + "{name} {action}: {result:?}" + ); + } + assert_eq!(stored_orders(&platform, &market, name), before, "{name}"); + } + } +} diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/burn/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/burn/mod.rs index 5c543bf6fca..78242a65169 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/burn/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/burn/mod.rs @@ -3963,10 +3963,10 @@ mod token_burn_tests { // +740 per document write from protocol version 14: the contract's version item is // one more node to rehash; -12_820: the documents expirations tree joins `Misc` // beside the token supplies tree the burn rewrites, reshaping the `Misc` Merk - // PROTOCOL_VERSION_15: -48_640: the decision proposals contract adds one - // sibling to the genesis contracts tree, which reshapes the tree so the token - // contract's node is billed fewer bytes to reach - 4_307_560, + // PROTOCOL_VERSION_15: +48_640: the decision proposals and token market contracts + // join the genesis contracts tree, which reshapes it so the token contract's node is + // billed more bytes to reach + 4_404_840, ) .await; } diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/direct_selling/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/direct_selling/mod.rs index 911fe3476d8..90730a76378 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/direct_selling/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/direct_selling/mod.rs @@ -31,10 +31,10 @@ mod token_selling_tests { // reads the total supply even though the token sets no max supply. 12_820 credits // less in fees: the documents expirations tree joins `Misc` beside the token // supplies tree the purchase rewrites, reshaping the `Misc` Merk - // PROTOCOL_VERSION_15: 48_640 credits less in fees: the decision proposals - // contract adds one sibling to the genesis contracts tree, which reshapes the tree - // so the token contract's node is billed fewer bytes to reach - 699_868_098_480, + // PROTOCOL_VERSION_15: 48_640 credits more in fees: the decision proposals and token + // market contracts join the genesis contracts tree, which reshapes it so the token + // contract's node is billed more bytes to reach + 699_868_001_200, ) .await; } diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/shielded/nullifier_batch_guard.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/shielded/nullifier_batch_guard.rs index c8258510b95..d9f0ae58a7c 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/shielded/nullifier_batch_guard.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/token/shielded/nullifier_batch_guard.rs @@ -734,7 +734,7 @@ async fn should_preserve_single_entry_full_processor_fees_and_result() { .value .unwrap() ), - "a1aba6416f1f58b9a2ae5bdad21b4aacba05a20ea4d698fb98d6aed180d2be57" + "fb76e051fe936ae3e39265a9cddf3d3297e901ca2c6048c1dcf85b82a92d8778" ); assert_eq!( pool.platform @@ -1871,9 +1871,9 @@ async fn should_preserve_a_real_single_payment_sponsor_on_first_check_recheck_an // the batch's estimate (protocol v15 note 14). let (estimated_storage, estimated_processing, actual_storage, actual_processing) = if shielded { - (171_126_000, 213_389_040, 155_142_000, 92_785_480) + (171_126_000, 213_389_040, 155_142_000, 92_739_560) } else { - (138_024_000, 117_713_120, 126_468_000, 3_916_360) + (138_024_000, 117_713_120, 126_468_000, 3_870_440) }; assert_eq!( result.execution_results().as_slice(), @@ -2160,39 +2160,39 @@ async fn should_preserve_single_entry_actions_errors_operations_and_settled_stat let (expected_validation, expected_settled) = match case { "shield" => ( "6a823c2db94612652926e6b9c1ae7feec9c2547dfd4cfc4e474318da1c16d59c", - "2fd1b754af2ac9b95cb06a74e36a2481c70ab460db1a8ddd5711033c1baab1a8", + "eb7e34a02f156dbe1d9c9609b41bd67a033d1cd3fe81c5f2e6fcef575fc88459", ), "valid-payment" => ( "9388d5cf63c4a571b9fb009679e2e34d0b681acc88f0f5195e7287c7efbd92f7", - "7a53825081966ae909620fc249a3d4dab317598f66eefbc3ba7b8338b4a06dbd", + "44684cf51317f0e60bc836e7bd671798145bb69bc7605241ebbc659028d50070", ), "failed-payment" => ( "d9b5fe191841bea952494f2a45e3833f925f4d367a1937b2ef1235a1aa01f51e", - "1eaeb4e54f91a4c95b41fc2966c8d9fce2b27ee01ade95fbeab66deeb2ba3859", + "365a97c4e20ae4ee695505a8d5dbb0dea5584c14eb4c6b223a3ae4f0b2f8b668", ), "within-bundle-repeat" => ( "8023e03a697a20f6a78396a05503a4bc583ca8f69e19bd7ee07a6ae0ce7be478", - "c7495c527f115ac6a78f8c709b1ac599a9ed683339f739a7e444cce767a180d7", + "b9cfc7cafbeafb436917f0b35bfa97df6be58b73a25e0159038558fdc9a2751c", ), "stored-repeat" => ( "05652ac85c972902e8c09d20a7d8586e17e9434a478b335da136ad85f73def01", - "0143d7edf2336ab89b1e83485eceeb29285d38492e16d8ae3645a1fa866eb881", + "9e1d6b0bc9097d817f4ca0ac952611f69398d8f201b6eb36556059b166488393", ), "open-mint-group" => ( "5c68381297c9bf68c5e85ebe6bb9e0f1289e2c07718eecfb5e6773d6009455bb", - "d559cb03752fa942008a30d52efb82584b49d68cc64aebabfacf01bf72d48f48", + "b96b1e9b643552413c2e5049f9f9b6ae92357a6ad462e2d3a673d239a355cfd9", ), "closing-mint-group" => ( "2cedc16732c76e0605b5f46085db3d69c12936a5af55a2a82f52ec9e9ca67e38", - "c68476bc881df5d44ec8ce84503eb24736abb56c8986079a47ab62f302c37108", + "78fd3bdabed41d31082162dfc95158872108872c79f3041ef3d71c7fa48dd905", ), "open-burn-group" => ( "e664a045f020ff84de1ca98f364fbabccfc141ad46aa82c79491a340bb0db5f8", - "94961c4ec632dca33b79cffcbf9d08981fc77142d392118c21d37a9055feaacb", + "38b6b6d2d77fb41c4262adaa6bbbd581dbb2c5d3195e3d11cec16411464e2758", ), "closing-burn-group" => ( "93e1732286f168c4a91d2765931e181c5721bfca1bfd5f93b8baacca03e00b2b", - "78bafb1ae5c4a7c804421ac3afbb192eb35a4f1bc093e9a02f1130d8c00dd4c3", + "6a4f89129b2bd16b078b1165a46775e20deb1808bfd239fd49e5f4e89e45453c", ), _ => unreachable!("enumerated characterization case"), }; diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/tests/restore_constraints.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/tests/restore_constraints.rs index 676f95277f8..c43c29bcb37 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/tests/restore_constraints.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/contract_user_moderation/tests/restore_constraints.rs @@ -414,9 +414,10 @@ async fn should_preserve_the_rule_free_restore_fee_and_document() { FeeResult { storage_fee: 9_612_000, // from protocol version 15 the restore reads its contract stored compiled (format - // version 2), about 1.8 times its bytes; the decision proposals contract registered at - // genesis from 15 adds a sibling to the contracts tree the restore's path crosses - processing_fee: 1_442_880, + // version 2), about 1.8 times its bytes; the decision proposals and token market + // contracts registered at genesis from 15 add siblings to the contracts tree the + // restore's path crosses + processing_fee: 1_492_880, ..Default::default() } ); diff --git a/packages/rs-drive/grovedb-structure.json b/packages/rs-drive/grovedb-structure.json index cba09241754..297822e727c 100644 --- a/packages/rs-drive/grovedb-structure.json +++ b/packages/rs-drive/grovedb-structure.json @@ -781,6 +781,69 @@ } ] }, + { + "id": "tokens.order_escrow", + "key": { + "type": "fixed", + "hex": "60", + "label": "TokenOrderEscrow", + "constant": "TOKEN_ORDER_ESCROW_KEY" + }, + "kinds": [ + "BigSumTree" + ], + "since": 15, + "presence": "always", + "source": "packages/rs-drive/src/drive/tokens/paths.rs", + "book": "data-model/token-marketplace.md", + "description": "The tokens open listings hold in escrow. The sum joins the identity balances and the shielded pools in the token conservation check.", + "children": [ + { + "id": "tokens.order_escrow.token", + "key": { + "type": "dynamic", + "name": "token_id", + "matcher": { + "type": "len", + "len": 32 + }, + "encoding": "identifier32", + "description": "The token id" + }, + "kinds": [ + "SumTree" + ], + "since": 15, + "presence": "lazy", + "source": "packages/rs-drive/src/drive/tokens/paths.rs", + "description": "The escrowed listings of one token; created with its first listing.", + "children": [ + { + "id": "tokens.order_escrow.token.order", + "key": { + "type": "dynamic", + "name": "order_id", + "matcher": { + "type": "len", + "len": 32 + }, + "encoding": "identifier32", + "description": "The listing's order id" + }, + "kinds": [ + "SumItem" + ], + "value": "token amount", + "since": 15, + "presence": "lazy", + "source": "packages/rs-drive/src/drive/tokens/paths.rs", + "description": "What one open listing still holds.", + "children": [] + } + ] + } + ] + }, { "id": "tokens.balances", "key": { @@ -1970,6 +2033,47 @@ "source": "packages/rs-drive/src/drive/mod.rs", "description": "Credits put aside in advance to fund specific state transitions, such as masternode votes on a contested resource.", "children": [ + { + "id": "prefunded_balances.for_token_orders", + "key": { + "type": "fixed", + "hex": "20", + "label": "ForTokenOrders", + "constant": "PREFUNDED_BALANCES_FOR_TOKEN_ORDERS" + }, + "kinds": [ + "SumTree" + ], + "since": 15, + "presence": "always", + "source": "packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs", + "book": "data-model/token-marketplace.md", + "description": "The credits the token marketplace holds in escrow for open offers.", + "children": [ + { + "id": "prefunded_balances.for_token_orders.offer", + "key": { + "type": "dynamic", + "name": "order_id", + "matcher": { + "type": "len", + "len": 32 + }, + "encoding": "identifier32", + "description": "The offer's order id" + }, + "kinds": [ + "SumItem" + ], + "value": "credits", + "since": 15, + "presence": "lazy", + "source": "packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs", + "description": "What one open offer still holds.", + "children": [] + } + ] + }, { "id": "prefunded_balances.owner_fee_pots", "key": { @@ -6297,7 +6401,10 @@ "tree": { "hex": "80", "left": { - "hex": "40" + "hex": "40", + "left": { + "hex": "20" + } }, "right": { "hex": "c0" @@ -6401,7 +6508,10 @@ "hex": "20" }, "right": { - "hex": "5c" + "hex": "5c", + "right": { + "hex": "60" + } } }, "right": { diff --git a/packages/rs-drive/src/cache/system_contracts.rs b/packages/rs-drive/src/cache/system_contracts.rs index bb83284ad3e..9ec224f4184 100644 --- a/packages/rs-drive/src/cache/system_contracts.rs +++ b/packages/rs-drive/src/cache/system_contracts.rs @@ -7,6 +7,7 @@ use platform_version::version::feature_initial_protocol_versions::{ APP_CONNECT_CONTRACT_INITIAL_PROTOCOL_VERSION, DECISION_PROPOSALS_CONTRACT_INITIAL_PROTOCOL_VERSION, MODERATION_CHARTERS_CONTRACT_INITIAL_PROTOCOL_VERSION, + TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION, }; use platform_version::version::{PlatformVersion, ProtocolVersion}; use std::collections::BTreeMap; @@ -193,6 +194,15 @@ impl SystemDataContracts { self.load(SystemDataContract::DecisionProposals, platform_version) } + /// Returns the token market contract materialized for `platform_version`: the open listings + /// and offers of the token marketplace (protocol version 15). + pub fn load_token_market( + &self, + platform_version: &PlatformVersion, + ) -> Result, Error> { + self.load(SystemDataContract::TokenMarket, platform_version) + } + /// Returns the system contract whose deterministic identifier matches `id`, materialized /// for `platform_version`. /// @@ -241,6 +251,8 @@ impl SystemDataContracts { SystemDataContract::DecisionProposals => { DECISION_PROPOSALS_CONTRACT_INITIAL_PROTOCOL_VERSION } + // Written to state by the transition to protocol version 15. + SystemDataContract::TokenMarket => TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION, // Never served from this cache: `WalletUtils` is only ever read from grovedb, and // the reserved `FeatureFlags` slot has no implementation. SystemDataContract::WalletUtils | SystemDataContract::FeatureFlags => return Ok(None), @@ -497,6 +509,27 @@ mod tests { .is_none()); } + #[test] + fn should_serve_token_market_only_from_its_activation_version() { + let contracts = SystemDataContracts::new(); + + assert!(contracts + .find_by_id(SystemDataContract::TokenMarket.id(), platform_version(14)) + .expect("expected the pre-activation lookup to succeed") + .is_none()); + assert!(contracts + .find_by_id( + SystemDataContract::TokenMarket.id(), + platform_version(TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION) + ) + .expect("expected the v15 lookup to succeed") + .is_some()); + assert!(contracts + .find_by_id(SystemDataContract::TokenMarket.id(), platform_version(14)) + .expect("an old-version lookup after materialization must succeed") + .is_none()); + } + #[test] fn should_serve_app_connect_only_from_its_activation_version() { let contracts = SystemDataContracts::new(); diff --git a/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/mod.rs b/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/mod.rs index 05708b980aa..42a27fcc7bd 100644 --- a/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/mod.rs +++ b/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -38,9 +39,16 @@ impl Drive { ); Ok(()) } + 1 => { + Self::add_estimation_costs_for_contract_fee_pot_update_v1( + pot, + estimated_costs_only_with_layer_info, + ); + Ok(()) + } version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_estimation_costs_for_contract_fee_pot_update".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/v1/mod.rs b/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/v1/mod.rs new file mode 100644 index 00000000000..96b629ff9f9 --- /dev/null +++ b/packages/rs-drive/src/drive/contract/fee_pots/estimated_costs/v1/mod.rs @@ -0,0 +1,30 @@ +use crate::drive::prefunded_specialized_balances::prefunded_specialized_balances_path; +use crate::drive::Drive; +use crate::util::estimated_layers::register_keeping_deeper_estimated_layers; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::data_contract::document_type::action_fees::ContractFeePot; +use grovedb::batch::KeyInfoPath; + +impl Drive { + /// v0 (protocol version 15), keeping a deeper description of the prefunded balances layer + /// that another write of the batch registered: the escrow of the token marketplace's offers + /// sits a level below the fee pots' keys. + #[inline(always)] + pub(super) fn add_estimation_costs_for_contract_fee_pot_update_v1( + pot: ContractFeePot, + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + register_keeping_deeper_estimated_layers( + estimated_costs_only_with_layer_info, + &[KeyInfoPath::from_known_path( + prefunded_specialized_balances_path(), + )], + |estimated_costs_only_with_layer_info| { + Self::add_estimation_costs_for_contract_fee_pot_update_v0( + pot, + estimated_costs_only_with_layer_info, + ) + }, + ); + } +} diff --git a/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/mod.rs b/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/mod.rs index 40d4a26092f..a34b3559eef 100644 --- a/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/mod.rs +++ b/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -31,9 +32,13 @@ impl Drive { Self::add_estimation_costs_for_balances_v0(estimated_costs_only_with_layer_info); Ok(()) } + 1 => { + Self::add_estimation_costs_for_balances_v1(estimated_costs_only_with_layer_info); + Ok(()) + } version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_estimation_costs_for_balances".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/v1/mod.rs b/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/v1/mod.rs new file mode 100644 index 00000000000..017dd1c5141 --- /dev/null +++ b/packages/rs-drive/src/drive/identity/estimation_costs/for_balances/v1/mod.rs @@ -0,0 +1,21 @@ +use crate::drive::Drive; +use crate::util::estimated_layers::register_keeping_deeper_estimated_layers; +use crate::util::estimated_layers::EstimatedLayers; +use grovedb::batch::KeyInfoPath; + +impl Drive { + /// Adds estimation costs for identity balances (protocol version 15): v0, keeping a deeper + /// description of the root layer that another write of the batch registered. An offer of the + /// token marketplace debits the buyer's balance and credits its escrow under the prefunded + /// specialized balances, which sit deeper in the root tree than the balances, whichever + /// registers first. + pub(super) fn add_estimation_costs_for_balances_v1( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + register_keeping_deeper_estimated_layers( + estimated_costs_only_with_layer_info, + &[KeyInfoPath::from_known_path([])], + Self::add_estimation_costs_for_balances_v0, + ); + } +} diff --git a/packages/rs-drive/src/drive/initialization/mod.rs b/packages/rs-drive/src/drive/initialization/mod.rs index ce157e315c0..5db1db9fdb7 100644 --- a/packages/rs-drive/src/drive/initialization/mod.rs +++ b/packages/rs-drive/src/drive/initialization/mod.rs @@ -6,6 +6,7 @@ mod v1; mod v2; mod v3; mod v4; +mod v5; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -32,9 +33,10 @@ impl Drive { 2 => self.create_initial_state_structure_v2(transaction, platform_version), 3 => self.create_initial_state_structure_v3(transaction, platform_version), 4 => self.create_initial_state_structure_v4(transaction, platform_version), + 5 => self.create_initial_state_structure_v5(transaction, platform_version), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "create_initial_state_structure".to_string(), - known_versions: vec![0, 1, 2, 3, 4], + known_versions: vec![0, 1, 2, 3, 4, 5], received: version, })), } diff --git a/packages/rs-drive/src/drive/initialization/v5/mod.rs b/packages/rs-drive/src/drive/initialization/v5/mod.rs new file mode 100644 index 00000000000..858f42f8894 --- /dev/null +++ b/packages/rs-drive/src/drive/initialization/v5/mod.rs @@ -0,0 +1,29 @@ +//! Drive Initialization +//! +//! Version 5: version 4 plus the token marketplace escrow trees (protocol version 15). + +use crate::drive::Drive; +use crate::error::Error; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// Creates the initial state structure. + pub(super) fn create_initial_state_structure_v5( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + self.create_initial_state_structure_v4(transaction, platform_version)?; + + // Token marketplace escrow trees (protocol version 15): the BigSumTree under the Tokens + // tree of the tokens open listings hold, and the SumTree under the prefunded specialized + // balances of the credits open offers hold. Last, after everything version 4 builds, and + // through the same helper as the upgrade path (`Platform::transition_to_version_15`), so + // a chain born at version 15 and one upgraded to it build byte-identical `[Tokens]` and + // `[PreFundedSpecializedBalances]` subtrees. + self.insert_token_order_escrow_trees(transaction, platform_version)?; + + Ok(()) + } +} diff --git a/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/mod.rs b/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/mod.rs index dd7ba4b2266..6bba21d219c 100644 --- a/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/mod.rs +++ b/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -43,9 +44,15 @@ impl Drive { ); Ok(()) } + 2 => { + Self::add_estimation_costs_for_prefunded_specialized_balance_update_v2( + estimated_costs_only_with_layer_info, + ); + Ok(()) + } version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_estimation_costs_for_prefunded_specialized_balance_update".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/v2/mod.rs b/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/v2/mod.rs new file mode 100644 index 00000000000..358e8dee4ce --- /dev/null +++ b/packages/rs-drive/src/drive/prefunded_specialized_balances/estimation_costs/for_prefunded_specialized_balance_update/v2/mod.rs @@ -0,0 +1,25 @@ +use crate::drive::prefunded_specialized_balances::prefunded_specialized_balances_path; +use crate::drive::Drive; +use crate::util::estimated_layers::register_keeping_deeper_estimated_layers; +use crate::util::estimated_layers::EstimatedLayers; +use grovedb::batch::KeyInfoPath; + +impl Drive { + /// Adds estimation costs for a prefunded specialized balance update (protocol version 15): + /// v1, keeping a deeper description of the root and the prefunded balances layer that another + /// write of the batch registered: the escrow of the token marketplace's offers sits two levels + /// below the voting balances' key in that layer. + #[inline(always)] + pub(super) fn add_estimation_costs_for_prefunded_specialized_balance_update_v2( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + register_keeping_deeper_estimated_layers( + estimated_costs_only_with_layer_info, + &[ + KeyInfoPath::from_known_path([]), + KeyInfoPath::from_known_path(prefunded_specialized_balances_path()), + ], + Self::add_estimation_costs_for_prefunded_specialized_balance_update_v1, + ); + } +} diff --git a/packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs b/packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs index 393d63e7138..055d96c0f3f 100644 --- a/packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs +++ b/packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs @@ -27,6 +27,11 @@ use crate::util::batch::GroveDbOpBatch; /// The key for prefunded balances for voting pub const PREFUNDED_BALANCES_FOR_VOTING: u8 = 128; +/// The key for the credits the token marketplace holds in escrow for open offers (protocol +/// version 15): a SumTree whose SumItems are keyed by order id. It sits under the prefunded +/// specialized balances root, a SumTree, so the credit conservation check counts it. +pub const PREFUNDED_BALANCES_FOR_TOKEN_ORDERS: u8 = 32; + /// prefunded specialized balances for voting pub fn prefunded_specialized_balances_path() -> [&'static [u8]; 1] { [Into::<&[u8; 1]>::into( @@ -34,6 +39,22 @@ pub fn prefunded_specialized_balances_path() -> [&'static [u8]; 1] { )] } +/// The credits held in escrow for the token marketplace's open offers +pub fn prefunded_specialized_balances_for_token_orders_path() -> [&'static [u8]; 2] { + [ + Into::<&[u8; 1]>::into(RootTree::PreFundedSpecializedBalances), + &[PREFUNDED_BALANCES_FOR_TOKEN_ORDERS], + ] +} + +/// The credits held in escrow for the token marketplace's open offers, as a vec +pub fn prefunded_specialized_balances_for_token_orders_path_vec() -> Vec> { + vec![ + Into::<&[u8; 1]>::into(RootTree::PreFundedSpecializedBalances).to_vec(), + vec![PREFUNDED_BALANCES_FOR_TOKEN_ORDERS], + ] +} + /// prefunded specialized balances vector pub fn prefunded_specialized_balances_path_vec() -> Vec> { vec![Into::<&[u8; 1]>::into(RootTree::PreFundedSpecializedBalances).to_vec()] diff --git a/packages/rs-drive/src/drive/prefunded_specialized_balances/structure.rs b/packages/rs-drive/src/drive/prefunded_specialized_balances/structure.rs index 6c0ac23e716..d34de62cd7b 100644 --- a/packages/rs-drive/src/drive/prefunded_specialized_balances/structure.rs +++ b/packages/rs-drive/src/drive/prefunded_specialized_balances/structure.rs @@ -1,7 +1,9 @@ use crate::drive::contract::paths::{ PREFUNDED_BALANCES_FOR_CONTRACT_MODERATOR_FEES, PREFUNDED_BALANCES_FOR_CONTRACT_OWNER_FEES, }; -use crate::drive::prefunded_specialized_balances::PREFUNDED_BALANCES_FOR_VOTING; +use crate::drive::prefunded_specialized_balances::{ + PREFUNDED_BALANCES_FOR_TOKEN_ORDERS, PREFUNDED_BALANCES_FOR_VOTING, +}; use crate::drive::RootTree; use crate::structure::{ElementKind, StructureNode}; @@ -53,6 +55,29 @@ pub(crate) fn structure() -> StructureNode { until the contract owner claims them.", "What the contract's owner can claim, at most once per epoch.", )) + .child( + StructureNode::fixed( + "for_token_orders", + &[PREFUNDED_BALANCES_FOR_TOKEN_ORDERS], + "ForTokenOrders", + "PREFUNDED_BALANCES_FOR_TOKEN_ORDERS", + ) + .kind(ElementKind::SumTree) + .since(15) + .source("packages/rs-drive/src/drive/prefunded_specialized_balances/mod.rs") + .book("data-model/token-marketplace.md") + .describe( + "The credits the token marketplace holds in escrow \ + for open offers.", + ) + .child( + StructureNode::identifier("offer", "order_id", "The offer's order id") + .kind(ElementKind::SumItem) + .lazy() + .value("credits") + .describe("What one open offer still holds."), + ), + ) .child(contract_fee_pots( "moderators_fee_pots", PREFUNDED_BALANCES_FOR_CONTRACT_MODERATOR_FEES, diff --git a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/mod.rs b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/mod.rs index 7818b611a8d..2203b2c24e5 100644 --- a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/mod.rs +++ b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -39,9 +40,10 @@ impl Drive { { 0 => self.calculate_total_tokens_balance_v0(transaction, platform_version), 1 => self.calculate_total_tokens_balance_v1(transaction, platform_version), + 2 => self.calculate_total_tokens_balance_v2(transaction, platform_version), version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "calculate_total_tokens_balance".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } diff --git a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v0/mod.rs b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v0/mod.rs index 6e723e81c4e..e7eecb2e56d 100644 --- a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v0/mod.rs +++ b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v0/mod.rs @@ -44,6 +44,9 @@ impl Drive { total_identity_token_balances, // Token shielded pools do not exist before protocol version 14. total_token_shielded_pool_balances: 0, + // v0 predates the token order escrow (protocol version 15): zero leaves the + // conservation check of every protocol version that selects v0 as it was. + total_token_order_escrow_balances: 0, }) } } diff --git a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v1/mod.rs b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v1/mod.rs index ddd56681587..89cfb39c925 100644 --- a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v1/mod.rs +++ b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v1/mod.rs @@ -53,6 +53,9 @@ impl Drive { total_tokens_in_platform, total_identity_token_balances, total_token_shielded_pool_balances, + // v1 predates the token order escrow (protocol version 15): zero leaves the + // conservation check of protocol version 14, which selects v1, as it was. + total_token_order_escrow_balances: 0, }) } } diff --git a/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v2/mod.rs b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v2/mod.rs new file mode 100644 index 00000000000..c4e69d0f301 --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/calculate_total_tokens_balance/v2/mod.rs @@ -0,0 +1,71 @@ +use crate::drive::balances::TOTAL_TOKEN_SUPPLIES_STORAGE_KEY; +use crate::drive::system::misc_path; +use crate::drive::tokens::paths::{ + tokens_root_path, TOKEN_BALANCES_KEY, TOKEN_ORDER_ESCROW_KEY, TOKEN_SHIELDED_POOLS_KEY, +}; +use crate::drive::Drive; +use crate::error::Error; +use crate::util::grove_operations::DirectQueryType; +use dpp::balances::total_tokens_balance::TotalTokensBalance; +use grovedb::TransactionArg; +use platform_version::version::PlatformVersion; + +impl Drive { + /// Version 2 (protocol version 15): the token marketplace escrow BigSumTree joins the + /// identity balances and the shielded pools on the balance side, so `identity balances + + /// shielded pool balances + escrowed listings` must equal the total supplies. Listing a token + /// moves it from its holder's balance into escrow without touching supply. + #[inline(always)] + pub(super) fn calculate_total_tokens_balance_v2( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result { + let mut drive_operations = vec![]; + let path_holding_total_credits = misc_path(); + let total_tokens_in_platform = self.grove_get_big_sum_tree_total_value( + (&path_holding_total_credits).into(), + TOTAL_TOKEN_SUPPLIES_STORAGE_KEY, + DirectQueryType::StatefulDirectQuery, + transaction, + &mut drive_operations, + &platform_version.drive, + )?; + + let tokens_root_path = tokens_root_path(); + + let total_identity_token_balances = self.grove_get_big_sum_tree_total_value( + (&tokens_root_path).into(), + &[TOKEN_BALANCES_KEY], + DirectQueryType::StatefulDirectQuery, + transaction, + &mut drive_operations, + &platform_version.drive, + )?; + + let total_token_shielded_pool_balances = self.grove_get_big_sum_tree_total_value( + (&tokens_root_path).into(), + &[TOKEN_SHIELDED_POOLS_KEY], + DirectQueryType::StatefulDirectQuery, + transaction, + &mut drive_operations, + &platform_version.drive, + )?; + + let total_token_order_escrow_balances = self.grove_get_big_sum_tree_total_value( + (&tokens_root_path).into(), + &[TOKEN_ORDER_ESCROW_KEY], + DirectQueryType::StatefulDirectQuery, + transaction, + &mut drive_operations, + &platform_version.drive, + )?; + + Ok(TotalTokensBalance { + total_tokens_in_platform, + total_identity_token_balances, + total_token_shielded_pool_balances, + total_token_order_escrow_balances, + }) + } +} diff --git a/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/mod.rs b/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/mod.rs index d0c168820e7..dd0beb71a99 100644 --- a/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/mod.rs +++ b/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/mod.rs @@ -1,4 +1,5 @@ mod v0; +mod v1; use crate::drive::Drive; use crate::error::drive::DriveError; @@ -45,9 +46,16 @@ impl Drive { ); Ok(()) } + 1 => { + Self::add_estimation_costs_for_token_balances_v1( + token_id, + estimated_costs_only_with_layer_info, + ); + Ok(()) + } version => Err(Error::Drive(DriveError::UnknownVersionMismatch { method: "add_estimation_costs_for_token_balances".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } diff --git a/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/v1/mod.rs b/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/v1/mod.rs new file mode 100644 index 00000000000..e9e459fa26c --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/estimated_costs/for_token_balances/v1/mod.rs @@ -0,0 +1,32 @@ +use crate::drive::tokens::paths::tokens_root_path; +use crate::drive::Drive; +use crate::util::estimated_layers::register_keeping_deeper_estimated_layers; +use crate::util::estimated_layers::EstimatedLayers; +use grovedb::batch::KeyInfoPath; + +impl Drive { + /// Adds estimation costs for token balances (protocol version 15): v0, keeping a deeper + /// description of the root and the Tokens layer that another write of the batch registered. + /// The escrow of the token marketplace's listings sits deeper in the Tokens layer than the + /// balances key v0 describes, and the offers' escrow deeper in the root tree, under the + /// prefunded specialized balances; a batch moving tokens or credits into or out of escrow + /// writes both, whichever registers first. + pub(super) fn add_estimation_costs_for_token_balances_v1( + token_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + register_keeping_deeper_estimated_layers( + estimated_costs_only_with_layer_info, + &[ + KeyInfoPath::from_known_path([]), + KeyInfoPath::from_known_path(tokens_root_path()), + ], + |estimated_costs_only_with_layer_info| { + Self::add_estimation_costs_for_token_balances_v0( + token_id, + estimated_costs_only_with_layer_info, + ) + }, + ); + } +} diff --git a/packages/rs-drive/src/drive/tokens/mod.rs b/packages/rs-drive/src/drive/tokens/mod.rs index 72e9be5897b..e5127d9461a 100644 --- a/packages/rs-drive/src/drive/tokens/mod.rs +++ b/packages/rs-drive/src/drive/tokens/mod.rs @@ -55,6 +55,11 @@ pub mod unfreeze; #[cfg(feature = "server")] pub mod calculate_total_tokens_balance; +/// The escrow of the token marketplace (protocol version 15): the tokens open listings hold and +/// the credits open offers hold. +#[cfg(feature = "server")] +pub mod order_escrow; + mod contract_info; mod direct_purchase; /// Distribution module diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/credits/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/credits/mod.rs new file mode 100644 index 00000000000..186bb1ab5cc --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/credits/mod.rs @@ -0,0 +1,146 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::fee::Credits; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// The operations that hold `credits` in escrow for the new offer `order_id`. The caller + /// removes the credits from the buyer's balance in the same batch: the escrow sits under the + /// prefunded specialized balances, which the credit conservation check counts. + pub fn add_to_credit_order_escrow_operations( + &self, + order_id: [u8; 32], + credits: Credits, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .add_credits + { + Some(0) => self.add_to_credit_order_escrow_operations_v0( + order_id, + credits, + estimated_costs_only_with_layer_info, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "add_to_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "add_to_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// The operations that set what the open offer `order_id` holds in escrow to `credits`, + /// after a partial fill. An offer filled entirely or cancelled is removed instead. + pub fn set_credit_order_escrow_operations( + &self, + order_id: [u8; 32], + credits: Credits, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .set_credits + { + Some(0) => self.set_credit_order_escrow_operations_v0( + order_id, + credits, + estimated_costs_only_with_layer_info, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "set_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "set_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// The operations that remove the escrow entry of the offer `order_id`, when it is filled + /// entirely or cancelled. + pub fn remove_credit_order_escrow_operations( + &self, + order_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .remove_credits + { + Some(0) => self.remove_credit_order_escrow_operations_v0( + order_id, + estimated_costs_only_with_layer_info, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "remove_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "remove_credit_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// What the offer `order_id` holds in escrow, `None` when it holds nothing. + pub fn fetch_credit_order_escrow( + &self, + order_id: [u8; 32], + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .fetch_credits + { + Some(0) => self.fetch_credit_order_escrow_v0( + order_id, + transaction, + drive_operations, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "fetch_credit_order_escrow".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "fetch_credit_order_escrow".to_string(), + known_versions: vec![0], + })), + } + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/credits/v0/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/credits/v0/mod.rs new file mode 100644 index 00000000000..24561b9e9fb --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/credits/v0/mod.rs @@ -0,0 +1,121 @@ +use crate::drive::prefunded_specialized_balances::{ + prefunded_specialized_balances_for_token_orders_path, + prefunded_specialized_balances_for_token_orders_path_vec, +}; +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::fees::op::LowLevelDriveOperation::GroveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::grove_operations::DirectQueryType; +use dpp::fee::Credits; +use dpp::version::PlatformVersion; +use dpp::ProtocolError; +use grovedb::batch::QualifiedGroveDbOp; +use grovedb::{Element, TransactionArg}; + +/// Escrowed credits are a SumItem, which holds an i64. +fn escrow_sum_value(credits: Credits) -> Result { + i64::try_from(credits).map_err(|_| { + ProtocolError::CriticalCorruptedCreditsCodeExecution(format!( + "credits to hold in escrow over i64 max, are {credits}" + )) + .into() + }) +} + +impl Drive { + pub(super) fn add_to_credit_order_escrow_operations_v0( + &self, + order_id: [u8; 32], + credits: Credits, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_credit_order_escrow( + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + Ok(vec![ + LowLevelDriveOperation::insert_for_known_path_key_element( + prefunded_specialized_balances_for_token_orders_path_vec(), + order_id.to_vec(), + Element::new_sum_item(escrow_sum_value(credits)?), + ), + ]) + } + + pub(super) fn set_credit_order_escrow_operations_v0( + &self, + order_id: [u8; 32], + credits: Credits, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_credit_order_escrow( + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + Ok(vec![ + LowLevelDriveOperation::replace_for_known_path_key_element( + prefunded_specialized_balances_for_token_orders_path_vec(), + order_id.to_vec(), + Element::new_sum_item(escrow_sum_value(credits)?), + ), + ]) + } + + pub(super) fn remove_credit_order_escrow_operations_v0( + &self, + order_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_credit_order_escrow( + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + Ok(vec![GroveOperation( + QualifiedGroveDbOp::delete_op( + prefunded_specialized_balances_for_token_orders_path_vec(), + order_id.to_vec(), + ) + .dont_check_for_backwards_references(), + )]) + } + + pub(super) fn fetch_credit_order_escrow_v0( + &self, + order_id: [u8; 32], + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let path = prefunded_specialized_balances_for_token_orders_path(); + let element = self.grove_get_raw_optional( + (&path).into(), + order_id.as_slice(), + DirectQueryType::StatefulDirectQuery, + transaction, + drive_operations, + &platform_version.drive, + )?; + match element { + None => Ok(None), + Some(Element::SumItem(credits, _)) if credits >= 0 => Ok(Some(credits as Credits)), + Some(_) => Err(Error::Drive(DriveError::CorruptedElementType( + "a credit order escrow entry must be a non-negative sum item", + ))), + } + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/mod.rs new file mode 100644 index 00000000000..bcd7f71888e --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/mod.rs @@ -0,0 +1,71 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::version::PlatformVersion; + +impl Drive { + /// Adds the estimated layers of a write to one listing's escrowed tokens. + pub(crate) fn add_estimation_costs_for_token_order_escrow( + token_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .estimated_costs_for_tokens + { + Some(0) => { + Self::add_estimation_costs_for_token_order_escrow_v0( + token_id, + estimated_costs_only_with_layer_info, + ); + Ok(()) + } + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "add_estimation_costs_for_token_order_escrow".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "add_estimation_costs_for_token_order_escrow".to_string(), + known_versions: vec![0], + })), + } + } + + /// Adds the estimated layers of a write to one offer's escrowed credits. + pub(crate) fn add_estimation_costs_for_credit_order_escrow( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .estimated_costs_for_credits + { + Some(0) => { + Self::add_estimation_costs_for_credit_order_escrow_v0( + estimated_costs_only_with_layer_info, + ); + Ok(()) + } + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "add_estimation_costs_for_credit_order_escrow".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "add_estimation_costs_for_credit_order_escrow".to_string(), + known_versions: vec![0], + })), + } + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/v0/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/v0/mod.rs new file mode 100644 index 00000000000..f749a910981 --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/estimated_costs/v0/mod.rs @@ -0,0 +1,135 @@ +use crate::drive::constants::AVERAGE_BALANCE_SIZE; +use crate::drive::prefunded_specialized_balances::{ + prefunded_specialized_balances_for_token_orders_path_vec, prefunded_specialized_balances_path, +}; +use crate::drive::tokens::paths::{ + token_order_escrow_path, token_order_escrow_root_path, tokens_root_path, +}; +use crate::drive::Drive; +use crate::util::estimated_layers::insert_estimated_layer_keeping_deeper; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::type_constants::DEFAULT_HASH_SIZE_U8; +use grovedb::batch::KeyInfoPath; +use grovedb::EstimatedLayerCount::{EstimatedLevel, PotentiallyAtMaxElements}; +use grovedb::EstimatedLayerSizes::{AllItems, AllSubtrees}; +use grovedb::EstimatedSumTrees::{AllBigSumTrees, AllSumTrees, NoSumTrees, SomeSumTrees}; +use grovedb::{EstimatedLayerInformation, TreeType}; + +/// The level of the Tokens Merk the escrow key sits on. +const TOKEN_ORDER_ESCROW_LEVEL_IN_TOKENS: u32 = 3; + +/// The level of the prefunded specialized balances Merk the offers' escrow key sits on: its root +/// is the voting balances, 128, over the fee pots, 64 and 192, and the escrow key, 32, sits below +/// 64. +const CREDIT_ORDER_ESCROW_LEVEL_IN_PREFUNDED_BALANCES: u32 = 2; + +impl Drive { + /// The layers a write to one listing's escrow entry goes through: the root, the Tokens + /// tree, where the escrow BigSumTree is a leaf on the third level, the escrow BigSumTree of + /// one SumTree per token, and the token's SumTree of one entry per open listing. + pub(super) fn add_estimation_costs_for_token_order_escrow_v0( + token_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + // Every description below keeps a deeper one another write of the batch registered. + insert_estimated_layer_keeping_deeper( + estimated_costs_only_with_layer_info, + KeyInfoPath::from_known_path([]), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: EstimatedLevel(2, false), + estimated_layer_sizes: AllSubtrees(1, NoSumTrees, None), + }, + ); + + // The escrow key sits deeper in the Tokens Merk than the balances key the token balance + // estimation describes at level 0, and a batch moving tokens into or out of escrow writes + // both. From protocol version 15 the token balance estimation keeps this deeper + // description too (`for_token_balances` 1), whichever registers first. + insert_estimated_layer_keeping_deeper( + estimated_costs_only_with_layer_info, + KeyInfoPath::from_known_path(tokens_root_path()), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: EstimatedLevel(TOKEN_ORDER_ESCROW_LEVEL_IN_TOKENS, false), + estimated_layer_sizes: AllSubtrees(1, AllBigSumTrees, None), + }, + ); + + estimated_costs_only_with_layer_info.insert( + KeyInfoPath::from_known_path(token_order_escrow_root_path()), + EstimatedLayerInformation { + tree_type: TreeType::BigSumTree, + estimated_layer_count: EstimatedLevel(10, false), + estimated_layer_sizes: AllSubtrees(DEFAULT_HASH_SIZE_U8, AllSumTrees, None), + }, + ); + + estimated_costs_only_with_layer_info.insert( + KeyInfoPath::from_known_path(token_order_escrow_path(&token_id)), + EstimatedLayerInformation { + tree_type: TreeType::SumTree, + estimated_layer_count: PotentiallyAtMaxElements, + estimated_layer_sizes: AllItems(DEFAULT_HASH_SIZE_U8, AVERAGE_BALANCE_SIZE, None), + }, + ); + } + + /// The layers a write to one offer's escrowed credits goes through: the root, the prefunded + /// specialized balances, where the escrow key is a leaf on the third level, and the SumTree + /// of one entry per open offer. The prefunded balance and contract fee pot estimations + /// describe the prefunded balances layer at level 1, their keys' depth: from protocol version + /// 15 they keep a deeper description too, so a batch writing to the escrow and to one of + /// them is priced at the escrow's depth whichever registers first. + pub(super) fn add_estimation_costs_for_credit_order_escrow_v0( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + ) { + insert_estimated_layer_keeping_deeper( + estimated_costs_only_with_layer_info, + KeyInfoPath::from_known_path([]), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: EstimatedLevel(3, false), + estimated_layer_sizes: AllSubtrees( + 1, + SomeSumTrees { + sum_trees_weight: 1, + big_sum_trees_weight: 0, + count_trees_weight: 0, + count_sum_trees_weight: 0, + non_sum_trees_weight: 1, + provable_sum_trees_weight: 0, + provable_count_trees_weight: 0, + provable_count_sum_trees_weight: 0, + provable_count_provable_sum_trees_weight: 0, + }, + None, + ), + }, + ); + + insert_estimated_layer_keeping_deeper( + estimated_costs_only_with_layer_info, + KeyInfoPath::from_known_path(prefunded_specialized_balances_path()), + EstimatedLayerInformation { + tree_type: TreeType::SumTree, + estimated_layer_count: EstimatedLevel( + CREDIT_ORDER_ESCROW_LEVEL_IN_PREFUNDED_BALANCES, + false, + ), + estimated_layer_sizes: AllSubtrees(1, AllSumTrees, None), + }, + ); + + estimated_costs_only_with_layer_info.insert( + KeyInfoPath::from_known_owned_path( + prefunded_specialized_balances_for_token_orders_path_vec(), + ), + EstimatedLayerInformation { + tree_type: TreeType::SumTree, + estimated_layer_count: PotentiallyAtMaxElements, + estimated_layer_sizes: AllItems(DEFAULT_HASH_SIZE_U8, AVERAGE_BALANCE_SIZE, None), + }, + ); + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/insert_trees.rs b/packages/rs-drive/src/drive/tokens/order_escrow/insert_trees.rs new file mode 100644 index 00000000000..c8d17cb7a45 --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/insert_trees.rs @@ -0,0 +1,53 @@ +use crate::drive::prefunded_specialized_balances::{ + prefunded_specialized_balances_path, PREFUNDED_BALANCES_FOR_TOKEN_ORDERS, +}; +use crate::drive::tokens::paths::{tokens_root_path, TOKEN_ORDER_ESCROW_KEY}; +use crate::drive::Drive; +use crate::error::Error; +use dpp::version::PlatformVersion; +use grovedb::{Element, TransactionArg}; +use grovedb_path::SubtreePath; + +impl Drive { + /// Inserts the two escrow trees of the token marketplace (protocol version 15) if they do not + /// exist yet: + /// + /// - `[Tokens, TOKEN_ORDER_ESCROW_KEY]`, an empty BigSumTree that holds one SumTree per + /// token, keyed by order id, of the tokens its open listings hold in escrow. It is counted + /// on the balance side of the token conservation check + /// (`calculate_total_tokens_balance` 2). + /// - `[PreFundedSpecializedBalances, PREFUNDED_BALANCES_FOR_TOKEN_ORDERS]`, an empty SumTree, + /// keyed by order id, of the credits the open offers hold in escrow. The prefunded + /// specialized balances root is a SumTree, so the credit conservation check counts it + /// without a change. + /// + /// CONSENSUS-CRITICAL: both the genesis path (`Drive::create_initial_state_structure_v5`) + /// and the upgrade path (`Platform::transition_to_version_15`) call this one helper, in the + /// same position, so a chain born at protocol version 15 and a chain upgraded to it build + /// byte-identical `[Tokens]` and `[PreFundedSpecializedBalances]` subtrees. + pub fn insert_token_order_escrow_trees( + &self, + transaction: TransactionArg, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let tokens_root = tokens_root_path(); + self.grove_insert_if_not_exists( + SubtreePath::from(&tokens_root), + &[TOKEN_ORDER_ESCROW_KEY], + Element::empty_big_sum_tree(), + transaction, + None, + &platform_version.drive, + )?; + let prefunded_root = prefunded_specialized_balances_path(); + self.grove_insert_if_not_exists( + SubtreePath::from(&prefunded_root), + &[PREFUNDED_BALANCES_FOR_TOKEN_ORDERS], + Element::empty_sum_tree(), + transaction, + None, + &platform_version.drive, + )?; + Ok(()) + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/mod.rs new file mode 100644 index 00000000000..dcebf600541 --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/mod.rs @@ -0,0 +1,1205 @@ +//! The escrow of the token marketplace (protocol version 15). +//! +//! An open listing holds the tokens it offers in `[Tokens, TOKEN_ORDER_ESCROW_KEY, token id, +//! order id]`, a SumItem of the token's escrow SumTree, and an open offer holds the credits it +//! offers in `[PreFundedSpecializedBalances, PREFUNDED_BALANCES_FOR_TOKEN_ORDERS, order id]`. Both +//! roots are inserted by `insert_token_order_escrow_trees`. + +mod credits; +mod estimated_costs; +mod insert_trees; +mod tokens; + +#[cfg(test)] +mod tests { + use crate::config::{DriveConfig, DEFAULT_QUERY_LIMIT}; + use crate::drive::prefunded_specialized_balances::prefunded_specialized_balances_path; + use crate::drive::tokens::paths::tokens_root_path; + use crate::drive::Drive; + use crate::error::drive::DriveError; + use crate::error::Error; + use crate::fees::op::LowLevelDriveOperation; + use crate::query::drive_document_sum_query::index_picker::find_range_summable_index_for_where_clauses; + use crate::query::drive_document_sum_query::{ + DocumentSumRequest, DocumentSumResponse, DriveDocumentSumQuery, SumMode, + }; + use crate::query::{WhereClause, WhereOperator}; + use crate::util::estimated_layers::EstimatedLayers; + use crate::util::object_size_info::DocumentInfo::DocumentRefInfo; + use crate::util::object_size_info::{DocumentAndContractInfo, OwnedDocumentInfo}; + use crate::util::test_helpers::setup::setup_drive_with_initial_state_structure; + use dpp::block::block_info::BlockInfo; + use dpp::data_contract::accessors::v0::DataContractV0Getters; + use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; + use dpp::data_contract::document_type::action_fees::ContractFeePot; + use dpp::document::{Document, DocumentV0}; + use dpp::fee::fee_result::FeeResult; + use dpp::identifier::Identifier; + use dpp::platform_value::Value; + use dpp::system_data_contracts::{load_system_data_contract, SystemDataContract}; + use dpp::version::PlatformVersion; + use grovedb::batch::KeyInfoPath; + use grovedb::EstimatedLayerCount::EstimatedLevel; + use grovedb::EstimatedLayerSizes::AllSubtrees; + use grovedb::EstimatedSumTrees::AllBigSumTrees; + use grovedb::{EstimatedLayerInformation, GroveDb, TreeType}; + use std::collections::BTreeMap; + + fn apply(drive: &Drive, operations: Vec) { + drive + .apply_batch_low_level_drive_operations( + None, + None, + operations, + &mut vec![], + &PlatformVersion::latest().drive, + ) + .expect("expected to apply the operations"); + } + + #[test] + fn should_hold_set_and_release_what_a_listing_escrows() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let order_id = [9; 32]; + let fetch = |drive: &Drive, token_id: [u8; 32]| { + drive + .fetch_token_order_escrow(token_id, order_id, None, &mut vec![], platform_version) + .expect("expected to fetch the escrow") + }; + + // A token that never had a listing has no escrow tree, and nothing in escrow. + assert_eq!(fetch(&drive, token_id), None); + + apply( + &drive, + drive + .add_to_token_order_escrow_operations( + token_id, + order_id, + 500, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive, token_id), Some(500)); + assert_eq!(fetch(&drive, [6; 32]), None); + + apply( + &drive, + drive + .set_token_order_escrow_operations( + token_id, + order_id, + 200, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive, token_id), Some(200)); + + apply( + &drive, + drive + .remove_token_order_escrow_operations( + token_id, + order_id, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive, token_id), None); + } + + #[test] + fn should_hold_set_and_release_what_an_offer_escrows() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let order_id = [9; 32]; + let fetch = |drive: &Drive| { + drive + .fetch_credit_order_escrow(order_id, None, &mut vec![], platform_version) + .expect("expected to fetch the escrow") + }; + + assert_eq!(fetch(&drive), None); + apply( + &drive, + drive + .add_to_credit_order_escrow_operations( + order_id, + 70_000, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive), Some(70_000)); + apply( + &drive, + drive + .set_credit_order_escrow_operations(order_id, 30_000, &mut None, platform_version) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive), Some(30_000)); + apply( + &drive, + drive + .remove_credit_order_escrow_operations(order_id, &mut None, platform_version) + .expect("expected the escrow operations"), + ); + assert_eq!(fetch(&drive), None); + } + + /// Listing moves tokens from a balance into escrow: the supply is unchanged, and the token + /// conservation check of protocol version 15 counts the escrow. The calculator of protocol + /// version 14, which reads no escrow, would see tokens missing. + #[test] + fn should_keep_the_token_supply_balanced_when_tokens_move_into_escrow() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let identity_id = [6; 32]; + let block_info = BlockInfo::default(); + + drive + .create_token_trees( + Identifier::from([4; 32]), + 0, + token_id, + false, + false, + &block_info, + true, + None, + platform_version, + ) + .expect("expected to create the token trees"); + drive + .token_mint( + token_id, + identity_id, + 1000, + true, + false, + &block_info, + true, + None, + platform_version, + ) + .expect("expected to mint"); + + let mut operations = drive + .remove_from_identity_token_balance_operations( + token_id, + identity_id, + 400, + &mut None, + None, + platform_version, + ) + .expect("expected the balance operations"); + operations.extend( + drive + .add_to_token_order_escrow_operations( + token_id, + [9; 32], + 400, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"), + ); + apply(&drive, operations); + + let total = drive + .calculate_total_tokens_balance(None, platform_version) + .expect("expected to calculate the total tokens balance"); + assert_eq!(total.total_tokens_in_platform, 1000); + assert_eq!(total.total_identity_token_balances, 600); + assert_eq!(total.total_token_order_escrow_balances, 400); + assert!(total.ok().expect("expected no overflow")); + + let before_the_marketplace = drive + .calculate_total_tokens_balance( + None, + PlatformVersion::get(14).expect("expected protocol version 14"), + ) + .expect("expected to calculate the total tokens balance"); + assert!(!before_the_marketplace.ok().expect("expected no overflow")); + } + + fn fetch_listing(drive: &Drive, token_id: [u8; 32], order_id: [u8; 32]) -> Option { + drive + .fetch_token_order_escrow( + token_id, + order_id, + None, + &mut vec![], + PlatformVersion::latest(), + ) + .expect("expected to fetch the escrow") + } + + fn fetch_offer(drive: &Drive, order_id: [u8; 32]) -> Option { + drive + .fetch_credit_order_escrow(order_id, None, &mut vec![], PlatformVersion::latest()) + .expect("expected to fetch the escrow") + } + + /// Two listings of a token that has none, built for one batch: the second sees the escrow + /// tree the first queues and does not insert it again. + #[test] + fn should_insert_a_tokens_escrow_tree_once_for_two_listings_in_one_batch() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + + let mut batch = drive + .add_to_token_order_escrow_operations( + token_id, + [1; 32], + 10, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected the first listing's operations"); + let second = drive + .add_to_token_order_escrow_operations( + token_id, + [2; 32], + 20, + &mut None, + None, + &mut Some(&mut batch), + platform_version, + ) + .expect("expected the second listing's operations"); + batch.extend(second); + apply(&drive, batch); + + assert_eq!(fetch_listing(&drive, token_id, [1; 32]), Some(10)); + assert_eq!(fetch_listing(&drive, token_id, [2; 32]), Some(20)); + } + + /// Without the operations queued before it, the second listing inserts the tree again, and + /// the batch is refused: what the shared operations prevent. + #[test] + fn should_refuse_a_batch_inserting_a_tokens_escrow_tree_twice() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let mut batch = vec![]; + for order_id in [[1; 32], [2; 32]] { + batch.extend( + drive + .add_to_token_order_escrow_operations( + token_id, + order_id, + 10, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected the listing's operations"), + ); + } + assert!(drive + .apply_batch_low_level_drive_operations( + None, + None, + batch, + &mut vec![], + &platform_version.drive, + ) + .is_err()); + } + + /// Every escrow write can be priced without state: the operations built for estimation + /// cost something to apply and change nothing. + #[test] + fn should_estimate_every_escrow_write_without_writing_it() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let order_id = [9; 32]; + + type Build<'a> = + Box) -> Vec + 'a>; + let writes: Vec<(&str, Build)> = vec![ + ( + "add a listing (the token's first)", + Box::new(|estimation| { + drive + .add_to_token_order_escrow_operations( + token_id, + order_id, + 500, + estimation, + None, + &mut None, + platform_version, + ) + .expect("expected the operations") + }), + ), + ( + "set a listing", + Box::new(|estimation| { + drive + .set_token_order_escrow_operations( + token_id, + order_id, + 200, + estimation, + platform_version, + ) + .expect("expected the operations") + }), + ), + ( + "remove a listing", + Box::new(|estimation| { + drive + .remove_token_order_escrow_operations( + token_id, + order_id, + estimation, + platform_version, + ) + .expect("expected the operations") + }), + ), + ( + "add an offer", + Box::new(|estimation| { + drive + .add_to_credit_order_escrow_operations( + order_id, + 70_000, + estimation, + platform_version, + ) + .expect("expected the operations") + }), + ), + ( + "set an offer", + Box::new(|estimation| { + drive + .set_credit_order_escrow_operations( + order_id, + 30_000, + estimation, + platform_version, + ) + .expect("expected the operations") + }), + ), + ( + "remove an offer", + Box::new(|estimation| { + drive + .remove_credit_order_escrow_operations( + order_id, + estimation, + platform_version, + ) + .expect("expected the operations") + }), + ), + ]; + + for (name, build) in writes { + let mut estimation = Some(EstimatedLayers::new()); + let operations = build(&mut estimation); + let mut costs = vec![]; + drive + .apply_batch_low_level_drive_operations( + estimation, + None, + operations, + &mut costs, + &platform_version.drive, + ) + .unwrap_or_else(|error| panic!("{name}: expected to estimate: {error:?}")); + let fee = Drive::calculate_fee( + None, + Some(costs), + &BlockInfo::default().epoch, + drive.config.epochs_per_era, + platform_version, + None, + ) + .unwrap_or_else(|error| panic!("{name}: expected a fee: {error:?}")); + assert!( + fee.processing_fee > 0, + "{name}: an estimated write costs something" + ); + assert_eq!(fetch_listing(&drive, token_id, order_id), None, "{name}"); + assert_eq!(fetch_offer(&drive, order_id), None, "{name}"); + } + } + + /// The escrow estimation describes the Tokens layer at the escrow key's depth, and keeps a + /// deeper description already registered for the same batch. + #[test] + fn should_keep_the_deeper_tokens_layer_estimate() { + let platform_version = PlatformVersion::latest(); + let tokens_layer_level = |estimation: &EstimatedLayers| match estimation + .get(&KeyInfoPath::from_known_path(tokens_root_path())) + .map(|layer| &layer.estimated_layer_count) + { + Some(EstimatedLevel(level, _)) => *level, + other => panic!("expected an estimated level, got {other:?}"), + }; + + // After the token balance estimation (level 0, the balances key), the escrow's is kept. + let mut estimation = EstimatedLayers::new(); + Drive::add_estimation_costs_for_token_balances( + [5; 32], + &mut estimation, + &platform_version.drive, + ) + .expect("expected the balance estimation"); + assert_eq!(tokens_layer_level(&estimation), 0); + Drive::add_estimation_costs_for_token_order_escrow( + [5; 32], + &mut estimation, + platform_version, + ) + .expect("expected the escrow estimation"); + assert_eq!(tokens_layer_level(&estimation), 3); + + // A deeper description stays. + let mut estimation = EstimatedLayers::new(); + estimation.insert( + KeyInfoPath::from_known_path(tokens_root_path()), + EstimatedLayerInformation { + tree_type: TreeType::NormalTree, + estimated_layer_count: EstimatedLevel(5, false), + estimated_layer_sizes: AllSubtrees(1, AllBigSumTrees, None), + }, + ); + Drive::add_estimation_costs_for_token_order_escrow( + [5; 32], + &mut estimation, + platform_version, + ) + .expect("expected the escrow estimation"); + assert_eq!(tokens_layer_level(&estimation), 5); + } + + /// Both escrows hold signed 8-byte values: `i64::MAX` is held, one more is refused. + #[test] + fn should_hold_up_to_i64_max_in_either_escrow() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let largest = i64::MAX as u64; + + apply( + &drive, + drive + .add_to_token_order_escrow_operations( + token_id, + [1; 32], + largest, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected i64::MAX to be held"), + ); + assert_eq!(fetch_listing(&drive, token_id, [1; 32]), Some(largest)); + assert!(drive + .add_to_token_order_escrow_operations( + token_id, + [2; 32], + largest + 1, + &mut None, + None, + &mut None, + platform_version, + ) + .is_err()); + assert!(drive + .set_token_order_escrow_operations( + token_id, + [1; 32], + largest + 1, + &mut None, + platform_version, + ) + .is_err()); + + apply( + &drive, + drive + .add_to_credit_order_escrow_operations( + [3; 32], + largest, + &mut None, + platform_version, + ) + .expect("expected i64::MAX to be held"), + ); + assert_eq!(fetch_offer(&drive, [3; 32]), Some(largest)); + assert!(drive + .add_to_credit_order_escrow_operations( + [4; 32], + largest + 1, + &mut None, + platform_version + ) + .is_err()); + assert!(drive + .set_credit_order_escrow_operations([3; 32], largest + 1, &mut None, platform_version) + .is_err()); + } + + /// The escrow methods are active from protocol version 15: at 14 every one of them refuses + /// with `VersionNotActive` instead of building operations for trees that do not exist there. + #[test] + fn should_refuse_every_escrow_method_before_protocol_version_15() { + let latest = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(latest)); + let token_id = [5; 32]; + let order_id = [9; 32]; + + let calls = |platform_version: &PlatformVersion| -> Vec<(&str, Result<(), Error>)> { + let mut estimation = EstimatedLayers::new(); + vec![ + ( + "add_tokens", + drive + .add_to_token_order_escrow_operations( + token_id, + order_id, + 500, + &mut None, + None, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "set_tokens", + drive + .set_token_order_escrow_operations( + token_id, + order_id, + 200, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "remove_tokens", + drive + .remove_token_order_escrow_operations( + token_id, + order_id, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "fetch_tokens", + drive + .fetch_token_order_escrow( + token_id, + order_id, + None, + &mut vec![], + platform_version, + ) + .map(|_| ()), + ), + ( + "add_credits", + drive + .add_to_credit_order_escrow_operations( + order_id, + 70_000, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "set_credits", + drive + .set_credit_order_escrow_operations( + order_id, + 30_000, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "remove_credits", + drive + .remove_credit_order_escrow_operations( + order_id, + &mut None, + platform_version, + ) + .map(|_| ()), + ), + ( + "fetch_credits", + drive + .fetch_credit_order_escrow(order_id, None, &mut vec![], platform_version) + .map(|_| ()), + ), + ( + "estimated_costs_for_tokens", + Drive::add_estimation_costs_for_token_order_escrow( + token_id, + &mut estimation, + platform_version, + ), + ), + ( + "estimated_costs_for_credits", + Drive::add_estimation_costs_for_credit_order_escrow( + &mut estimation, + platform_version, + ), + ), + ] + }; + + let before = PlatformVersion::get(14).expect("expected protocol version 14"); + for (name, result) in calls(before) { + assert!( + matches!( + result, + Err(Error::Drive(DriveError::VersionNotActive { .. })) + ), + "{name} at protocol version 14: expected VersionNotActive, got {result:?}" + ); + } + for (name, result) in calls(latest) { + assert!( + result.is_ok(), + "{name} at the latest protocol version: {result:?}" + ); + } + } + + /// What an escrow write costs, priced without state (`estimated`) and applied to the state + /// it writes to (`applied`). + fn estimated_and_applied_fees( + drive: &Drive, + build: impl Fn(&mut Option) -> Vec, + ) -> (FeeResult, FeeResult) { + let platform_version = PlatformVersion::latest(); + let fee = |costs: Vec| { + Drive::calculate_fee( + None, + Some(costs), + &BlockInfo::default().epoch, + drive.config.epochs_per_era, + platform_version, + None, + ) + .expect("expected a fee") + }; + + let mut estimation = Some(EstimatedLayers::new()); + let operations = build(&mut estimation); + let mut estimated_costs = vec![]; + drive + .apply_batch_low_level_drive_operations( + estimation, + None, + operations, + &mut estimated_costs, + &platform_version.drive, + ) + .expect("expected to estimate"); + + let operations = build(&mut None); + let mut applied_costs = vec![]; + drive + .apply_batch_low_level_drive_operations( + None, + None, + operations, + &mut applied_costs, + &platform_version.drive, + ) + .expect("expected to apply"); + + (fee(estimated_costs), fee(applied_costs)) + } + + /// Characterization: the estimate of each escrow write covers what applying it costs on the + /// initial state, where the offers' escrow key sits on the third level of the prefunded + /// specialized balances Merk and a listing's on the fourth of the Tokens Merk. + #[test] + fn should_estimate_at_least_what_an_escrow_write_costs() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + + let (estimated, applied) = estimated_and_applied_fees(&drive, |estimation| { + drive + .add_to_credit_order_escrow_operations( + [9; 32], + 70_000, + estimation, + platform_version, + ) + .expect("expected the operations") + }); + assert!( + estimated.processing_fee >= applied.processing_fee, + "an offer's escrow: estimated {estimated:?}, applied {applied:?}" + ); + assert!(estimated.storage_fee >= applied.storage_fee); + + let (estimated, applied) = estimated_and_applied_fees(&drive, |estimation| { + drive + .set_credit_order_escrow_operations([9; 32], 30_000, estimation, platform_version) + .expect("expected the operations") + }); + assert!( + estimated.processing_fee >= applied.processing_fee, + "an offer's escrow after a fill: estimated {estimated:?}, applied {applied:?}" + ); + + let (estimated, applied) = estimated_and_applied_fees(&drive, |estimation| { + drive + .add_to_token_order_escrow_operations( + token_id, + [1; 32], + 500, + estimation, + None, + &mut None, + platform_version, + ) + .expect("expected the operations") + }); + assert!( + estimated.processing_fee >= applied.processing_fee, + "a token's first listing: estimated {estimated:?}, applied {applied:?}" + ); + assert!(estimated.storage_fee >= applied.storage_fee); + + let (estimated, applied) = estimated_and_applied_fees(&drive, |estimation| { + drive + .set_token_order_escrow_operations( + token_id, + [1; 32], + 200, + estimation, + platform_version, + ) + .expect("expected the operations") + }); + assert!( + estimated.processing_fee >= applied.processing_fee, + "a listing after a fill: estimated {estimated:?}, applied {applied:?}" + ); + } + + /// The offers' escrow is described at its key's depth in the prefunded specialized balances + /// layer, and from protocol version 15 the voting balance and fee pot estimations of the + /// same batch keep that deeper description, whichever registers first. + #[test] + fn should_keep_the_deeper_prefunded_balances_layer_estimate() { + let platform_version = PlatformVersion::latest(); + let prefunded_layer_level = |estimation: &EstimatedLayers| match estimation + .get(&KeyInfoPath::from_known_path( + prefunded_specialized_balances_path(), + )) + .map(|layer| &layer.estimated_layer_count) + { + Some(EstimatedLevel(level, _)) => *level, + other => panic!("expected an estimated level, got {other:?}"), + }; + let voting = |estimation: &mut EstimatedLayers, platform_version: &PlatformVersion| { + Drive::add_estimation_costs_for_prefunded_specialized_balance_update( + estimation, + &platform_version.drive, + ) + .expect("expected the voting balance estimation") + }; + let fee_pot = |estimation: &mut EstimatedLayers, platform_version: &PlatformVersion| { + Drive::add_estimation_costs_for_contract_fee_pot_update( + ContractFeePot::Owner, + estimation, + &platform_version.drive, + ) + .expect("expected the fee pot estimation") + }; + let escrow = |estimation: &mut EstimatedLayers| { + Drive::add_estimation_costs_for_credit_order_escrow(estimation, platform_version) + .expect("expected the escrow estimation") + }; + + type Sibling = fn(&mut EstimatedLayers, &PlatformVersion); + let siblings: [Sibling; 2] = [voting, fee_pot]; + for sibling in siblings { + // The escrow first, the sibling after: the sibling keeps the escrow's depth. + let mut estimation = EstimatedLayers::new(); + escrow(&mut estimation); + assert_eq!(prefunded_layer_level(&estimation), 2); + sibling(&mut estimation, platform_version); + assert_eq!(prefunded_layer_level(&estimation), 2); + + // The sibling first, the escrow after. + let mut estimation = EstimatedLayers::new(); + sibling(&mut estimation, platform_version); + assert_eq!(prefunded_layer_level(&estimation), 1); + escrow(&mut estimation); + assert_eq!(prefunded_layer_level(&estimation), 2); + + // Protocol version 14's siblings describe the layer at their keys' level, as before. + let mut estimation = EstimatedLayers::new(); + escrow(&mut estimation); + sibling( + &mut estimation, + PlatformVersion::get(14).expect("expected protocol version 14"), + ); + assert_eq!(prefunded_layer_level(&estimation), 1); + } + } + + /// A batch moving tokens between a balance and escrow describes the Tokens layer at the + /// escrow key's depth whichever of the two operations is built first. At protocol version 14 + /// the balance estimation built last lowers it to the balances key, as it always did. + #[test] + fn should_keep_the_escrow_depth_whichever_of_balance_and_escrow_is_built_first() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [5; 32]; + let tokens_layer_level = |estimation: &Option| match estimation + .as_ref() + .expect("expected an estimation") + .get(&KeyInfoPath::from_known_path(tokens_root_path())) + .map(|layer| &layer.estimated_layer_count) + { + Some(EstimatedLevel(level, _)) => *level, + other => panic!("expected an estimated level, got {other:?}"), + }; + let balance = |estimation: &mut Option, + platform_version: &PlatformVersion| { + drive + .remove_from_identity_token_balance_operations( + token_id, + [7; 32], + 500, + estimation, + None, + platform_version, + ) + .expect("expected the balance operations"); + }; + let escrow = |estimation: &mut Option| { + drive + .add_to_token_order_escrow_operations( + token_id, + [9; 32], + 500, + estimation, + None, + &mut None, + platform_version, + ) + .expect("expected the escrow operations"); + }; + + let mut estimation = Some(EstimatedLayers::new()); + balance(&mut estimation, platform_version); + escrow(&mut estimation); + assert_eq!(tokens_layer_level(&estimation), 3, "the balance first"); + + let mut estimation = Some(EstimatedLayers::new()); + escrow(&mut estimation); + balance(&mut estimation, platform_version); + assert_eq!(tokens_layer_level(&estimation), 3, "the escrow first"); + + let mut estimation = Some(EstimatedLayers::new()); + escrow(&mut estimation); + balance( + &mut estimation, + PlatformVersion::get(14).expect("expected protocol version 14"), + ); + assert_eq!(tokens_layer_level(&estimation), 0, "protocol version 14"); + } + + /// An offer's batch debits the buyer's balance and credits the offer's escrow: built in + /// either order, the estimation describes the root at the escrow's depth (3) and the + /// prefunded specialized balances layer at the escrow key's (2). At protocol version 14 the + /// balance estimation built last lowers the root to the balances' level (1), as it always + /// did. + #[test] + fn should_keep_the_credit_escrow_depths_whichever_of_debit_and_escrow_is_built_first() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let level_at = |estimation: &Option, path: KeyInfoPath| match estimation + .as_ref() + .expect("expected an estimation") + .get(&path) + .map(|layer| &layer.estimated_layer_count) + { + Some(EstimatedLevel(level, _)) => *level, + other => panic!("expected an estimated level, got {other:?}"), + }; + let root = || KeyInfoPath::from_known_path([]); + let prefunded = || KeyInfoPath::from_known_path(prefunded_specialized_balances_path()); + let debit = |estimation: &mut Option, + platform_version: &PlatformVersion| { + drive + .remove_from_identity_balance_operations( + [7; 32], + 70_000, + estimation, + None, + platform_version, + ) + .expect("expected the debit operations"); + }; + let escrow = |estimation: &mut Option| { + drive + .add_to_credit_order_escrow_operations( + [9; 32], + 70_000, + estimation, + platform_version, + ) + .expect("expected the escrow operations"); + }; + + let mut estimation = Some(EstimatedLayers::new()); + debit(&mut estimation, platform_version); + escrow(&mut estimation); + assert_eq!(level_at(&estimation, root()), 3, "the debit first"); + assert_eq!(level_at(&estimation, prefunded()), 2, "the debit first"); + + let mut estimation = Some(EstimatedLayers::new()); + escrow(&mut estimation); + debit(&mut estimation, platform_version); + assert_eq!(level_at(&estimation, root()), 3, "the escrow first"); + assert_eq!(level_at(&estimation, prefunded()), 2, "the escrow first"); + + let mut estimation = Some(EstimatedLayers::new()); + escrow(&mut estimation); + debit( + &mut estimation, + PlatformVersion::get(14).expect("expected protocol version 14"), + ); + assert_eq!(level_at(&estimation, root()), 1, "protocol version 14"); + } + + /// Orders priced at two scales sit apart in the token market contract's `depth` index + /// (`tokenId`, `decimals`, `price`): a sum for the token at decimals 8 counts only the + /// orders placed at 8 and one at decimals 2 only those at 2, though all are at the same + /// price, and a proven per-price sum at one scale verifies against the path query a client + /// rebuilds and holds that scale's orders alone. + #[test] + fn should_sum_the_depth_of_each_price_scale_apart() { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let contract = load_system_data_contract(SystemDataContract::TokenMarket, platform_version) + .expect("expected the token market contract"); + drive + .apply_contract( + &contract, + BlockInfo::default(), + true, + None, + None, + platform_version, + ) + .expect("expected to store the token market contract"); + let ask_type = contract + .document_type_for_name("ask") + .expect("expected the ask type"); + let token_id = Identifier::from([5; 32]); + let maker_id = Identifier::from([7; 32]); + + for (i, (decimals, remaining)) in + [(8i64, 300i64), (8, 200), (2, 50)].into_iter().enumerate() + { + let document: Document = DocumentV0 { + contract_version: None, + id: Identifier::from([i as u8 + 1; 32]), + owner_id: maker_id, + properties: BTreeMap::from([ + ( + "tokenId".to_string(), + Value::Identifier(token_id.to_buffer()), + ), + ("price".to_string(), Value::I64(100)), + ("amount".to_string(), Value::I64(remaining)), + ("remaining".to_string(), Value::I64(remaining)), + ("decimals".to_string(), Value::I64(decimals)), + ]), + revision: Some(1), + created_at: Some(1_000 + i as u64), + updated_at: Some(1_000 + i as u64), + transferred_at: None, + created_at_block_height: None, + updated_at_block_height: None, + transferred_at_block_height: None, + created_at_core_block_height: None, + updated_at_core_block_height: None, + transferred_at_core_block_height: None, + creator_id: None, + moderated_at: None, + moderated_by: None, + } + .into(); + drive + .add_document_for_contract( + DocumentAndContractInfo { + owned_document_info: OwnedDocumentInfo { + document_info: DocumentRefInfo((&document, None)), + owner_id: Some(maker_id.to_buffer()), + }, + contract: &contract, + document_type: ask_type, + }, + false, + BlockInfo::default(), + true, + None, + platform_version, + None, + ) + .expect("expected to store the listing"); + } + + // Every price of the token at one scale: the depth index's properties, the last one a + // range. + let at_scale = |decimals: i64| { + vec![ + WhereClause { + field: "tokenId".to_string(), + operator: WhereOperator::Equal, + value: Value::Identifier(token_id.to_buffer()), + }, + WhereClause { + field: "decimals".to_string(), + operator: WhereOperator::Equal, + value: Value::I64(decimals), + }, + WhereClause { + field: "price".to_string(), + operator: WhereOperator::GreaterThan, + value: Value::I64(0), + }, + ] + }; + let drive_config = DriveConfig::default(); + let sum = |where_clauses: Vec| { + let request = DocumentSumRequest { + contract: &contract, + document_type: ask_type, + sum_property: "remaining".to_string(), + where_clauses, + order_clauses: Vec::new(), + mode: SumMode::Aggregate, + limit: None, + prove: false, + drive_config: &drive_config, + resolved_time_ranges: vec![], + }; + match drive + .execute_document_sum_request(request, None, platform_version) + .expect("expected the depth sum") + { + DocumentSumResponse::Aggregate(total) => total, + other => panic!("expected an aggregate, got {other:?}"), + } + }; + assert_eq!(sum(at_scale(8)), 500); + assert_eq!(sum(at_scale(2)), 50); + + // A proven per-price sum at decimals 8, verified as a client does. + let where_clauses = at_scale(8); + let request = DocumentSumRequest { + contract: &contract, + document_type: ask_type, + sum_property: "remaining".to_string(), + where_clauses: where_clauses.clone(), + order_clauses: Vec::new(), + mode: SumMode::GroupByRange, + limit: None, + prove: true, + drive_config: &drive_config, + resolved_time_ranges: vec![], + }; + let DocumentSumResponse::Proof(proof) = drive + .execute_document_sum_request(request, None, platform_version) + .expect("expected the proven depth sum") + else { + panic!("expected a proof"); + }; + let index = find_range_summable_index_for_where_clauses( + ask_type.indexes(), + &where_clauses, + "remaining", + &[], + ) + .expect("expected the depth index to serve the query"); + assert_eq!(index.name, "depth"); + let path_query = DriveDocumentSumQuery { + document_type: ask_type, + contract_id: contract.id().to_buffer(), + document_type_name: "ask".to_string(), + index, + where_clauses, + sum_property: "remaining".to_string(), + } + .distinct_sum_path_query(Some(DEFAULT_QUERY_LIMIT), true, platform_version) + .expect("expected the client's path query"); + let (proof_root, elements) = + GroveDb::verify_query(&proof, &path_query, &platform_version.drive.grove_version) + .expect("expected the proof to verify"); + let live_root = drive + .grove + .root_hash(None, &platform_version.drive.grove_version) + .value + .expect("expected the current Drive root"); + assert_eq!( + proof_root, live_root, + "the depth proof authenticates the state holding the orders" + ); + let sums: Vec = elements + .iter() + .filter_map(|(_, _, element)| { + element + .as_ref() + .map(|element| element.sum_value_or_default()) + }) + .collect(); + assert_eq!( + sums, + vec![500], + "one price bucket at decimals 8, holding 500" + ); + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/tokens/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/tokens/mod.rs new file mode 100644 index 00000000000..620fe9250ff --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/tokens/mod.rs @@ -0,0 +1,153 @@ +mod v0; + +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use dpp::balances::credits::TokenAmount; +use dpp::version::PlatformVersion; +use grovedb::TransactionArg; + +impl Drive { + /// The operations that hold `amount` of the token `token_id` in escrow for the new listing + /// `order_id`, creating the token's escrow tree with its first listing. The caller removes + /// the amount from the seller's balance in the same batch: escrow moves tokens, it does not + /// create them, and the token conservation check counts the escrow. + /// + /// `previous_batch_operations` are the operations already queued for the same batch: a + /// caller placing several listings of a token in one batch passes them, so the token's escrow + /// tree is inserted once, by the first. + #[allow(clippy::too_many_arguments)] + pub fn add_to_token_order_escrow_operations( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + amount: TokenAmount, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + previous_batch_operations: &mut Option<&mut Vec>, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version.drive.methods.token.order_escrow.add_tokens { + Some(0) => self.add_to_token_order_escrow_operations_v0( + token_id, + order_id, + amount, + estimated_costs_only_with_layer_info, + transaction, + previous_batch_operations, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "add_to_token_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "add_to_token_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// The operations that set what the open listing `order_id` of `token_id` holds in escrow to + /// `amount`, after a partial fill. `amount` is above zero: a listing filled entirely is + /// removed instead. + pub fn set_token_order_escrow_operations( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + amount: TokenAmount, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version.drive.methods.token.order_escrow.set_tokens { + Some(0) => self.set_token_order_escrow_operations_v0( + token_id, + order_id, + amount, + estimated_costs_only_with_layer_info, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "set_token_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "set_token_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// The operations that remove the escrow entry of the listing `order_id` of `token_id`, + /// when it is filled entirely or cancelled. The token's escrow tree stays. + pub fn remove_token_order_escrow_operations( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .remove_tokens + { + Some(0) => self.remove_token_order_escrow_operations_v0( + token_id, + order_id, + estimated_costs_only_with_layer_info, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "remove_token_order_escrow_operations".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "remove_token_order_escrow_operations".to_string(), + known_versions: vec![0], + })), + } + } + + /// What the listing `order_id` of `token_id` holds in escrow, `None` when it holds nothing. + pub fn fetch_token_order_escrow( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + match platform_version + .drive + .methods + .token + .order_escrow + .fetch_tokens + { + Some(0) => self.fetch_token_order_escrow_v0( + token_id, + order_id, + transaction, + drive_operations, + platform_version, + ), + Some(version) => Err(Error::Drive(DriveError::UnknownVersionMismatch { + method: "fetch_token_order_escrow".to_string(), + known_versions: vec![0], + received: version, + })), + None => Err(Error::Drive(DriveError::VersionNotActive { + method: "fetch_token_order_escrow".to_string(), + known_versions: vec![0], + })), + } + } +} diff --git a/packages/rs-drive/src/drive/tokens/order_escrow/tokens/v0/mod.rs b/packages/rs-drive/src/drive/tokens/order_escrow/tokens/v0/mod.rs new file mode 100644 index 00000000000..2c1cb4982c9 --- /dev/null +++ b/packages/rs-drive/src/drive/tokens/order_escrow/tokens/v0/mod.rs @@ -0,0 +1,152 @@ +use crate::drive::tokens::paths::{ + token_order_escrow_path, token_order_escrow_path_vec, token_order_escrow_root_path, +}; +use crate::drive::Drive; +use crate::error::drive::DriveError; +use crate::error::Error; +use crate::fees::op::LowLevelDriveOperation; +use crate::fees::op::LowLevelDriveOperation::GroveOperation; +use crate::util::estimated_layers::EstimatedLayers; +use crate::util::grove_operations::{BatchInsertTreeApplyType, DirectQueryType}; +use crate::util::object_size_info::PathKeyInfo::PathFixedSizeKeyRef; +use dpp::balances::credits::TokenAmount; +use dpp::version::PlatformVersion; +use dpp::ProtocolError; +use grovedb::batch::QualifiedGroveDbOp; +use grovedb::{Element, TransactionArg, TreeType}; + +/// An escrowed amount is a SumItem, which holds an i64. +fn escrow_sum_value(amount: TokenAmount) -> Result { + i64::try_from(amount).map_err(|_| { + ProtocolError::CriticalCorruptedCreditsCodeExecution(format!( + "token amount to hold in escrow over i64 max, is {amount}" + )) + .into() + }) +} + +impl Drive { + #[allow(clippy::too_many_arguments)] + pub(super) fn add_to_token_order_escrow_operations_v0( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + amount: TokenAmount, + estimated_costs_only_with_layer_info: &mut Option, + transaction: TransactionArg, + previous_batch_operations: &mut Option<&mut Vec>, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let mut drive_operations = vec![]; + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_token_order_escrow( + token_id, + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + let tree_apply_type = if estimated_costs_only_with_layer_info.is_none() { + BatchInsertTreeApplyType::StatefulBatchInsertTree + } else { + BatchInsertTreeApplyType::StatelessBatchInsertTree { + in_tree_type: TreeType::BigSumTree, + tree_type: TreeType::SumTree, + flags_len: 0, + } + }; + + // The token's escrow tree is created with its first listing, whether that is in state or + // queued earlier in the same batch. + self.batch_insert_empty_tree_if_not_exists( + PathFixedSizeKeyRef::<2>((token_order_escrow_root_path(), token_id.as_slice())), + TreeType::SumTree, + None, + tree_apply_type, + transaction, + previous_batch_operations, + &mut drive_operations, + &platform_version.drive, + )?; + + drive_operations.push(LowLevelDriveOperation::insert_for_known_path_key_element( + token_order_escrow_path_vec(token_id), + order_id.to_vec(), + Element::new_sum_item(escrow_sum_value(amount)?), + )); + + Ok(drive_operations) + } + + pub(super) fn set_token_order_escrow_operations_v0( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + amount: TokenAmount, + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_token_order_escrow( + token_id, + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + Ok(vec![ + LowLevelDriveOperation::replace_for_known_path_key_element( + token_order_escrow_path_vec(token_id), + order_id.to_vec(), + Element::new_sum_item(escrow_sum_value(amount)?), + ), + ]) + } + + pub(super) fn remove_token_order_escrow_operations_v0( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + estimated_costs_only_with_layer_info: &mut Option, + platform_version: &PlatformVersion, + ) -> Result, Error> { + if let Some(estimated_costs_only_with_layer_info) = estimated_costs_only_with_layer_info { + Self::add_estimation_costs_for_token_order_escrow( + token_id, + estimated_costs_only_with_layer_info, + platform_version, + )?; + } + + Ok(vec![GroveOperation( + QualifiedGroveDbOp::delete_op(token_order_escrow_path_vec(token_id), order_id.to_vec()) + .dont_check_for_backwards_references(), + )]) + } + + pub(super) fn fetch_token_order_escrow_v0( + &self, + token_id: [u8; 32], + order_id: [u8; 32], + transaction: TransactionArg, + drive_operations: &mut Vec, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let path = token_order_escrow_path(&token_id); + let element = self.grove_get_raw_optional( + (&path).into(), + order_id.as_slice(), + DirectQueryType::StatefulDirectQuery, + transaction, + drive_operations, + &platform_version.drive, + )?; + match element { + None => Ok(None), + Some(Element::SumItem(amount, _)) if amount >= 0 => Ok(Some(amount as TokenAmount)), + Some(_) => Err(Error::Drive(DriveError::CorruptedElementType( + "a token order escrow entry must be a non-negative sum item", + ))), + } + } +} diff --git a/packages/rs-drive/src/drive/tokens/paths.rs b/packages/rs-drive/src/drive/tokens/paths.rs index 69fc5945300..63037812436 100644 --- a/packages/rs-drive/src/drive/tokens/paths.rs +++ b/packages/rs-drive/src/drive/tokens/paths.rs @@ -30,6 +30,14 @@ pub const TOKEN_DISTRIBUTIONS_KEY: u8 = 32; /// balance SumItem. The BigSumTree total is the amount of every token currently shielded and /// joins the identity balances on the balance side of the token conservation check. pub const TOKEN_SHIELDED_POOLS_KEY: u8 = 224; +/// Key for the token order escrow tree (a BigSumTree of per-token SumTrees). +/// +/// Introduced in protocol version 15 for the token marketplace. Each token that has had an open +/// listing owns one child SumTree keyed by its token id, created with its first listing, whose +/// SumItems, keyed by order id, are the tokens each open listing holds in escrow. The BigSumTree +/// total is every token held in escrow and joins the identity balances and the shielded pools on +/// the balance side of the token conservation check (`calculate_total_tokens_balance` 2). +pub const TOKEN_ORDER_ESCROW_KEY: u8 = 96; // The Token Merk tree looks like // TOKEN_BALANCES_KEY @@ -37,6 +45,9 @@ pub const TOKEN_SHIELDED_POOLS_KEY: u8 = 224; // TOKEN_STATUS_INFO_KEY TOKEN_IDENTITY_INFO_KEY // / \ / // TOKEN_DISTRIBUTIONS_KEY TOKEN_DIRECT_SELL_PRICE_KEY TOKEN_CONTRACT_INFO_KEY +// +// TOKEN_SHIELDED_POOLS_KEY (protocol version 14) is the right child of TOKEN_IDENTITY_INFO_KEY, +// and TOKEN_ORDER_ESCROW_KEY (protocol version 15) the right child of TOKEN_DIRECT_SELL_PRICE_KEY. // The token distribution Tree level @@ -696,6 +707,37 @@ pub fn token_shielded_pools_root_path_vec() -> Vec> { vec![vec![RootTree::Tokens as u8], vec![TOKEN_SHIELDED_POOLS_KEY]] } +/// The root path of the token order escrow tree +pub fn token_order_escrow_root_path() -> [&'static [u8]; 2] { + [ + Into::<&[u8; 1]>::into(RootTree::Tokens), + &[TOKEN_ORDER_ESCROW_KEY], + ] +} + +/// The root path of the token order escrow tree as a vec +pub fn token_order_escrow_root_path_vec() -> Vec> { + vec![vec![RootTree::Tokens as u8], vec![TOKEN_ORDER_ESCROW_KEY]] +} + +/// The path of one token's escrow tree, whose SumItems are keyed by order id +pub fn token_order_escrow_path(token_id: &[u8; 32]) -> [&[u8]; 3] { + [ + Into::<&[u8; 1]>::into(RootTree::Tokens), + &[TOKEN_ORDER_ESCROW_KEY], + token_id, + ] +} + +/// The path of one token's escrow tree as a vec +pub fn token_order_escrow_path_vec(token_id: [u8; 32]) -> Vec> { + vec![ + vec![RootTree::Tokens as u8], + vec![TOKEN_ORDER_ESCROW_KEY], + token_id.to_vec(), + ] +} + #[cfg(feature = "server")] #[cfg(test)] mod tests { diff --git a/packages/rs-drive/src/drive/tokens/structure.rs b/packages/rs-drive/src/drive/tokens/structure.rs index 6dec7165950..435e2b87491 100644 --- a/packages/rs-drive/src/drive/tokens/structure.rs +++ b/packages/rs-drive/src/drive/tokens/structure.rs @@ -6,7 +6,7 @@ use crate::drive::tokens::paths::{ TOKEN_BALANCES_KEY, TOKEN_BLOCK_TIMED_DISTRIBUTIONS_KEY, TOKEN_CONTRACT_INFO_KEY, TOKEN_DIRECT_SELL_PRICE_KEY, TOKEN_DISTRIBUTIONS_KEY, TOKEN_EPOCH_TIMED_DISTRIBUTIONS_KEY, TOKEN_IDENTITY_INFO_KEY, TOKEN_MS_TIMED_DISTRIBUTIONS_KEY, - TOKEN_ONCE_PER_IDENTITY_DISTRIBUTIONS_KEY, + TOKEN_ONCE_PER_IDENTITY_DISTRIBUTIONS_KEY, TOKEN_ORDER_ESCROW_KEY, TOKEN_PERPETUAL_DISTRIBUTIONS_FOR_IDENTITIES_LAST_CLAIM_KEY, TOKEN_PERPETUAL_DISTRIBUTIONS_INFO_KEY, TOKEN_PERPETUAL_DISTRIBUTIONS_KEY, TOKEN_PRE_PROGRAMMED_DISTRIBUTIONS_FOR_IDENTITIES_LAST_CLAIM_KEY, @@ -44,6 +44,7 @@ pub(crate) fn structure() -> StructureNode { .children(vec![ distributions(), shielded_pools(), + order_escrow(), StructureNode::fixed( "status", &[TOKEN_STATUS_INFO_KEY], @@ -372,6 +373,37 @@ fn distributions() -> StructureNode { ]) } +/// The tokens the token marketplace holds in escrow for open listings, from protocol 15. +fn order_escrow() -> StructureNode { + StructureNode::fixed( + "order_escrow", + &[TOKEN_ORDER_ESCROW_KEY], + "TokenOrderEscrow", + "TOKEN_ORDER_ESCROW_KEY", + ) + .kind(ElementKind::BigSumTree) + .since(15) + .source(SOURCE) + .book("data-model/token-marketplace.md") + .describe( + "The tokens open listings hold in escrow. The sum \ + joins the identity balances and the shielded pools \ + in the token conservation check.", + ) + .child( + token("The escrowed listings of one token; created with its first listing.") + .kind(ElementKind::SumTree) + .lazy() + .child( + StructureNode::identifier("order", "order_id", "The listing's order id") + .kind(ElementKind::SumItem) + .lazy() + .value("token amount") + .describe("What one open listing still holds."), + ), + ) +} + /// One Orchard pool per token that enables shielded balances in protocol 14. fn shielded_pools() -> StructureNode { StructureNode::fixed( diff --git a/packages/rs-drive/src/structure/tests.rs b/packages/rs-drive/src/structure/tests.rs index 978dd03afc1..fb21f3d998c 100644 --- a/packages/rs-drive/src/structure/tests.rs +++ b/packages/rs-drive/src/structure/tests.rs @@ -1770,6 +1770,43 @@ mod fixtures { .expect("expected to commit the token anchor"); conformance_of(&drive, "token_shielded_pool", run); } + /// The token marketplace escrow: two listings of one token holding tokens, and two offers + /// holding credits. + fn token_order_escrow(run: &mut FixtureRun) { + let platform_version = PlatformVersion::latest(); + let drive = setup_drive_with_initial_state_structure(Some(platform_version)); + let token_id = [7; 32]; + for (order_id, amount) in [([1; 32], 500), ([2; 32], 30)] { + apply_operations( + &drive, + drive + .add_to_token_order_escrow_operations( + token_id, + order_id, + amount, + &mut None, + None, + &mut None, + platform_version, + ) + .expect("expected the listing escrow operations"), + ); + } + for (order_id, credits) in [([3; 32], 70_000), ([4; 32], 1_000)] { + apply_operations( + &drive, + drive + .add_to_credit_order_escrow_operations( + order_id, + credits, + &mut None, + platform_version, + ) + .expect("expected the offer escrow operations"), + ); + } + conformance_of(&drive, "token_order_escrow", run); + } /// Documents of a type declaring a `ttl`: stored without storage flags, each with an entry /// in the documents expirations tree under the time it expires, and a lifetime storage fee /// pool. Two of them expire together, created in the same block. @@ -1884,6 +1921,7 @@ mod fixtures { contract_groups_and_bound_keys(&mut run); spent_nullifiers(&mut run); token_shielded_pool(&mut run); + token_order_escrow(&mut run); run } diff --git a/packages/rs-drive/src/util/estimated_layers.rs b/packages/rs-drive/src/util/estimated_layers.rs index 23e349941bd..338db063ca1 100644 --- a/packages/rs-drive/src/util/estimated_layers.rs +++ b/packages/rs-drive/src/util/estimated_layers.rs @@ -337,6 +337,35 @@ impl EstimatedLayers { } } + /// Describes the layer at `path` as `layer` unless the description written last for it is + /// at least as deep (`estimate_levels`, which gives a description by element count a depth + /// too: one that may hold the most elements a layer can is 32 levels deep, the worst case); + /// the estimate keeps the description it does not write among the path's other + /// descriptions either way. + pub(crate) fn insert_keeping_deeper( + &mut self, + path: KeyInfoPath, + layer: EstimatedLayerInformation, + ) { + match self.last_written.entry(path) { + Entry::Occupied(mut written) => { + let kept = if written.get().estimated_layer_count.estimate_levels() + >= layer.estimated_layer_count.estimate_levels() + { + layer + } else { + written.insert(layer) + }; + if kept != *written.get() { + keep_other_description(&mut self.other_descriptions, written.key(), kept); + } + } + Entry::Vacant(unwritten) => { + unwritten.insert(layer); + } + } + } + /// The last description written for the layer at `path`. pub fn get(&self, path: &KeyInfoPath) -> Option<&EstimatedLayerInformation> { self.last_written.get(path) @@ -376,10 +405,41 @@ impl EstimatedLayers { } } +/// Describes the layer at `path` as `layer`, unless the estimate already describes it at least as +/// deep ([`EstimatedLayers::insert_keeping_deeper`]). +pub(crate) fn insert_estimated_layer_keeping_deeper( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + path: KeyInfoPath, + layer: EstimatedLayerInformation, +) { + estimated_costs_only_with_layer_info.insert_keeping_deeper(path, layer); +} + +/// Runs `register`, an estimation that overwrites the descriptions of the layers at `paths`, and +/// puts back each description it replaced that was deeper. +pub(crate) fn register_keeping_deeper_estimated_layers( + estimated_costs_only_with_layer_info: &mut EstimatedLayers, + paths: &[KeyInfoPath], + register: impl FnOnce(&mut EstimatedLayers), +) { + let before: Vec<(KeyInfoPath, EstimatedLayerInformation)> = paths + .iter() + .filter_map(|path| { + estimated_costs_only_with_layer_info + .get(path) + .map(|layer| (path.clone(), *layer)) + }) + .collect(); + register(estimated_costs_only_with_layer_info); + for (path, layer) in before { + insert_estimated_layer_keeping_deeper(estimated_costs_only_with_layer_info, path, layer); + } +} + #[cfg(test)] mod tests { use super::*; - use grovedb::EstimatedLayerCount::ApproximateElements; + use grovedb::EstimatedLayerCount::{ApproximateElements, PotentiallyAtMaxElements}; use grovedb::EstimatedLayerSizes::{AllItems, AllReference, AllSubtrees, Mix}; use grovedb::EstimatedSumTrees::{AllBigSumTrees, NoSumTrees}; use grovedb::TreeType; @@ -592,4 +652,90 @@ mod tests { ); assert_eq!(resolved.get(&undescribed), None); } + + fn layer(level: u32) -> EstimatedLayerInformation { + at_level(level, AllSubtrees(1, NoSumTrees, None)) + } + + fn level_at(estimation: &EstimatedLayers, path: &KeyInfoPath) -> Option { + estimation + .get(path) + .and_then(|layer| match layer.estimated_layer_count { + EstimatedLevel(level, _) => Some(level), + _ => None, + }) + } + + #[test] + fn should_insert_unless_a_deeper_or_equal_level_is_described() { + let path = KeyInfoPath::from_known_path([[1u8].as_slice()]); + let mut estimation = EstimatedLayers::new(); + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), layer(1)); + assert_eq!(level_at(&estimation, &path), Some(1)); + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), layer(2)); + assert_eq!(level_at(&estimation, &path), Some(2)); + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), layer(1)); + assert_eq!(level_at(&estimation, &path), Some(2)); + // A description by element count has a depth too: one that may hold the most elements + // a layer can (32 levels, the worst case) is kept over a shallower level, and one of a + // few elements gives way to a deeper level. + let at_max = EstimatedLayerInformation { + estimated_layer_count: PotentiallyAtMaxElements, + ..layer(0) + }; + estimation.insert(path.clone(), at_max); + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), layer(5)); + assert_eq!(estimation.get(&path), Some(&at_max)); + estimation.insert( + path.clone(), + EstimatedLayerInformation { + estimated_layer_count: ApproximateElements(3), + ..layer(0) + }, + ); + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), layer(5)); + assert_eq!(level_at(&estimation, &path), Some(5)); + } + + /// `insert_estimated_layer_keeping_deeper` writes the deeper of two descriptions and keeps + /// the other: a shallower description of wider nodes and a deeper one of narrower nodes + /// resolve to the wider nodes at the deeper depth, in either order. + #[test] + fn should_resolve_with_the_nodes_of_a_description_not_kept_deeper() { + let path = KeyInfoPath::from_known_path([[1u8].as_slice()]); + let shallow_wide = at_level(3, AllItems(32, 8, None)); + let deep_narrow = at_level(4, AllItems(1, 1, None)); + let deep_wide = at_level(4, AllItems(32, 8, None)); + assert!(credits(&deep_wide) > credits(&deep_narrow)); + for order in orders(&[shallow_wide, deep_narrow]) { + let mut estimation = EstimatedLayers::new(); + for description in &order { + insert_estimated_layer_keeping_deeper(&mut estimation, path.clone(), *description); + } + assert_eq!(estimation.get(&path), Some(&deep_narrow), "order {order:?}"); + let resolved = estimation + .resolve(PlatformVersion::latest()) + .expect("expected to resolve"); + assert_eq!(resolved.get(&path), Some(&deep_wide), "order {order:?}"); + } + } + + #[test] + fn should_put_back_a_deeper_level_an_estimation_overwrote() { + let deep = KeyInfoPath::from_known_path([[1u8].as_slice()]); + let shallow = KeyInfoPath::from_known_path([[2u8].as_slice()]); + let mut estimation = EstimatedLayers::new(); + estimation.insert(deep.clone(), layer(2)); + estimation.insert(shallow.clone(), layer(0)); + register_keeping_deeper_estimated_layers( + &mut estimation, + &[deep.clone(), shallow.clone()], + |estimation| { + estimation.insert(deep.clone(), layer(1)); + estimation.insert(shallow.clone(), layer(1)); + }, + ); + assert_eq!(level_at(&estimation, &deep), Some(2)); + assert_eq!(level_at(&estimation, &shallow), Some(1)); + } } diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs index be36896eedc..67533814d1c 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/v11.rs @@ -15,6 +15,9 @@ use super::{ /// - `create_domain_data_trigger` 2, which keeps only the parent domain checks of a DPNS domain /// create: the DPNS contract v3 (`SYSTEM_DATA_CONTRACT_VERSIONS_V4`) checks the normalized /// names, the preorder and the identity record with schema keywords. +/// - the data trigger `bindings` 3, which refuse a document transition's replace or delete of a +/// token market contract order (`ask` and `bid`): only the token marketplace transitions write +/// them. /// - the batch `advanced_structure` 2: v1, then the structure checks of token actions protocol /// v15 introduces, which read only the contract the action carries and so run in check_tx too. /// A `TokenConfigUpdate` can make a token tradeable only if it is transferable. @@ -63,6 +66,7 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V11: DriveAbciValidationVersions = document_reference_validation: 1, // a consumed commitment keeps what its lookup read document_create_transition_structure_validation: 2, // a decision proposal states its fund, a rule within the limits and an end date one to four weeks away data_triggers: DriveAbciValidationDataTriggerAndBindingVersions { + bindings: 3, // the token market contract's orders are written by the marketplace transitions only triggers: DriveAbciValidationDataTriggerVersions { create_domain_data_trigger: 2, ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 @@ -71,10 +75,6 @@ pub const DRIVE_ABCI_VALIDATION_VERSIONS_V11: DriveAbciValidationVersions = .data_triggers .triggers }, - ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 - .state_transitions - .batch_state_transition - .data_triggers }, ..DRIVE_ABCI_VALIDATION_VERSIONS_V10 .state_transitions diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/mod.rs b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/mod.rs index a44447be92a..650f5adbce7 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/mod.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/mod.rs @@ -1,7 +1,8 @@ -use versioned_feature_core::FeatureVersion; +use versioned_feature_core::{FeatureVersion, OptionalFeatureVersion}; pub mod v1; pub mod v2; +pub mod v3; #[derive(Clone, Debug, Default)] pub struct DriveTokenMethodVersions { @@ -10,6 +11,26 @@ pub struct DriveTokenMethodVersions { pub update: DriveTokenUpdateMethodVersions, pub calculate_total_tokens_balance: FeatureVersion, pub distribution: DriveTokenDistributionMethodVersions, + /// The escrow of the token marketplace (protocol version 15): the tokens open listings hold + /// and the credits open offers hold. + pub order_escrow: DriveTokenOrderEscrowMethodVersions, +} + +/// The escrow methods of the token marketplace: `None` in every table before protocol version +/// 15, whose transitions are refused there, so a call before 15 is an error rather than a +/// silent write. +#[derive(Clone, Debug, Default)] +pub struct DriveTokenOrderEscrowMethodVersions { + pub add_tokens: OptionalFeatureVersion, + pub set_tokens: OptionalFeatureVersion, + pub remove_tokens: OptionalFeatureVersion, + pub fetch_tokens: OptionalFeatureVersion, + pub add_credits: OptionalFeatureVersion, + pub set_credits: OptionalFeatureVersion, + pub remove_credits: OptionalFeatureVersion, + pub fetch_credits: OptionalFeatureVersion, + pub estimated_costs_for_tokens: OptionalFeatureVersion, + pub estimated_costs_for_credits: OptionalFeatureVersion, } #[derive(Clone, Debug, Default)] diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v1.rs b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v1.rs index aa16f2f4b37..7f3280a7831 100644 --- a/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v1.rs +++ b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v1.rs @@ -1,6 +1,7 @@ use crate::version::drive_versions::drive_token_method_versions::{ DriveTokenDistributionMethodVersions, DriveTokenFetchMethodVersions, DriveTokenMethodVersions, - DriveTokenProveMethodVersions, DriveTokenUpdateMethodVersions, + DriveTokenOrderEscrowMethodVersions, DriveTokenProveMethodVersions, + DriveTokenUpdateMethodVersions, }; pub const DRIVE_TOKEN_METHOD_VERSIONS_V1: DriveTokenMethodVersions = DriveTokenMethodVersions { @@ -69,4 +70,18 @@ pub const DRIVE_TOKEN_METHOD_VERSIONS_V1: DriveTokenMethodVersions = DriveTokenM mark_once_per_identity_release_as_distributed: 0, evonode_participation_rewards: 0, }, + // The token marketplace's escrow is active from protocol version 15 + // (`DRIVE_TOKEN_METHOD_VERSIONS_V3`). + order_escrow: DriveTokenOrderEscrowMethodVersions { + add_tokens: None, + set_tokens: None, + remove_tokens: None, + fetch_tokens: None, + add_credits: None, + set_credits: None, + remove_credits: None, + fetch_credits: None, + estimated_costs_for_tokens: None, + estimated_costs_for_credits: None, + }, }; diff --git a/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v3.rs b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v3.rs new file mode 100644 index 00000000000..84a628706b2 --- /dev/null +++ b/packages/rs-platform-version/src/version/drive_versions/drive_token_method_versions/v3.rs @@ -0,0 +1,28 @@ +use crate::version::drive_versions::drive_token_method_versions::v2::DRIVE_TOKEN_METHOD_VERSIONS_V2; +use crate::version::drive_versions::drive_token_method_versions::{ + DriveTokenMethodVersions, DriveTokenOrderEscrowMethodVersions, +}; + +/// Drive token methods for protocol v15+. +/// +/// Identical to [`super::v2::DRIVE_TOKEN_METHOD_VERSIONS_V2`] except +/// `calculate_total_tokens_balance` is bumped to `2`: the tokens the token marketplace holds in +/// escrow for open listings join the identity balances and the shielded pools on the balance +/// side of the token conservation check; and the token marketplace's escrow methods, `None` +/// before, are active. +pub const DRIVE_TOKEN_METHOD_VERSIONS_V3: DriveTokenMethodVersions = DriveTokenMethodVersions { + calculate_total_tokens_balance: 2, + order_escrow: DriveTokenOrderEscrowMethodVersions { + add_tokens: Some(0), + set_tokens: Some(0), + remove_tokens: Some(0), + fetch_tokens: Some(0), + add_credits: Some(0), + set_credits: Some(0), + remove_credits: Some(0), + fetch_credits: Some(0), + estimated_costs_for_tokens: Some(0), + estimated_costs_for_credits: Some(0), + }, + ..DRIVE_TOKEN_METHOD_VERSIONS_V2 +}; diff --git a/packages/rs-platform-version/src/version/drive_versions/v10.rs b/packages/rs-platform-version/src/version/drive_versions/v10.rs index 8aef3e6c3c4..3aface1463c 100644 --- a/packages/rs-platform-version/src/version/drive_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_versions/v10.rs @@ -1,21 +1,48 @@ use crate::version::drive_versions::drive_contract_method_versions::v5::DRIVE_CONTRACT_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_contract_method_versions::{ + DriveContractFeePotMethodVersions, DriveContractMethodVersions, +}; use crate::version::drive_versions::drive_document_method_versions::v5::DRIVE_DOCUMENT_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_identity_method_versions::v2::DRIVE_IDENTITY_METHOD_VERSIONS_V2; +use crate::version::drive_versions::drive_identity_method_versions::{ + DriveIdentityCostEstimationMethodVersions, DriveIdentityMethodVersions, +}; use crate::version::drive_versions::drive_state_transition_method_versions::v5::DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5; +use crate::version::drive_versions::drive_token_method_versions::v3::DRIVE_TOKEN_METHOD_VERSIONS_V3; use crate::version::drive_versions::drive_verify_method_versions::v4::DRIVE_VERIFY_METHOD_VERSIONS_V4; use crate::version::drive_versions::drive_vote_method_versions::v4::DRIVE_VOTE_METHOD_VERSIONS_V4; use crate::version::drive_versions::v9::DRIVE_VERSION_V9; use crate::version::drive_versions::{ - DriveBatchOperationsMethodVersion, DriveMethodVersions, DrivePlatformSystemMethodVersions, + DriveBatchOperationsMethodVersion, DriveInitializationMethodVersions, DriveMethodVersions, + DrivePlatformSystemMethodVersions, DrivePrefundedSpecializedMethodVersions, DriveSystemEstimationCostsMethodVersions, DriveVersion, }; -/// Drive version 10. -/// Introduced in protocol v15: +/// Drive version 10, introduced in protocol v15: /// -/// * **Compiled contract storage**: `DRIVE_CONTRACT_METHOD_VERSIONS_V5`: applying a contract -/// writes nothing when its format version 1 bytes match the stored contract's, whatever -/// format the stored one is in, as contracts are stored compiled from this version +/// * **Compiled contracts**: with `DRIVE_CONTRACT_METHOD_VERSIONS_V5`, applying a contract writes +/// nothing when its format version 1 bytes match the stored contract's, whatever format the +/// stored one is in, as contracts are stored compiled from this version /// (`contract_storage_serialization_version` 2). +/// * **Escrow trees**: `create_initial_state_structure` 5 adds the two escrow trees of the +/// token marketplace: `[Tokens, TOKEN_ORDER_ESCROW_KEY]`, the BigSumTree of the tokens open +/// listings hold, and `[PreFundedSpecializedBalances, PREFUNDED_BALANCES_FOR_TOKEN_ORDERS]`, +/// the SumTree of the credits open offers hold. The upgrade path +/// (`Platform::transition_to_version_15`) inserts them through the same helper. +/// * **Token conservation**: `DRIVE_TOKEN_METHOD_VERSIONS_V3` bumps +/// `calculate_total_tokens_balance` to 2, which counts the escrowed listings on the balance +/// side; its escrow methods, `None` before, are active. +/// * **Escrow fee estimates**: the offers' escrow key sits a level below the other keys of the +/// prefunded specialized balances layer, so `estimated_cost_for_prefunded_specialized_balance_update` +/// 2 and `add_estimation_costs_for_contract_fee_pot_update` 1 keep a deeper description of that +/// layer another write of the batch registered, rather than lowering it to their keys' level; +/// likewise the listings' escrow key sits deeper in the Tokens layer than the balances key, and +/// `for_token_balances` 1 keeps a deeper description of the Tokens layer. The escrows also sit +/// deeper in the root tree than the identity balances and the token balances, so +/// `for_balances` 1, `for_token_balances` 1 and the voting balance estimation 2 keep a deeper +/// description of the root as well. Beyond the estimators, `apply_drive_operations` 2 merges +/// the descriptions of every write of a batch (below), so no other write of the batch (a +/// document, a nonce) lowers what the escrows describe, whichever is built first. /// * **Yes/no vote polls**: `DRIVE_VOTE_METHOD_VERSIONS_V4` bumps /// `insert.register_identity_vote` to 1, which registers a vote of either poll kind, and /// `cleanup.remove_all_votes_given_by_identities` to 1, which also removes the yes/no votes of @@ -49,15 +76,37 @@ use crate::version::drive_versions::{ /// Everything else matches `DRIVE_VERSION_V9`. pub const DRIVE_VERSION_V10: DriveVersion = DriveVersion { methods: DriveMethodVersions { - contract: DRIVE_CONTRACT_METHOD_VERSIONS_V5, // changed in v10: compiled contract storage + initialization: DriveInitializationMethodVersions { + create_initial_state_structure: 5, // changed in v10: adds the token marketplace escrow trees + }, + token: DRIVE_TOKEN_METHOD_VERSIONS_V3, // changed in v10: calculate_total_tokens_balance 2 counts the token marketplace escrow; order_escrow active + contract: DriveContractMethodVersions { + fee_pots: DriveContractFeePotMethodVersions { + add_estimation_costs_for_contract_fee_pot_update: 1, // changed in v10: keeps a deeper prefunded balances layer description + ..DRIVE_CONTRACT_METHOD_VERSIONS_V5.fee_pots + }, + ..DRIVE_CONTRACT_METHOD_VERSIONS_V5 // changed in v10: apply_contract_with_serialization 1 (compiled contract storage) + }, + identity: DriveIdentityMethodVersions { + cost_estimation: DriveIdentityCostEstimationMethodVersions { + for_balances: 1, // changed in v10: keeps a deeper root description + for_token_balances: 1, // changed in v10: keeps a deeper root and Tokens layer description + ..DRIVE_IDENTITY_METHOD_VERSIONS_V2.cost_estimation + }, + ..DRIVE_IDENTITY_METHOD_VERSIONS_V2 + }, document: DRIVE_DOCUMENT_METHOD_VERSIONS_V5, // changed in v10: private document type stores, delete estimates - vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind - verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers - state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read batch_operations: DriveBatchOperationsMethodVersion { apply_drive_operations: 2, // changed in v10: a batch's estimate is resolved (note 14) ..DRIVE_VERSION_V9.methods.batch_operations }, + prefunded_specialized_balances: DrivePrefundedSpecializedMethodVersions { + estimated_cost_for_prefunded_specialized_balance_update: 2, // changed in v10: keeps a deeper prefunded balances layer description + ..DRIVE_VERSION_V9.methods.prefunded_specialized_balances + }, + vote: DRIVE_VOTE_METHOD_VERSIONS_V4, // changed in v10: the yes/no poll kind + verify: DRIVE_VERIFY_METHOD_VERSIONS_V4, // changed in v10: the yes/no and private document entries proof verifiers + state_transitions: DRIVE_STATE_TRANSITION_METHOD_VERSIONS_V5, // changed in v10: a consume deletes what its lookup read platform_system: DrivePlatformSystemMethodVersions { estimation_costs: DriveSystemEstimationCostsMethodVersions { for_total_system_credits_update: 1, // changed in v10: `Misc` described with its subtrees diff --git a/packages/rs-platform-version/src/version/system_data_contract_versions/mod.rs b/packages/rs-platform-version/src/version/system_data_contract_versions/mod.rs index 07e706b2342..9423cdf4135 100644 --- a/packages/rs-platform-version/src/version/system_data_contract_versions/mod.rs +++ b/packages/rs-platform-version/src/version/system_data_contract_versions/mod.rs @@ -18,4 +18,7 @@ pub struct SystemDataContractVersions { pub app_connect: FeatureVersion, pub moderation_charters: FeatureVersion, pub decision_proposals: FeatureVersion, + /// The token market contract (protocol version 15): the open listings and offers of the + /// token marketplace. 0 before it exists. + pub token_market: FeatureVersion, } diff --git a/packages/rs-platform-version/src/version/system_data_contract_versions/v1.rs b/packages/rs-platform-version/src/version/system_data_contract_versions/v1.rs index 7315fb5a7ef..3009b3618db 100644 --- a/packages/rs-platform-version/src/version/system_data_contract_versions/v1.rs +++ b/packages/rs-platform-version/src/version/system_data_contract_versions/v1.rs @@ -17,4 +17,5 @@ pub const SYSTEM_DATA_CONTRACT_VERSIONS_V1: SystemDataContractVersions = // either: no schema generation is selected here. moderation_charters: 0, decision_proposals: 0, + token_market: 0, }; diff --git a/packages/rs-platform-version/src/version/system_data_contract_versions/v2.rs b/packages/rs-platform-version/src/version/system_data_contract_versions/v2.rs index 2b500c1ee5e..f0da722313c 100644 --- a/packages/rs-platform-version/src/version/system_data_contract_versions/v2.rs +++ b/packages/rs-platform-version/src/version/system_data_contract_versions/v2.rs @@ -22,4 +22,5 @@ pub const SYSTEM_DATA_CONTRACT_VERSIONS_V2: SystemDataContractVersions = // either: no schema generation is selected here. moderation_charters: 0, decision_proposals: 0, + token_market: 0, }; diff --git a/packages/rs-platform-version/src/version/system_data_contract_versions/v3.rs b/packages/rs-platform-version/src/version/system_data_contract_versions/v3.rs index e338b0f1fb1..00a18e94298 100644 --- a/packages/rs-platform-version/src/version/system_data_contract_versions/v3.rs +++ b/packages/rs-platform-version/src/version/system_data_contract_versions/v3.rs @@ -34,4 +34,5 @@ pub const SYSTEM_DATA_CONTRACT_VERSIONS_V3: SystemDataContractVersions = app_connect: 1, moderation_charters: 1, decision_proposals: 0, + token_market: 0, }; diff --git a/packages/rs-platform-version/src/version/system_data_contract_versions/v4.rs b/packages/rs-platform-version/src/version/system_data_contract_versions/v4.rs index 8035dfe6e7f..62e75215a69 100644 --- a/packages/rs-platform-version/src/version/system_data_contract_versions/v4.rs +++ b/packages/rs-platform-version/src/version/system_data_contract_versions/v4.rs @@ -5,8 +5,10 @@ use crate::version::system_data_contract_versions::SystemDataContractVersions; // reveal and deletion of the writer's own preorder from an earlier block // (preorders unsalted, one per owner and name, the first valid one of a name // chosen), and the new domain's identity record held to its owner. Domains can -// no longer be deleted. v3 (dpns: 2) remains for PROTOCOL_VERSION_14 chain -// replay. +// no longer be deleted. The token market contract (token_market: 1), the open +// listings and offers of the token marketplace, is written to state by the +// upgrade to 15 and registered at genesis from 15 on. v3 (dpns: 2, no token +// market) remains for PROTOCOL_VERSION_14 chain replay. // // The decision proposals contract (decision_proposals: 1) is registered at // genesis from protocol version 15 and written to state by the upgrade to it: @@ -23,5 +25,6 @@ pub const SYSTEM_DATA_CONTRACT_VERSIONS_V4: SystemDataContractVersions = document_history: 1, app_connect: 1, moderation_charters: 1, + token_market: 1, decision_proposals: 1, }; diff --git a/packages/rs-platform-version/src/version/v15.rs b/packages/rs-platform-version/src/version/v15.rs index 910b208204b..a7919273a43 100644 --- a/packages/rs-platform-version/src/version/v15.rs +++ b/packages/rs-platform-version/src/version/v15.rs @@ -112,9 +112,10 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// `DRIVE_ABCI_VALIDATION_VERSIONS_V11` selects `create_domain_data_trigger` /// 2, which keeps only the parent domain checks: a top-level domain only by /// the contract owner, the parent present, no subdomains under a name, and -/// the parent's subdomain rule. The data trigger bindings stay at 2: their -/// `domain` Replace and Delete rejects are never reached, as +/// the parent's subdomain rule. DPNS needs no new data trigger bindings: +/// its `domain` Replace and Delete rejects are never reached, as /// `documentsMutable` and `canBeDeleted` false refuse both first (10404). +/// (The bindings move to 3 for the token market contract alone, note 17.) /// /// 4. **`countOf` and `sumOf` match byte array keys**: `CONTRACT_VERSIONS_V7` /// selects `parse_property_constraints` 1, whose grammar binds a filter key @@ -370,13 +371,34 @@ pub const PROTOCOL_VERSION_15: ProtocolVersion = 15; /// 16. **Zero consensus evidence budget**: consensus parameter update 2 installs /// the complete evidence policy at activation, including a zero byte budget. /// Protocol v14 and earlier retain their existing evidence limits. +/// +/// 17. **The token marketplace's state**: `DRIVE_VERSION_V10` creates the two escrow trees at +/// genesis (`create_initial_state_structure` 5) and `transition_to_version_15` inserts them +/// through the same helper: `[Tokens, TOKEN_ORDER_ESCROW_KEY]`, a BigSumTree of one SumTree +/// per token of the tokens its open listings hold, and `[PreFundedSpecializedBalances, +/// PREFUNDED_BALANCES_FOR_TOKEN_ORDERS]`, a SumTree of the credits open offers hold. Its +/// `calculate_total_tokens_balance` 2 counts the escrowed tokens on the balance side of the +/// token conservation check; the credit check already counts every prefunded specialized +/// balance. The escrow methods (`order_escrow`) are `None` in every earlier table. The offers' +/// escrow key sits two levels into the prefunded specialized balances layer, below the keys +/// the voting balance and contract fee pot estimations describe, so their v2 and v1 keep a +/// deeper description of that layer another write of the batch registered, and +/// `apply_drive_operations` 2 (note 14) keeps it whatever the order of the batch's writes. +/// `SYSTEM_DATA_CONTRACT_VERSIONS_V4` selects the token market contract v1, whose +/// `ask` and `bid` types hold the open orders, each with the token's decimals when it was +/// placed, which prices its fills and keys its `book` and `depth` index entries +/// (`tokenId`, `decimals`, `price`) so prices of different scales never mix: registered at +/// genesis from this version and +/// stored without storage flags by `transition_to_version_15`. Users cannot write them: +/// `creationRestrictionMode` 2 refuses a create, and the data trigger `bindings` 3 +/// (`DRIVE_ABCI_VALIDATION_VERSIONS_V11`) refuse a replace or a delete. pub const PLATFORM_V15: PlatformVersion = PlatformVersion { protocol_version: PROTOCOL_VERSION_15, - drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10) and verify_private_document_entries Some(0) a page of a private type's store (note 13); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store, and fetch_private_document_entries and prove_private_document_entries Some(0) read and prove a page of it (note 13); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14) + drive: DRIVE_VERSION_V10, // changed: apply_contract_with_serialization 1 writes nothing for a contract whose format version 1 bytes match the stored one's (note 8); vote method versions v4: register_identity_vote 1 registers a yes/no vote, remove_all_votes_given_by_identities 1 also removes the yes/no votes of a masternode that left the list, every yes_no method and remove_vote_poll_end_date_query_operations Some(0) make yes/no polls active, add_vote_poll_end_date_query_operations 1 adds only the entry when the batch already queues the end date's tree with other storage flags, and verify methods v4 make the yes/no proof verifiers active (note 10) and verify_private_document_entries Some(0) a page of a private type's store (note 13); state transition method versions v5: document_create_transition 1 deletes a consumed document from what its lookup read (note 11); document method versions v5: insert_empty_private_document_store_operation Some(0) creates a private document type's store, and fetch_private_document_entries and prove_private_document_entries Some(0) read and prove a page of it (note 13); apply_drive_operations 2 resolves a batch's estimate, remove_reference_for_index_level_for_contract_operations 2, stateless_delete_of_non_tree_for_costs 1 and for_total_system_credits_update 1 describe their layers as they are (note 14); create_initial_state_structure 5 adds the token marketplace escrow trees; calculate_total_tokens_balance 2 counts escrowed tokens; the escrow primitives; the identity balance, token balance, prefunded specialized balance and contract fee pot estimations (for_balances 1, for_token_balances 1, estimated_cost_for_prefunded_specialized_balance_update 2, add_estimation_costs_for_contract_fee_pot_update 1) keep a deeper layer description (note 17) drive_abci: DriveAbciVersion { structs: DRIVE_ABCI_STRUCTURE_VERSIONS_V2, methods: DRIVE_ABCI_METHOD_VERSIONS_V11, // changed: the yes/no poll's tally, record, clean-up and finish hook are Some(0) (note 10); verify_chain_lock_locally 1 rejects a chain lock locally only when Platform holds the quorums active at its signing height, and verify_chain_lock_through_core 1 has Core's verifychainlock check the others before submitchainlock; consensus_params_update 2 installs the full evidence policy (note 16) - validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V11, // changed: identity update advanced_structure 1 refuses unpaid an identity update whose added key's proof of possession does not verify (note 2); create_domain_data_trigger 2 (note 3); contract update transform_into_action 1 holds an update to the compiled size cap in check_tx (note 8); batch advanced_structure 2 refuses a token config update on a read-only contract (note 9); masternode vote transform_into_action 1 looks up a masternode's previous answer on a yes/no poll (note 10); document_reference_validation 1 keeps what a consumed commitment's lookup read (note 11); document create structure_validation 2 holds a decision proposal to its fund, rule and end date (note 12) + validation_and_processing: DRIVE_ABCI_VALIDATION_VERSIONS_V11, // changed: identity update advanced_structure 1 refuses unpaid an identity update whose added key's proof of possession does not verify (note 2); create_domain_data_trigger 2 (note 3); contract update transform_into_action 1 holds an update to the compiled size cap in check_tx (note 8); batch advanced_structure 2 refuses a token config update on a read-only contract (note 9); masternode vote transform_into_action 1 looks up a masternode's previous answer on a yes/no poll (note 10); document_reference_validation 1 keeps what a consumed commitment's lookup read (note 11); document create structure_validation 2 holds a decision proposal to its fund, rule and end date (note 12); data trigger bindings 3 refuse a replace or a delete of a token market order (note 17) withdrawal_constants: DRIVE_ABCI_WITHDRAWAL_CONSTANTS_V3, query: DRIVE_ABCI_QUERY_VERSIONS_V2, checkpoints: DRIVE_ABCI_CHECKPOINT_PARAMETERS_V1, @@ -397,7 +419,7 @@ pub const PLATFORM_V15: PlatformVersion = PlatformVersion { methods: DPP_METHOD_VERSIONS_V3, factory_versions: DPP_FACTORY_VERSIONS_V1, }, - system_data_contracts: SYSTEM_DATA_CONTRACT_VERSIONS_V4, // changed: DPNS v3 checks a name's create with keywords (note 3); the decision proposals contract (note 12) + system_data_contracts: SYSTEM_DATA_CONTRACT_VERSIONS_V4, // changed: DPNS v3 checks a name's create with keywords (note 3); the decision proposals contract (note 12); the token market contract is selected (note 17) fee_version: FEE_VERSION4, // changed: decision proposals prefund their poll with 0.5 Dash (note 12) system_limits: SYSTEM_LIMITS_V5, // changed: max_compiled_contract_size caps a contract stored compiled at 1 MiB (note 8); max_private_document_entry_size caps a private type's entries at 4096 bytes, and the private document store limits size its store (note 13) consensus: ConsensusVersions { diff --git a/packages/rs-sdk-ffi/Cargo.toml b/packages/rs-sdk-ffi/Cargo.toml index ceb2afe6be0..9fc876344d4 100644 --- a/packages/rs-sdk-ffi/Cargo.toml +++ b/packages/rs-sdk-ffi/Cargo.toml @@ -27,6 +27,7 @@ rs-sdk-trusted-context-provider = { path = "../rs-sdk-trusted-context-provider", "app-connect-contract", "moderation-charters-contract", "decision-proposals-contract", + "token-market-contract", ] } simple-signer = { path = "../simple-signer" } async-trait = { version = "0.1.83" } diff --git a/packages/rs-sdk-trusted-context-provider/Cargo.toml b/packages/rs-sdk-trusted-context-provider/Cargo.toml index 443651c9d15..67c595fe060 100644 --- a/packages/rs-sdk-trusted-context-provider/Cargo.toml +++ b/packages/rs-sdk-trusted-context-provider/Cargo.toml @@ -10,6 +10,9 @@ description = "Trusted HTTP-based context provider for Dash Platform SDK" dash-async = { path = "../rs-dash-async" } dash-context-provider = { path = "../rs-context-provider" } +# The system data contract ids, without any contract: the provider refuses a contract under a +# system id before that contract exists, in every build. +data-contracts = { path = "../data-contracts", default-features = false } # Verifies the quorum proofs behind `VerifiedHttpContextProvider`. dash-core-proof = { path = "../rs-core-proof" } dpp = { path = "../rs-dpp", default-features = false, features = [ @@ -49,6 +52,7 @@ all-system-contracts = [ "app-connect-contract", "moderation-charters-contract", "decision-proposals-contract", + "token-market-contract", ] # Individual contract features - these enable specific contracts in DPP @@ -62,6 +66,7 @@ document-history-contract = ["dpp/document-history-contract"] app-connect-contract = ["dpp/app-connect-contract"] moderation-charters-contract = ["dpp/moderation-charters-contract"] decision-proposals-contract = ["dpp/decision-proposals-contract"] +token-market-contract = ["dpp/token-market-contract"] [target.'cfg(target_arch = "wasm32")'.dependencies] # Runs the browser's non-`Send` quorum fetches and timers on the local executor. diff --git a/packages/rs-sdk-trusted-context-provider/src/provider.rs b/packages/rs-sdk-trusted-context-provider/src/provider.rs index ba49e1e44c8..00a71e108a4 100644 --- a/packages/rs-sdk-trusted-context-provider/src/provider.rs +++ b/packages/rs-sdk-trusted-context-provider/src/provider.rs @@ -3,7 +3,9 @@ use crate::get_quorum_base_url; use crate::quorum_refresh::{ contained, sleep, FetchedQuorums, QuorumRefreshes, QUORUM_LIST_REQUEST_TIMEOUT, }; -use crate::system_contracts::{platform_activation_height, system_data_contract}; +use crate::system_contracts::{ + is_inactive_system_contract, platform_activation_height, system_data_contract, +}; use crate::types::{PreviousQuorumsResponse, QuorumData, QuorumsResponse}; use arc_swap::ArcSwap; @@ -1001,6 +1003,11 @@ impl ContextProvider for TrustedHttpContextProvider { id: &Identifier, platform_version: &PlatformVersion, ) -> Result>, ContextProviderError> { + // A system contract not active yet is served under its id from no source + if is_inactive_system_contract(id, platform_version) { + return Ok(None); + } + // First check known contracts cache let known = self.known_contracts.lock().unwrap(); if let Some(contract) = known.get(id) { @@ -1056,6 +1063,7 @@ impl ContextProvider for TrustedHttpContextProvider { mod tests { use super::*; use crate::quorum_refresh::MIN_GAP; + use data_contracts::SystemDataContract; use dpp::bls::SecretKey; use std::io::{BufRead, BufReader, Write}; use std::net::{TcpListener, TcpStream}; @@ -2364,6 +2372,94 @@ mod tests { assert_eq!(contract.id(), id); } + /// The token market system contract is served only from its activation version on. + #[cfg(any(feature = "token-market-contract", feature = "all-system-contracts"))] + #[test] + fn should_serve_token_market_only_from_protocol_15() { + use dpp::data_contract::accessors::v0::DataContractV0Getters; + use dpp::version::PlatformVersion; + + // A numeric loopback URL avoids DNS; contract lookups make no HTTP requests. + let provider = TrustedHttpContextProvider::new_with_url( + Network::Testnet, + "https://127.0.0.1".to_string(), + NonZeroUsize::new(100).unwrap(), + ) + .unwrap(); + let id = SystemDataContract::TokenMarket.id(); + + assert!(provider + .get_data_contract(&id, PlatformVersion::get(14).unwrap()) + .expect("a pre-activation lookup must not error") + .is_none()); + + let contract = provider + .get_data_contract(&id, PlatformVersion::get(15).unwrap()) + .expect("the lookup must succeed at protocol version 15") + .expect("the token market contract must be served at protocol version 15"); + assert_eq!(contract.id(), id); + + // A copy preloaded into the known contracts is not served before activation either. + provider.add_known_contract((*contract).clone()); + assert!(provider + .get_data_contract(&id, PlatformVersion::get(14).unwrap()) + .expect("a pre-activation lookup must not error") + .is_none()); + assert!(provider + .get_data_contract(&id, PlatformVersion::get(15).unwrap()) + .expect("the lookup must succeed at protocol version 15") + .is_some()); + } + + /// A build without the token market feature still refuses a contract preloaded under the + /// token market's id before protocol version 15, and serves it from 15. + #[test] + fn should_refuse_a_preloaded_token_market_id_before_protocol_15_in_any_build() { + use dpp::data_contract::config::DataContractConfig; + use dpp::data_contract::v1::DataContractV1; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + let platform_version = PlatformVersion::latest(); + let id = SystemDataContract::TokenMarket.id(); + let contract = DataContract::V1(DataContractV1 { + id, + version: 1, + owner_id: Identifier::from([2u8; 32]), + document_types: BTreeMap::new(), + config: DataContractConfig::default_for_version(platform_version) + .expect("expected a default config"), + schema_defs: None, + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + groups: BTreeMap::new(), + tokens: BTreeMap::new(), + keywords: vec![], + description: None, + }); + // A numeric loopback URL avoids DNS; contract lookups make no HTTP requests. + let provider = TrustedHttpContextProvider::new_with_url( + Network::Testnet, + "https://127.0.0.1".to_string(), + NonZeroUsize::new(100).unwrap(), + ) + .unwrap() + .with_known_contracts(vec![contract]); + + assert!(provider + .get_data_contract(&id, PlatformVersion::get(14).unwrap()) + .expect("a pre-activation lookup must not error") + .is_none()); + assert!(provider + .get_data_contract(&id, platform_version) + .expect("the lookup must succeed from protocol version 15") + .is_some()); + } + #[test] fn test_domain_resolution_check() { // Test with a domain that should resolve (using localhost) diff --git a/packages/rs-sdk-trusted-context-provider/src/system_contracts.rs b/packages/rs-sdk-trusted-context-provider/src/system_contracts.rs index c96ed4d5d6e..24647131c36 100644 --- a/packages/rs-sdk-trusted-context-provider/src/system_contracts.rs +++ b/packages/rs-sdk-trusted-context-provider/src/system_contracts.rs @@ -15,6 +15,7 @@ use dpp::prelude::{CoreBlockHeight, DataContract, Identifier}; feature = "app-connect-contract", feature = "moderation-charters-contract", feature = "decision-proposals-contract", + feature = "token-market-contract", feature = "all-system-contracts" ))] use dpp::system_data_contracts::{load_system_data_contract, SystemDataContract}; @@ -30,9 +31,23 @@ use dpp::version::feature_initial_protocol_versions::DECISION_PROPOSALS_CONTRACT feature = "all-system-contracts" ))] use dpp::version::feature_initial_protocol_versions::MODERATION_CHARTERS_CONTRACT_INITIAL_PROTOCOL_VERSION; +use dpp::version::feature_initial_protocol_versions::TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION; use dpp::version::PlatformVersion; use std::sync::Arc; +/// Whether `id` names a system data contract that does not exist yet at +/// `platform_version`: the token market contract before protocol version 15. A +/// provider serves no contract under such an id, neither one preloaded into its +/// known contracts nor one its build loads, whichever contract features it has, so +/// each provider asks this before its known contracts. +pub(crate) fn is_inactive_system_contract( + id: &Identifier, + platform_version: &PlatformVersion, +) -> bool { + *id == data_contracts::SystemDataContract::TokenMarket.id() + && platform_version.protocol_version < TOKEN_MARKETPLACE_INITIAL_PROTOCOL_VERSION +} + /// The system data contract with this id, if its cargo feature is enabled and /// it exists at `platform_version`; `Ok(None)` otherwise. #[cfg_attr( @@ -47,6 +62,7 @@ use std::sync::Arc; feature = "app-connect-contract", feature = "moderation-charters-contract", feature = "decision-proposals-contract", + feature = "token-market-contract", feature = "all-system-contracts" )), allow(unused_variables) @@ -67,6 +83,7 @@ pub(crate) fn system_data_contract( feature = "app-connect-contract", feature = "moderation-charters-contract", feature = "decision-proposals-contract", + feature = "token-market-contract", feature = "all-system-contracts" ))] { @@ -214,6 +231,22 @@ pub(crate) fn system_data_contract( )) }); } + + #[cfg(any(feature = "token-market-contract", feature = "all-system-contracts"))] + // Below protocol version 15 the token market contract is absent + // (`is_inactive_system_contract`). + if *id == SystemDataContract::TokenMarket.id() + && !is_inactive_system_contract(id, platform_version) + { + return load_system_data_contract(SystemDataContract::TokenMarket, platform_version) + .map(|contract| Some(Arc::new(contract))) + .map_err(|e| { + ContextProviderError::Generic(format!( + "Failed to load TokenMarket contract: {}", + e + )) + }); + } } Ok(None) @@ -227,3 +260,39 @@ pub(crate) fn platform_activation_height(network: Network) -> CoreBlockHeight { Network::Devnet | Network::Regtest => 1, // Devnet/Regtest activation height } } + +#[cfg(test)] +mod tests { + use super::*; + + /// The shared loader serves the token market contract only from protocol version 15, + /// whatever its caller checked: before, none, without an error. + #[cfg(any(feature = "token-market-contract", feature = "all-system-contracts"))] + #[test] + fn should_load_no_token_market_contract_before_protocol_15() { + let id = data_contracts::SystemDataContract::TokenMarket.id(); + assert!( + system_data_contract(&id, PlatformVersion::get(14).expect("protocol version 14")) + .expect("a pre-activation load does not error") + .is_none() + ); + assert!( + system_data_contract(&id, PlatformVersion::get(15).expect("protocol version 15")) + .expect("the load succeeds at protocol version 15") + .is_some() + ); + } + + #[test] + fn should_mark_only_the_token_market_inactive_before_protocol_15() { + let pv14 = PlatformVersion::get(14).expect("protocol version 14"); + let pv15 = PlatformVersion::get(15).expect("protocol version 15"); + let token_market = data_contracts::SystemDataContract::TokenMarket.id(); + assert!(is_inactive_system_contract(&token_market, pv14)); + assert!(!is_inactive_system_contract(&token_market, pv15)); + assert!(!is_inactive_system_contract( + &data_contracts::SystemDataContract::DPNS.id(), + pv14 + )); + } +} diff --git a/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs b/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs index a8b066c6804..3e3506154dc 100644 --- a/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs +++ b/packages/rs-sdk-trusted-context-provider/src/verified_provider.rs @@ -12,7 +12,9 @@ use crate::get_quorum_base_url; use crate::proof_fetches::{ProofFetches, Timing}; use crate::proof_transport::ProofTransport; -use crate::system_contracts::{platform_activation_height, system_data_contract}; +use crate::system_contracts::{ + is_inactive_system_contract, platform_activation_height, system_data_contract, +}; use dash_context_provider::{ContextProvider, ContextProviderError, QuorumKeyFuture}; use dash_core_proof::{ProofNetwork, State}; use dpp::dashcore::sml::llmq_type::network::NetworkLLMQExt; @@ -423,6 +425,10 @@ impl ContextProvider for VerifiedHttpContextProvider { id: &Identifier, platform_version: &PlatformVersion, ) -> Result>, ContextProviderError> { + // A system contract not active yet is served under its id from no source + if is_inactive_system_contract(id, platform_version) { + return Ok(None); + } if let Some(contract) = self.get_known_contract(id) { return Ok(Some(contract)); } @@ -1973,4 +1979,69 @@ mod tests { 1_090_319 ); } + + /// A contract under `id` with no document type, as an application could preload one. + fn empty_contract(id: Identifier) -> DataContract { + use dpp::data_contract::config::DataContractConfig; + use dpp::data_contract::v1::DataContractV1; + use std::collections::BTreeMap; + + DataContract::V1(DataContractV1 { + id, + version: 1, + owner_id: Identifier::from([2u8; 32]), + document_types: BTreeMap::new(), + config: DataContractConfig::default_for_version(PlatformVersion::latest()) + .expect("a default config"), + schema_defs: None, + created_at: None, + updated_at: None, + created_at_block_height: None, + updated_at_block_height: None, + created_at_epoch: None, + updated_at_epoch: None, + groups: BTreeMap::new(), + tokens: BTreeMap::new(), + keywords: vec![], + description: None, + }) + } + + /// The token market contract does not exist before protocol version 15: no contract is + /// served under its id then, not even one preloaded into the known contracts, whichever + /// contract features the build has. From 15 the preloaded one is. + #[test] + fn should_refuse_a_preloaded_token_market_id_before_protocol_15_in_any_build() { + let provider = with_sources(&["https://127.0.0.1"]).expect("a provider"); + let id = data_contracts::SystemDataContract::TokenMarket.id(); + provider.add_known_contract(empty_contract(id)); + + assert!(provider + .get_data_contract(&id, PlatformVersion::get(14).expect("protocol version 14")) + .expect("a pre-activation lookup does not error") + .is_none()); + assert!(provider + .get_data_contract(&id, PlatformVersion::get(15).expect("protocol version 15")) + .expect("the lookup succeeds at protocol version 15") + .is_some()); + } + + /// A build loading the token market contract, with none preloaded, serves none before + /// protocol version 15, without an error, and the system contract from 15. + #[cfg(any(feature = "token-market-contract", feature = "all-system-contracts"))] + #[test] + fn should_load_the_token_market_contract_only_from_protocol_15() { + let provider = with_sources(&["https://127.0.0.1"]).expect("a provider"); + let id = data_contracts::SystemDataContract::TokenMarket.id(); + + assert!(provider + .get_data_contract(&id, PlatformVersion::get(14).expect("protocol version 14")) + .expect("a pre-activation lookup does not error") + .is_none()); + let contract = provider + .get_data_contract(&id, PlatformVersion::get(15).expect("protocol version 15")) + .expect("the lookup succeeds at protocol version 15") + .expect("the token market contract is served at protocol version 15"); + assert_eq!(contract.id(), id); + } } diff --git a/packages/rs-sdk/Cargo.toml b/packages/rs-sdk/Cargo.toml index 3d016298c7d..9ee8655dab6 100644 --- a/packages/rs-sdk/Cargo.toml +++ b/packages/rs-sdk/Cargo.toml @@ -153,6 +153,7 @@ document-history-contract = ["dpp/document-history-contract"] app-connect-contract = ["dpp/app-connect-contract"] moderation-charters-contract = ["dpp/moderation-charters-contract"] decision-proposals-contract = ["dpp/decision-proposals-contract"] +token-market-contract = ["dpp/token-market-contract"] token_reward_explanations = ["dpp/token-reward-explanations"] diff --git a/packages/token-market-contract/.mocharc.yml b/packages/token-market-contract/.mocharc.yml new file mode 100644 index 00000000000..164b941c1b6 --- /dev/null +++ b/packages/token-market-contract/.mocharc.yml @@ -0,0 +1,2 @@ +require: test/bootstrap.js +recursive: true diff --git a/packages/token-market-contract/Cargo.toml b/packages/token-market-contract/Cargo.toml new file mode 100644 index 00000000000..c338081d99c --- /dev/null +++ b/packages/token-market-contract/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "token-market-contract" +description = "Token market data contract schema and tools. The open orders of the token marketplace" +version.workspace = true +edition = "2021" +rust-version.workspace = true +license = "MIT" + +[dependencies] +thiserror = "2.0.12" +platform-version = { path = "../rs-platform-version" } +serde_json = { version = "1.0" } +platform-value = { path = "../rs-platform-value" } +[dev-dependencies] +base58 = "0.2.0" diff --git a/packages/token-market-contract/LICENSE b/packages/token-market-contract/LICENSE new file mode 100644 index 00000000000..3be95833750 --- /dev/null +++ b/packages/token-market-contract/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2019 Dash Core Group, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/token-market-contract/README.md b/packages/token-market-contract/README.md new file mode 100644 index 00000000000..425fbc3fc50 --- /dev/null +++ b/packages/token-market-contract/README.md @@ -0,0 +1,63 @@ +# Token Market Contract + +The token market system contract holds the open orders of the token +marketplace: the listings (`ask`) holders make to sell a tradeable token at a +price, and the offers (`bid`) anyone makes to buy one. It activates at protocol +version 15, registered at genesis by chains born at 15 and inserted by the +upgrade to 15, and has the same ID on every network: +`AE6yYq65VdfGCg2MSyV9vV7cWGiFWjbwBuy7THesDh4e`. + +Only the token marketplace transitions write these documents. Users cannot +create them (`creationRestrictionMode` 2), and a replace or delete sent as a +document transition is refused. The order's owner is its maker, who pays the +order's storage and gets it back when the order closes. What the order holds +in escrow (the listed tokens of an ask, the offered credits of a bid) is kept +by Platform under the order's id, outside this contract. + +## `ask` + +Tokens a holder offers for sale at a price. + +| Property | Type | Meaning | +| --- | --- | --- | +| `tokenId` | identifier, required | The token for sale | +| `price` | integer, required | The credits the seller asks per whole token (10 to the power of `decimals` base units) | +| `amount` | integer, required | The base units listed when the order was placed | +| `remaining` | integer, required | The base units still for sale; the order is deleted when it is filled entirely | +| `decimals` | integer, required | The token's decimals when the order was placed, generated from `tokenId` (`generatedFrom` `sys.tokens.decimals`) and kept after. Every fill prices the order at this scale, so a later change to the token's decimals does not change what an open order costs or pays | + +## `bid` + +An offer to buy a token at a price: the same properties as an `ask`, read from +the buyer's side, and `escrow`. + +| Property | Type | Meaning | +| --- | --- | --- | +| `escrow` | integer, required | The credits still held in escrow for the order; what is left once it is filled goes back to the buyer | + +## Indexes + +Both types have the same three: + +- `book` (`tokenId`, `decimals`, `price`, `$createdAt`): the order book of a + token, by price and then by age, for one scale of price. Prices are per whole + token at each order's own `decimals`, so a query names the token's current + decimals; after a token changes its decimals, the orders placed before the + change sit apart, under the old decimals, until they close. +- `depth` (`tokenId`, `decimals`, `price`): keeps a sum per price, and over any + range of prices at one scale, of what is open: the base units of the asks (`remaining`) and the + credits of the bids (`escrow`). Each side sums what it holds in escrow, which + the token's supply bounds for asks and the credits on Platform bound for bids, + so the sums cannot overflow. +- `byOwner` (`$ownerId`, `tokenId`, `$createdAt`): the open orders of an + identity. + +## Install + +```sh +npm install @dashevo/token-market-contract +``` + +## License + +[MIT](LICENSE) © Dash Core Group, Inc. diff --git a/packages/token-market-contract/eslint.config.mjs b/packages/token-market-contract/eslint.config.mjs new file mode 100644 index 00000000000..cdf50e57d0d --- /dev/null +++ b/packages/token-market-contract/eslint.config.mjs @@ -0,0 +1,10 @@ +import baseConfig from '../../eslint/base.mjs'; +import mochaTestConfig from '../../eslint/mocha-tests.mjs'; + +export default [ + ...baseConfig, + mochaTestConfig, + { + ignores: ['dist/**', 'node_modules/**'], + }, +]; diff --git a/packages/token-market-contract/lib/systemIds.js b/packages/token-market-contract/lib/systemIds.js new file mode 100644 index 00000000000..14788ab902f --- /dev/null +++ b/packages/token-market-contract/lib/systemIds.js @@ -0,0 +1,4 @@ +module.exports = { + ownerId: '11111111111111111111111111111111', + contractId: 'AE6yYq65VdfGCg2MSyV9vV7cWGiFWjbwBuy7THesDh4e', +}; diff --git a/packages/token-market-contract/package.json b/packages/token-market-contract/package.json new file mode 100644 index 00000000000..f49cd5a3fbd --- /dev/null +++ b/packages/token-market-contract/package.json @@ -0,0 +1,27 @@ +{ + "name": "@dashevo/token-market-contract", + "version": "5.0.1", + "description": "A system contract for the open orders of the token marketplace", + "scripts": { + "lint": "eslint .", + "test": "yarn run test:unit", + "test:unit": "mocha 'test/unit/**/*.spec.js'" + }, + "contributors": [ + { + "name": "Samuel Westrich", + "email": "sam@dash.org", + "url": "https://github.com/quantumexplorer" + } + ], + "license": "MIT", + "devDependencies": { + "@dashevo/wasm-dpp": "workspace:*", + "chai": "^4.3.10", + "dirty-chai": "^2.0.1", + "eslint": "^9.18.0", + "mocha": "^11.1.0", + "sinon": "^18.0.1", + "sinon-chai": "^3.7.0" + } +} diff --git a/packages/token-market-contract/schema/v1/token-market-contract-documents.json b/packages/token-market-contract/schema/v1/token-market-contract-documents.json new file mode 100644 index 00000000000..e1ce44a0402 --- /dev/null +++ b/packages/token-market-contract/schema/v1/token-market-contract-documents.json @@ -0,0 +1,221 @@ +{ + "ask": { + "type": "object", + "description": "Tokens a holder offers for sale at a price. Written only by the token marketplace transitions: the listed tokens are held in escrow under the order's id until it is filled or cancelled.", + "creationRestrictionMode": 2, + "indices": [ + { + "name": "book", + "properties": [ + { + "tokenId": "asc" + }, + { + "decimals": "asc" + }, + { + "price": "asc" + }, + { + "$createdAt": "asc" + } + ] + }, + { + "name": "depth", + "properties": [ + { + "tokenId": "asc" + }, + { + "decimals": "asc" + }, + { + "price": "asc" + } + ], + "summable": "remaining", + "rangeSummable": true + }, + { + "name": "byOwner", + "properties": [ + { + "$ownerId": "asc" + }, + { + "tokenId": "asc" + }, + { + "$createdAt": "asc" + } + ] + } + ], + "properties": { + "tokenId": { + "type": "array", + "byteArray": true, + "minItems": 32, + "maxItems": 32, + "contentMediaType": "application/x.dash.dpp.identifier", + "description": "The token the order is for", + "position": 0 + }, + "price": { + "type": "integer", + "minimum": 1, + "description": "The credits the seller asks per whole token (10 to the power of the order's decimals base units)", + "position": 1 + }, + "amount": { + "type": "integer", + "minimum": 1, + "description": "The token amount, in base units, listed when the order was placed", + "position": 2 + }, + "remaining": { + "type": "integer", + "minimum": 1, + "description": "The token amount, in base units, still open. The order is deleted when it is filled entirely", + "position": 3 + }, + "decimals": { + "type": "integer", + "minimum": 0, + "maximum": 255, + "description": "The token's decimals when the order was placed, generated from tokenId: the price is per 10 to the power of this many base units for the life of the order, even if the token's decimals change later", + "generatedFrom": { + "function": "sys.tokens.decimals", + "params": [ + "tokenId" + ] + }, + "position": 4 + } + }, + "required": [ + "tokenId", + "price", + "amount", + "remaining", + "decimals", + "$createdAt", + "$updatedAt" + ], + "additionalProperties": false + }, + "bid": { + "type": "object", + "description": "An offer to buy a token at a price. Written only by the token marketplace transitions: the offered credits are held in escrow under the order's id until it is filled or cancelled.", + "creationRestrictionMode": 2, + "indices": [ + { + "name": "book", + "properties": [ + { + "tokenId": "asc" + }, + { + "decimals": "asc" + }, + { + "price": "asc" + }, + { + "$createdAt": "asc" + } + ] + }, + { + "name": "depth", + "properties": [ + { + "tokenId": "asc" + }, + { + "decimals": "asc" + }, + { + "price": "asc" + } + ], + "summable": "escrow", + "rangeSummable": true + }, + { + "name": "byOwner", + "properties": [ + { + "$ownerId": "asc" + }, + { + "tokenId": "asc" + }, + { + "$createdAt": "asc" + } + ] + } + ], + "properties": { + "tokenId": { + "type": "array", + "byteArray": true, + "minItems": 32, + "maxItems": 32, + "contentMediaType": "application/x.dash.dpp.identifier", + "description": "The token the order is for", + "position": 0 + }, + "price": { + "type": "integer", + "minimum": 1, + "description": "The credits the buyer pays per whole token (10 to the power of the order's decimals base units)", + "position": 1 + }, + "amount": { + "type": "integer", + "minimum": 1, + "description": "The token amount, in base units, wanted when the order was placed", + "position": 2 + }, + "remaining": { + "type": "integer", + "minimum": 1, + "description": "The token amount, in base units, still open. The order is deleted when it is filled entirely", + "position": 3 + }, + "escrow": { + "type": "integer", + "minimum": 0, + "description": "The credits still held in escrow for the order. They cover what the open amount costs at the order's price; what is left once it is filled goes back to the buyer", + "position": 4 + }, + "decimals": { + "type": "integer", + "minimum": 0, + "maximum": 255, + "description": "The token's decimals when the order was placed, generated from tokenId: the price is per 10 to the power of this many base units for the life of the order, even if the token's decimals change later", + "generatedFrom": { + "function": "sys.tokens.decimals", + "params": [ + "tokenId" + ] + }, + "position": 5 + } + }, + "required": [ + "tokenId", + "price", + "amount", + "remaining", + "decimals", + "escrow", + "$createdAt", + "$updatedAt" + ], + "additionalProperties": false + } +} diff --git a/packages/token-market-contract/src/error.rs b/packages/token-market-contract/src/error.rs new file mode 100644 index 00000000000..d01bbcc91cf --- /dev/null +++ b/packages/token-market-contract/src/error.rs @@ -0,0 +1,17 @@ +use platform_version::version::FeatureVersion; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + /// Platform expected some specific versions + #[error("platform unknown version on {method}, received: {received}")] + UnknownVersionMismatch { + /// method + method: String, + /// the allowed versions for this method + known_versions: Vec, + /// requested core height + received: FeatureVersion, + }, + #[error("schema deserialize error: {0}")] + InvalidSchemaJson(#[from] serde_json::Error), +} diff --git a/packages/token-market-contract/src/lib.rs b/packages/token-market-contract/src/lib.rs new file mode 100644 index 00000000000..436f487cd99 --- /dev/null +++ b/packages/token-market-contract/src/lib.rs @@ -0,0 +1,139 @@ +mod error; +pub mod v1; + +pub use crate::error::Error; +use platform_value::{Identifier, IdentifierBytes32}; +use platform_version::version::PlatformVersion; +use serde_json::Value; + +pub const ID_BYTES: [u8; 32] = [ + 137, 20, 125, 33, 9, 113, 160, 140, 158, 199, 2, 183, 75, 230, 71, 38, 213, 195, 65, 13, 129, + 32, 10, 91, 226, 116, 230, 164, 77, 101, 0, 147, +]; + +pub const OWNER_ID_BYTES: [u8; 32] = [0; 32]; + +pub const ID: Identifier = Identifier(IdentifierBytes32(ID_BYTES)); +pub const OWNER_ID: Identifier = Identifier(IdentifierBytes32(OWNER_ID_BYTES)); + +/// The contract's definitions for `platform_version`. Version 0, the value the tables of +/// protocol versions below 15 carry, names no schema generation and is refused like any +/// unknown version: the contract does not exist before its activation. +pub fn load_definitions(platform_version: &PlatformVersion) -> Result, Error> { + match platform_version.system_data_contracts.token_market { + 1 => Ok(None), + version => Err(Error::UnknownVersionMismatch { + method: "token_market_contract::load_definitions".to_string(), + known_versions: vec![1], + received: version, + }), + } +} + +pub fn load_documents_schemas(platform_version: &PlatformVersion) -> Result { + match platform_version.system_data_contracts.token_market { + 1 => v1::load_documents_schemas(), + version => Err(Error::UnknownVersionMismatch { + method: "token_market_contract::load_documents_schemas".to_string(), + known_versions: vec![1], + received: version, + }), + } +} + +#[cfg(test)] +mod tests { + use base58::FromBase58; + + use super::*; + + #[test] + fn should_match_the_published_system_contract_id() { + let base58_decoded = "AE6yYq65VdfGCg2MSyV9vV7cWGiFWjbwBuy7THesDh4e" + .from_base58() + .expect("expected a base58 id"); + assert_eq!(base58_decoded, ID_BYTES); + } + + #[test] + fn should_load_the_schema_at_the_latest_platform_version() { + use v1::document_types::{ask, bid}; + + let schema = load_documents_schemas(PlatformVersion::latest()).expect("schema loads"); + let declared = |name: &str, properties: &[&str], summed: &str| { + let document_type = schema + .get(name) + .unwrap_or_else(|| panic!("the {name} document type is declared")); + let declared_properties = document_type + .get("properties") + .and_then(Value::as_object) + .unwrap_or_else(|| panic!("{name} has properties")); + for property in properties { + assert!( + declared_properties.contains_key(*property), + "{name} declares {property}" + ); + } + let indexes = document_type + .get("indices") + .and_then(Value::as_array) + .unwrap_or_else(|| panic!("{name} has indexes")); + let declared_indexes: Vec<&str> = indexes + .iter() + .filter_map(|index| index.get("name").and_then(Value::as_str)) + .collect(); + assert_eq!( + declared_indexes, + [ + ask::indexes::BOOK, + ask::indexes::DEPTH, + ask::indexes::BY_OWNER + ], + "the indexes of {name}" + ); + assert_eq!( + indexes[1].get("summable").and_then(Value::as_str), + Some(summed), + "the depth index of {name} sums {summed}" + ); + // Only the platform writes orders. + assert_eq!(document_type["creationRestrictionMode"], 2); + }; + + declared( + ask::NAME, + &[ + ask::properties::TOKEN_ID, + ask::properties::PRICE, + ask::properties::AMOUNT, + ask::properties::REMAINING, + ], + ask::properties::REMAINING, + ); + declared( + bid::NAME, + &[ + bid::properties::TOKEN_ID, + bid::properties::PRICE, + bid::properties::AMOUNT, + bid::properties::REMAINING, + bid::properties::ESCROW, + ], + bid::properties::ESCROW, + ); + assert_eq!(schema.as_object().map(|types| types.len()), Some(2)); + } + + #[test] + fn should_refuse_to_load_below_the_activation_version() { + let platform_version = PlatformVersion::get(14).expect("protocol version 14"); + assert!(matches!( + load_documents_schemas(platform_version), + Err(Error::UnknownVersionMismatch { received: 0, .. }) + )); + assert!(matches!( + load_definitions(platform_version), + Err(Error::UnknownVersionMismatch { received: 0, .. }) + )); + } +} diff --git a/packages/token-market-contract/src/v1/mod.rs b/packages/token-market-contract/src/v1/mod.rs new file mode 100644 index 00000000000..0c1af4cbe67 --- /dev/null +++ b/packages/token-market-contract/src/v1/mod.rs @@ -0,0 +1,51 @@ +use crate::error::Error; +use serde_json::Value; + +pub mod document_types { + /// Tokens a holder offers for sale at a price, held in escrow until filled or cancelled. + pub mod ask { + pub const NAME: &str = "ask"; + + pub mod properties { + pub const TOKEN_ID: &str = "tokenId"; + pub const PRICE: &str = "price"; + pub const AMOUNT: &str = "amount"; + pub const REMAINING: &str = "remaining"; + pub const DECIMALS: &str = "decimals"; + } + + pub mod indexes { + pub const BOOK: &str = "book"; + pub const DEPTH: &str = "depth"; + pub const BY_OWNER: &str = "byOwner"; + } + } + + /// An offer to buy a token at a price, its credits held in escrow until filled or + /// cancelled. + pub mod bid { + pub const NAME: &str = "bid"; + + pub mod properties { + pub const TOKEN_ID: &str = "tokenId"; + pub const PRICE: &str = "price"; + pub const AMOUNT: &str = "amount"; + pub const REMAINING: &str = "remaining"; + pub const ESCROW: &str = "escrow"; + pub const DECIMALS: &str = "decimals"; + } + + pub mod indexes { + pub const BOOK: &str = "book"; + pub const DEPTH: &str = "depth"; + pub const BY_OWNER: &str = "byOwner"; + } + } +} + +pub fn load_documents_schemas() -> Result { + serde_json::from_str(include_str!( + "../../schema/v1/token-market-contract-documents.json" + )) + .map_err(Error::InvalidSchemaJson) +} diff --git a/packages/token-market-contract/test/bootstrap.js b/packages/token-market-contract/test/bootstrap.js new file mode 100644 index 00000000000..7af04f464d7 --- /dev/null +++ b/packages/token-market-contract/test/bootstrap.js @@ -0,0 +1,30 @@ +const sinon = require('sinon'); +const sinonChai = require('sinon-chai'); + +const { expect, use } = require('chai'); +const dirtyChai = require('dirty-chai'); + +const { + default: loadWasmDpp, +} = require('@dashevo/wasm-dpp'); + +use(dirtyChai); +use(sinonChai); + +exports.mochaHooks = { + beforeAll: loadWasmDpp, + + beforeEach() { + if (!this.sinon) { + this.sinon = sinon.createSandbox(); + } else { + this.sinon.restore(); + } + }, + + afterEach() { + this.sinon.restore(); + }, +}; + +global.expect = expect; diff --git a/packages/token-market-contract/test/unit/tokenMarketContract.spec.js b/packages/token-market-contract/test/unit/tokenMarketContract.spec.js new file mode 100644 index 00000000000..c744bb74bd2 --- /dev/null +++ b/packages/token-market-contract/test/unit/tokenMarketContract.spec.js @@ -0,0 +1,136 @@ +const crypto = require('crypto'); + +const { + DashPlatformProtocol, + JsonSchemaError, +} = require('@dashevo/wasm-dpp'); +const generateRandomIdentifier = require('@dashevo/wasm-dpp/lib/test/utils/generateRandomIdentifierAsync'); + +const { expect } = require('chai'); +const tokenMarketContractDocumentsSchema = require('../../schema/v1/token-market-contract-documents.json'); + +const expectJsonSchemaError = (validationResult, errorCount = 1) => { + const errors = validationResult.getErrors(); + expect(errors) + .to + .have + .length(errorCount); + + const error = validationResult.getErrors()[0]; + expect(error) + .to + .be + .instanceof(JsonSchemaError); + + return error; +}; + +describe('Token Market Contract', () => { + let dpp; + let dataContract; + let identityId; + + beforeEach(async () => { + dpp = new DashPlatformProtocol( + { generate: () => crypto.randomBytes(32) }, + ); + + identityId = await generateRandomIdentifier(); + + // The deployed contract stores every integer as a signed 8-byte value + // (`sizedIntegerTypes: false`), as a summed property must be: the default + // config would infer u64 for `remaining` and `escrow`, which a sum cannot + // hold. `create` takes no config, so the schemas go into the raw form of a + // placeholder contract whose config is set to match, validated on load. + const placeholder = dpp.dataContract.create(identityId, BigInt(1), { + placeholder: { + type: 'object', + properties: { + name: { type: 'string', maxLength: 1, position: 0 }, + }, + additionalProperties: false, + }, + }); + const rawDataContract = placeholder.toObject(); + rawDataContract.config.sizedIntegerTypes = false; + rawDataContract.documentSchemas = tokenMarketContractDocumentsSchema; + + dataContract = await dpp.dataContract.createFromObject(rawDataContract); + }); + + const validate = (type, raw) => { + const document = dpp.document.create(dataContract, identityId, type, raw); + return document.validate(dpp.protocolVersion); + }; + + const order = (extra = {}) => ({ + tokenId: crypto.randomBytes(32), + price: 100, + amount: 1000, + remaining: 1000, + decimals: 2, + ...extra, + }); + + ['ask', 'bid'].forEach((type) => { + const raw = (extra = {}) => (type === 'bid' ? order({ escrow: 1, ...extra }) : order(extra)); + + describe(type, () => { + it('should accept a valid order', () => { + expect(validate(type, raw()).isValid()).to.be.true(); + }); + + ['tokenId', 'price', 'amount', 'remaining'].forEach((property) => { + it(`should require ${property}`, () => { + const document = raw(); + delete document[property]; + + const error = expectJsonSchemaError(validate(type, document)); + + expect(error.keyword).to.equal('required'); + expect(error.params.missingProperty).to.equal(property); + }); + }); + + // `decimals` stays required: the platform writes the token's decimals + // (`generatedFrom` `sys.tokens.decimals`) before it checks the order, and + // a client, which reads no state, leaves them out and has its check pass + it('should accept an order leaving out the decimals its token generates', () => { + const document = raw(); + delete document.decimals; + + expect(validate(type, document).isValid()).to.be.true(); + }); + + ['price', 'amount', 'remaining'].forEach((property) => { + it(`should refuse a ${property} below 1`, () => { + const error = expectJsonSchemaError(validate(type, raw({ [property]: 0 }))); + + expect(error.keyword).to.equal('minimum'); + }); + }); + + it('should accept decimals from 0 to 255 and refuse more', () => { + expect(validate(type, raw({ decimals: 0 })).isValid()).to.be.true(); + expect(validate(type, raw({ decimals: 255 })).isValid()).to.be.true(); + + const error = expectJsonSchemaError(validate(type, raw({ decimals: 256 }))); + + expect(error.keyword).to.equal('maximum'); + }); + + it('should refuse an unknown property', () => { + const error = expectJsonSchemaError(validate(type, { ...raw(), note: 'x' })); + + expect(error.keyword).to.equal('additionalProperties'); + }); + }); + }); + + it('should require the escrow of a bid', () => { + const error = expectJsonSchemaError(validate('bid', order())); + + expect(error.keyword).to.equal('required'); + expect(error.params.missingProperty).to.equal('escrow'); + }); +}); diff --git a/packages/wasm-sdk/Cargo.toml b/packages/wasm-sdk/Cargo.toml index a74f484b076..090791898d1 100644 --- a/packages/wasm-sdk/Cargo.toml +++ b/packages/wasm-sdk/Cargo.toml @@ -17,6 +17,7 @@ default = [ "keywords-contract", "moderation-charters-contract", "decision-proposals-contract", + "token-market-contract", "mocks", ] @@ -61,6 +62,10 @@ decision-proposals-contract = [ "dash-sdk/decision-proposals-contract", "rs-sdk-trusted-context-provider/decision-proposals-contract", ] +token-market-contract = [ + "dash-sdk/token-market-contract", + "rs-sdk-trusted-context-provider/token-market-contract", +] token_reward_explanations = ["dash-sdk/token_reward_explanations"] diff --git a/yarn.lock b/yarn.lock index 7fc3bd8fd5b..ae726571b8d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2018,6 +2018,20 @@ __metadata: languageName: unknown linkType: soft +"@dashevo/token-market-contract@workspace:packages/token-market-contract": + version: 0.0.0-use.local + resolution: "@dashevo/token-market-contract@workspace:packages/token-market-contract" + dependencies: + "@dashevo/wasm-dpp": "workspace:*" + chai: "npm:^4.3.10" + dirty-chai: "npm:^2.0.1" + eslint: "npm:^9.18.0" + mocha: "npm:^11.1.0" + sinon: "npm:^18.0.1" + sinon-chai: "npm:^3.7.0" + languageName: unknown + linkType: soft + "@dashevo/wallet-lib@workspace:*, @dashevo/wallet-lib@workspace:packages/wallet-lib": version: 0.0.0-use.local resolution: "@dashevo/wallet-lib@workspace:packages/wallet-lib"