From eca4e15f12b17c781f4847b9ed04bd274fa00b87 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Wed, 30 Sep 2026 23:53:52 +0700 Subject: [PATCH 01/17] fix(dashmate): keep Core's service field for Platform with Core v24 Core v24 leaves `service`, `platformP2PPort` and `platformHTTPPort` out of its masternode list RPC output unless started with `-deprecatedrpc=service`, and Drive and DAPI read those fields. Render `deprecatedrpc=service` unconditionally in the default section of dash.conf so it applies on every network. Core v23 accepts the option and ignores it. Rollout follows earlier template-only changes to dash.conf (adding and removing `deprecatedrpc=hpmn`): no migration. Every command re-renders the service configs when loading migrates the config format version, so an upgrade from 4.1.x or earlier picks the line up on the first dashmate command. Every 4.2.0 prerelease stamps the format version as 4.2.0 (the newest migration key), so an upgrade from a 4.2.0 prerelease to a later prerelease or to 4.2.0 itself runs no migration and does not re-render. Those nodes need `dashmate config render --config=` and a Core restart before they run Core v24. The new test renders dash.conf for mainnet, testnet, devnet and local and checks the option sits before the first network section. Red to green: 4 failing before the template change, 4 passing after. Co-Authored-By: Claude Opus 5.5 --- .../dashmate/templates/core/dash.conf.dot | 3 ++ .../test/unit/templates/coreTemplate.spec.js | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 packages/dashmate/test/unit/templates/coreTemplate.spec.js diff --git a/packages/dashmate/templates/core/dash.conf.dot b/packages/dashmate/templates/core/dash.conf.dot index 7e001947960..957a589bd93 100644 --- a/packages/dashmate/templates/core/dash.conf.dot +++ b/packages/dashmate/templates/core/dash.conf.dot @@ -31,6 +31,9 @@ debuglogfile=/var/log/dash/debug.log # JSON RPC server=1 rpcwallet=main +# Core leaves a masternode's `service`, `platformP2PPort` and `platformHTTPPort` +# out of RPC output unless they are requested here. Platform reads them. +deprecatedrpc=service rpcworkqueue=64 rpcthreads=16 rpcwhitelistdefault=0 diff --git a/packages/dashmate/test/unit/templates/coreTemplate.spec.js b/packages/dashmate/test/unit/templates/coreTemplate.spec.js new file mode 100644 index 00000000000..559505ccbc0 --- /dev/null +++ b/packages/dashmate/test/unit/templates/coreTemplate.spec.js @@ -0,0 +1,33 @@ +import { expect } from 'chai'; +import HomeDir from '../../../src/config/HomeDir.js'; +import getBaseConfigFactory from '../../../configs/defaults/getBaseConfigFactory.js'; +import renderTemplateFactory from '../../../src/templates/renderTemplateFactory.js'; +import renderServiceTemplatesFactory from '../../../src/templates/renderServiceTemplatesFactory.js'; + +describe('Core template', () => { + let config; + let renderServiceTemplates; + + beforeEach(() => { + config = getBaseConfigFactory(HomeDir.createTemp())(); + renderServiceTemplates = renderServiceTemplatesFactory(renderTemplateFactory()); + }); + + // Drive and DAPI read `service`, `platformP2PPort` and `platformHTTPPort` from + // Core's masternode list RPCs. Core v24 leaves them out unless the deprecated + // fields are requested, and Platform cannot process a masternode list without + // them. + ['mainnet', 'testnet', 'devnet', 'local'].forEach((network) => { + it(`should keep Core's deprecated masternode service fields in RPC output on ${network}`, () => { + config.set('network', network); + + const dashConf = renderServiceTemplates(config)['core/dash.conf']; + + // Options under a [section] header apply to that chain only, so the + // option has to be in the default section to cover every network. + const [defaultSection] = dashConf.split(/^\[/m); + + expect(defaultSection).to.match(/^deprecatedrpc=service$/m); + }); + }); +}); From a4d7a38511aae8d96045049e7435c2407b5d5487 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:31:01 +0700 Subject: [PATCH 02/17] build: bump rust-dashcore to parse Core v24 masternode lists Pin rust-dashcore to 538522bc (dashpay/rust-dashcore#1093), whose rpc-json parses the masternode lists Dash Core v24 prints, and adapt drive-abci to it. Only rpc-json differs from the previous pin e4208c90. The re-exported `dpp::dashcore_rpc` (and `dash_sdk::dashcore_rpc`) masternode types change in a breaking way for Rust consumers; nothing nodes agree on changes. DMNState and DMNStateDiff rename the flat platform ports to the deprecated `legacy_platform_p2p_port` / `legacy_platform_http_port`, add the nested `addresses` and the `payouts` list, and make `owner_address`, `payout_address` and `collateral_address` optional, since a shared masternode has none of them. A Tor or I2P `service` reads as `[::]:0` and a `-1` platform port as absent, instead of failing the whole list. Adapt every reader and struct literal in drive-abci, behaviour-identical for every masternode list Core printed before v24 (all three addresses always present): - platform ports are read from the legacy fields exactly as before, with `#[allow(deprecated)]` on the single statement, pattern or field that touches one; `addresses` is not read; - create_owner_identity v0/v1 and transition_to_version_4 need a value from the now optional owner/payout address and fail with DashCoreBadResponseError when it is absent, instead of inventing one; what an added masternode without them gets is decided separately; - the stored MasternodeV0/MasternodeStateV0 format is unchanged: the addresses are stored as before (an absent one as zero bytes, which only a list the node could not process until now can produce) and read back as present; `payouts` and `addresses` are not stored and read back as `None`; - test fixtures wrap the same values in `Some`, drawing the same random numbers in the same order, so seeded strategy tests are unchanged. No behaviour change for pre-v24 lists, so no new test; the drive-abci tests, including the mainnet listdiff fixture test, pass unchanged. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 24 +-- Cargo.toml | 16 +- .../block_end/update_state_cache/v0/mod.rs | 14 +- .../create_operator_identity/v0/mod.rs | 14 +- .../create_owner_identity/v0/mod.rs | 23 ++- .../create_owner_identity/v1/mod.rs | 25 ++-- .../create_voter_identity/v0/mod.rs | 14 +- .../get_operator_identifier/v0/mod.rs | 14 +- .../update_operator_identity/v0/mod.rs | 140 +++++++++++------- .../update_state_masternode_list/v0/mod.rs | 32 ++-- .../v0/mod.rs | 9 +- .../storage/store_platform_state/v1/mod.rs | 22 ++- .../state_transition/state_transitions/mod.rs | 28 ++-- .../src/platform_types/masternode/v0/mod.rs | 27 ++-- .../platform_state/accessors.rs | 22 ++- .../src/platform_types/validator/v0/mod.rs | 5 +- .../masternode_list_item_helpers.rs | 36 +++-- .../tests/strategy_tests/masternodes.rs | 34 +++-- 18 files changed, 321 insertions(+), 178 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb10ccb208f..eb0cbc7553f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1654,7 +1654,7 @@ dependencies = [ [[package]] name = "dash-network" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "cbindgen 0.29.4", "grovedb-bincode", @@ -1665,7 +1665,7 @@ dependencies = [ [[package]] name = "dash-network-seeds" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "dash-network", ] @@ -1760,7 +1760,7 @@ dependencies = [ [[package]] name = "dash-spv" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "async-trait", "chrono", @@ -1789,7 +1789,7 @@ dependencies = [ [[package]] name = "dashcore" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "anyhow", "base64-compat", @@ -1815,12 +1815,12 @@ dependencies = [ [[package]] name = "dashcore-private" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" [[package]] name = "dashcore-rpc" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "dashcore-rpc-json", "hex", @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "dashcore-rpc-json" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "dashcore", "grovedb-bincode", @@ -1848,7 +1848,7 @@ dependencies = [ [[package]] name = "dashcore_hashes" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "dashcore-private", "grovedb-bincode", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "git-state" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" [[package]] name = "glob" @@ -4154,7 +4154,7 @@ dependencies = [ [[package]] name = "key-wallet" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "aes", "async-trait", @@ -4183,7 +4183,7 @@ dependencies = [ [[package]] name = "key-wallet-ffi" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "cbindgen 0.29.4", "dash-network", @@ -4199,7 +4199,7 @@ dependencies = [ [[package]] name = "key-wallet-manager" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=e4208c90786a6854bd498315bcb571ef24182c15#e4208c90786a6854bd498315bcb571ef24182c15" +source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" dependencies = [ "async-trait", "dashcore", diff --git a/Cargo.toml b/Cargo.toml index 08264c2c489..0d73bad6547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,14 +65,14 @@ grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8 grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } -dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } -dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "e4208c90786a6854bd498315bcb571ef24182c15" } +dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } tokio-metrics = "0.5" # Size-tuned profile for the iOS `rs-unified-sdk-ffi` staticlib, which diff --git a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs index dd5415f44bf..14f613d8302 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/block_end/update_state_cache/v0/mod.rs @@ -282,7 +282,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: "1.2.3.4:1234".parse().expect("socket address"), @@ -290,14 +290,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; block_platform_state diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_operator_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_operator_identity/v0/mod.rs index 8e9be14377c..bab5291b1d4 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_operator_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_operator_identity/v0/mod.rs @@ -51,7 +51,7 @@ mod tests { pro_tx_hash: ProTxHash::from_byte_array(pro_tx), collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.2.3.4:1234").unwrap(), @@ -59,14 +59,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![1u8; 48], operator_payout_address, platform_node_id, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs index 503fe5ac07c..7bdd4753d5c 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs @@ -1,3 +1,4 @@ +use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -15,10 +16,16 @@ where masternode: &MasternodeListItem, platform_version: &PlatformVersion, ) -> Result { + let payout_address = masternode.state.payout_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no payout address", + masternode.pro_tx_hash + ))) + })?; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; identity.add_public_keys([Self::get_owner_identity_withdrawal_key( - masternode.state.payout_address, + payout_address, 0, platform_version, )?]); @@ -52,7 +59,7 @@ mod tests { pro_tx_hash: ProTxHash::from_byte_array(pro_tx), collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.2.3.4:1234").unwrap(), @@ -60,14 +67,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address, + payout_address: Some(payout_address), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs index ac593abf23d..597f4a287e3 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs @@ -1,3 +1,4 @@ +use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -14,19 +15,23 @@ where masternode: &MasternodeListItem, platform_version: &PlatformVersion, ) -> Result { + let payout_address = masternode.state.payout_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no payout address", + masternode.pro_tx_hash + ))) + })?; + let owner_address = masternode.state.owner_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no owner address", + masternode.pro_tx_hash + ))) + })?; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; identity.add_public_keys([ - Self::get_owner_identity_withdrawal_key( - masternode.state.payout_address, - 0, - platform_version, - )?, - Self::get_owner_identity_owner_key( - masternode.state.owner_address, - 1, - platform_version, - )?, + Self::get_owner_identity_withdrawal_key(payout_address, 0, platform_version)?, + Self::get_owner_identity_owner_key(owner_address, 1, platform_version)?, ]); Ok(identity) } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_voter_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_voter_identity/v0/mod.rs index b5a1b148cbd..0d667868338 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_voter_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_voter_identity/v0/mod.rs @@ -60,7 +60,7 @@ mod tests { pro_tx_hash: ProTxHash::from_byte_array(pro_tx), collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.2.3.4:1234").unwrap(), @@ -68,14 +68,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address, - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/get_operator_identifier/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/get_operator_identifier/v0/mod.rs index 8cb6c517ca9..960bc25b933 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/get_operator_identifier/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/get_operator_identifier/v0/mod.rs @@ -37,7 +37,7 @@ mod tests { pro_tx_hash: ProTxHash::from_byte_array(pro_tx), collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.2.3.4:1234").unwrap(), @@ -45,14 +45,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs index e8ff92b902a..f059b250783 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_operator_identity/v0/mod.rs @@ -528,7 +528,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -536,14 +536,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -605,7 +609,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -613,14 +617,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -665,7 +673,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -673,14 +681,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(new_operator_payout_address), platform_node_id: Some(node_id), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -738,7 +750,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -746,14 +758,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -815,7 +831,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -823,14 +839,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(original_node_id), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -876,7 +896,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -884,14 +904,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(new_platform_node_id), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -964,7 +988,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -972,14 +996,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: original_pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -1094,7 +1122,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -1102,14 +1130,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: original_pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -1195,7 +1227,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -1203,14 +1235,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: original_pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -1256,7 +1292,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str("1.0.1.1:1234").unwrap(), @@ -1264,14 +1300,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator: new_pub_key_operator.clone(), operator_payout_address: Some(operator_payout_address), platform_node_id: Some(node_id_bytes), - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs index f3ada50f19b..1138c3aad35 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs @@ -72,11 +72,13 @@ where validator.node_ip = address.ip().to_string(); } - if let Some(p2p_port) = dmn_state_diff.platform_p2p_port { + #[allow(deprecated)] + if let Some(p2p_port) = dmn_state_diff.legacy_platform_p2p_port { validator.platform_p2p_port = p2p_port as u16; } - if let Some(http_port) = dmn_state_diff.platform_http_port { + #[allow(deprecated)] + if let Some(http_port) = dmn_state_diff.legacy_platform_http_port { validator.platform_http_port = http_port as u16; } } @@ -169,10 +171,12 @@ where } // these 3 fields are the only fields that are useful for validators. If they change we need to update // validator sets + #[allow(deprecated)] + let platform_p2p_port_changed = state_diff.legacy_platform_p2p_port.is_some(); if is_hpmn && (state_diff.pose_ban_height.is_some() || state_diff.service.is_some() - || state_diff.platform_p2p_port.is_some()) + || platform_p2p_port_changed) { // we updated the ban status the IP or the platform port, we need to update the validator in the validator list Self::update_masternode_in_validator_sets(pro_tx_hash, state_diff, state); @@ -222,7 +226,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: "1.2.3.4:1234".parse().expect("socket address"), @@ -230,14 +234,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } @@ -257,11 +265,15 @@ mod tests { owner_address: None, voting_address: None, payout_address: None, + payouts: None, pub_key_operator: None, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, } } 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 229787b8b78..9229b17454f 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 @@ -191,8 +191,15 @@ impl Platform { .max() .expect("there must be keys, we already checked"); + let owner_address = masternode.state.owner_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no owner address", + masternode.pro_tx_hash + ))) + })?; + let new_owner_key = Self::get_owner_identity_owner_key( - masternode.state.owner_address, + owner_address, last_key_id + 1, platform_version, )?; diff --git a/packages/rs-drive-abci/src/execution/storage/store_platform_state/v1/mod.rs b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v1/mod.rs index 762e21bbbd2..c4919e1fd19 100644 --- a/packages/rs-drive-abci/src/execution/storage/store_platform_state/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/storage/store_platform_state/v1/mod.rs @@ -231,7 +231,7 @@ mod tests { pro_tx_hash: pro_tx_hash(byte), collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: "1.2.3.4:1234".parse().expect("socket address"), @@ -239,14 +239,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [byte; 20], + owner_address: Some([byte; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } @@ -432,11 +436,15 @@ mod tests { owner_address: None, voting_address: None, payout_address: None, + payouts: None, pub_key_operator: None, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, )); let removed_quorum = QuorumHash::from_byte_array([0xAA; 32]); diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs index f0ea147d58c..5976e20fa48 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/mod.rs @@ -751,7 +751,7 @@ pub(in crate::execution) mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen()), collateral_index: 0, - collateral_address: rng.gen(), + collateral_address: Some(rng.gen()), operator_reward: 0.0, state: DMNState { service: SocketAddr::new(IpAddr::V4(random_ip), 19999), @@ -759,14 +759,18 @@ pub(in crate::execution) mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address, + owner_address: Some(owner_address), voting_address: rng.gen(), - payout_address, + payout_address: Some(payout_address), + payouts: None, pub_key_operator: vec![], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }, ); @@ -840,7 +844,7 @@ pub(in crate::execution) mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen()), collateral_index: 0, - collateral_address: rng.gen(), + collateral_address: Some(rng.gen()), operator_reward: 0.0, state: DMNState { service: SocketAddr::new(IpAddr::V4(random_ip), 19999), @@ -848,14 +852,18 @@ pub(in crate::execution) mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen(), + owner_address: Some(rng.gen()), voting_address, - payout_address: rng.gen(), + payout_address: Some(rng.gen()), + payouts: None, pub_key_operator: vec![], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }, ); diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index 43e9dba0359..b18b9833026 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -62,7 +62,7 @@ impl From for MasternodeV0 { pro_tx_hash, collateral_hash, collateral_index, - collateral_address, + collateral_address: collateral_address.unwrap_or_default(), operator_reward, state: state.into(), } @@ -86,7 +86,7 @@ impl From for MasternodeListItem { pro_tx_hash, collateral_hash, collateral_index, - collateral_address, + collateral_address: Some(collateral_address), operator_reward, state: state.into(), } @@ -139,6 +139,8 @@ pub struct MasternodeStateV0 { impl From for MasternodeStateV0 { fn from(value: DMNState) -> Self { + // The payout list and the nested addresses are not stored. + #[allow(deprecated)] let DMNState { service, registered_height, @@ -148,11 +150,13 @@ impl From for MasternodeStateV0 { owner_address, voting_address, payout_address, + payouts: _, pub_key_operator, operator_payout_address, platform_node_id, - platform_p2p_port, - platform_http_port, + legacy_platform_p2p_port: platform_p2p_port, + legacy_platform_http_port: platform_http_port, + addresses: _, } = value; Self { @@ -161,9 +165,9 @@ impl From for MasternodeStateV0 { pose_revived_height, pose_ban_height, revocation_reason, - owner_address, + owner_address: owner_address.unwrap_or_default(), voting_address, - payout_address, + payout_address: payout_address.unwrap_or_default(), pub_key_operator, operator_payout_address, platform_node_id, @@ -191,20 +195,23 @@ impl From for DMNState { platform_http_port, } = value; + #[allow(deprecated)] Self { service, registered_height, pose_revived_height, pose_ban_height, revocation_reason, - owner_address, + owner_address: Some(owner_address), voting_address, - payout_address, + payout_address: Some(payout_address), + payouts: None, pub_key_operator, operator_payout_address, platform_node_id, - platform_p2p_port, - platform_http_port, + legacy_platform_p2p_port: platform_p2p_port, + legacy_platform_http_port: platform_http_port, + addresses: None, } } } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs index c3b16df2223..73a1990a4ea 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs @@ -895,7 +895,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array([0u8; 32]), collateral_index: 0, - collateral_address: [0u8; 20], + collateral_address: Some([0u8; 20]), operator_reward: 0.0, state: DMNState { service: "1.2.3.4:1234".parse().expect("socket address"), @@ -903,14 +903,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: [0u8; 20], + owner_address: Some([0u8; 20]), voting_address: [0u8; 20], - payout_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, pub_key_operator: vec![0u8; 48], operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, } } @@ -928,11 +932,15 @@ mod tests { owner_address: None, voting_address: None, payout_address: None, + payouts: None, pub_key_operator: None, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, } } diff --git a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs index f52b335b54e..2fbab063bd1 100644 --- a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs @@ -22,12 +22,13 @@ impl NewValidatorIfMasternodeInState for ValidatorV0 { ) -> Option { let MasternodeListItem { state, .. } = state.hpmn_masternode_list().get(&pro_tx_hash)?; + #[allow(deprecated)] let DMNState { service, platform_node_id, pose_ban_height, - platform_p2p_port, - platform_http_port, + legacy_platform_p2p_port: platform_p2p_port, + legacy_platform_http_port: platform_http_port, .. } = state; let Some(platform_http_port) = platform_http_port else { diff --git a/packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs b/packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs index 82f61aaf0ba..3ada8f30353 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/masternode_list_item_helpers.rs @@ -15,8 +15,10 @@ impl UpdateMasternodeListItem for MasternodeListItem { .filter(|&field_idx| match field_idx { 4 => self.state.operator_payout_address.is_some(), 5 => self.state.platform_node_id.is_some(), - 6 => self.state.platform_p2p_port.is_some(), - 7 => self.state.platform_http_port.is_some(), + #[allow(deprecated)] + 6 => self.state.legacy_platform_p2p_port.is_some(), + #[allow(deprecated)] + 7 => self.state.legacy_platform_http_port.is_some(), _ => true, }) .collect(); @@ -29,9 +31,9 @@ impl UpdateMasternodeListItem for MasternodeListItem { available_fields.retain(|&idx| idx != field_idx); match field_idx { - 0 => self.state.owner_address = rng.gen::<[u8; 20]>(), + 0 => self.state.owner_address = Some(rng.gen::<[u8; 20]>()), 1 => self.state.voting_address = rng.gen::<[u8; 20]>(), - 2 => self.state.payout_address = rng.gen::<[u8; 20]>(), + 2 => self.state.payout_address = Some(rng.gen::<[u8; 20]>()), 3 => { let private_key_operator_bytes = bls_signatures::PrivateKey::generate_dash(rng) .expect("expected to generate a private key") @@ -55,13 +57,17 @@ impl UpdateMasternodeListItem for MasternodeListItem { *address = rng.gen::<[u8; 20]>(); } } - 6 => { - if let Some(ref mut port) = self.state.platform_p2p_port { + 6 => + { + #[allow(deprecated)] + if let Some(ref mut port) = self.state.legacy_platform_p2p_port { *port = rng.gen_range(1024..=65535); } } - 7 => { - if let Some(ref mut port) = self.state.platform_http_port { + 7 => + { + #[allow(deprecated)] + if let Some(ref mut port) = self.state.legacy_platform_http_port { *port = rng.gen_range(1024..=65535); } } @@ -101,7 +107,7 @@ mod tests { pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str(format!("1.0.{}.{}:1234", i / 256, i % 256).as_str()) @@ -110,14 +116,18 @@ mod tests { pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: rng.gen::<[u8; 20]>(), - payout_address: rng.gen::<[u8; 20]>(), + payout_address: Some(rng.gen::<[u8; 20]>()), + payouts: None, pub_key_operator, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; diff --git a/packages/rs-drive-abci/tests/strategy_tests/masternodes.rs b/packages/rs-drive-abci/tests/strategy_tests/masternodes.rs index 55c2794bf59..60d53f8fabc 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/masternodes.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/masternodes.rs @@ -244,7 +244,7 @@ pub fn generate_test_masternodes( pro_tx_hash, collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str(format!("1.0.{}.{}:1234", i / 256, i % 256).as_str()) @@ -253,14 +253,18 @@ pub fn generate_test_masternodes( pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: generate_voting_address(rng, add_voting_keys_to_signer), - payout_address: generate_payout_address(rng, add_payout_keys_to_signer), + payout_address: Some(generate_payout_address(rng, add_payout_keys_to_signer)), + payouts: None, pub_key_operator, operator_payout_address: None, platform_node_id: None, - platform_p2p_port: None, - platform_http_port: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, }, }; @@ -381,7 +385,7 @@ pub fn generate_test_masternodes( pro_tx_hash: ProTxHash::from_byte_array(rng.gen::<[u8; 32]>()), collateral_hash: Txid::from_byte_array(rng.gen::<[u8; 32]>()), collateral_index: 0, - collateral_address: [0; 20], + collateral_address: Some([0; 20]), operator_reward: 0.0, state: DMNState { service: SocketAddr::from_str(format!("1.1.{}.{}:1234", i / 256, i % 256).as_str()) @@ -390,14 +394,18 @@ pub fn generate_test_masternodes( pose_revived_height: None, pose_ban_height: None, revocation_reason: 0, - owner_address: rng.gen::<[u8; 20]>(), + owner_address: Some(rng.gen::<[u8; 20]>()), voting_address: generate_voting_address(rng, add_voting_keys_to_signer), - payout_address: generate_payout_address(rng, add_payout_keys_to_signer), + payout_address: Some(generate_payout_address(rng, add_payout_keys_to_signer)), + payouts: None, pub_key_operator, operator_payout_address: None, platform_node_id: Some(rng.gen::<[u8; 20]>()), - platform_p2p_port: Some(3010), - platform_http_port: Some(8080), + #[allow(deprecated)] + legacy_platform_p2p_port: Some(3010), + #[allow(deprecated)] + legacy_platform_http_port: Some(8080), + addresses: None, }, }; @@ -530,12 +538,14 @@ pub fn generate_test_masternodes( SocketAddr::new(IpAddr::V4(random_ip), old_port); } if update.p2p_port { - if let Some(port) = hpmn_list_item_b.state.platform_p2p_port.as_mut() { + #[allow(deprecated)] + if let Some(port) = hpmn_list_item_b.state.legacy_platform_p2p_port.as_mut() { *port += 1 } } if update.http_port { - if let Some(port) = hpmn_list_item_b.state.platform_http_port.as_mut() { + #[allow(deprecated)] + if let Some(port) = hpmn_list_item_b.state.legacy_platform_http_port.as_mut() { *port += 1 } } From 6a2ed41945ed8403b1084adc9ef10e337008a7bf Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:56:44 +0700 Subject: [PATCH 03/17] fix(drive-abci): process Core v24 shared and multi-payout masternodes Dash Core v24 lists masternodes every shipped Drive fails to parse, which halts the chain: a shared masternode has no owner, payout or collateral address, and an extended-address masternode has a `payouts` list instead of `payoutAddress`. Now that rust-dashcore parses them, decide what an added one gets. Its single payout address is the payout address, else the only entry of a one-entry payout list (Core turns a payout address into such a list when a masternode moves to extended addresses), else none. - No owner address (shared): no owner identity. Voter and operator identities as for any masternode. - Owner address and a single payout address: unchanged, TRANSFER key 0 and OWNER key 1, byte-identical to a legacy payout address. - Owner address and several payouts: an owner identity with only the OWNER key 1; key id 0 stays free. - create_owner_identity v0 (protocol versions 1-3), whose identity holds only the TRANSFER key: no owner identity without a single payout address. - transition_to_version_4 adds no OWNER key for a masternode without an owner address, after the existing skip of masternodes without owner identity keys. - Diffs are unchanged: only `payout_address` rotates the TRANSFER key; a `payouts` change is parsed but not acted on. - Stored masternodes keep their format: an absent owner, payout or collateral address is stored as zero bytes and now reads back as absent; `payouts` is not stored. - Drive logs at debug level, with the ProTx hash and the reason, each added masternode that gets no owner identity or one with only the OWNER key, and each masternode transition_to_version_4 gives no OWNER key. In-place change to shipped generations: create_owner_identity v0 (protocol versions 1-3) and v1 (4-14), update_masternode_identities v0 and perform_events_on_first_block_of_protocol_change v0 (transition_to_version_4, every version). Every input that takes a new branch is an added masternode without an owner address or without a single payout address, which only a Core v24 masternode list contains and every shipped binary rejects at deserialization, so no committed block was produced from one. Pre-v24 inputs are unchanged: Core prints an owner and payout address for every earlier masternode and refuses a null owner key, and a single payout gives the legacy identity byte for byte. The stored-state conversion is not consensus state; nothing that decides a block reads a stored payout or collateral address or the payout list. Tests, red to green: - should_update_masternode_identities_from_a_core_v24_masternode_list runs Core v24 `protx listdiff` JSON (shared, one payout, Tor primary address with two payouts, Evo with `-1` ports, IPv6 Evo with two payouts, then a legacy-to-extended-address diff and payout-list-only diffs) through update_masternode_list with the mock Core RPC. Against rust-dashcore e4208c90 it fails parsing: CoreRpc(Json(Error("missing field `payoutAddress`", line: 74, column: 7))) on the parent commit it fails with DashCoreBadResponseError("masternode 27978dd8...0bef has no payout address") and it passes here. - create_owner_identity: shared (protocol versions 1 and latest), single payout equals payout address byte for byte (1 and latest), several payouts give OWNER key 1 only, several payouts on version 1 give no owner identity. All four fail on the parent commit with DashCoreBadResponseError and pass here. - should_reload_absent_addresses_as_absent fails on the parent commit (Some([0; 20]) read back) and passes here. - should_add_no_owner_key_for_a_masternode_without_owner_address_on_transition_to_version_4 fails on the parent commit with DashCoreBadResponseError and passes here. - should_not_change_owner_identity_keys_on_a_payout_list_change and should_reload_a_masternode_with_every_address_unchanged pin unchanged behaviour and pass on both. Co-Authored-By: Claude Opus 5.5 --- .../create_owner_identity/mod.rs | 288 +++++++++++++++++- .../create_owner_identity/v0/mod.rs | 39 ++- .../create_owner_identity/v1/mod.rs | 55 ++-- .../update_masternode_identities/v0/mod.rs | 206 ++++++++++++- .../update_masternode_list/mod.rs | 271 ++++++++++++++++ .../v0/mod.rs | 150 ++++++++- .../src/platform_types/masternode/v0/mod.rs | 144 ++++++++- .../core_v24_protx_list_diffs/1-1250.json | 279 +++++++++++++++++ .../core_v24_protx_list_diffs/1250-1260.json | 61 ++++ 9 files changed, 1436 insertions(+), 57 deletions(-) create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1-1250.json create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1250-1260.json diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs index f341cee2f1b..6b715c9e469 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs @@ -5,7 +5,7 @@ use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; -use dpp::dashcore_rpc::dashcore_rpc_json::MasternodeListItem; +use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeListItem}; use dpp::identity::Identity; use dpp::version::PlatformVersion; @@ -25,12 +25,13 @@ where /// /// # Returns /// - /// * Result - Returns the constructed identity for the owner if successful. - /// Otherwise, returns an error. + /// * Result, Error> - The owner identity, or `None` when the masternode + /// has no key an owner identity of this version holds: a shared masternode has no owner + /// and no payout address. Otherwise, returns an error. pub(crate) fn create_owner_identity( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result { + ) -> Result, Error> { match platform_version .drive_abci .methods @@ -48,3 +49,282 @@ where } } } + +/// The one address the owner reward of a masternode is paid to, which its owner identity's +/// TRANSFER key holds: the payout address, or the only entry of a payout list with one entry +/// (Core turns a payout address into such a list when a masternode moves to extended +/// addresses). `None` for a shared masternode, which has neither, and for a reward split +/// between several payouts. +/// +/// The stored masternode list does not keep payout lists, so this is only meaningful for a +/// masternode as Core reported it, never for one read from the stored list. +/// +/// Shared by the shipped `create_owner_identity` v0 and v1: changing it changes both. +fn effective_payout_address(state: &DMNState) -> Option<[u8; 20]> { + match (state.payout_address, state.payouts.as_deref()) { + (Some(payout_address), _) => Some(payout_address), + (None, Some([payout])) => Some(payout.address), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use crate::rpc::core::MockCoreRPCLike; + use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; + use dpp::block::block_info::BlockInfo; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; + use dpp::identifier::MasternodeIdentifiers; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{Identity, KeyID, Purpose}; + use dpp::prelude::Identifier; + use dpp::serialization::PlatformSerializable; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + const PRO_TX_HASH: [u8; 32] = [0x21; 32]; + const OWNER_ADDRESS: [u8; 20] = [0x22; 20]; + const VOTING_ADDRESS: [u8; 20] = [0x23; 20]; + const PAYOUT_ADDRESS: [u8; 20] = [0x24; 20]; + const SECOND_PAYOUT_ADDRESS: [u8; 20] = [0x25; 20]; + const PUB_KEY_OPERATOR: [u8; 48] = [0x26; 48]; + + /// A Regular masternode as Core lists it: a shared one has neither owner, payout nor + /// collateral address; an extended-address one has `payouts` instead of `payout_address`. + fn masternode( + owner_address: Option<[u8; 20]>, + payout_address: Option<[u8; 20]>, + payouts: Option>, + ) -> MasternodeListItem { + MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + collateral_hash: Txid::from_byte_array([0x27; 32]), + collateral_index: 0, + collateral_address: owner_address.map(|_| [0x28; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address, + voting_address: VOTING_ADDRESS, + payout_address, + payouts, + pub_key_operator: PUB_KEY_OPERATOR.to_vec(), + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + } + } + + fn p2pkh_payout(key_hash: [u8; 20], reward: u16) -> DMNPayout { + DMNPayout { + address: key_hash, + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(key_hash)), + reward, + } + } + + fn shared_masternode() -> MasternodeListItem { + masternode(None, None, None) + } + + fn legacy_masternode() -> MasternodeListItem { + masternode(Some(OWNER_ADDRESS), Some(PAYOUT_ADDRESS), None) + } + + fn single_payout_masternode() -> MasternodeListItem { + masternode( + Some(OWNER_ADDRESS), + None, + Some(vec![p2pkh_payout(PAYOUT_ADDRESS, 10000)]), + ) + } + + fn multi_payout_masternode() -> MasternodeListItem { + masternode( + Some(OWNER_ADDRESS), + None, + Some(vec![ + p2pkh_payout(PAYOUT_ADDRESS, 7000), + p2pkh_payout(SECOND_PAYOUT_ADDRESS, 3000), + ]), + ) + } + + /// A platform on `platform_version` whose masternode identities were created for + /// `masternode`, added on the first block. + fn platform_with_added_masternode( + masternode: MasternodeListItem, + platform_version: &PlatformVersion, + ) -> TempPlatform { + let platform = TestPlatformBuilder::new() + .with_initial_protocol_version(platform_version.protocol_version) + .build_with_mock_rpc() + .set_genesis_state(); + + let transaction = platform.drive.grove.start_transaction(); + platform + .update_masternode_identities( + MasternodeListDiff { + base_height: 0, + block_height: 1, + added_mns: vec![masternode], + removed_mns: vec![], + updated_mns: vec![], + }, + &BTreeMap::new(), + &BlockInfo::default(), + None, + &transaction, + platform_version, + ) + .expect("expected to create the masternode identities"); + platform + .drive + .grove + .commit_transaction(transaction) + .unwrap() + .expect("expected to commit"); + platform + } + + fn fetch_identity( + platform: &TempPlatform, + identity_id: Identifier, + platform_version: &PlatformVersion, + ) -> Option { + platform + .drive + .fetch_full_identity(identity_id.to_buffer(), None, platform_version) + .expect("expected to fetch an identity") + } + + fn owner_identity( + platform: &TempPlatform, + platform_version: &PlatformVersion, + ) -> Option { + fetch_identity(platform, PRO_TX_HASH.into(), platform_version) + } + + fn assert_voter_and_operator_identities_exist( + platform: &TempPlatform, + platform_version: &PlatformVersion, + ) { + let voter_identifier = Identifier::create_voter_identifier(&PRO_TX_HASH, &VOTING_ADDRESS); + let operator_identifier = + Identifier::create_operator_identifier(&PRO_TX_HASH, &PUB_KEY_OPERATOR); + assert!( + fetch_identity(platform, voter_identifier, platform_version).is_some(), + "the voter identity must be created" + ); + assert!( + fetch_identity(platform, operator_identifier, platform_version).is_some(), + "the operator identity must be created" + ); + } + + /// Both generations: the owner identity carries the owner's keys, and a shared + /// masternode has no owner key (its owners are its share holders), so it gets no owner + /// identity. Its voter and operator identities are created as for any masternode. + #[test] + fn should_create_no_owner_identity_for_a_shared_masternode() { + for platform_version in [PlatformVersion::first(), PlatformVersion::latest()] { + let platform = platform_with_added_masternode(shared_masternode(), platform_version); + + assert_eq!( + owner_identity(&platform, platform_version), + None, + "protocol version {}", + platform_version.protocol_version + ); + assert_voter_and_operator_identities_exist(&platform, platform_version); + } + } + + /// Both generations: Core turns a payout address into a payout list with one entry + /// when a masternode moves to extended addresses, so a single payout must give the + /// owner identity a payout address gives, byte for byte. + #[test] + fn should_create_the_same_owner_identity_for_a_single_payout_as_for_a_payout_address() { + for platform_version in [PlatformVersion::first(), PlatformVersion::latest()] { + let legacy_owner_identity = owner_identity( + &platform_with_added_masternode(legacy_masternode(), platform_version), + platform_version, + ) + .expect("expected an owner identity for a payout address"); + let single_payout_owner_identity = owner_identity( + &platform_with_added_masternode(single_payout_masternode(), platform_version), + platform_version, + ) + .expect("expected an owner identity for a single payout"); + + assert_eq!( + single_payout_owner_identity + .serialize_to_bytes() + .expect("expected to serialize"), + legacy_owner_identity + .serialize_to_bytes() + .expect("expected to serialize"), + "protocol version {}", + platform_version.protocol_version + ); + } + } + + /// Several payouts have no single address for the TRANSFER key id 0, so the owner + /// identity gets only the OWNER key id 1 a payout address's owner identity has, and key + /// id 0 stays free. + #[test] + fn should_create_an_owner_identity_with_only_the_owner_key_for_several_payouts() { + let platform_version = PlatformVersion::latest(); + let multi_payout_owner_identity = owner_identity( + &platform_with_added_masternode(multi_payout_masternode(), platform_version), + platform_version, + ) + .expect("expected an owner identity for several payouts"); + let legacy_owner_identity = owner_identity( + &platform_with_added_masternode(legacy_masternode(), platform_version), + platform_version, + ) + .expect("expected an owner identity for a payout address"); + + let owner_key = legacy_owner_identity + .public_keys() + .get(&1) + .expect("expected the OWNER key id 1") + .clone(); + assert_eq!(owner_key.purpose(), Purpose::OWNER); + assert_eq!(owner_key.data().as_slice(), OWNER_ADDRESS.as_slice()); + assert_eq!( + multi_payout_owner_identity.public_keys(), + &BTreeMap::from([(1 as KeyID, owner_key)]) + ); + } + + /// The first generation's owner identity holds only the TRANSFER key of the payout + /// address, so several payouts leave it with no key: no owner identity is created, as + /// for a shared masternode. + #[test] + fn should_create_no_owner_identity_for_several_payouts_in_protocol_versions_without_owner_keys() + { + let platform_version = PlatformVersion::first(); + let platform = platform_with_added_masternode(multi_payout_masternode(), platform_version); + + assert_eq!(owner_identity(&platform, platform_version), None); + assert_voter_and_operator_identities_exist(&platform, platform_version); + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs index 7bdd4753d5c..dcb7634b183 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs @@ -1,4 +1,4 @@ -use crate::error::execution::ExecutionError; +use super::effective_payout_address; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -15,13 +15,21 @@ where pub(super) fn create_owner_identity_v0( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result { - let payout_address = masternode.state.payout_address.ok_or_else(|| { - Error::Execution(ExecutionError::DashCoreBadResponseError(format!( - "masternode {} has no payout address", - masternode.pro_tx_hash - ))) - })?; + ) -> Result, Error> { + // The identity holds only the payout address's TRANSFER key, so a masternode without a + // single payout address (shared, or paid to several payouts) gets none. Only a Core v24 + // masternode list has such a masternode, and no earlier binary can parse one, so no + // committed block reached this branch. A chain on Core v24 with V24 active must start at + // protocol version 4 or later: an Evo paid to several payouts gets no owner identity + // here, so paying it as a block proposer would fail. + let Some(payout_address) = effective_payout_address(&masternode.state) else { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "create_owner_identity_v0", + "no owner identity: the masternode has no single payout address" + ); + return Ok(None); + }; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; identity.add_public_keys([Self::get_owner_identity_withdrawal_key( @@ -29,7 +37,7 @@ where 0, platform_version, )?]); - Ok(identity) + Ok(Some(identity)) } pub(crate) fn get_owner_identifier( @@ -105,7 +113,8 @@ mod tests { let mn = make_masternode(pro_tx, [0x12u8; 20]); let identity = Platform::::create_owner_identity_v0(&mn, platform_version) - .expect("create_owner_identity_v0 must succeed"); + .expect("create_owner_identity_v0 must succeed") + .expect("a masternode with a payout address has an owner identity"); assert_eq!(identity.id(), Identifier::from(pro_tx)); assert_eq!( @@ -122,10 +131,12 @@ mod tests { let a = make_masternode([0x01u8; 32], [0u8; 20]); let b = make_masternode([0x02u8; 32], [0u8; 20]); - let ia = - Platform::::create_owner_identity_v0(&a, platform_version).expect("a"); - let ib = - Platform::::create_owner_identity_v0(&b, platform_version).expect("b"); + let ia = Platform::::create_owner_identity_v0(&a, platform_version) + .expect("a") + .expect("a has an owner identity"); + let ib = Platform::::create_owner_identity_v0(&b, platform_version) + .expect("b") + .expect("b has an owner identity"); assert_ne!(ia.id(), ib.id()); } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs index 597f4a287e3..0eb2ddadece 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs @@ -1,4 +1,4 @@ -use crate::error::execution::ExecutionError; +use super::effective_payout_address; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -14,25 +14,42 @@ where pub(super) fn create_owner_identity_v1( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result { - let payout_address = masternode.state.payout_address.ok_or_else(|| { - Error::Execution(ExecutionError::DashCoreBadResponseError(format!( - "masternode {} has no payout address", - masternode.pro_tx_hash - ))) - })?; - let owner_address = masternode.state.owner_address.ok_or_else(|| { - Error::Execution(ExecutionError::DashCoreBadResponseError(format!( - "masternode {} has no owner address", - masternode.pro_tx_hash - ))) - })?; + ) -> Result, Error> { + // Only a Core v24 masternode list has a masternode without an owner address or without + // a single payout address, and no earlier binary can parse one, so no committed block + // reached the branches below that skip a key. + // + // A shared masternode has no owner key: its owners are its share holders. + let Some(owner_address) = masternode.state.owner_address else { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "create_owner_identity_v1", + "no owner identity: the masternode has no owner address" + ); + return Ok(None); + }; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; - identity.add_public_keys([ - Self::get_owner_identity_withdrawal_key(payout_address, 0, platform_version)?, - Self::get_owner_identity_owner_key(owner_address, 1, platform_version)?, - ]); - Ok(identity) + // Several payouts have no single address for the TRANSFER key, so the identity holds + // only the OWNER key, under the same id as next to a TRANSFER key. + if let Some(payout_address) = effective_payout_address(&masternode.state) { + identity.add_public_keys([Self::get_owner_identity_withdrawal_key( + payout_address, + 0, + platform_version, + )?]); + } else { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "create_owner_identity_v1", + "owner identity with only the OWNER key: no single payout address" + ); + } + identity.add_public_keys([Self::get_owner_identity_owner_key( + owner_address, + 1, + platform_version, + )?]); + Ok(Some(identity)) } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs index 6cdff83e2f1..06458c123a9 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs @@ -81,10 +81,17 @@ where "create operator identity" ); - drive_operations.push(IdentityOperation(AddNewIdentity { - identity: owner_identity, - is_masternode_identity: true, - })); + // A masternode with none of the keys an owner identity holds, such as a shared + // masternode, has no owner identity. Only an added masternode without an owner + // address, or with `payouts` instead of `payoutAddress`, gets none, and no earlier + // binary can deserialize a masternode list holding one, so no committed block + // skipped an owner identity here. + if let Some(owner_identity) = owner_identity { + drive_operations.push(IdentityOperation(AddNewIdentity { + identity: owner_identity, + is_masternode_identity: true, + })); + } drive_operations.push(IdentityOperation(AddNewIdentity { identity: voter_identity, @@ -166,3 +173,194 @@ where Ok(()) } } + +#[cfg(test)] +mod tests { + use crate::config::PlatformConfig; + use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods}; + use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::block::block_info::BlockInfo; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{KeyID, Purpose}; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + /// A payout change of an extended-address masternode reaches Drive only as `payouts`, + /// never as `payout_address`. Only a `payout_address` change rotates the owner identity's + /// TRANSFER key; acting on `payouts` as well would change what a block does to owner + /// identities, which needs a new method version. + #[test] + fn should_not_change_owner_identity_keys_on_a_payout_list_change() { + let platform_version = PlatformVersion::latest(); + let platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + + let pro_tx_hash = ProTxHash::from_byte_array([0x51; 32]); + let owner_address = [0x52; 20]; + let payout_address = [0x53; 20]; + let masternode = MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0x54; 32]), + collateral_index: 0, + collateral_address: Some([0x55; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some(owner_address), + voting_address: [0x56; 20], + payout_address: Some(payout_address), + payouts: None, + pub_key_operator: vec![0x57; 48], + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + }; + + let transaction = platform.drive.grove.start_transaction(); + platform + .update_masternode_identities( + MasternodeListDiff { + base_height: 0, + block_height: 1, + added_mns: vec![masternode.clone()], + removed_mns: vec![], + updated_mns: vec![], + }, + &BTreeMap::new(), + &BlockInfo::default(), + None, + &transaction, + platform_version, + ) + .expect("expected to create the masternode identities"); + + let mut platform_state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + platform_state.insert_masternode(masternode); + + let apply_update = |state_diff: DMNStateDiff| { + platform + .update_masternode_identities( + MasternodeListDiff { + base_height: 1, + block_height: 2, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, state_diff)], + }, + &BTreeMap::new(), + &BlockInfo::default(), + Some(&platform_state), + &transaction, + platform_version, + ) + .expect("expected to process the masternode update"); + }; + let owner_identity_keys = || -> BTreeMap, bool)> { + platform + .drive + .fetch_full_identity( + pro_tx_hash.to_byte_array(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch the owner identity") + .expect("expected the owner identity to exist") + .public_keys() + .iter() + .map(|(key_id, key)| { + ( + *key_id, + (key.purpose(), key.data().to_vec(), key.is_disabled()), + ) + }) + .collect() + }; + let keys_before = BTreeMap::from([ + (0, (Purpose::TRANSFER, payout_address.to_vec(), false)), + (1, (Purpose::OWNER, owner_address.to_vec(), false)), + ]); + assert_eq!(owner_identity_keys(), keys_before); + + let payout = |address: [u8; 20], reward: u16| DMNPayout { + address, + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(address)), + reward, + }; + for payouts in [ + // The payout address moved into a list, as Core does when a masternode moves to + // extended addresses. + vec![payout(payout_address, 10000)], + // Another single payout address. + vec![payout([0x58; 20], 10000)], + // Several payouts. + vec![payout([0x58; 20], 2500), payout([0x59; 20], 7500)], + ] { + apply_update(DMNStateDiff { + payouts: Some(payouts.clone()), + ..empty_state_diff() + }); + assert_eq!(owner_identity_keys(), keys_before, "payouts {payouts:?}"); + } + + // The same processing does rotate the TRANSFER key on a payout address change. + apply_update(DMNStateDiff { + payout_address: Some([0x5a; 20]), + ..empty_state_diff() + }); + assert_eq!( + owner_identity_keys(), + BTreeMap::from([ + (0, (Purpose::TRANSFER, payout_address.to_vec(), true)), + (1, (Purpose::OWNER, owner_address.to_vec(), false)), + (2, (Purpose::TRANSFER, vec![0x5a; 20], false)), + ]) + ); + } + + fn empty_state_diff() -> DMNStateDiff { + DMNStateDiff { + service: None, + registered_height: None, + last_paid_height: None, + consecutive_payments: None, + pose_penalty: None, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: None, + owner_address: None, + voting_address: None, + payout_address: None, + payouts: None, + pub_key_operator: None, + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + } + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index a656a484c91..ef114d68582 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -73,12 +73,26 @@ where mod test { use super::*; use crate::config::PlatformConfig; + use crate::platform_types::platform_state::platform_state_for_saving::v2::{ + deserialize_masternode_entry, serialize_masternode_entry, + }; + use crate::platform_types::platform_state::PlatformStateV0Methods; + use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::ProTxHash; use dpp::dashcore_rpc::json::MasternodeListDiff; + use dpp::identifier::MasternodeIdentifiers; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{KeyID, Purpose}; + use dpp::prelude::Identifier; + use std::collections::BTreeMap; use std::env; use std::fs::File; use std::io::BufReader; use std::path::PathBuf; + use std::str::FromStr; #[test] fn test_update_masternode_list() { @@ -198,4 +212,261 @@ mod test { .unwrap() .expect("expected to commit"); } + + /// Dash Core v24 prints masternodes that differ from every earlier list: a shared + /// masternode has no owner, payout or collateral address; an extended-address masternode + /// has a `payouts` list instead of `payoutAddress`; a Tor primary address is not an + /// `ip:port` `service`; an Evo without addresses has `-1` platform ports. The list must be + /// processed rather than fail the block, and the masternode identities must be: + /// - shared: voter and operator identities, no owner identity (it has no owner key); + /// - one payout: the same owner identity as a legacy payout address (TRANSFER key 0, + /// OWNER key 1); + /// - several payouts: an owner identity with only the OWNER key 1; + /// - a later payout change through `payouts`: no key change, since only a `payoutAddress` + /// change rotates the TRANSFER key. + #[test] + fn should_update_masternode_identities_from_a_core_v24_masternode_list() { + let platform_version = PlatformVersion::latest(); + + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig::default()) + .build_with_mock_rpc() + .set_genesis_state(); + + let genesis_core_block_height = 1250; + let first_block_core_block_height = 1260; + + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| { + let file_name = if block == genesis_core_block_height { + "1-1250.json" + } else { + "1250-1260.json" + }; + let json = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/supporting_files/core_v24_protx_list_diffs") + .join(file_name), + ) + .expect("expected to read the masternode list diff"); + // Parse it as the Core RPC client parses the `protx listdiff` response, so a + // response the client rejects fails the update with the same error. + Ok(serde_json::from_str(&json)?) + }); + + let transaction = platform.drive.grove.start_transaction(); + + let mut init_chain_platform_state = platform.state.load().as_ref().clone(); + + platform + .update_masternode_list( + None, + &mut init_chain_platform_state, + genesis_core_block_height, + true, + &BlockInfo { + height: 1, + core_height: genesis_core_block_height, + time_ms: 1, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list"); + + let platform_state = init_chain_platform_state.clone(); + let mut block_platform_state = platform_state.clone(); + + platform + .update_masternode_list( + Some(&platform_state), + &mut block_platform_state, + first_block_core_block_height, + false, + &BlockInfo { + height: 1, + core_height: first_block_core_block_height, + time_ms: 2, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list diff"); + + assert_eq!(block_platform_state.full_masternode_list().len(), 7); + assert_eq!(block_platform_state.hpmn_masternode_list().len(), 3); + + // An Evo without addresses prints `-1` platform ports, which read as no port, so it + // cannot be a validator, like any Evo without ports. The legacy Evo is one, with the + // ports and address the diff gave it. + let validator = |pro_tx_hash: &str| { + ValidatorV0::new_validator_if_masternode_in_state( + ProTxHash::from_str(pro_tx_hash).expect("expected a pro tx hash"), + None, + &block_platform_state, + ) + }; + assert!( + validator("6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706").is_none() + ); + let legacy_evo_validator = + validator("ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0") + .expect("expected the legacy Evo to be a validator"); + assert_eq!(legacy_evo_validator.node_ip, "192.0.2.40"); + assert_eq!(legacy_evo_validator.platform_p2p_port, 36656); + assert_eq!(legacy_evo_validator.platform_http_port, 1443); + + // Every parsed masternode survives a store and reload, except for the payout list and + // nested addresses, which are not stored. + for masternode in block_platform_state.full_masternode_list().values() { + let bytes = serialize_masternode_entry(masternode, platform_version) + .expect("expected to serialize the masternode entry"); + let mut expected = masternode.clone(); + expected.state.payouts = None; + expected.state.addresses = None; + assert_eq!( + deserialize_masternode_entry(&bytes) + .expect("expected to deserialize the masternode entry"), + expected + ); + } + + let hash160 = |hex: &str| -> Vec { hex::decode(hex).expect("expected hex") }; + + // The keys of an identity, none of them disabled; `None` if there is no identity. + let identity_keys = + |identity_id: Identifier| -> Option)>> { + platform + .drive + .fetch_full_identity( + identity_id.to_buffer(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch an identity") + .map(|identity| { + identity + .public_keys() + .iter() + .map(|(key_id, key)| { + assert!(key.disabled_at().is_none(), "key {key_id} is disabled"); + (*key_id, (key.purpose(), key.data().to_vec())) + }) + .collect() + }) + }; + + let owner_key = |hex: &str| (Purpose::OWNER, hash160(hex)); + let transfer_key = |hex: &str| (Purpose::TRANSFER, hash160(hex)); + + let voting_421c = "421c03add2c804421451c4e022258778175e60d8"; + let voting_064c = "064cd21ff210c1a92adaea49a4768a41c2aef1bc"; + + // (pro_tx_hash, voting key hash, operator public key, owner identity keys) + let expected = [ + ( + // Legacy Evo whose payout address the diff moves into `payouts` + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", + voting_421c, + "8ed3f0c208efbcfc815cbfb94490dc68cf2e29d44dd9f8a91e20e06057aa110d7062c8ab7ccc85a9ff0c88760157f563".to_string(), + Some(BTreeMap::from([ + (0, transfer_key("75d57974b6e29a4a70df57a3b11195ce0a0dc817")), + (1, owner_key("1f67d90f35e3c5070c368ae6f3635aac357e47df")), + ])), + ), + ( + // One payout, which the diff replaces with two + "27978dd892b7c876c238be1a6141461c2824f3497dd0058e160979bc8f0a0bef", + voting_421c, + "a792ce1af5f7bb9281053b3934cb8b08d00d075a56498e1a525388ce467f188e8a80911fd96a20982baa9b9678452534".to_string(), + Some(BTreeMap::from([ + (0, transfer_key("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")), + (1, owner_key("b740a2ab3f631e4dfe15debf746364f1d8352c50")), + ])), + ), + ( + // Shared + "a4d26868017c0ccffe2efe50944ef4211834660cca834c6e9f86dec6a88246fa", + voting_421c, + "b1".repeat(48), + None, + ), + ( + // One payout, which the diff replaces with another single payout + "813a7c3f28817988a8e6ce66e07e43e261e78398373bfbaae94c898645111d6b", + voting_421c, + "b2".repeat(48), + Some(BTreeMap::from([ + (0, transfer_key("5555555555555555555555555555555555555555")), + (1, owner_key("0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a")), + ])), + ), + ( + // Tor primary address, two payouts; the owner and voting keys are the same + "3023ffd989974768b0dfc347410ad923fa6d3f1eee90180bd0c435e81cb1a82f", + voting_064c, + "b3".repeat(48), + Some(BTreeMap::from([(1, owner_key(voting_064c))])), + ), + ( + // Evo without addresses, one payout + "6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706", + voting_421c, + "b4".repeat(48), + Some(BTreeMap::from([ + (0, transfer_key("9999999999999999999999999999999999999999")), + (1, owner_key("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")), + ])), + ), + ( + // Evo with an IPv6 primary address, two payouts + "aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783", + voting_421c, + "b5".repeat(48), + Some(BTreeMap::from([( + 1, + owner_key("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"), + )])), + ), + ]; + + for (pro_tx_hash, voting_key_hash, operator_public_key, owner_identity_keys) in expected { + let pro_tx_hash = ProTxHash::from_str(pro_tx_hash) + .expect("expected a pro tx hash") + .to_byte_array(); + let voting_key_hash: [u8; 20] = hash160(voting_key_hash) + .try_into() + .expect("expected 20 bytes"); + let operator_public_key = hex::decode(operator_public_key).expect("expected hex"); + + assert_eq!( + identity_keys(pro_tx_hash.into()), + owner_identity_keys, + "owner identity of {}", + hex::encode(pro_tx_hash) + ); + assert!( + identity_keys(Identifier::create_voter_identifier( + &pro_tx_hash, + &voting_key_hash + )) + .is_some(), + "voter identity of {}", + hex::encode(pro_tx_hash) + ); + assert!( + identity_keys(Identifier::create_operator_identifier( + &pro_tx_hash, + &operator_public_key + )) + .is_some(), + "operator identity of {}", + hex::encode(pro_tx_hash) + ); + } + } } 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 9229b17454f..13e7b5a13bf 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 @@ -186,18 +186,22 @@ impl Platform { continue; } + // A shared masternode has no owner key to add. Only a Core v24 masternode list has + // one, and no earlier binary can parse it, so no committed block reached this branch. + let Some(owner_address) = masternode.state.owner_address else { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "transition_to_version_4", + "no OWNER key added: the masternode has no owner address" + ); + continue; + }; + let last_key_id = *old_owner_identity_keys .keys() .max() .expect("there must be keys, we already checked"); - let owner_address = masternode.state.owner_address.ok_or_else(|| { - Error::Execution(ExecutionError::DashCoreBadResponseError(format!( - "masternode {} has no owner address", - masternode.pro_tx_hash - ))) - })?; - let new_owner_key = Self::get_owner_identity_owner_key( owner_address, last_key_id + 1, @@ -1043,6 +1047,138 @@ mod tests { assert!(result.is_ok()); } + /// Version 4 gives every owner identity the OWNER key of its masternode's owner address. + /// A shared masternode has no owner address, so its identity, if one exists under its + /// ProTx hash, gets no key; the owner identities of other masternodes still get theirs. + #[test] + fn should_add_no_owner_key_for_a_masternode_without_owner_address_on_transition_to_version_4() { + use dpp::dashcore::{ProTxHash, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeListItem, MasternodeType}; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{Identity, KeyID, Purpose}; + use std::collections::BTreeMap; + + let platform_version = PlatformVersion::latest(); + let platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + + let masternode = + |pro_tx_hash: [u8; 32], owner_address: Option<[u8; 20]>| MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash: ProTxHash::from_byte_array(pro_tx_hash), + collateral_hash: Txid::from_byte_array([0x41; 32]), + collateral_index: 0, + collateral_address: owner_address.map(|_| [0x42; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address, + voting_address: [0x43; 20], + payout_address: owner_address.map(|_| [0x44; 20]), + payouts: None, + pub_key_operator: vec![0x45; 48], + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + }; + + let owner_address = [0x46; 20]; + let with_owner = [0x47; 32]; + let shared_with_identity = [0x48; 32]; + let shared_without_identity = [0x49; 32]; + + let mut platform_state = platform.state.load().as_ref().clone(); + for item in [ + masternode(with_owner, Some(owner_address)), + masternode(shared_with_identity, None), + masternode(shared_without_identity, None), + ] { + platform_state.insert_masternode(item); + } + + // Owner identities as created before version 4: only the TRANSFER key id 0. + for pro_tx_hash in [with_owner, shared_with_identity] { + let mut identity = + Identity::create_basic_identity(pro_tx_hash.into(), platform_version) + .expect("expected an identity"); + identity.add_public_keys([ + Platform::::get_owner_identity_withdrawal_key( + [0x44; 20], + 0, + platform_version, + ) + .expect("expected a withdrawal key"), + ]); + platform + .drive + .add_new_identity( + identity, + true, + &BlockInfo::default(), + true, + None, + platform_version, + ) + .expect("expected to add the owner identity"); + } + + let transaction = platform.drive.grove.start_transaction(); + platform + .transition_to_version_4( + &platform_state, + &BlockInfo::default(), + &transaction, + platform_version, + ) + .expect("expected the transition to version 4 to succeed"); + + let identity_keys = |pro_tx_hash: [u8; 32]| -> BTreeMap)> { + platform + .drive + .fetch_full_identity(pro_tx_hash, Some(&transaction), platform_version) + .expect("expected to fetch an identity") + .expect("expected the identity to exist") + .public_keys() + .iter() + .map(|(key_id, key)| (*key_id, (key.purpose(), key.data().to_vec()))) + .collect() + }; + + assert_eq!( + identity_keys(with_owner), + BTreeMap::from([ + (0, (Purpose::TRANSFER, vec![0x44; 20])), + (1, (Purpose::OWNER, owner_address.to_vec())), + ]) + ); + assert_eq!( + identity_keys(shared_with_identity), + BTreeMap::from([(0, (Purpose::TRANSFER, vec![0x44; 20]))]) + ); + assert_eq!( + platform + .drive + .fetch_full_identity( + shared_without_identity, + Some(&transaction), + platform_version + ) + .expect("expected to fetch an identity"), + None + ); + } + #[test] fn test_transition_to_version_6_inserts_wallet_utils_contract() { let platform_version = PlatformVersion::latest(); diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index b18b9833026..2ecd3a87da5 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -23,7 +23,8 @@ pub struct MasternodeV0 { pub collateral_hash: Txid, /// The index of the collateral transaction output. pub collateral_index: u32, - /// The address where the collateral is stored. + /// The address where the collateral is stored; zero bytes when it has none, as for a + /// shared masternode. pub collateral_address: [u8; 20], /// The amount of the operator's reward for running the masternode. pub operator_reward: f32, @@ -62,7 +63,7 @@ impl From for MasternodeV0 { pro_tx_hash, collateral_hash, collateral_index, - collateral_address: collateral_address.unwrap_or_default(), + collateral_address: stored_address(collateral_address), operator_reward, state: state.into(), } @@ -86,13 +87,27 @@ impl From for MasternodeListItem { pro_tx_hash, collateral_hash, collateral_index, - collateral_address: Some(collateral_address), + collateral_address: loaded_address(collateral_address), operator_reward, state: state.into(), } } } +/// The stored form of an address a masternode may not have. The stored format has no room for +/// an absent address, so it is stored as zero bytes, and zero bytes read back as absent. That +/// loses no owner address: Core refuses a null owner key for every masternode but a shared one, +/// which has none. A payout or collateral address of zero bytes also reads back as absent; +/// nothing reads those from the stored list. +fn stored_address(address: Option<[u8; 20]>) -> [u8; 20] { + address.unwrap_or_default() +} + +/// The address a [`stored_address`] holds. +fn loaded_address(address: [u8; 20]) -> Option<[u8; 20]> { + (address != [0u8; 20]).then_some(address) +} + /// A `MasternodeState` contains information about a masternode's state. #[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)] pub struct MasternodeStateV0 { @@ -112,13 +127,15 @@ pub struct MasternodeStateV0 { /// Reason for the masternode's revocation (encoded as an integer). pub revocation_reason: u32, - /// The masternode owner's public address. + /// The masternode owner's public address; zero bytes when it has none, as a shared + /// masternode. pub owner_address: [u8; 20], /// The masternode voting public address. pub voting_address: [u8; 20], - /// The masternode payout public address. + /// The masternode payout public address; zero bytes when it has none, as a shared + /// masternode or one with a payout list. pub payout_address: [u8; 20], /// The masternode operator's public key. @@ -165,9 +182,9 @@ impl From for MasternodeStateV0 { pose_revived_height, pose_ban_height, revocation_reason, - owner_address: owner_address.unwrap_or_default(), + owner_address: stored_address(owner_address), voting_address, - payout_address: payout_address.unwrap_or_default(), + payout_address: stored_address(payout_address), pub_key_operator, operator_payout_address, platform_node_id, @@ -202,9 +219,9 @@ impl From for DMNState { pose_revived_height, pose_ban_height, revocation_reason, - owner_address: Some(owner_address), + owner_address: loaded_address(owner_address), voting_address, - payout_address: Some(payout_address), + payout_address: loaded_address(payout_address), payouts: None, pub_key_operator, operator_payout_address, @@ -215,3 +232,112 @@ impl From for DMNState { } } } + +#[cfg(test)] +mod tests { + use crate::platform_types::platform_state::platform_state_for_saving::v2::{ + deserialize_masternode_entry, serialize_masternode_entry, + }; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, MasternodeAddresses, MasternodeListItem, MasternodeType, + }; + use dpp::version::PlatformVersion; + + fn masternode( + node_type: MasternodeType, + collateral_address: Option<[u8; 20]>, + owner_address: Option<[u8; 20]>, + payout_address: Option<[u8; 20]>, + payouts: Option>, + ) -> MasternodeListItem { + let is_evo = node_type == MasternodeType::Evo; + MasternodeListItem { + node_type, + pro_tx_hash: ProTxHash::from_byte_array([0x31; 32]), + collateral_hash: Txid::from_byte_array([0x32; 32]), + collateral_index: 1, + collateral_address, + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 10, + pose_revived_height: None, + pose_ban_height: Some(20), + revocation_reason: 0, + owner_address, + voting_address: [0x33; 20], + payout_address, + payouts, + pub_key_operator: vec![0x34; 48], + operator_payout_address: Some([0x35; 20]), + platform_node_id: is_evo.then_some([0x36; 20]), + #[allow(deprecated)] + legacy_platform_p2p_port: is_evo.then_some(26656), + #[allow(deprecated)] + legacy_platform_http_port: is_evo.then_some(443), + addresses: None, + }, + } + } + + fn p2pkh_payout(key_hash: [u8; 20], reward: u16) -> DMNPayout { + DMNPayout { + address: key_hash, + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(key_hash)), + reward, + } + } + + fn stored_and_loaded(masternode: &MasternodeListItem) -> MasternodeListItem { + let bytes = serialize_masternode_entry(masternode, PlatformVersion::latest()) + .expect("expected to serialize the masternode entry"); + deserialize_masternode_entry(&bytes).expect("expected to deserialize the masternode entry") + } + + /// A masternode from a list Core printed before v24 has every address, and reloads as + /// it was stored. + #[test] + fn should_reload_a_masternode_with_every_address_unchanged() { + let masternode = masternode( + MasternodeType::Evo, + Some([0x37; 20]), + Some([0x38; 20]), + Some([0x39; 20]), + None, + ); + + assert_eq!(stored_and_loaded(&masternode), masternode); + } + + /// The stored format has no room for an absent address: a shared masternode's owner, + /// payout and collateral addresses are stored as zero bytes and must reload as absent, + /// not as an address of zeros. The payout list and nested addresses are not stored. + #[test] + fn should_reload_absent_addresses_as_absent() { + let mut shared = masternode(MasternodeType::Regular, None, None, None, None); + shared.state.addresses = Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![], + platform_https: vec![], + }); + let mut expected = shared.clone(); + expected.state.addresses = None; + assert_eq!(stored_and_loaded(&shared), expected); + + let multi_payout = masternode( + MasternodeType::Evo, + Some([0x37; 20]), + Some([0x38; 20]), + None, + Some(vec![ + p2pkh_payout([0x3a; 20], 5000), + p2pkh_payout([0x3b; 20], 5000), + ]), + ); + let mut expected = multi_payout.clone(); + expected.state.payouts = None; + assert_eq!(stored_and_loaded(&multi_payout), expected); + } +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1-1250.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1-1250.json new file mode 100644 index 00000000000..9c2826584a9 --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1-1250.json @@ -0,0 +1,279 @@ +{ + "baseHeight": 1, + "blockHeight": 1250, + "addedMNs": [ + { + "type": "Evo", + "proTxHash": "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", + "collateralHash": "5c1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f8", + "collateralIndex": 0, + "collateralAddress": "yh6REqVqc88VTnJ5rCXPYnQNtrc72YmiXB", + "operatorReward": 0, + "state": { + "version": 2, + "service": "192.0.2.40:9999", + "addresses": { + "core_p2p": [ + "192.0.2.40:9999" + ], + "platform_https": [ + "192.0.2.40:443" + ], + "platform_p2p": [ + "192.0.2.40:26656" + ] + }, + "registeredHeight": 900, + "lastPaidHeight": 1190, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yPBWCdMRY5PsS3hJzs7csbdWQVRR85yxUz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "9e391c2c041a122a779bafe09d6c47ea600dfcbe", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payoutAddress": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", + "pubKeyOperator": "8ed3f0c208efbcfc815cbfb94490dc68cf2e29d44dd9f8a91e20e06057aa110d7062c8ab7ccc85a9ff0c88760157f563" + } + }, + { + "type": "Regular", + "proTxHash": "27978dd892b7c876c238be1a6141461c2824f3497dd0058e160979bc8f0a0bef", + "collateralHash": "6d2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f809", + "collateralIndex": 1, + "collateralAddress": "yguoUwpMVtQCgq49SzH8r5QUuUmYJd89fR", + "operatorReward": 0, + "state": { + "version": 3, + "service": "192.0.2.41:9999", + "addresses": { + "core_p2p": [ + "192.0.2.41:9999" + ] + }, + "registeredHeight": 1100, + "lastPaidHeight": 1180, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yd2PwFoqtEJdnJVSEzBDMxVnFVgEvJyvyY", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "payouts": [ + { + "address": "yi6ouEk1CRe5j27k3rsnQftoqRiD29vExs", + "script": "76a914eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee88ac", + "reward": 10000 + } + ], + "pubKeyOperator": "a792ce1af5f7bb9281053b3934cb8b08d00d075a56498e1a525388ce467f188e8a80911fd96a20982baa9b9678452534" + } + }, + { + "type": "Regular", + "proTxHash": "a4d26868017c0ccffe2efe50944ef4211834660cca834c6e9f86dec6a88246fa", + "collateralHash": "a4d26868017c0ccffe2efe50944ef4211834660cca834c6e9f86dec6a88246fa", + "collateralIndex": 1, + "operatorReward": 0, + "state": { + "version": 3, + "service": "192.0.2.30:9999", + "addresses": { + "core_p2p": [ + "192.0.2.30:9999" + ] + }, + "registeredHeight": 1210, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "shares": [ + { + "amount": 60000000000, + "refundAddress": "yMsgnH1xKGa85n4bq2imrZbG2KgrmGttAV", + "refundScript": "76a914111111111111111111111111111111111111111188ac", + "rewardAddress": "yPRviMJaEHGafsc5EKovpa5Nw2Jewo9mdj", + "rewardScript": "76a914222222222222222222222222222222222222222288ac", + "ownerAddress": "yQzAeRbC9Hy3Fy9Ydcu5naZVqivT95Ka2R" + }, + { + "amount": 40000000000, + "refundAddress": "ySYQaVsp4JfVr4h22uzEkb3ckRYFHJwGq9", + "refundScript": "76a914444444444444444444444444444444444444444488ac", + "rewardAddress": "ySYQaVsp4JfVr4h22uzEkb3ckRYFHJwGq9", + "rewardScript": "76a914444444444444444444444444444444444444444488ac", + "ownerAddress": "yVetSeT3tL4R2FmxqWAYgc1rZpmqbMcFhL" + } + ], + "earlyPeriodBlocks": 1000, + "earlyPenalty": 100000000, + "pubKeyOperator": "b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1" + } + }, + { + "type": "Regular", + "proTxHash": "813a7c3f28817988a8e6ce66e07e43e261e78398373bfbaae94c898645111d6b", + "collateralHash": "ff6226e6c97bfcf40b6d04e12e3f75678024988823bfba28cde2a9ac11b1a765", + "collateralIndex": 1, + "collateralAddress": "yNqYnF9sHURjwRmhZMLFGQ3WjC5DZNJMUi", + "operatorReward": 0, + "state": { + "version": 3, + "service": "192.0.2.31:9999", + "addresses": { + "core_p2p": [ + "192.0.2.31:9999" + ] + }, + "registeredHeight": 1220, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMEXdf8kwSX7NSipRnrtQ66d4WFtBCi71e", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "payouts": [ + { + "address": "yU6eWaARyKMxSAEVSD5PibXjf8A3TH4gqJ", + "script": "76a914555555555555555555555555555555555555555588ac", + "reward": 10000 + } + ], + "pubKeyOperator": "b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2" + } + }, + { + "type": "Regular", + "proTxHash": "3023ffd989974768b0dfc347410ad923fa6d3f1eee90180bd0c435e81cb1a82f", + "collateralHash": "35215134107b5e423d327cab12d2b4c60a9b769301096e05a95916676d2f7867", + "collateralIndex": 0, + "collateralAddress": "yd2PwFoqtEJdnJVSEzBDMxVnFVgEvJyvyY", + "operatorReward": 0, + "state": { + "version": 3, + "service": "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9999", + "addresses": { + "core_p2p": [ + "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9999", + "192.0.2.32:9999" + ] + }, + "registeredHeight": 1230, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yLtkvxSueGSufQZQq8L9GVHch9QRqJqGkZ", + "votingAddress": "yLtkvxSueGSufQZQq8L9GVHch9QRqJqGkZ", + "payouts": [ + { + "address": "yYmNJo2HiMTLCSrue6Lrccz6PE1S1ELq2T", + "script": "76a914888888888888888888888888888888888888888888ac", + "reward": 7000 + }, + { + "address": "8qK9EafotWgreuSxH2x8ySZnKVDVivy3Zf", + "script": "a914777777777777777777777777777777777777777787", + "reward": 3000 + } + ], + "pubKeyOperator": "b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3b3" + } + }, + { + "type": "Evo", + "proTxHash": "6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706", + "collateralHash": "cbf3c744b1c18fe1866e79972818c98fb1a268736f1757595185032c808321af", + "collateralIndex": 0, + "collateralAddress": "ybhjexnMcGckdJCyUwFu3F25zPo4mqQg1k", + "operatorReward": 0, + "state": { + "version": 3, + "service": "[::]:0", + "addresses": {}, + "registeredHeight": 1240, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMKqWbyVzZPFkvLndPz1kSba4CBAbA9oZg", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "cbf3c744b1c18fe1866e79972818c98fb1a26873", + "platformP2PPort": -1, + "platformHTTPPort": -1, + "payouts": [ + { + "address": "yaKcEsJudN9nnYQP3PS1adUDHvdE6sjNiQ", + "script": "76a914999999999999999999999999999999999999999988ac", + "reward": 10000 + } + ], + "pubKeyOperator": "b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4b4" + } + }, + { + "type": "Evo", + "proTxHash": "aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783", + "collateralHash": "59cd030a1a4cd43a53c3a600c20f64ed07399873aecd2830b843e6a84283ba29", + "collateralIndex": 0, + "collateralAddress": "yNqYnF9sHURjwRmhZMLFGQ3WjC5DZNJMUi", + "operatorReward": 0, + "state": { + "version": 3, + "service": "[2001:db8::4]:9999", + "addresses": { + "core_p2p": [ + "[2001:db8::4]:9999" + ], + "platform_p2p": [ + "[2001:db8::4]:26656" + ], + "platform_https": [ + "[2001:db8::4]:443" + ] + }, + "registeredHeight": 1250, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMR9PYpF3gFQ9Pxkq1796o6X3t6SxnHXrq", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "4bcc85253e395ec272998a0722ac3ee1dd3965f7", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payouts": [ + { + "address": "ybsrAwbXYNrFNdwrSgXAYdxLCdF2GdFbqY", + "script": "76a914aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa88ac", + "reward": 5000 + }, + { + "address": "ydS671t9TPYhxjVKqycKWeST7KrpUM1c4t", + "script": "76a914bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb88ac", + "reward": 5000 + } + ], + "pubKeyOperator": "b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5b5" + } + } + ], + "removedMNs": [], + "updatedMNs": [] +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1250-1260.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1250-1260.json new file mode 100644 index 00000000000..e935e1508df --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1250-1260.json @@ -0,0 +1,61 @@ +{ + "baseHeight": 1250, + "blockHeight": 1260, + "addedMNs": [], + "removedMNs": [], + "updatedMNs": [ + { + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0": { + "version": 3, + "service": "192.0.2.40:9999", + "payouts": [ + { + "address": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", + "script": "76a91475d57974b6e29a4a70df57a3b11195ce0a0dc81788ac", + "reward": 10000 + } + ], + "platformP2PPort": 36656, + "platformHTTPPort": 1443, + "addresses": { + "core_p2p": [ + "192.0.2.40:9999" + ], + "platform_p2p": [ + "192.0.2.40:36656" + ], + "platform_https": [ + "192.0.2.40:1443" + ] + } + } + }, + { + "27978dd892b7c876c238be1a6141461c2824f3497dd0058e160979bc8f0a0bef": { + "payouts": [ + { + "address": "yezL36AmNQFAYq2oFGhUUeva22UccZnF8F", + "script": "76a914cccccccccccccccccccccccccccccccccccccccc88ac", + "reward": 2500 + }, + { + "address": "ygYZyATPHQwd8vaGeZndSfQgvj6Qro5WbT", + "script": "76a914dddddddddddddddddddddddddddddddddddddddd88ac", + "reward": 7500 + } + ] + } + }, + { + "813a7c3f28817988a8e6ce66e07e43e261e78398373bfbaae94c898645111d6b": { + "payouts": [ + { + "address": "yiNjY5GEMnEWtSyeegFATjPepVU48ATT7E", + "script": "76a914f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f188ac", + "reward": 10000 + } + ] + } + } + ] +} From 3bfc924ccb7e4d63583b7402af173a14b4f0bd62 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:07:41 +0700 Subject: [PATCH 04/17] fix(drive-abci): take evonode platform ports from nested addresses Core v24 keeps an ExtAddr evonode's platform ports in its network info. Its legacy scalar ports stay zero, and a diff lists the legacy `platformP2PPort` / `platformHTTPPort` fields only when those scalars change. A ProUpServTx that only moves the ports therefore reaches Drive as a diff carrying the change only in `addresses`, which Drive ignored: the validator kept its old port and Tenderdash kept dialling it. A validator built later from the state got the same stale port, port 0 for an evonode revoked after V24 and revived since, or no validator for an evonode first listed with `-1` ports. A port is now the nested entry, else the legacy field as it is, zero included (`PlatformPorts::of`: rust-dashcore's `platform_{p2p,http}_address()`, which fall back that way). Keeping a zero legacy port keeps validator-set membership what earlier binaries build: they make every evonode with both legacy ports a member, whatever their value, and Core 23 and 24 list a revoked legacy evonode with `addresses: {}` beside its flat ports. Validator-set membership is consensus-relevant: validator_set_update rotates the quorum when the proposer is the set's last member, banned members included, and the next quorum's hash is NextValidatorsHash. `MasternodeStateV0` stores neither `addresses` nor the payout list, so two invariants keep membership the same on every node: - After every masternode list update, the masternode lists in memory equal what a node that restarts loads from storage. - Validator membership is a function of the committed Core diff windows: every node asks Core for the same windows, fixed by the committed core heights, and applies them in the same order. One consequence: an ExtAddr evonode registered and revoked within one window arrives as an added item with `-1` ports and is not a member, while one revoked in a later window stays a banned member with its stored ports. Both follow from the committed windows, so every node agrees, and shipped binaries halt on the `-1` added item, so no earlier behaviour constrains it. - `insert_masternode` and `apply_masternode_state_diff` keep each state in stored form (`in_stored_form`: through `MasternodeStateV0`): the resolved ports move into the legacy port fields, `addresses` and `payouts` become `None`, and an owner or payout address of zero bytes reads as absent, exactly as after a reload. Identity updates are unaffected: they read added masternodes and diffs as Core reported them. `any_stored_masternode_changes` compares in the same form. - The stored state keeps the resolved ports, else the legacy value unchanged, even one outside the port range, as earlier binaries store it; a state loaded back is then already in stored form. - A validator built from the masternode state takes the resolved ports. - The validator refresh on a diff compares the ports resolved before and after it, reading the updated state rather than the diff, since an ExtAddr evonode's port move is only in `addresses`. It triggers as before (ban, service, or a P2P port change) and writes only the ports that changed, which for lists without nested ports, or with nested ports mirroring the legacy ones, is exactly what the legacy fields in the diff set before. - Drive logs at debug level, with the ProTx hash, an evonode left out of a validator set because its platform ports do not resolve. Only the port is taken. The validator host stays the `service` IP: a legacy evonode's port-only diff pairs its port with the placeholder host 255.255.255.255. In-place change to shipped generations: update_state_masternode_list v0 and the validator built from masternode state (every protocol version). Node addresses are not consensus-hashed: Tenderdash's ValidatorSet.Hash is merkle(threshold public key, quorum hash). Validator-set membership is unchanged for every input old binaries accept: an evonode is a member when it has both ports and a platform node id; a resolved port is present whenever the legacy field is, zero included; and every evonode old binaries can parse lists both legacy fields (Core before v24 prints them for every evonode; Core v24 prints them together with `service`, which old binaries require, and as `-1` for an evonode without network info, which old binaries reject). The update loop carries this justification as a comment. The test that a payment-only diff leaves the state clean now builds its state with insert_masternode, as Drive holds it: its zero-byte fixture addresses are not in stored form. Test would have caught this in CI. With this commit's port reading reverted to the parent's (legacy fields only, from the state and from the diff, and no stored-form normalization), these fail and pass here: - an ExtAddr evonode's port move as an addresses-only diff, with `service` too, and an HTTPS-only move: the validator keeps 36656 or 443 instead of 36657 or 8443; - should_move_the_validator_of_an_ext_addr_evonode_on_a_core_v24_port_only_diff runs Core v24 `protx listdiff` JSON (the new fixture core_v24_protx_list_diffs/1260-1270.json: `service` and the whole `addresses`, no legacy ports) through update_masternode_list. With the parent's source files it fails: the existing member and a new quorum's validator keep 36656/1443 instead of 36657/8443; - a state with zero legacy ports gets port 0 instead of 36656, and without legacy ports no validator; - the stored state keeps the stale legacy port 36656 instead of 36657; - should_hold_in_stored_form_exactly_what_the_store_reads_back: the in-memory state differs from what the store reads back (it also fails when only the stored-form normalization is removed); - should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads stores and reloads the platform state after each of an ExtAddr evonode's registration, addresses-only port move and revoke (legacy ports 0, no `addresses`); with only the stored-form normalization removed, the lists in memory differ from the reloaded ones after the registration; - should_not_advertise_an_evonode_whose_network_info_core_emptied pins the banned member a new quorum gets, with the ports it last had; with the parent's port reading it gets none; - should_store_legacy_ports_unchanged_without_nested_ports fails for a legacy value outside the port range when the stored form drops the legacy fallback. Guard tests, passing before and after: - should_build_the_validators_earlier_binaries_built_from_mainnet_masternode_lists: every evonode of the mainnet listdiff fixtures, applied the way Drive applies them, as listed and with `addresses` synthesized as Core 23 prints them (GetNetInfoWithLegacyFields for added items; the core entry, and changed ports on the service IP or the 255.255.255.255 placeholder, for diffs), gets the validator an earlier binary builds from the legacy fields alone, for a new quorum and as a refreshed member of an existing set. It passes with the parent's source files. It fails when the legacy fallback is dropped (as listed), and when the stored form skips the legacy fallback once `addresses` is listed (with Core 23 addresses only). - should_move_the_validator_of_a_legacy_evonode_on_a_core_v24_port_diff: a legacy evonode's port diff before V24 (legacy port plus a 255.255.255.255 entry, fixture core_v24_protx_list_diffs/before_v24; Core accepts a legacy evonode's ProUpServTx only before V24) moves the validator's P2P port and keeps its IP. - should_make_an_evonode_with_a_tor_primary_address_a_validator_on_the_unspecified_ip: an ExtAddr evonode whose primary address is Tor is a validator on `::` with the ports of its platform entries, and its list processes. - The placeholder host never becomes the validator host; a core-address-only diff keeps the platform ports, before and after a restart; zero legacy ports without nested ones keep the evonode a member with port 0; a revoked legacy evonode listed with `addresses: {}` builds the member its legacy ports give; a legacy evonode revoked after V24 gives, whether or not the node restarted, exactly the member earlier binaries build (banned, on `::`, ports 0) and the same stored state; every port test checks that a new quorum, built from memory or after a reload, gets the same member as the refreshed validator. Co-Authored-By: Claude Opus 5.5 --- .../update_masternode_list/mod.rs | 547 ++++++++++++++- .../update_state_masternode_list/v0/mod.rs | 645 +++++++++++++++++- .../src/platform_types/masternode/v0/mod.rs | 217 +++++- .../platform_state/accessors.rs | 30 +- .../src/platform_types/validator/v0/mod.rs | 249 ++++++- .../core_v24_protx_list_diffs/1260-1270.json | 68 ++ .../before_v24/1-1000.json | 45 ++ .../before_v24/1000-1010.json | 18 + 8 files changed, 1761 insertions(+), 58 deletions(-) create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index ef114d68582..3c4750508ec 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -78,19 +78,29 @@ mod test { }; use crate::platform_types::platform_state::PlatformStateV0Methods; use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; + use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; + use crate::platform_types::validator_set::ValidatorSet; use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; - use dpp::dashcore::ProTxHash; - use dpp::dashcore_rpc::json::MasternodeListDiff; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash}; + use dpp::dashcore_rpc::json::{ + DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; use dpp::identifier::MasternodeIdentifiers; use dpp::identity::accessors::IdentityGettersV0; use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dpp::identity::{KeyID, Purpose}; use dpp::prelude::Identifier; - use std::collections::BTreeMap; + use rand::rngs::StdRng; + use rand::SeedableRng; + use serde_json::{json, Value}; + use std::collections::{BTreeMap, BTreeSet}; use std::env; use std::fs::File; use std::io::BufReader; + use std::net::SocketAddr; use std::path::PathBuf; use std::str::FromStr; @@ -213,6 +223,319 @@ mod test { .expect("expected to commit"); } + /// A `protx listdiff` response from the mainnet fixtures. With + /// `core_23_addresses`, every masternode and diff also lists the + /// `addresses` Core 23 prints beside the legacy fields. `evonodes` + /// collects the ProTx hashes of the evonodes listed so far. + fn mainnet_list_diff( + file_name: &str, + core_23_addresses: bool, + evonodes: &mut BTreeSet, + ) -> MasternodeListDiff { + let json = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/supporting_files/mainnet_protx_list_diffs") + .join(file_name), + ) + .expect("expected to read the masternode list diff"); + let mut list_diff: Value = serde_json::from_str(&json).expect("expected JSON"); + for masternode in list_diff["addedMNs"] + .as_array_mut() + .expect("expected added masternodes") + { + let is_evonode = masternode["type"] == "Evo"; + if is_evonode { + let pro_tx_hash = masternode["proTxHash"].as_str().expect("a ProTx hash"); + evonodes.insert(pro_tx_hash.to_string()); + } + if core_23_addresses { + let addresses = core_23_state_addresses(&masternode["state"], is_evonode); + masternode["state"]["addresses"] = addresses; + } + } + if core_23_addresses { + for update in list_diff["updatedMNs"] + .as_array_mut() + .expect("expected updated masternodes") + { + for (pro_tx_hash, state_diff) in update.as_object_mut().expect("a diff") { + let is_evonode = evonodes.contains(pro_tx_hash); + if let Some(addresses) = core_23_diff_addresses(state_diff, is_evonode) { + state_diff["addresses"] = addresses; + } + } + } + } + serde_json::from_value(list_diff).expect("expected a masternode list diff") + } + + /// A listed `service`, `None` for a masternode without an address, which + /// Core lists as the unspecified address with port 0. + fn listed_service(service: &Value) -> Option { + let service: SocketAddr = service + .as_str() + .expect("a service") + .parse() + .expect("an ip:port service"); + (!(service.ip().is_unspecified() && service.port() == 0)).then_some(service) + } + + /// `port` on the IP of `service`, as Core prints a network address. + fn on_service_ip(service: SocketAddr, port: u16) -> String { + SocketAddr::new(service.ip(), port).to_string() + } + + fn listed_port(port: &Value) -> u16 { + port.as_u64() + .and_then(|port| u16::try_from(port).ok()) + .expect("a port") + } + + /// The `addresses` Core 23 lists for a masternode that predates extended + /// addresses (`GetNetInfoWithLegacyFields`): its core entry is `service`, + /// and an evonode's platform entries pair the IP of `service` with the + /// legacy ports. A masternode without an address lists no entry. + fn core_23_state_addresses(state: &Value, is_evonode: bool) -> Value { + let Some(service) = listed_service(&state["service"]) else { + return json!({}); + }; + let mut addresses = json!({ "core_p2p": [service.to_string()] }); + if is_evonode { + let http_port = listed_port(&state["platformHTTPPort"]); + let p2p_port = listed_port(&state["platformP2PPort"]); + addresses["platform_https"] = json!([on_service_ip(service, http_port)]); + addresses["platform_p2p"] = json!([on_service_ip(service, p2p_port)]); + } + addresses + } + + /// The `addresses` Core 23 lists in the diff of a masternode that + /// predates extended addresses (`CDeterministicMNStateDiff::ToJson`): with + /// a changed `service`, its core entry; for an evonode, each changed + /// platform port, on the IP of the changed `service`, else on the + /// placeholder host 255.255.255.255. `None` when that lists nothing. + fn core_23_diff_addresses(state_diff: &Value, is_evonode: bool) -> Option { + let service: Option = state_diff.get("service").map(|service| { + service + .as_str() + .expect("a service") + .parse() + .expect("an ip:port service") + }); + let mut addresses = serde_json::Map::new(); + if let Some(service) = state_diff.get("service").and_then(listed_service) { + addresses.insert("core_p2p".to_string(), json!([service.to_string()])); + } + if is_evonode { + for (port_field, purpose) in [ + ("platformP2PPort", "platform_p2p"), + ("platformHTTPPort", "platform_https"), + ] { + if let Some(port) = state_diff.get(port_field).map(listed_port) { + let entry = match service { + Some(service) => on_service_ip(service, port), + None => format!("255.255.255.255:{port}"), + }; + addresses.insert(purpose.to_string(), json!([entry])); + } + } + } + (!addresses.is_empty()).then_some(Value::Object(addresses)) + } + + /// The validator every binary before nested addresses builds for a listed + /// evonode: its ports are the legacy port fields as listed, and it is a + /// validator only with both of them and a platform node id. + fn validator_from_legacy_ports(evonode: &MasternodeListItem) -> Option { + let state = &evonode.state; + #[allow(deprecated)] + let (p2p_port, http_port) = ( + state.legacy_platform_p2p_port?, + state.legacy_platform_http_port?, + ); + Some(ValidatorV0 { + pro_tx_hash: evonode.pro_tx_hash, + public_key: None, + node_ip: state.service.ip().to_string(), + node_id: PubkeyHash::from_byte_array(state.platform_node_id?), + core_port: state.service.port(), + platform_http_port: http_port as u16, + platform_p2p_port: p2p_port as u16, + is_banned: state.pose_ban_height.is_some(), + }) + } + + /// How every binary before nested addresses refreshes a validator-set + /// member from its evonode's diff: on a ban, `service` or legacy P2P port + /// change, it takes the ban, the IP and the legacy ports the diff lists. + fn refresh_from_legacy_ports(member: &mut ValidatorV0, state_diff: &DMNStateDiff) { + #[allow(deprecated)] + let (p2p_port, http_port) = ( + state_diff.legacy_platform_p2p_port, + state_diff.legacy_platform_http_port, + ); + if state_diff.pose_ban_height.is_none() + && state_diff.service.is_none() + && p2p_port.is_none() + { + return; + } + if let Some(pose_ban_height) = state_diff.pose_ban_height { + member.is_banned = pose_ban_height.is_some(); + } + if let Some(service) = state_diff.service { + member.node_ip = service.ip().to_string(); + } + if let Some(p2p_port) = p2p_port { + member.platform_p2p_port = p2p_port as u16; + } + if let Some(http_port) = http_port { + member.platform_http_port = http_port as u16; + } + } + + /// Asserts that `state` lists exactly the `listed` evonodes and gives each + /// the validator binaries before nested addresses build for it. + fn assert_validators_from_legacy_ports( + state: &PlatformState, + listed: &BTreeMap, + context: &str, + ) { + assert!( + state.hpmn_masternode_list().keys().eq(listed.keys()), + "{context}: the same evonodes are listed" + ); + for (pro_tx_hash, evonode) in listed { + assert_eq!( + ValidatorV0::new_validator_if_masternode_in_state(*pro_tx_hash, None, state), + validator_from_legacy_ports(evonode), + "{context}: evonode {pro_tx_hash}" + ); + } + } + + /// Validator-set membership decides quorum rotation, and every binary + /// before nested addresses built it from the legacy port fields alone. + /// For every mainnet evonode, as the fixtures list it and with the + /// `addresses` Core 23 lists beside the legacy fields, the validator a new + /// quorum gets and the member an existing set refreshes from a diff must + /// be exactly what those binaries build. + #[test] + fn should_build_the_validators_earlier_binaries_built_from_mainnet_masternode_lists() { + let platform_version = PlatformVersion::latest(); + let init_core_height = 2128896; + let next_core_height = 2129440; + let quorum_hash = QuorumHash::from_byte_array([0x55; 32]); + + for core_23_addresses in [false, true] { + let context = if core_23_addresses { + "with Core 23 addresses" + } else { + "as listed" + }; + let mut evonodes = BTreeSet::new(); + let init_list = mainnet_list_diff("1-2128896.json", core_23_addresses, &mut evonodes); + let list_diff = + mainnet_list_diff("2128896-2129440.json", core_23_addresses, &mut evonodes); + assert!( + list_diff + .updated_mns + .iter() + .any(|(pro_tx_hash, state_diff)| { + evonodes.contains(&pro_tx_hash.to_string()) && state_diff.service.is_some() + }), + "the diff moves an evonode" + ); + + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + let responses = [init_list.clone(), list_diff.clone()]; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| { + Ok(responses[usize::from(block != init_core_height)].clone()) + }); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Mainnet), + ) + .expect("platform state"); + + // The evonodes as every earlier binary holds them. + let is_evonode = + |masternode: &&MasternodeListItem| masternode.node_type == MasternodeType::Evo; + let mut listed: BTreeMap = init_list + .added_mns + .iter() + .filter(is_evonode) + .map(|evonode| (evonode.pro_tx_hash, evonode.clone())) + .collect(); + + platform + .update_state_masternode_list_v0(&mut state, init_core_height, true) + .expect("expected to apply the masternode list"); + assert_validators_from_legacy_ports(&state, &listed, context); + + let members: BTreeMap = listed + .iter() + .filter_map(|(pro_tx_hash, evonode)| { + validator_from_legacy_ports(evonode).map(|member| (*pro_tx_hash, member)) + }) + .collect(); + assert_eq!( + members.len(), + listed.len(), + "{context}: every evonode is a member" + ); + state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: init_core_height, + members: members.clone(), + threshold_public_key: SecretKey::::random( + &mut StdRng::seed_from_u64(1), + ) + .public_key(), + }), + ); + + platform + .update_state_masternode_list_v0(&mut state, next_core_height, false) + .expect("expected to apply the masternode list diff"); + + let mut refreshed_members = members.clone(); + for evonode in list_diff.added_mns.iter().filter(is_evonode) { + listed.insert(evonode.pro_tx_hash, evonode.clone()); + } + for (pro_tx_hash, state_diff) in &list_diff.updated_mns { + if let Some(evonode) = listed.get_mut(pro_tx_hash) { + evonode.state.apply_diff(state_diff.clone()); + } + if let Some(member) = refreshed_members.get_mut(pro_tx_hash) { + refresh_from_legacy_ports(member, state_diff); + } + } + for pro_tx_hash in &list_diff.removed_mns { + listed.remove(pro_tx_hash); + refreshed_members.remove(pro_tx_hash); + } + + assert_validators_from_legacy_ports(&state, &listed, context); + assert_ne!( + refreshed_members, members, + "{context}: the diff refreshes members" + ); + assert_eq!( + state.validator_sets()[&quorum_hash].members(), + &refreshed_members, + "{context}: the refreshed members" + ); + } + } + /// Dash Core v24 prints masternodes that differ from every earlier list: a shared /// masternode has no owner, payout or collateral address; an extended-address masternode /// has a `payouts` list instead of `payoutAddress`; a Tor primary address is not an @@ -469,4 +792,222 @@ mod test { ); } } + + /// The quorum of the validator set `state_after_core_v24_lists` adds. + const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55; 32]; + + /// Runs one masternode list update per `(core height, fixture)`, in order, + /// as Drive runs one per block, with Core reporting the fixture's + /// `protx listdiff` JSON from `core_v24_protx_list_diffs` for that height. + /// Before the last update, one validator set lists every evonode that is a + /// validator then. Returns the state after the last update. + fn state_after_core_v24_lists(fixtures: &[(u32, &str)]) -> PlatformState { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig::default()) + .build_with_mock_rpc() + .set_genesis_state(); + + let lists: BTreeMap = fixtures + .iter() + .map(|(core_height, file_name)| { + let json = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/supporting_files/core_v24_protx_list_diffs") + .join(file_name), + ) + .expect("expected to read the masternode list diff"); + (*core_height, json) + }) + .collect(); + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| Ok(serde_json::from_str(&lists[&block])?)); + + let transaction = platform.drive.grove.start_transaction(); + let mut platform_state: Option = None; + let mut block_platform_state = platform.state.load().as_ref().clone(); + for (index, (core_height, _)) in fixtures.iter().enumerate() { + let block = index as u64 + 1; + if block == fixtures.len() as u64 { + let members = block_platform_state + .hpmn_masternode_list() + .keys() + .filter_map(|pro_tx_hash| { + ValidatorV0::new_validator_if_masternode_in_state( + *pro_tx_hash, + None, + &block_platform_state, + ) + .map(|member| (*pro_tx_hash, member)) + }) + .collect(); + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + block_platform_state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: *core_height, + members, + threshold_public_key: SecretKey::::random( + &mut StdRng::seed_from_u64(1), + ) + .public_key(), + }), + ); + } + platform + .update_masternode_list( + platform_state.as_ref(), + &mut block_platform_state, + *core_height, + index == 0, + &BlockInfo { + height: block, + core_height: *core_height, + time_ms: block, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list"); + platform_state = Some(block_platform_state.clone()); + } + block_platform_state + } + + /// The validator of `pro_tx_hash` in the validator set + /// `state_after_core_v24_lists` adds, and the one a new quorum gets. + fn existing_and_new_validator( + state: &PlatformState, + pro_tx_hash: &str, + ) -> (Option, Option) { + let pro_tx_hash = ProTxHash::from_str(pro_tx_hash).expect("expected a pro tx hash"); + let validator_set = + &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; + ( + validator_set.members().get(&pro_tx_hash).cloned(), + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), + ) + } + + /// An ExtAddr evonode keeps its platform ports in its network info, so a + /// ProUpServTx that only moves them reaches Drive as a diff with `service` + /// and the whole `addresses`, and no legacy port field. Tenderdash must be + /// told the new ports, by the existing validator set and by one built + /// later, or it keeps dialling the old ones. + #[test] + fn should_move_the_validator_of_an_ext_addr_evonode_on_a_core_v24_port_only_diff() { + let state = state_after_core_v24_lists(&[ + (1250, "1-1250.json"), + (1260, "1250-1260.json"), + (1270, "1260-1270.json"), + ]); + + let expected = ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "192.0.2.40".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("9e391c2c041a122a779bafe09d6c47ea600dfcbe") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 9999, + platform_http_port: 8443, + platform_p2p_port: 36657, + is_banned: false, + }; + assert_eq!( + existing_and_new_validator( + &state, + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0" + ), + (Some(expected.clone()), Some(expected)) + ); + } + + /// Core lists an evonode whose primary core address is a Tor address with + /// that address as `service`, which reads as `[::]:0`. It becomes a + /// validator on the unspecified IP with the ports of its platform entries: + /// unreachable, but its list does not fail the block. + #[test] + fn should_make_an_evonode_with_a_tor_primary_address_a_validator_on_the_unspecified_ip() { + let state = state_after_core_v24_lists(&[ + (1250, "1-1250.json"), + (1260, "1250-1260.json"), + (1270, "1260-1270.json"), + ]); + + let (_, new_quorum_validator) = existing_and_new_validator( + &state, + "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + ); + assert_eq!( + new_quorum_validator, + Some(ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("9cea9116b333eba7631804e1be7efd62c56d5b58") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 26656, + is_banned: false, + }) + ); + } + + /// Before V24, a ProUpServTx that only moves a legacy evonode's P2P port + /// reaches Drive as the legacy port field and a platform entry on the + /// placeholder host 255.255.255.255, since the diff carries no address. + /// The validator takes the new port and keeps the evonode's IP. + #[test] + fn should_move_the_validator_of_a_legacy_evonode_on_a_core_v24_port_diff() { + let state = state_after_core_v24_lists(&[ + (1000, "before_v24/1-1000.json"), + (1010, "before_v24/1000-1010.json"), + ]); + + let expected = ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "192.0.2.60".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("416e5b04d3047def070bea5175431ca7732b4b7b") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 9999, + platform_http_port: 443, + platform_p2p_port: 36657, + is_banned: false, + }; + assert_eq!( + existing_and_new_validator( + &state, + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76" + ), + (Some(expected.clone()), Some(expected)) + ); + } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs index 1138c3aad35..c10e8444b44 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs @@ -1,9 +1,11 @@ use crate::error::Error; use crate::execution::types::update_state_masternode_list_outcome; +use crate::platform_types::masternode::v0::apply_diff_in_stored_form; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; +use crate::platform_types::validator::v0::PlatformPorts; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; use crate::rpc::core::CoreRPCLike; use dpp::dashcore::{ProTxHash, QuorumHash}; @@ -42,19 +44,21 @@ where /// Updates a masternode in the validator sets. /// /// This function updates the properties of the masternode that matches the given `pro_tx_hash`. - /// The properties are updated based on the provided `dmn_state_diff` information. - /// If a matching masternode is found, the function updates its ban status, service address, - /// platform P2P port, and platform HTTP port accordingly. + /// The ban status and service address are updated based on the provided `dmn_state_diff` + /// information, the platform P2P and HTTP ports from `new_platform_ports`. /// /// # Arguments /// /// * `pro_tx_hash` - The `ProTxHash` of the masternode to be updated /// * `dmn_state_diff` - The `DMNStateDiff` containing the updated masternode information + /// * `new_platform_ports` - The platform ports that resolve to a new value in the updated + /// masternode state /// * `state` - The platform state whose validator sets are updated; only the /// sets that list the masternode are touched fn update_masternode_in_validator_sets( pro_tx_hash: &ProTxHash, dmn_state_diff: &DMNStateDiff, + new_platform_ports: PlatformPorts, state: &mut PlatformState, ) { for quorum_hash in Self::validator_sets_with_member(state, pro_tx_hash) { @@ -72,18 +76,28 @@ where validator.node_ip = address.ip().to_string(); } - #[allow(deprecated)] - if let Some(p2p_port) = dmn_state_diff.legacy_platform_p2p_port { - validator.platform_p2p_port = p2p_port as u16; + if let Some(p2p_port) = new_platform_ports.p2p { + validator.platform_p2p_port = p2p_port; } - #[allow(deprecated)] - if let Some(http_port) = dmn_state_diff.legacy_platform_http_port { - validator.platform_http_port = http_port as u16; + if let Some(http_port) = new_platform_ports.http { + validator.platform_http_port = http_port; } } } + /// The platform ports an evonode's state resolves to, or `None` for a + /// masternode that is not an evonode. + fn evonode_platform_ports( + state: &PlatformState, + pro_tx_hash: &ProTxHash, + ) -> Option { + state + .hpmn_masternode_list() + .get(pro_tx_hash) + .map(|evonode| PlatformPorts::of(&evonode.state)) + } + /// Whether applying `updated_mns` would change any item of `masternode_list`. /// /// Core reports a masternode as updated whenever any field of its state @@ -97,9 +111,7 @@ where ) -> bool { updated_mns.iter().any(|(pro_tx_hash, state_diff)| { masternode_list.get(pro_tx_hash).is_some_and(|item| { - let mut updated = item.state.clone(); - updated.apply_diff(state_diff.clone()); - updated != item.state + apply_diff_in_stored_form(&item.state, state_diff) != item.state }) }) } @@ -164,22 +176,43 @@ where state.insert_masternode(masternode.clone()); } + // Every protocol version runs this code. Validator node addresses are + // not consensus state: Tenderdash hashes a validator set as + // merkle(threshold public key, quorum hash). Membership is, because + // quorum rotation keys off the set's members, and it stays what + // earlier binaries built for every masternode list they accept: an + // evonode is a member when it has a platform node id and both ports, a + // port resolves whenever its legacy field is present, and every + // evonode in such a list has both legacy fields. for (pro_tx_hash, state_diff) in updated_mns { - let is_hpmn = state.hpmn_masternode_list().contains_key(pro_tx_hash); + let platform_ports_before = Self::evonode_platform_ports(state, pro_tx_hash); if !state.apply_masternode_state_diff(pro_tx_hash, state_diff) { continue; } - // these 3 fields are the only fields that are useful for validators. If they change we need to update - // validator sets - #[allow(deprecated)] - let platform_p2p_port_changed = state_diff.legacy_platform_p2p_port.is_some(); - if is_hpmn - && (state_diff.pose_ban_height.is_some() - || state_diff.service.is_some() - || platform_p2p_port_changed) + // Only evonodes are validators. + let (Some(platform_ports_before), Some(platform_ports)) = ( + platform_ports_before, + Self::evonode_platform_ports(state, pro_tx_hash), + ) else { + continue; + }; + // The ports are read from the updated state, not from the diff: an + // ExtAddr evonode's port move reaches us only in `addresses`, and a + // legacy evonode's diff lists there just the entries that changed. + let new_platform_ports = changed_platform_ports(platform_ports_before, platform_ports); + // the ban status, the IP and the platform P2P port are the only fields that are useful for + // validators. If they change we need to update validator sets + if state_diff.pose_ban_height.is_some() + || state_diff.service.is_some() + || new_platform_ports.p2p.is_some() { // we updated the ban status the IP or the platform port, we need to update the validator in the validator list - Self::update_masternode_in_validator_sets(pro_tx_hash, state_diff, state); + Self::update_masternode_in_validator_sets( + pro_tx_hash, + state_diff, + new_platform_ports, + state, + ); } } @@ -206,19 +239,42 @@ where } } +/// The ports of `after` that resolve to a value other than in `before`. +/// +/// A port that stops resolving is not reported, so the validator keeps its +/// last known port. +fn changed_platform_ports(before: PlatformPorts, after: PlatformPorts) -> PlatformPorts { + PlatformPorts { + p2p: after.p2p.filter(|_| after.p2p != before.p2p), + http: after.http.filter(|_| after.http != before.http), + } +} + #[cfg(test)] mod tests { - use crate::config::PlatformConfig; + use crate::config::{PlatformConfig, PlatformTestConfig}; + use crate::platform_types::masternode::v0::MasternodeV0; + use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; + use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, PlatformPorts}; + use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; + use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; + use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator::v0::ValidatorV0; + use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; - use dpp::dashcore::{Network, ProTxHash, Txid}; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ - DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + DMNPayout, DMNState, DMNStateDiff, MasternodeAddresses, MasternodeListDiff, + MasternodeListItem, MasternodeType, }; use dpp::version::PlatformVersion; - use std::collections::BTreeSet; + use rand::rngs::StdRng; + use rand::SeedableRng; + use std::collections::{BTreeMap, BTreeSet}; fn masternode(pro_tx_hash: ProTxHash) -> MasternodeListItem { MasternodeListItem { @@ -296,9 +352,7 @@ mod tests { &PlatformConfig::default_for_network(Network::Testnet), ) .expect("platform state"); - state - .full_masternode_list_mut() - .insert(pro_tx_hash, masternode(pro_tx_hash)); + state.insert_masternode(masternode(pro_tx_hash)); state.mark_saved(); platform @@ -452,4 +506,537 @@ mod tests { assert!(state.masternode_changes.removed.is_empty()); assert!(!state.masternode_changes.rewrite_all); } + + /// The IP every test evonode is registered with. + const SERVICE_IP: &str = "1.2.3.4"; + const EVONODE_PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55u8; 32]; + + fn evonode_pro_tx_hash() -> ProTxHash { + ProTxHash::from_byte_array(EVONODE_PRO_TX_HASH) + } + + /// An evonode with platform ports 36656 (P2P) and 443 (HTTPS), as Core + /// lists it with `-deprecatedrpc=service`: the legacy port fields carry + /// the live ports and `addresses` repeats them. + fn evonode() -> MasternodeListItem { + let mut item = masternode(evonode_pro_tx_hash()); + item.node_type = MasternodeType::Evo; + item.state.platform_node_id = Some([7u8; 20]); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = Some(36656); + item.state.legacy_platform_http_port = Some(443); + } + item.state.addresses = Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:443".to_string()], + }); + item + } + + /// The same evonode in a state whose legacy ports are absent: the ports + /// are only in `addresses`. + fn evonode_without_legacy_ports() -> MasternodeListItem { + let mut item = evonode(); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = None; + item.state.legacy_platform_http_port = None; + } + item + } + + /// `item` as a node that restarted holds it: loaded back from storage. + fn reloaded(item: MasternodeListItem) -> MasternodeListItem { + MasternodeV0::from(item).into() + } + + /// Runs one masternode list update per diff, in order, for a state + /// holding `evonode` as a member of one validator set, with Core reporting + /// the diffs for it, and returns the state afterwards. + fn state_after_diffs(evonode: MasternodeListItem, diffs: Vec) -> PlatformState { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + + let pro_tx_hash = evonode.pro_tx_hash; + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + state.insert_masternode(evonode); + let validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: SERVICE_IP.to_string(), + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: false, + }; + let mut rng = StdRng::seed_from_u64(1); + state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: 1, + members: BTreeMap::from([(pro_tx_hash, validator)]), + threshold_public_key: SecretKey::::random(&mut rng).public_key(), + }), + ); + state.mark_saved(); + + let block_count = diffs.len() as u32; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, diffs[block as usize - 1].clone())], + }) + }); + + for block in 1..=block_count { + platform + .update_state_masternode_list_v0(&mut state, block, false) + .expect("update must succeed"); + } + + state + } + + /// The evonode's member of the validator set. + fn validator_in(state: &PlatformState) -> ValidatorV0 { + state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)].members() + [&evonode_pro_tx_hash()] + .clone() + } + + /// The validator a quorum Drive learns about later gets for the evonode, + /// once from the state as held in memory and once as a node that + /// restarted loads it back. Validator-set membership decides quorum + /// rotation, so every node must build the same one. + fn validators_for_a_new_quorum(state: &PlatformState) -> [Option; 2] { + let pro_tx_hash = evonode_pro_tx_hash(); + let mut restarted = state.clone(); + restarted.insert_masternode(reloaded(state.full_masternode_list()[&pro_tx_hash].clone())); + [state, &restarted].map(|state| { + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state) + }) + } + + /// Asserts that the evonode's member of the existing validator set has + /// the given ports, and that a new quorum gets the same member. + fn assert_validator_ports(state: &PlatformState, p2p_port: u16, http_port: u16) { + let validator = validator_in(state); + assert_eq!(validator.node_ip, SERVICE_IP); + assert_eq!(validator.platform_p2p_port, p2p_port); + assert_eq!(validator.platform_http_port, http_port); + for new_quorum_validator in validators_for_a_new_quorum(state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// The network info of the evonode after moving to platform ports 36657 + /// (P2P) and 8443 (HTTPS), as Core prints an ExtAddr one. + fn moved_platform_ports() -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36657".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + } + } + + /// A node that restarts reloads its masternode lists from storage, and + /// validator-set membership, which decides quorum rotation, must not + /// depend on whether it did. So after every masternode list update the + /// lists held in memory must be exactly what storage gives back. The + /// updates: an ExtAddr evonode added with its platform ports in + /// `addresses` and a payout list, a port move that only `addresses` + /// carries, then a revoke that zeroes the legacy ports and carries no + /// `addresses`. + #[test] + fn should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads() { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig { + testing_configs: PlatformTestConfig { + store_platform_state: true, + ..PlatformTestConfig::default_minimal_verifications() + }, + ..Default::default() + }) + .build_with_mock_rpc() + .set_genesis_state(); + + let mut added = evonode(); + added.collateral_address = Some([0x21; 20]); + added.state.owner_address = Some([0x22; 20]); + added.state.payout_address = None; + added.state.payouts = Some(vec![DMNPayout { + address: [0x23; 20], + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array([0x23; 20])), + reward: 10000, + }]); + let pro_tx_hash = added.pro_tx_hash; + let port_move = DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }; + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(3)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + let (added_mns, updated_mns) = match block { + 1 => (vec![added.clone()], vec![]), + 2 => (vec![], vec![(pro_tx_hash, port_move.clone())]), + _ => (vec![], vec![(pro_tx_hash, revoke.clone())]), + }; + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns, + removed_mns: vec![], + updated_mns, + }) + }); + + let transaction = platform.drive.grove.start_transaction(); + let mut state = platform.state.load().as_ref().clone(); + // The ports and ban each update leaves the evonode with. + let expected = [ + (Some(36656), Some(443), false), + (Some(36657), Some(8443), false), + (Some(0), Some(0), true), + ]; + for (block, (p2p, http, is_banned)) in (1..).zip(expected) { + platform + .update_state_masternode_list_v0(&mut state, block, false) + .expect("update must succeed"); + platform + .store_platform_state(&state, Some(&transaction), platform_version) + .expect("store platform state"); + state.mark_saved(); + let reloaded = Platform::::fetch_platform_state( + &platform.drive, + Some(&transaction), + platform_version, + ) + .expect("fetch platform state") + .expect("a state was stored"); + + assert_eq!( + reloaded.full_masternode_list(), + state.full_masternode_list(), + "the full list after block {block}" + ); + assert_eq!( + reloaded.hpmn_masternode_list(), + state.hpmn_masternode_list(), + "the evonode list after block {block}" + ); + let evonode = &reloaded.hpmn_masternode_list()[&pro_tx_hash].state; + assert_eq!( + ( + PlatformPorts::of(evonode), + evonode.pose_ban_height.is_some() + ), + (PlatformPorts { p2p, http }, is_banned), + "the evonode after block {block}" + ); + } + } + + /// An ExtAddr evonode keeps its platform ports in its network info and its + /// legacy port fields at zero, so a ProUpServTx that only moves its ports + /// reaches Drive as a diff whose only network field is `addresses` + /// (`service` too only with `-deprecatedrpc=service`). Tenderdash must be + /// told the new port, or it keeps dialling the old one. + #[test] + fn should_update_validator_ports_from_an_addresses_only_diff() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// The same port move as Core prints it with `-deprecatedrpc=service`: + /// `service` repeats because the network info changed, while the legacy + /// port fields are absent because they did not change. + #[test] + fn should_update_validator_ports_from_addresses_when_the_diff_also_carries_service() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// An ExtAddr evonode that moves only its HTTPS port lists its whole + /// network info again, the unchanged P2P entry included. + #[test] + fn should_update_the_validator_https_port_when_only_it_moved() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 8443); + } + + /// When a legacy evonode changes only a platform port, the diff does not + /// carry the node's IP, so Core pairs the new port in `addresses` with the + /// placeholder host 255.255.255.255. Only the port may be taken from it. + #[test] + fn should_not_take_the_placeholder_host_from_a_legacy_evonode_port_diff() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 443); + } + + /// A legacy evonode's diff lists in `addresses` only the entries that + /// changed, next to the changed legacy port field. A later diff that + /// changes the other port must not undo an earlier one's port. + #[test] + fn should_keep_each_platform_port_from_diffs_that_list_one_entry_each() { + let state = state_after_diffs( + evonode(), + vec![ + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }, + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_http_port: Some(8443), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec![], + platform_https: vec!["255.255.255.255:8443".to_string()], + })), + ..empty_state_diff() + }, + ], + ); + + assert_eq!(validator_in(&state).platform_p2p_port, 36657); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + let new_quorum_validator = new_quorum_validator.expect("the evonode has ports"); + assert_eq!(new_quorum_validator.platform_p2p_port, 36657); + assert_eq!(new_quorum_validator.platform_http_port, 8443); + } + } + + /// A diff of a legacy evonode's core address carries `addresses` with the + /// core entry only, so the platform ports must not be read as cleared. + #[test] + fn should_keep_validator_ports_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("5.6.7.8:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["5.6.7.8:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + let validator = validator_in(&state); + assert_eq!(validator.node_ip, "5.6.7.8"); + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// A node that restarted holds the evonode without `addresses`. A diff + /// that lists only the core entry must leave its platform ports as they + /// are, as on a node that kept its state in memory. + #[test] + fn should_keep_validator_ports_after_a_restart_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + reloaded(evonode()), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 443); + } + + /// After V24 a legacy evonode's revoke, or an operator key change, makes + /// it ExtAddr with an empty network info: Core prints its platform ports + /// as 0 and no `addresses`. Earlier binaries accept that diff, so every + /// node must build the validator they build from the legacy fields: a + /// banned member on the unspecified IP with ports 0. A node that kept the + /// evonode's `addresses` in memory and a node that restarted must both + /// build it, because validator-set membership decides quorum rotation. + #[test] + fn should_agree_on_the_validator_after_a_legacy_evonode_is_revoked_whether_or_not_the_node_restarted( + ) { + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + let kept = state_after_diffs(evonode(), vec![revoke.clone()]); + let restarted = state_after_diffs(reloaded(evonode()), vec![revoke]); + + let pro_tx_hash = evonode_pro_tx_hash(); + let new_quorum_validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 0, + platform_p2p_port: 0, + is_banned: true, + }; + // A refresh of an existing member takes the ban, the IP and the ports + // from the diff, and keeps its node id and core port. + let refreshed_member = ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + }; + for state in [&kept, &restarted] { + assert_eq!( + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), + Some(new_quorum_validator.clone()) + ); + assert_eq!(validator_in(state), refreshed_member); + } + let stored = |state: &PlatformState| { + MasternodeV0::from(state.full_masternode_list()[&pro_tx_hash].clone()).state + }; + assert_eq!(stored(&kept), stored(&restarted)); + } + + /// When Core resets an ExtAddr evonode's operator fields (ProUpRevTx, or + /// a ProUpRegTx that changes the operator key), its network info becomes + /// empty: the diff has `service` `[::]:0` and no `addresses` key, so the + /// platform ports keep resolving to the old endpoint. That endpoint is + /// never advertised, because Core bans the masternode in the same + /// transition and only a ProUpServTx, which sends new `addresses`, revives + /// it. The evonode stays a member, banned, with the ports it last had. + #[test] + fn should_not_advertise_an_evonode_whose_network_info_core_emptied() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + ..empty_state_diff() + }], + ); + + let validator_set = + &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; + let new_quorum_validator = ValidatorV0 { + pro_tx_hash: evonode_pro_tx_hash(), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: true, + }; + assert_eq!( + validator_set.members()[&evonode_pro_tx_hash()], + ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + } + ); + assert!( + validator_set.to_update().validator_updates.is_empty(), + "a banned validator is left out of the validator set update" + ); + for validator in validators_for_a_new_quorum(&state) { + assert_eq!( + validator, + Some(new_quorum_validator.clone()), + "a validator set built later gets the same banned member" + ); + } + } } diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index 2ecd3a87da5..bcb48376dfc 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -2,7 +2,7 @@ pub mod accessors; use dpp::bincode::{Decode, Encode}; -use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeType}; +use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, DMNStateDiff, MasternodeType}; use dpp::dashcore_rpc::json::MasternodeListItem; use std::fmt::{Debug, Formatter}; @@ -154,9 +154,34 @@ pub struct MasternodeStateV0 { pub platform_http_port: Option, } +/// `state` in the form Drive keeps masternode states in, in memory as in +/// storage: `MasternodeStateV0` has no `addresses`, so the platform ports +/// resolved from them move into the legacy port fields and `addresses` is +/// dropped. It has no payout list either, so `payouts` is dropped too. +/// +/// A node that restarted loads its masternode states in this form. Keeping +/// them in it in memory too means every node derives the same validators +/// from the same Core diffs, however long it has run. +pub(crate) fn in_stored_form(state: DMNState) -> DMNState { + MasternodeStateV0::from(state).into() +} + +/// `state`, kept in stored form, with a Core state diff applied, in stored +/// form again. +pub(crate) fn apply_diff_in_stored_form(state: &DMNState, diff: &DMNStateDiff) -> DMNState { + let mut updated = state.clone(); + updated.apply_diff(diff.clone()); + in_stored_form(updated) +} + impl From for MasternodeStateV0 { fn from(value: DMNState) -> Self { - // The payout list and the nested addresses are not stored. + // The payout list and the nested addresses are not stored. The stored state keeps the + // platform ports resolved from the addresses instead: an ExtAddr evonode's legacy port + // fields stay zero and go stale when its ports move. Loaded back as legacy ports, they + // resolve to the same ports. Without a nested port the legacy field is stored as it is. + let resolved_platform_p2p_port = value.platform_p2p_address().map(|(_, port)| port); + let resolved_platform_http_port = value.platform_http_address().map(|(_, port)| port); #[allow(deprecated)] let DMNState { service, @@ -188,8 +213,8 @@ impl From for MasternodeStateV0 { pub_key_operator, operator_payout_address, platform_node_id, - platform_p2p_port, - platform_http_port, + platform_p2p_port: resolved_platform_p2p_port.or(platform_p2p_port), + platform_http_port: resolved_platform_http_port.or(platform_http_port), } } } @@ -235,9 +260,11 @@ impl From for DMNState { #[cfg(test)] mod tests { + use super::*; use crate::platform_types::platform_state::platform_state_for_saving::v2::{ deserialize_masternode_entry, serialize_masternode_entry, }; + use crate::platform_types::validator::v0::PlatformPorts; use dpp::dashcore::hashes::Hash; use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ @@ -340,4 +367,186 @@ mod tests { expected.state.payouts = None; assert_eq!(stored_and_loaded(&multi_payout), expected); } + + /// An evonode registered at 1.2.3.4 with the given legacy platform ports + /// and nested addresses. + fn evonode_state( + legacy_platform_p2p_port: Option, + legacy_platform_http_port: Option, + addresses: Option, + ) -> DMNState { + #[allow(deprecated)] + DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: Some([7u8; 20]), + legacy_platform_p2p_port, + legacy_platform_http_port, + addresses, + } + } + + fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![p2p.to_string()], + platform_https: vec![https.to_string()], + } + } + + /// The stored state keeps no `addresses`, so it must keep the ports they + /// carry: an ExtAddr evonode's legacy port fields stay zero, and after a + /// port move they are stale. + #[test] + fn should_store_the_platform_ports_from_addresses() { + let stored = MasternodeStateV0::from(evonode_state( + Some(36656), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + )); + + assert_eq!(stored.platform_p2p_port, Some(36657)); + assert_eq!(stored.platform_http_port, Some(8443)); + } + + /// A node that restarts must resolve the same platform ports as one that + /// kept its state in memory, or it advertises stale validator ports. + #[test] + fn should_resolve_the_same_platform_ports_after_a_store_and_load() { + let in_memory = evonode_state( + Some(36656), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ); + + let loaded = DMNState::from(MasternodeStateV0::from(in_memory.clone())); + + assert_eq!(PlatformPorts::of(&loaded), PlatformPorts::of(&in_memory)); + assert_eq!( + in_stored_form(loaded.clone()), + loaded, + "a loaded state is already in stored form" + ); + } + + /// Without nested platform ports the legacy fields are stored as they are, + /// zero included, and so is a value no port can have: that is what every + /// earlier binary stores, and a state loaded back is then already in + /// stored form. + #[test] + fn should_store_legacy_ports_unchanged_without_nested_ports() { + for (p2p, http) in [ + (Some(26656), Some(443)), + (Some(0), Some(0)), + (None, None), + (Some(70000), Some(70000)), + ] { + let stored = MasternodeStateV0::from(evonode_state(p2p, http, None)); + assert_eq!( + (stored.platform_p2p_port, stored.platform_http_port), + (p2p, http) + ); + let loaded = DMNState::from(stored); + assert_eq!(in_stored_form(loaded.clone()), loaded); + + let core_entry_only = MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![], + platform_https: vec![], + }; + let stored = MasternodeStateV0::from(evonode_state(p2p, http, Some(core_entry_only))); + assert_eq!( + (stored.platform_p2p_port, stored.platform_http_port), + (p2p, http) + ); + } + } + + /// Masternode states are kept in memory in stored form so that a node + /// that restarted holds the same state as one that did not. That only + /// holds while the stored form is exactly what the store writes and + /// reads back. + #[test] + fn should_hold_in_stored_form_exactly_what_the_store_reads_back() { + let revoked = |addresses: Option| DMNState { + service: "[::]:0".parse().expect("socket address"), + pose_ban_height: Some(200), + revocation_reason: 1, + platform_node_id: Some([0u8; 20]), + ..evonode_state(Some(26656), Some(443), addresses) + }; + let placeholder_host = MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + }; + let on_ipv6 = DMNState { + service: "[2001:db8::1]:9999".parse().expect("socket address"), + ..evonode_state( + None, + None, + Some(platform_addresses( + "[2001:db8::1]:36656", + "[2001:db8::1]:443", + )), + ) + }; + let states = [ + evonode_state(Some(26656), Some(443), None), + evonode_state(Some(0), Some(0), None), + evonode_state(None, None, None), + evonode_state( + Some(36656), + Some(443), + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + ), + evonode_state( + Some(0), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ), + evonode_state( + None, + None, + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ), + evonode_state(Some(36657), Some(443), Some(placeholder_host)), + on_ipv6, + revoked(None), + revoked(Some(MasternodeAddresses::default())), + ]; + + for state in states { + let masternode = MasternodeListItem { + node_type: MasternodeType::Evo, + pro_tx_hash: ProTxHash::from_byte_array([0x77u8; 32]), + collateral_hash: Txid::from_byte_array([0x11u8; 32]), + collateral_index: 1, + collateral_address: Some([0x22u8; 20]), + operator_reward: 0.0, + state: state.clone(), + }; + let entry = serialize_masternode_entry(&masternode, PlatformVersion::latest()) + .expect("serializable masternode"); + let loaded = deserialize_masternode_entry(&entry).expect("its own entry"); + + assert_eq!( + loaded, + MasternodeListItem { + state: in_stored_form(state.clone()), + ..masternode + }, + "state: {state:?}" + ); + } + } } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs index 73a1990a4ea..4cf479b6431 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs @@ -1,5 +1,6 @@ use crate::error::execution::ExecutionError; use crate::error::Error; +use crate::platform_types::masternode::v0::{apply_diff_in_stored_form, in_stored_form}; use crate::platform_types::platform_state::masternode_list_changes::MasternodeListChanges; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::signature_verification_quorum_set::SignatureVerificationQuorumSet; @@ -138,10 +139,12 @@ pub trait PlatformStateV0Methods { /// Sets the current instant lock validating quorums. fn set_instant_lock_validating_quorums(&mut self, quorums: SignatureVerificationQuorumSet); - /// Sets the full masternode list. + /// Sets the full masternode list. Its states must be in stored form (see + /// `in_stored_form`), as `insert_masternode` keeps them. fn set_full_masternode_list(&mut self, list: BTreeMap); - /// Sets the list of high performance masternodes. + /// Sets the list of high performance masternodes. Its states must be in + /// stored form (see `in_stored_form`), as `insert_masternode` keeps them. fn set_hpmn_masternode_list(&mut self, list: BTreeMap); /// Sets the platform initialization information. fn set_genesis_block_info(&mut self, info: Option); @@ -170,19 +173,23 @@ pub trait PlatformStateV0Methods { /// Returns a mutable reference to the instant lock validating quorums. fn instant_lock_validating_quorums_mut(&mut self) -> &mut SignatureVerificationQuorumSet; - /// Returns a mutable reference to the full masternode list. + /// Returns a mutable reference to the full masternode list. States written + /// through it must be in stored form (see `in_stored_form`). fn full_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Returns a mutable reference to the list of high performance masternodes. + /// States written through it must be in stored form (see `in_stored_form`). fn hpmn_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Adds or replaces a masternode in the full list, and in the HPMN list when - /// it is an Evo node. Records the change for the per-entry store. + /// it is an Evo node. Records the change for the per-entry store. The + /// state is kept in the form it is stored in (see `in_stored_form`). fn insert_masternode(&mut self, masternode: MasternodeListItem); /// Applies a Core state diff to a listed masternode in both lists, recording - /// an entry change only when its stored state changes. Returns false, - /// changing nothing, when the masternode is not listed. + /// an entry change only when its stored state changes. The state stays in + /// the form it is stored in. Returns false, changing nothing, when the + /// masternode is not listed. fn apply_masternode_state_diff( &mut self, pro_tx_hash: &ProTxHash, @@ -556,7 +563,8 @@ impl PlatformStateV0Methods for PlatformState { &mut self.hpmn_masternode_list } - fn insert_masternode(&mut self, masternode: MasternodeListItem) { + fn insert_masternode(&mut self, mut masternode: MasternodeListItem) { + masternode.state = in_stored_form(masternode.state); let pro_tx_hash = masternode.pro_tx_hash; if masternode.node_type == MasternodeType::Evo { self.hpmn_masternode_list @@ -577,11 +585,11 @@ impl PlatformStateV0Methods for PlatformState { let Some(masternode) = self.full_masternode_list.get_mut(pro_tx_hash) else { return false; }; - let previous_state = masternode.state.clone(); - masternode.state.apply_diff(state_diff.clone()); - let entry_changed = masternode.state != previous_state; + let updated_state = apply_diff_in_stored_form(&masternode.state, state_diff); + let entry_changed = updated_state != masternode.state; + masternode.state = updated_state; if let Some(hpmn) = self.hpmn_masternode_list.get_mut(pro_tx_hash) { - hpmn.state.apply_diff(state_diff.clone()); + hpmn.state = apply_diff_in_stored_form(&hpmn.state, state_diff); } self.heavy_fields_dirty = true; // A mixed Core diff can include payment-only updates alongside real diff --git a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs index 2fbab063bd1..3197955fa8b 100644 --- a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs @@ -22,31 +22,258 @@ impl NewValidatorIfMasternodeInState for ValidatorV0 { ) -> Option { let MasternodeListItem { state, .. } = state.hpmn_masternode_list().get(&pro_tx_hash)?; - #[allow(deprecated)] + let platform_ports = PlatformPorts::of(state); + let PlatformPorts { + p2p: Some(platform_p2p_port), + http: Some(platform_http_port), + } = platform_ports + else { + tracing::debug!( + pro_tx_hash = %pro_tx_hash, + ?platform_ports, + "evonode is not a validator: its platform ports do not resolve" + ); + return None; + }; let DMNState { service, platform_node_id, pose_ban_height, - legacy_platform_p2p_port: platform_p2p_port, - legacy_platform_http_port: platform_http_port, .. } = state; - let Some(platform_http_port) = platform_http_port else { - return None; - }; - let Some(platform_p2p_port) = platform_p2p_port else { - return None; - }; let platform_node_id = (*platform_node_id)?; + // Only the ports come from the platform entries: the validator is + // advertised on the core IP from `service`. Some(ValidatorV0 { pro_tx_hash, public_key, node_ip: service.ip().to_string(), node_id: PubkeyHash::from_byte_array(platform_node_id), core_port: service.port(), - platform_http_port: *platform_http_port as u16, - platform_p2p_port: *platform_p2p_port as u16, + platform_http_port, + platform_p2p_port, is_banned: pose_ban_height.is_some(), }) } } + +/// The platform ports a masternode's state gives its validator. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct PlatformPorts { + /// The port Tenderdash dials the validator on. + pub p2p: Option, + /// The platform HTTPS port. + pub http: Option, +} + +impl PlatformPorts { + /// Resolves each port from the nested `addresses` entry first, else from + /// the legacy port field as it is, zero included: rust-dashcore's + /// `platform_{p2p,http}_address()` fall back that way. + /// + /// A zero legacy port is kept rather than read as "not set": whether an + /// evonode is a validator decides quorum rotation, so it must not depend + /// on how a port is read. + pub(crate) fn of(state: &DMNState) -> Self { + Self { + p2p: state + .platform_p2p_address() + .and_then(|(_host, port)| u16::try_from(port).ok()), + http: state + .platform_http_address() + .and_then(|(_host, port)| u16::try_from(port).ok()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::PlatformConfig; + use dpp::dashcore::{Network, Txid}; + use dpp::dashcore_rpc::json::{MasternodeAddresses, MasternodeType}; + use dpp::version::PlatformVersion; + + const PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + + /// An evonode registered at 1.2.3.4 with the given legacy platform ports + /// and nested addresses. + fn evonode_state( + legacy_platform_p2p_port: Option, + legacy_platform_http_port: Option, + addresses: Option, + ) -> DMNState { + #[allow(deprecated)] + DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: Some([7u8; 20]), + legacy_platform_p2p_port, + legacy_platform_http_port, + addresses, + } + } + + fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![p2p.to_string()], + platform_https: vec![https.to_string()], + } + } + + fn validator_for(dmn_state: DMNState) -> Option { + let platform_version = PlatformVersion::latest(); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + let pro_tx_hash = ProTxHash::from_byte_array(PRO_TX_HASH); + state.insert_masternode(MasternodeListItem { + node_type: MasternodeType::Evo, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0u8; 32]), + collateral_index: 0, + collateral_address: Some([0u8; 20]), + operator_reward: 0.0, + state: dmn_state, + }); + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + } + + /// An ExtAddr evonode keeps its platform ports in its network info; its + /// legacy port fields stay zero. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_zero() { + let validator = validator_for(evonode_state( + Some(0), + Some(0), + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// A state whose legacy ports are absent takes its ports from + /// `addresses`. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_absent() { + let validator = validator_for(evonode_state( + None, + None, + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// The validator is advertised on the core IP from `service`, whatever + /// host `addresses` names for the platform services: a legacy evonode's + /// port-only diff pairs the port with the placeholder host + /// 255.255.255.255. + #[test] + fn should_advertise_the_service_ip_whatever_host_addresses_name() { + let validator = validator_for(evonode_state( + Some(36656), + Some(443), + Some(platform_addresses( + "255.255.255.255:36656", + "203.0.113.7:443", + )), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.core_port, 9999); + } + + /// Core before v23 lists no nested addresses; the legacy ports apply. + #[test] + fn should_take_legacy_ports_without_addresses() { + let validator = validator_for(evonode_state(Some(26656), Some(443), None)) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.platform_p2p_port, 26656); + assert_eq!(validator.platform_http_port, 443); + } + + /// Validator-set membership decides quorum rotation, so it must not + /// depend on how a node reads a port: an evonode whose legacy ports are 0 + /// and that lists no nested ports stays a validator, with those ports. + #[test] + fn should_keep_an_evonode_with_zero_legacy_ports_as_a_validator() { + let validator = validator_for(evonode_state(Some(0), Some(0), None)) + .expect("membership does not depend on the port value"); + + assert_eq!(validator.platform_p2p_port, 0); + assert_eq!(validator.platform_http_port, 0); + } + + /// A revoked legacy evonode as Core lists it with + /// `-deprecatedrpc=service`: its network info is empty, so `service` is + /// `[::]:0` and `addresses` has no platform entry, while the legacy port + /// fields keep the ports it was registered with. + fn revoked_legacy_evonode_state(addresses: &str) -> DMNState { + serde_json::from_str(&format!( + r#"{{ + "service": "[::]:0", + "addresses": {addresses}, + "registeredHeight": 100, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": 200, + "revocationReason": 1, + "ownerAddress": "yPBWCdMRY5PsS3hJzs7csbdWQVRR85yxUz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "payoutAddress": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", + "pubKeyOperator": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "platformNodeID": "0000000000000000000000000000000000000000", + "platformP2PPort": 26656, + "platformHTTPPort": 443 + }}"# + )) + .expect("Core's JSON for a revoked evonode") + } + + /// Whether an evonode is a validator decides quorum rotation, so every + /// node must build the same member for it. For an evonode whose listed + /// `addresses` hold no platform entry, that is the member the legacy + /// port fields alone give: banned, on the unspecified IP, with its + /// registered ports. + #[test] + fn should_build_a_revoked_legacy_evonode_from_its_legacy_ports() { + for addresses in [r#"{}"#, r#"{"core_p2p": []}"#] { + let validator = validator_for(revoked_legacy_evonode_state(addresses)); + + assert_eq!( + validator, + Some(ValidatorV0 { + pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 26656, + is_banned: true, + }), + "addresses: {addresses}" + ); + } + } +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json new file mode 100644 index 00000000000..7611ce1aa1c --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json @@ -0,0 +1,68 @@ +{ + "baseHeight": 1260, + "blockHeight": 1270, + "addedMNs": [ + { + "type": "Evo", + "proTxHash": "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + "collateralHash": "89b952e1ac0ecdf8245dc368c955a866538031c6685a562e579adeda25f7a8f3", + "collateralIndex": 0, + "collateralAddress": "yRuFRszcgUcV8jMEdg8MJ7Yync7GkNkp7d", + "operatorReward": 0, + "state": { + "version": 3, + "service": "4zmxpqirulb4blkyeufp3nd37wvcvpz6fibfwvgeybtfttkkni7h3mdd.onion:9999", + "addresses": { + "core_p2p": [ + "4zmxpqirulb4blkyeufp3nd37wvcvpz6fibfwvgeybtfttkkni7h3mdd.onion:9999" + ], + "platform_p2p": [ + "192.0.2.50:26656" + ], + "platform_https": [ + "192.0.2.50:443" + ] + }, + "registeredHeight": 1265, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMWTGVez6o7YXsaj2cEGT9bU3a1jHbmDtJ", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "9cea9116b333eba7631804e1be7efd62c56d5b58", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payouts": [ + { + "address": "yfzihVQvxhkkp4rTSw3sLYQzxbaifaTUTZ", + "script": "76a914d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d788ac", + "reward": 10000 + } + ], + "pubKeyOperator": "b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6" + } + } + ], + "removedMNs": [], + "updatedMNs": [ + { + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0": { + "service": "192.0.2.40:9999", + "addresses": { + "core_p2p": [ + "192.0.2.40:9999" + ], + "platform_p2p": [ + "192.0.2.40:36657" + ], + "platform_https": [ + "192.0.2.40:8443" + ] + } + } + } + ] +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json new file mode 100644 index 00000000000..311ade66dfd --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json @@ -0,0 +1,45 @@ +{ + "baseHeight": 1, + "blockHeight": 1000, + "addedMNs": [ + { + "type": "Evo", + "proTxHash": "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76", + "collateralHash": "b881e375496b24e492820639e8dbe466b86af721eba6fcfdd5c9acf9d3e0afff", + "collateralIndex": 0, + "collateralAddress": "yRzZJpqMjbUdXCyCqHFUeU3vnJ2Z5Pzs3Q", + "operatorReward": 0, + "state": { + "version": 2, + "service": "192.0.2.60:9999", + "addresses": { + "core_p2p": [ + "192.0.2.60:9999" + ], + "platform_https": [ + "192.0.2.60:443" + ], + "platform_p2p": [ + "192.0.2.60:26656" + ] + }, + "registeredHeight": 900, + "lastPaidHeight": 990, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMbm9SVj9uygvMChEDMPoW6R3Fw1fLRaRz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "416e5b04d3047def070bea5175431ca7732b4b7b", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payoutAddress": "yaQv7p9egV1wB22MEzZ8vyyAHcYWUTNndN", + "pubKeyOperator": "b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7" + } + } + ], + "removedMNs": [], + "updatedMNs": [] +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json new file mode 100644 index 00000000000..25b973d90b7 --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json @@ -0,0 +1,18 @@ +{ + "baseHeight": 1000, + "blockHeight": 1010, + "addedMNs": [], + "removedMNs": [], + "updatedMNs": [ + { + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76": { + "platformP2PPort": 36657, + "addresses": { + "platform_p2p": [ + "255.255.255.255:36657" + ] + } + } + } + ] +} From 9fba1bec2b3a058ad73aea46595eaea353cb9a58 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:10:07 +0700 Subject: [PATCH 05/17] fix(drive-abci): bracket IPv6 hosts in validator node addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drive built each validator's Tenderdash node address as `tcp://{node_id}@{node_ip}:{port}`, which leaves an IPv6 host unbracketed (`tcp://…@2001:db8::1:26656`). A URL authority needs IPv6 hosts in brackets (RFC 3986). Core v24 lets an ExtAddr evonode's primary address be IPv6. Format the host and port through `SocketAddr`, so IPv6 is written as `[2001:db8::1]:26656`; IPv4 addresses are formatted exactly as before. All four places that build a node address (to_update, to_update_owned, and both branches of update_difference) now share one helper. Tenderdash v1.8.1 (the version dashmate runs) already reads both forms to the same address, because Go's net/url enforces strict colons only for http(s) URLs, and it writes the bracketed form itself (net.JoinHostPort). So this changes no Tenderdash behaviour today; it stops depending on that leniency. Validator node addresses are not part of Tenderdash's validator-set hash, so the change is not consensus- affecting. Test would have caught this in CI: the IPv6 node-address test fails before the fix (`…@2001:db8::1:30`), passes after; the IPv4 test pins the unchanged format for every builder. validator_set tests: 12 passed; clippy -D warnings clean. End to end, from Core v24 `protx listdiff` JSON through update_masternode_list to the Tenderdash validator update, the Core v24 fixture test's IPv6 evonode gets `tcp://…@[2001:db8::4]:26656` and the Tor-primary evonode `tcp://…@[::]:26656`; before the fix they fail with `…@2001:db8::4:26656` and `…@:::26656`. Co-Authored-By: Claude Opus 5.5 --- .../update_masternode_list/mod.rs | 41 ++++++- .../platform_types/validator_set/v0/mod.rs | 104 +++++++++++++----- 2 files changed, 119 insertions(+), 26 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index 3c4750508ec..5600d5a6a1d 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -79,7 +79,7 @@ mod test { use crate::platform_types::platform_state::PlatformStateV0Methods; use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; - use crate::platform_types::validator_set::ValidatorSet; + use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; use crate::test::helpers::setup::TestPlatformBuilder; use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; use dpp::core_types::validator_set::v0::ValidatorSetV0; @@ -643,6 +643,17 @@ mod test { assert_eq!(legacy_evo_validator.platform_p2p_port, 36656); assert_eq!(legacy_evo_validator.platform_http_port, 1443); + // An Evo on an IPv6 primary address is advertised to Tenderdash with + // its host in brackets, as a URL authority requires. + let ipv6_evo_validator = + validator("aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783") + .expect("expected the IPv6 Evo to be a validator"); + assert_eq!(ipv6_evo_validator.node_ip, "2001:db8::4"); + assert_eq!( + node_addresses([ipv6_evo_validator]), + ["tcp://4bcc85253e395ec272998a0722ac3ee1dd3965f7@[2001:db8::4]:26656"] + ); + // Every parsed masternode survives a store and reload, except for the payout list and // nested addresses, which are not stored. for masternode in block_platform_state.full_masternode_list().values() { @@ -796,6 +807,30 @@ mod test { /// The quorum of the validator set `state_after_core_v24_lists` adds. const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55; 32]; + /// The node addresses Drive hands Tenderdash for `validators`, as the + /// members of one validator set. + fn node_addresses(validators: impl IntoIterator) -> Vec { + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: 1, + members: validators + .into_iter() + .map(|validator| (validator.pro_tx_hash, validator)) + .collect(), + threshold_public_key: SecretKey::::random(&mut StdRng::seed_from_u64( + 1, + )) + .public_key(), + }) + .to_update() + .validator_updates + .into_iter() + .map(|validator_update| validator_update.node_address) + .collect() + } + /// Runs one masternode list update per `(core height, fixture)`, in order, /// as Drive runs one per block, with Core reporting the fixture's /// `protx listdiff` JSON from `core_v24_protx_list_diffs` for that height. @@ -971,6 +1006,10 @@ mod test { is_banned: false, }) ); + assert_eq!( + node_addresses(new_quorum_validator), + ["tcp://9cea9116b333eba7631804e1be7efd62c56d5b58@[::]:26656"] + ); } /// Before V24, a ProUpServTx that only moves a legacy evonode's P2P port diff --git a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs index b63af8af882..77dfff64371 100644 --- a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs @@ -2,7 +2,7 @@ use crate::error::execution::ExecutionError; use crate::error::Error; use dpp::dashcore::hashes::Hash; -use dpp::dashcore::ProTxHash; +use dpp::dashcore::{ProTxHash, PubkeyHash}; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::validator::v0::NewValidatorIfMasternodeInState; @@ -11,6 +11,7 @@ use dpp::core_types::validator::v0::ValidatorV0; pub use dpp::core_types::validator_set::v0::*; use dpp::dashcore_rpc::json::QuorumInfoResult; use std::collections::BTreeMap; +use std::net::{IpAddr, SocketAddr}; use tenderdash_abci::proto::abci::ValidatorSetUpdate; use tenderdash_abci::proto::crypto::public_key::Sum::Bls12381; use tenderdash_abci::proto::{abci, crypto}; @@ -91,12 +92,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { None } else { - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(node_id, node_ip, *platform_p2p_port); Some(Ok(abci::ValidatorUpdate { pub_key: (*public_key).map(|public_key| crypto::PublicKey { @@ -121,12 +118,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { None } else { - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(node_id, node_ip, *platform_p2p_port); Some(Ok(abci::ValidatorUpdate { pub_key: (*public_key).map(|public_key| crypto::PublicKey { @@ -178,12 +171,7 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { return None; } - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = validator_node_address(node_id, node_ip, *platform_p2p_port); Some(abci::ValidatorUpdate { pub_key: public_key.as_ref().map(|public_key| crypto::PublicKey { sum: Some(Bls12381(public_key.0.to_compressed().to_vec())), @@ -224,12 +212,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if is_banned { return None; } - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(&node_id, &node_ip, platform_p2p_port); Some(abci::ValidatorUpdate { pub_key: public_key.map(|public_key| crypto::PublicKey { @@ -309,6 +293,23 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { } } +/// The address Tenderdash dials a validator on: `tcp://@:`. +/// +/// Formatting the host and port as a socket address writes an IPv6 host in +/// brackets, as a URL authority requires. `node_ip` always holds a formatted +/// IP address; anything else is written as it is. +fn validator_node_address(node_id: &PubkeyHash, node_ip: &str, platform_p2p_port: u16) -> String { + let host_port = match node_ip.parse::() { + Ok(ip) => SocketAddr::new(ip, platform_p2p_port).to_string(), + Err(_) => format!("{}:{}", node_ip, platform_p2p_port), + }; + format!( + "tcp://{}@{}", + hex::encode(node_id.to_byte_array()), + host_port + ) +} + #[cfg(test)] mod tests { use super::*; @@ -546,4 +547,57 @@ mod tests { // threshold_public_key is still present assert!(update.threshold_public_key.is_some()); } + + /// A set holding one validator, on `node_ip`. + fn set_with_validator_on(pro_tx_hash: ProTxHash, node_ip: &str) -> ValidatorSetV0 { + let validator = ValidatorV0 { + node_ip: node_ip.to_string(), + ..make_validator(pro_tx_hash, false) + }; + // Same seeds, so every such set shares its threshold key. + make_set(1, 100, 1, BTreeMap::from([(pro_tx_hash, validator)])) + } + + /// Every node address Drive hands Tenderdash for a validator on `node_ip`: + /// from `to_update`, `to_update_owned`, and `update_difference` both when + /// the validator is unchanged and when it just moved there. + fn node_addresses_for_validator_on(node_ip: &str) -> Vec { + let pro_tx_hash = ProTxHash::from_byte_array([9; 32]); + let set = set_with_validator_on(pro_tx_hash, node_ip); + let before_the_move = set_with_validator_on(pro_tx_hash, "192.168.0.2"); + [ + set.to_update(), + set.clone().to_update_owned(), + set.update_difference(&set).expect("same quorum"), + before_the_move + .update_difference(&set) + .expect("same quorum"), + ] + .into_iter() + .map(|update| { + assert_eq!(update.validator_updates.len(), 1); + update.validator_updates[0].node_address.clone() + }) + .collect() + } + + /// A URL authority needs an IPv6 host in brackets (RFC 3986). Tenderdash + /// writes validator addresses that way; it only reads the unbracketed form + /// because Go's URL parser does not enforce this outside http(s). + #[test] + fn should_bracket_an_ipv6_host_in_the_validator_node_address() { + let expected = format!("tcp://{}@[2001:db8::1]:30", "07".repeat(20)); + for node_address in node_addresses_for_validator_on("2001:db8::1") { + assert_eq!(node_address, expected); + } + } + + /// An IPv4 host takes no brackets. + #[test] + fn should_write_an_ipv4_validator_node_address_without_brackets() { + let expected = format!("tcp://{}@192.168.0.1:30", "07".repeat(20)); + for node_address in node_addresses_for_validator_on("192.168.0.1") { + assert_eq!(node_address, expected); + } + } } From 820bf2c0d25a61874f3d0266b71c0757949335c1 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 22:56:09 +0700 Subject: [PATCH 06/17] build: bump rust-dashcore for bracketed IPv6 legacy platform hosts Moves the rust-dashcore pin to 40268cc0 (dashpay/rust-dashcore#1093), whose platform address accessors bracket an IPv6 node IP in the legacy port fallback, as nested entries already are. Drive takes only the port from these accessors, so nothing it decides changes. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 24 ++++++++++++------------ Cargo.toml | 16 ++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb0cbc7553f..abe92a65ca5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1654,7 +1654,7 @@ dependencies = [ [[package]] name = "dash-network" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "cbindgen 0.29.4", "grovedb-bincode", @@ -1665,7 +1665,7 @@ dependencies = [ [[package]] name = "dash-network-seeds" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "dash-network", ] @@ -1760,7 +1760,7 @@ dependencies = [ [[package]] name = "dash-spv" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "async-trait", "chrono", @@ -1789,7 +1789,7 @@ dependencies = [ [[package]] name = "dashcore" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "anyhow", "base64-compat", @@ -1815,12 +1815,12 @@ dependencies = [ [[package]] name = "dashcore-private" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" [[package]] name = "dashcore-rpc" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "dashcore-rpc-json", "hex", @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "dashcore-rpc-json" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "dashcore", "grovedb-bincode", @@ -1848,7 +1848,7 @@ dependencies = [ [[package]] name = "dashcore_hashes" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "dashcore-private", "grovedb-bincode", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "git-state" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" [[package]] name = "glob" @@ -4154,7 +4154,7 @@ dependencies = [ [[package]] name = "key-wallet" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "aes", "async-trait", @@ -4183,7 +4183,7 @@ dependencies = [ [[package]] name = "key-wallet-ffi" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "cbindgen 0.29.4", "dash-network", @@ -4199,7 +4199,7 @@ dependencies = [ [[package]] name = "key-wallet-manager" version = "0.45.0" -source = "git+https://github.com/dashpay/rust-dashcore?rev=538522bc6789defed276a358d7504bdd1ebb7656#538522bc6789defed276a358d7504bdd1ebb7656" +source = "git+https://github.com/dashpay/rust-dashcore?rev=40268cc0402a8933ec539f16b2d634c4e25876ad#40268cc0402a8933ec539f16b2d634c4e25876ad" dependencies = [ "async-trait", "dashcore", diff --git a/Cargo.toml b/Cargo.toml index 0d73bad6547..b3c14730fac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,14 +65,14 @@ grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8 grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } grovedb-commitment-tree = { git = "https://github.com/dashpay/grovedb", rev = "dce8252f8665bf06c7bd788e739efba354141690" } -dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } -dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "538522bc6789defed276a358d7504bdd1ebb7656" } +dashcore = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +dash-network-seeds = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +dash-spv = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +key-wallet = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +key-wallet-ffi = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +key-wallet-manager = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +dash-network = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } +dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore", rev = "40268cc0402a8933ec539f16b2d634c4e25876ad" } tokio-metrics = "0.5" # Size-tuned profile for the iOS `rs-unified-sdk-ffi` staticlib, which From 7c67e011ef88b38211137a7d5afec7fe5c780c7b Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 22:56:10 +0700 Subject: [PATCH 07/17] test(drive-abci): pin that a stored address of zero bytes reads back as absent The stored masternode list writes an absent owner, payout or collateral address as zero bytes and reads zero bytes back as absent, so a present address of zero bytes does not round-trip. Core never prints one (it refuses a null owner key for every masternode but a shared one, and nothing reads a stored payout or collateral address), so this is the intended collision; pin it with a test instead of leaving it to prose, and say on the collateral_address accessor that zero bytes mean absent. No behaviour change; the test passes before and after. Co-Authored-By: Claude Opus 5.5 --- .../platform_types/masternode/v0/accessors.rs | 3 ++- .../src/platform_types/masternode/v0/mod.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/accessors.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/accessors.rs index 6406323f28c..c48a8957993 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/accessors.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/accessors.rs @@ -11,7 +11,8 @@ pub trait MasternodeAccessorsV0 { fn collateral_hash(&self) -> Txid; /// The index of the collateral transaction output. fn collateral_index(&self) -> u32; - /// The address where the collateral is stored. + /// The address where the collateral is stored; zero bytes when it has none, as for a + /// shared masternode. fn collateral_address(&self) -> [u8; 20]; /// The amount of the operator's reward for running the masternode. fn operator_reward(&self) -> f32; diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index 2ecd3a87da5..c67b646feb8 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -340,4 +340,22 @@ mod tests { expected.state.payouts = None; assert_eq!(stored_and_loaded(&multi_payout), expected); } + + /// Zero bytes stand for an absent address in the stored list, so an address of zero bytes + /// reads back as absent. Core never prints one: it refuses a null owner key for every + /// masternode but a shared one, and nothing reads a stored payout or collateral address. + #[test] + fn should_reload_an_address_of_zero_bytes_as_absent() { + let zero = masternode( + MasternodeType::Regular, + Some([0u8; 20]), + Some([0u8; 20]), + Some([0u8; 20]), + None, + ); + let reloaded = stored_and_loaded(&zero); + assert_eq!(reloaded.collateral_address, None); + assert_eq!(reloaded.state.owner_address, None); + assert_eq!(reloaded.state.payout_address, None); + } } From b926396ce154778a06639a9db3d255dcab7763f2 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:07:41 +0700 Subject: [PATCH 08/17] fix(drive-abci): take evonode platform ports from nested addresses Core v24 keeps an ExtAddr evonode's platform ports in its network info. Its legacy scalar ports stay zero, and a diff lists the legacy `platformP2PPort` / `platformHTTPPort` fields only when those scalars change. A ProUpServTx that only moves the ports therefore reaches Drive as a diff carrying the change only in `addresses`, which Drive ignored: the validator kept its old port and Tenderdash kept dialling it. A validator built later from the state got the same stale port, port 0 for an evonode revoked after V24 and revived since, or no validator for an evonode first listed with `-1` ports. A port is now the nested entry, else the legacy field as it is, zero included (`PlatformPorts::of`: rust-dashcore's `platform_{p2p,http}_address()`, which fall back that way). Keeping a zero legacy port keeps validator-set membership what earlier binaries build: they make every evonode with both legacy ports a member, whatever their value, and Core 23 and 24 list a revoked legacy evonode with `addresses: {}` beside its flat ports. Validator-set membership is consensus-relevant: validator_set_update rotates the quorum when the proposer is the set's last member, banned members included, and the next quorum's hash is NextValidatorsHash. `MasternodeStateV0` stores neither `addresses` nor the payout list, so two invariants keep membership the same on every node: - After every masternode list update, the masternode lists in memory equal what a node that restarts loads from storage. - Validator membership is a function of the committed Core diff windows: every node asks Core for the same windows, fixed by the committed core heights, and applies them in the same order. One consequence: an ExtAddr evonode registered and revoked within one window arrives as an added item with `-1` ports and is not a member, while one revoked in a later window stays a banned member with its stored ports. Both follow from the committed windows, so every node agrees, and shipped binaries halt on the `-1` added item, so no earlier behaviour constrains it. - `insert_masternode` and `apply_masternode_state_diff` keep each state in stored form (`in_stored_form`: through `MasternodeStateV0`): the resolved ports move into the legacy port fields, `addresses` and `payouts` become `None`, and an owner or payout address of zero bytes reads as absent, exactly as after a reload. Identity updates are unaffected: they read added masternodes and diffs as Core reported them. `any_stored_masternode_changes` compares in the same form. - The stored state keeps the resolved ports, else the legacy value unchanged, even one outside the port range, as earlier binaries store it; a state loaded back is then already in stored form. - A validator built from the masternode state takes the resolved ports. - The validator refresh on a diff compares the ports resolved before and after it, reading the updated state rather than the diff, since an ExtAddr evonode's port move is only in `addresses`. It triggers as before (ban, service, or a P2P port change) and writes only the ports that changed, which for lists without nested ports, or with nested ports mirroring the legacy ones, is exactly what the legacy fields in the diff set before. - Drive logs at debug level, with the ProTx hash, an evonode left out of a validator set because its platform ports do not resolve. Only the port is taken. The validator host stays the `service` IP: a legacy evonode's port-only diff pairs its port with the placeholder host 255.255.255.255. In-place change to shipped generations: update_state_masternode_list v0 and the validator built from masternode state (every protocol version). Node addresses are not consensus-hashed: Tenderdash's ValidatorSet.Hash is merkle(threshold public key, quorum hash). Validator-set membership is unchanged for every input old binaries accept: an evonode is a member when it has both ports and a platform node id; a resolved port is present whenever the legacy field is, zero included; and every evonode old binaries can parse lists both legacy fields (Core before v24 prints them for every evonode; Core v24 prints them together with `service`, which old binaries require, and as `-1` for an evonode without network info, which old binaries reject). The update loop carries this justification as a comment. The test that a payment-only diff leaves the state clean now builds its state with insert_masternode, as Drive holds it: its zero-byte fixture addresses are not in stored form. Test would have caught this in CI. With this commit's port reading reverted to the parent's (legacy fields only, from the state and from the diff, and no stored-form normalization), these fail and pass here: - an ExtAddr evonode's port move as an addresses-only diff, with `service` too, and an HTTPS-only move: the validator keeps 36656 or 443 instead of 36657 or 8443; - should_move_the_validator_of_an_ext_addr_evonode_on_a_core_v24_port_only_diff runs Core v24 `protx listdiff` JSON (the new fixture core_v24_protx_list_diffs/1260-1270.json: `service` and the whole `addresses`, no legacy ports) through update_masternode_list. With the parent's source files it fails: the existing member and a new quorum's validator keep 36656/1443 instead of 36657/8443; - a state with zero legacy ports gets port 0 instead of 36656, and without legacy ports no validator; - the stored state keeps the stale legacy port 36656 instead of 36657; - should_hold_in_stored_form_exactly_what_the_store_reads_back: the in-memory state differs from what the store reads back (it also fails when only the stored-form normalization is removed); - should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads stores and reloads the platform state after each of an ExtAddr evonode's registration, addresses-only port move and revoke (legacy ports 0, no `addresses`); with only the stored-form normalization removed, the lists in memory differ from the reloaded ones after the registration; - should_not_advertise_an_evonode_whose_network_info_core_emptied pins the banned member a new quorum gets, with the ports it last had; with the parent's port reading it gets none; - should_store_legacy_ports_unchanged_without_nested_ports fails for a legacy value outside the port range when the stored form drops the legacy fallback. Guard tests, passing before and after: - should_build_the_validators_earlier_binaries_built_from_mainnet_masternode_lists: every evonode of the mainnet listdiff fixtures, applied the way Drive applies them, as listed and with `addresses` synthesized as Core 23 prints them (GetNetInfoWithLegacyFields for added items; the core entry, and changed ports on the service IP or the 255.255.255.255 placeholder, for diffs), gets the validator an earlier binary builds from the legacy fields alone, for a new quorum and as a refreshed member of an existing set. It passes with the parent's source files. It fails when the legacy fallback is dropped (as listed), and when the stored form skips the legacy fallback once `addresses` is listed (with Core 23 addresses only). - should_move_the_validator_of_a_legacy_evonode_on_a_core_v24_port_diff: a legacy evonode's port diff before V24 (legacy port plus a 255.255.255.255 entry, fixture core_v24_protx_list_diffs/before_v24; Core accepts a legacy evonode's ProUpServTx only before V24) moves the validator's P2P port and keeps its IP. - should_make_an_evonode_with_a_tor_primary_address_a_validator_on_the_unspecified_ip: an ExtAddr evonode whose primary address is Tor is a validator on `::` with the ports of its platform entries, and its list processes. - The placeholder host never becomes the validator host; a core-address-only diff keeps the platform ports, before and after a restart; zero legacy ports without nested ones keep the evonode a member with port 0; a revoked legacy evonode listed with `addresses: {}` builds the member its legacy ports give; a legacy evonode revoked after V24 gives, whether or not the node restarted, exactly the member earlier binaries build (banned, on `::`, ports 0) and the same stored state; every port test checks that a new quorum, built from memory or after a reload, gets the same member as the refreshed validator. Co-Authored-By: Claude Opus 5.5 --- .../update_masternode_list/mod.rs | 547 ++++++++++++++- .../update_state_masternode_list/v0/mod.rs | 646 +++++++++++++++++- .../src/platform_types/masternode/v0/mod.rs | 217 +++++- .../platform_state/accessors.rs | 30 +- .../src/platform_types/validator/v0/mod.rs | 251 ++++++- .../core_v24_protx_list_diffs/1260-1270.json | 68 ++ .../before_v24/1-1000.json | 45 ++ .../before_v24/1000-1010.json | 18 + 8 files changed, 1764 insertions(+), 58 deletions(-) create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json create mode 100644 packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index ef114d68582..3c4750508ec 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -78,19 +78,29 @@ mod test { }; use crate::platform_types::platform_state::PlatformStateV0Methods; use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; + use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; + use crate::platform_types::validator_set::ValidatorSet; use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; - use dpp::dashcore::ProTxHash; - use dpp::dashcore_rpc::json::MasternodeListDiff; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash}; + use dpp::dashcore_rpc::json::{ + DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; use dpp::identifier::MasternodeIdentifiers; use dpp::identity::accessors::IdentityGettersV0; use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dpp::identity::{KeyID, Purpose}; use dpp::prelude::Identifier; - use std::collections::BTreeMap; + use rand::rngs::StdRng; + use rand::SeedableRng; + use serde_json::{json, Value}; + use std::collections::{BTreeMap, BTreeSet}; use std::env; use std::fs::File; use std::io::BufReader; + use std::net::SocketAddr; use std::path::PathBuf; use std::str::FromStr; @@ -213,6 +223,319 @@ mod test { .expect("expected to commit"); } + /// A `protx listdiff` response from the mainnet fixtures. With + /// `core_23_addresses`, every masternode and diff also lists the + /// `addresses` Core 23 prints beside the legacy fields. `evonodes` + /// collects the ProTx hashes of the evonodes listed so far. + fn mainnet_list_diff( + file_name: &str, + core_23_addresses: bool, + evonodes: &mut BTreeSet, + ) -> MasternodeListDiff { + let json = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/supporting_files/mainnet_protx_list_diffs") + .join(file_name), + ) + .expect("expected to read the masternode list diff"); + let mut list_diff: Value = serde_json::from_str(&json).expect("expected JSON"); + for masternode in list_diff["addedMNs"] + .as_array_mut() + .expect("expected added masternodes") + { + let is_evonode = masternode["type"] == "Evo"; + if is_evonode { + let pro_tx_hash = masternode["proTxHash"].as_str().expect("a ProTx hash"); + evonodes.insert(pro_tx_hash.to_string()); + } + if core_23_addresses { + let addresses = core_23_state_addresses(&masternode["state"], is_evonode); + masternode["state"]["addresses"] = addresses; + } + } + if core_23_addresses { + for update in list_diff["updatedMNs"] + .as_array_mut() + .expect("expected updated masternodes") + { + for (pro_tx_hash, state_diff) in update.as_object_mut().expect("a diff") { + let is_evonode = evonodes.contains(pro_tx_hash); + if let Some(addresses) = core_23_diff_addresses(state_diff, is_evonode) { + state_diff["addresses"] = addresses; + } + } + } + } + serde_json::from_value(list_diff).expect("expected a masternode list diff") + } + + /// A listed `service`, `None` for a masternode without an address, which + /// Core lists as the unspecified address with port 0. + fn listed_service(service: &Value) -> Option { + let service: SocketAddr = service + .as_str() + .expect("a service") + .parse() + .expect("an ip:port service"); + (!(service.ip().is_unspecified() && service.port() == 0)).then_some(service) + } + + /// `port` on the IP of `service`, as Core prints a network address. + fn on_service_ip(service: SocketAddr, port: u16) -> String { + SocketAddr::new(service.ip(), port).to_string() + } + + fn listed_port(port: &Value) -> u16 { + port.as_u64() + .and_then(|port| u16::try_from(port).ok()) + .expect("a port") + } + + /// The `addresses` Core 23 lists for a masternode that predates extended + /// addresses (`GetNetInfoWithLegacyFields`): its core entry is `service`, + /// and an evonode's platform entries pair the IP of `service` with the + /// legacy ports. A masternode without an address lists no entry. + fn core_23_state_addresses(state: &Value, is_evonode: bool) -> Value { + let Some(service) = listed_service(&state["service"]) else { + return json!({}); + }; + let mut addresses = json!({ "core_p2p": [service.to_string()] }); + if is_evonode { + let http_port = listed_port(&state["platformHTTPPort"]); + let p2p_port = listed_port(&state["platformP2PPort"]); + addresses["platform_https"] = json!([on_service_ip(service, http_port)]); + addresses["platform_p2p"] = json!([on_service_ip(service, p2p_port)]); + } + addresses + } + + /// The `addresses` Core 23 lists in the diff of a masternode that + /// predates extended addresses (`CDeterministicMNStateDiff::ToJson`): with + /// a changed `service`, its core entry; for an evonode, each changed + /// platform port, on the IP of the changed `service`, else on the + /// placeholder host 255.255.255.255. `None` when that lists nothing. + fn core_23_diff_addresses(state_diff: &Value, is_evonode: bool) -> Option { + let service: Option = state_diff.get("service").map(|service| { + service + .as_str() + .expect("a service") + .parse() + .expect("an ip:port service") + }); + let mut addresses = serde_json::Map::new(); + if let Some(service) = state_diff.get("service").and_then(listed_service) { + addresses.insert("core_p2p".to_string(), json!([service.to_string()])); + } + if is_evonode { + for (port_field, purpose) in [ + ("platformP2PPort", "platform_p2p"), + ("platformHTTPPort", "platform_https"), + ] { + if let Some(port) = state_diff.get(port_field).map(listed_port) { + let entry = match service { + Some(service) => on_service_ip(service, port), + None => format!("255.255.255.255:{port}"), + }; + addresses.insert(purpose.to_string(), json!([entry])); + } + } + } + (!addresses.is_empty()).then_some(Value::Object(addresses)) + } + + /// The validator every binary before nested addresses builds for a listed + /// evonode: its ports are the legacy port fields as listed, and it is a + /// validator only with both of them and a platform node id. + fn validator_from_legacy_ports(evonode: &MasternodeListItem) -> Option { + let state = &evonode.state; + #[allow(deprecated)] + let (p2p_port, http_port) = ( + state.legacy_platform_p2p_port?, + state.legacy_platform_http_port?, + ); + Some(ValidatorV0 { + pro_tx_hash: evonode.pro_tx_hash, + public_key: None, + node_ip: state.service.ip().to_string(), + node_id: PubkeyHash::from_byte_array(state.platform_node_id?), + core_port: state.service.port(), + platform_http_port: http_port as u16, + platform_p2p_port: p2p_port as u16, + is_banned: state.pose_ban_height.is_some(), + }) + } + + /// How every binary before nested addresses refreshes a validator-set + /// member from its evonode's diff: on a ban, `service` or legacy P2P port + /// change, it takes the ban, the IP and the legacy ports the diff lists. + fn refresh_from_legacy_ports(member: &mut ValidatorV0, state_diff: &DMNStateDiff) { + #[allow(deprecated)] + let (p2p_port, http_port) = ( + state_diff.legacy_platform_p2p_port, + state_diff.legacy_platform_http_port, + ); + if state_diff.pose_ban_height.is_none() + && state_diff.service.is_none() + && p2p_port.is_none() + { + return; + } + if let Some(pose_ban_height) = state_diff.pose_ban_height { + member.is_banned = pose_ban_height.is_some(); + } + if let Some(service) = state_diff.service { + member.node_ip = service.ip().to_string(); + } + if let Some(p2p_port) = p2p_port { + member.platform_p2p_port = p2p_port as u16; + } + if let Some(http_port) = http_port { + member.platform_http_port = http_port as u16; + } + } + + /// Asserts that `state` lists exactly the `listed` evonodes and gives each + /// the validator binaries before nested addresses build for it. + fn assert_validators_from_legacy_ports( + state: &PlatformState, + listed: &BTreeMap, + context: &str, + ) { + assert!( + state.hpmn_masternode_list().keys().eq(listed.keys()), + "{context}: the same evonodes are listed" + ); + for (pro_tx_hash, evonode) in listed { + assert_eq!( + ValidatorV0::new_validator_if_masternode_in_state(*pro_tx_hash, None, state), + validator_from_legacy_ports(evonode), + "{context}: evonode {pro_tx_hash}" + ); + } + } + + /// Validator-set membership decides quorum rotation, and every binary + /// before nested addresses built it from the legacy port fields alone. + /// For every mainnet evonode, as the fixtures list it and with the + /// `addresses` Core 23 lists beside the legacy fields, the validator a new + /// quorum gets and the member an existing set refreshes from a diff must + /// be exactly what those binaries build. + #[test] + fn should_build_the_validators_earlier_binaries_built_from_mainnet_masternode_lists() { + let platform_version = PlatformVersion::latest(); + let init_core_height = 2128896; + let next_core_height = 2129440; + let quorum_hash = QuorumHash::from_byte_array([0x55; 32]); + + for core_23_addresses in [false, true] { + let context = if core_23_addresses { + "with Core 23 addresses" + } else { + "as listed" + }; + let mut evonodes = BTreeSet::new(); + let init_list = mainnet_list_diff("1-2128896.json", core_23_addresses, &mut evonodes); + let list_diff = + mainnet_list_diff("2128896-2129440.json", core_23_addresses, &mut evonodes); + assert!( + list_diff + .updated_mns + .iter() + .any(|(pro_tx_hash, state_diff)| { + evonodes.contains(&pro_tx_hash.to_string()) && state_diff.service.is_some() + }), + "the diff moves an evonode" + ); + + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + let responses = [init_list.clone(), list_diff.clone()]; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| { + Ok(responses[usize::from(block != init_core_height)].clone()) + }); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Mainnet), + ) + .expect("platform state"); + + // The evonodes as every earlier binary holds them. + let is_evonode = + |masternode: &&MasternodeListItem| masternode.node_type == MasternodeType::Evo; + let mut listed: BTreeMap = init_list + .added_mns + .iter() + .filter(is_evonode) + .map(|evonode| (evonode.pro_tx_hash, evonode.clone())) + .collect(); + + platform + .update_state_masternode_list_v0(&mut state, init_core_height, true) + .expect("expected to apply the masternode list"); + assert_validators_from_legacy_ports(&state, &listed, context); + + let members: BTreeMap = listed + .iter() + .filter_map(|(pro_tx_hash, evonode)| { + validator_from_legacy_ports(evonode).map(|member| (*pro_tx_hash, member)) + }) + .collect(); + assert_eq!( + members.len(), + listed.len(), + "{context}: every evonode is a member" + ); + state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: init_core_height, + members: members.clone(), + threshold_public_key: SecretKey::::random( + &mut StdRng::seed_from_u64(1), + ) + .public_key(), + }), + ); + + platform + .update_state_masternode_list_v0(&mut state, next_core_height, false) + .expect("expected to apply the masternode list diff"); + + let mut refreshed_members = members.clone(); + for evonode in list_diff.added_mns.iter().filter(is_evonode) { + listed.insert(evonode.pro_tx_hash, evonode.clone()); + } + for (pro_tx_hash, state_diff) in &list_diff.updated_mns { + if let Some(evonode) = listed.get_mut(pro_tx_hash) { + evonode.state.apply_diff(state_diff.clone()); + } + if let Some(member) = refreshed_members.get_mut(pro_tx_hash) { + refresh_from_legacy_ports(member, state_diff); + } + } + for pro_tx_hash in &list_diff.removed_mns { + listed.remove(pro_tx_hash); + refreshed_members.remove(pro_tx_hash); + } + + assert_validators_from_legacy_ports(&state, &listed, context); + assert_ne!( + refreshed_members, members, + "{context}: the diff refreshes members" + ); + assert_eq!( + state.validator_sets()[&quorum_hash].members(), + &refreshed_members, + "{context}: the refreshed members" + ); + } + } + /// Dash Core v24 prints masternodes that differ from every earlier list: a shared /// masternode has no owner, payout or collateral address; an extended-address masternode /// has a `payouts` list instead of `payoutAddress`; a Tor primary address is not an @@ -469,4 +792,222 @@ mod test { ); } } + + /// The quorum of the validator set `state_after_core_v24_lists` adds. + const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55; 32]; + + /// Runs one masternode list update per `(core height, fixture)`, in order, + /// as Drive runs one per block, with Core reporting the fixture's + /// `protx listdiff` JSON from `core_v24_protx_list_diffs` for that height. + /// Before the last update, one validator set lists every evonode that is a + /// validator then. Returns the state after the last update. + fn state_after_core_v24_lists(fixtures: &[(u32, &str)]) -> PlatformState { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig::default()) + .build_with_mock_rpc() + .set_genesis_state(); + + let lists: BTreeMap = fixtures + .iter() + .map(|(core_height, file_name)| { + let json = std::fs::read_to_string( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/supporting_files/core_v24_protx_list_diffs") + .join(file_name), + ) + .expect("expected to read the masternode list diff"); + (*core_height, json) + }) + .collect(); + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| Ok(serde_json::from_str(&lists[&block])?)); + + let transaction = platform.drive.grove.start_transaction(); + let mut platform_state: Option = None; + let mut block_platform_state = platform.state.load().as_ref().clone(); + for (index, (core_height, _)) in fixtures.iter().enumerate() { + let block = index as u64 + 1; + if block == fixtures.len() as u64 { + let members = block_platform_state + .hpmn_masternode_list() + .keys() + .filter_map(|pro_tx_hash| { + ValidatorV0::new_validator_if_masternode_in_state( + *pro_tx_hash, + None, + &block_platform_state, + ) + .map(|member| (*pro_tx_hash, member)) + }) + .collect(); + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + block_platform_state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: *core_height, + members, + threshold_public_key: SecretKey::::random( + &mut StdRng::seed_from_u64(1), + ) + .public_key(), + }), + ); + } + platform + .update_masternode_list( + platform_state.as_ref(), + &mut block_platform_state, + *core_height, + index == 0, + &BlockInfo { + height: block, + core_height: *core_height, + time_ms: block, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list"); + platform_state = Some(block_platform_state.clone()); + } + block_platform_state + } + + /// The validator of `pro_tx_hash` in the validator set + /// `state_after_core_v24_lists` adds, and the one a new quorum gets. + fn existing_and_new_validator( + state: &PlatformState, + pro_tx_hash: &str, + ) -> (Option, Option) { + let pro_tx_hash = ProTxHash::from_str(pro_tx_hash).expect("expected a pro tx hash"); + let validator_set = + &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; + ( + validator_set.members().get(&pro_tx_hash).cloned(), + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), + ) + } + + /// An ExtAddr evonode keeps its platform ports in its network info, so a + /// ProUpServTx that only moves them reaches Drive as a diff with `service` + /// and the whole `addresses`, and no legacy port field. Tenderdash must be + /// told the new ports, by the existing validator set and by one built + /// later, or it keeps dialling the old ones. + #[test] + fn should_move_the_validator_of_an_ext_addr_evonode_on_a_core_v24_port_only_diff() { + let state = state_after_core_v24_lists(&[ + (1250, "1-1250.json"), + (1260, "1250-1260.json"), + (1270, "1260-1270.json"), + ]); + + let expected = ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "192.0.2.40".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("9e391c2c041a122a779bafe09d6c47ea600dfcbe") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 9999, + platform_http_port: 8443, + platform_p2p_port: 36657, + is_banned: false, + }; + assert_eq!( + existing_and_new_validator( + &state, + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0" + ), + (Some(expected.clone()), Some(expected)) + ); + } + + /// Core lists an evonode whose primary core address is a Tor address with + /// that address as `service`, which reads as `[::]:0`. It becomes a + /// validator on the unspecified IP with the ports of its platform entries: + /// unreachable, but its list does not fail the block. + #[test] + fn should_make_an_evonode_with_a_tor_primary_address_a_validator_on_the_unspecified_ip() { + let state = state_after_core_v24_lists(&[ + (1250, "1-1250.json"), + (1260, "1250-1260.json"), + (1270, "1260-1270.json"), + ]); + + let (_, new_quorum_validator) = existing_and_new_validator( + &state, + "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + ); + assert_eq!( + new_quorum_validator, + Some(ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("9cea9116b333eba7631804e1be7efd62c56d5b58") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 26656, + is_banned: false, + }) + ); + } + + /// Before V24, a ProUpServTx that only moves a legacy evonode's P2P port + /// reaches Drive as the legacy port field and a platform entry on the + /// placeholder host 255.255.255.255, since the diff carries no address. + /// The validator takes the new port and keeps the evonode's IP. + #[test] + fn should_move_the_validator_of_a_legacy_evonode_on_a_core_v24_port_diff() { + let state = state_after_core_v24_lists(&[ + (1000, "before_v24/1-1000.json"), + (1010, "before_v24/1000-1010.json"), + ]); + + let expected = ValidatorV0 { + pro_tx_hash: ProTxHash::from_str( + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76", + ) + .expect("expected a pro tx hash"), + public_key: None, + node_ip: "192.0.2.60".to_string(), + node_id: PubkeyHash::from_byte_array( + hex::decode("416e5b04d3047def070bea5175431ca7732b4b7b") + .expect("expected hex") + .try_into() + .expect("expected 20 bytes"), + ), + core_port: 9999, + platform_http_port: 443, + platform_p2p_port: 36657, + is_banned: false, + }; + assert_eq!( + existing_and_new_validator( + &state, + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76" + ), + (Some(expected.clone()), Some(expected)) + ); + } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs index 1138c3aad35..97f41c08ea5 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs @@ -1,9 +1,11 @@ use crate::error::Error; use crate::execution::types::update_state_masternode_list_outcome; +use crate::platform_types::masternode::v0::apply_diff_in_stored_form; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; +use crate::platform_types::validator::v0::PlatformPorts; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; use crate::rpc::core::CoreRPCLike; use dpp::dashcore::{ProTxHash, QuorumHash}; @@ -42,19 +44,21 @@ where /// Updates a masternode in the validator sets. /// /// This function updates the properties of the masternode that matches the given `pro_tx_hash`. - /// The properties are updated based on the provided `dmn_state_diff` information. - /// If a matching masternode is found, the function updates its ban status, service address, - /// platform P2P port, and platform HTTP port accordingly. + /// The ban status and service address are updated based on the provided `dmn_state_diff` + /// information, the platform P2P and HTTP ports from `new_platform_ports`. /// /// # Arguments /// /// * `pro_tx_hash` - The `ProTxHash` of the masternode to be updated /// * `dmn_state_diff` - The `DMNStateDiff` containing the updated masternode information + /// * `new_platform_ports` - The platform ports that resolve to a new value in the updated + /// masternode state /// * `state` - The platform state whose validator sets are updated; only the /// sets that list the masternode are touched fn update_masternode_in_validator_sets( pro_tx_hash: &ProTxHash, dmn_state_diff: &DMNStateDiff, + new_platform_ports: PlatformPorts, state: &mut PlatformState, ) { for quorum_hash in Self::validator_sets_with_member(state, pro_tx_hash) { @@ -72,18 +76,28 @@ where validator.node_ip = address.ip().to_string(); } - #[allow(deprecated)] - if let Some(p2p_port) = dmn_state_diff.legacy_platform_p2p_port { - validator.platform_p2p_port = p2p_port as u16; + if let Some(p2p_port) = new_platform_ports.p2p { + validator.platform_p2p_port = p2p_port; } - #[allow(deprecated)] - if let Some(http_port) = dmn_state_diff.legacy_platform_http_port { - validator.platform_http_port = http_port as u16; + if let Some(http_port) = new_platform_ports.http { + validator.platform_http_port = http_port; } } } + /// The platform ports an evonode's state resolves to, or `None` for a + /// masternode that is not an evonode. + fn evonode_platform_ports( + state: &PlatformState, + pro_tx_hash: &ProTxHash, + ) -> Option { + state + .hpmn_masternode_list() + .get(pro_tx_hash) + .map(|evonode| PlatformPorts::of(&evonode.state)) + } + /// Whether applying `updated_mns` would change any item of `masternode_list`. /// /// Core reports a masternode as updated whenever any field of its state @@ -97,9 +111,7 @@ where ) -> bool { updated_mns.iter().any(|(pro_tx_hash, state_diff)| { masternode_list.get(pro_tx_hash).is_some_and(|item| { - let mut updated = item.state.clone(); - updated.apply_diff(state_diff.clone()); - updated != item.state + apply_diff_in_stored_form(&item.state, state_diff) != item.state }) }) } @@ -164,22 +176,44 @@ where state.insert_masternode(masternode.clone()); } + // Every protocol version runs this code. Validator node addresses are + // not consensus state: Tenderdash hashes a validator set as + // merkle(threshold public key, quorum hash). Membership is, because + // quorum rotation keys off the set's members, and it stays what + // earlier binaries built for every masternode list they accept: an + // evonode is a member when it has a platform node id and both ports, a + // port resolves whenever its legacy field is present and within the + // port range (every legacy port Core can print fits a u16), and every + // evonode in such a list has both legacy fields. for (pro_tx_hash, state_diff) in updated_mns { - let is_hpmn = state.hpmn_masternode_list().contains_key(pro_tx_hash); + let platform_ports_before = Self::evonode_platform_ports(state, pro_tx_hash); if !state.apply_masternode_state_diff(pro_tx_hash, state_diff) { continue; } - // these 3 fields are the only fields that are useful for validators. If they change we need to update - // validator sets - #[allow(deprecated)] - let platform_p2p_port_changed = state_diff.legacy_platform_p2p_port.is_some(); - if is_hpmn - && (state_diff.pose_ban_height.is_some() - || state_diff.service.is_some() - || platform_p2p_port_changed) + // Only evonodes are validators. + let (Some(platform_ports_before), Some(platform_ports)) = ( + platform_ports_before, + Self::evonode_platform_ports(state, pro_tx_hash), + ) else { + continue; + }; + // The ports are read from the updated state, not from the diff: an + // ExtAddr evonode's port move reaches us only in `addresses`, and a + // legacy evonode's diff lists there just the entries that changed. + let new_platform_ports = changed_platform_ports(platform_ports_before, platform_ports); + // the ban status, the IP and the platform P2P port are the only fields that are useful for + // validators. If they change we need to update validator sets + if state_diff.pose_ban_height.is_some() + || state_diff.service.is_some() + || new_platform_ports.p2p.is_some() { // we updated the ban status the IP or the platform port, we need to update the validator in the validator list - Self::update_masternode_in_validator_sets(pro_tx_hash, state_diff, state); + Self::update_masternode_in_validator_sets( + pro_tx_hash, + state_diff, + new_platform_ports, + state, + ); } } @@ -206,19 +240,42 @@ where } } +/// The ports of `after` that resolve to a value other than in `before`. +/// +/// A port that stops resolving is not reported, so the validator keeps its +/// last known port. +fn changed_platform_ports(before: PlatformPorts, after: PlatformPorts) -> PlatformPorts { + PlatformPorts { + p2p: after.p2p.filter(|_| after.p2p != before.p2p), + http: after.http.filter(|_| after.http != before.http), + } +} + #[cfg(test)] mod tests { - use crate::config::PlatformConfig; + use crate::config::{PlatformConfig, PlatformTestConfig}; + use crate::platform_types::masternode::v0::MasternodeV0; + use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; + use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, PlatformPorts}; + use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; + use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; + use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator::v0::ValidatorV0; + use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; - use dpp::dashcore::{Network, ProTxHash, Txid}; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ - DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + DMNPayout, DMNState, DMNStateDiff, MasternodeAddresses, MasternodeListDiff, + MasternodeListItem, MasternodeType, }; use dpp::version::PlatformVersion; - use std::collections::BTreeSet; + use rand::rngs::StdRng; + use rand::SeedableRng; + use std::collections::{BTreeMap, BTreeSet}; fn masternode(pro_tx_hash: ProTxHash) -> MasternodeListItem { MasternodeListItem { @@ -296,9 +353,7 @@ mod tests { &PlatformConfig::default_for_network(Network::Testnet), ) .expect("platform state"); - state - .full_masternode_list_mut() - .insert(pro_tx_hash, masternode(pro_tx_hash)); + state.insert_masternode(masternode(pro_tx_hash)); state.mark_saved(); platform @@ -452,4 +507,537 @@ mod tests { assert!(state.masternode_changes.removed.is_empty()); assert!(!state.masternode_changes.rewrite_all); } + + /// The IP every test evonode is registered with. + const SERVICE_IP: &str = "1.2.3.4"; + const EVONODE_PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55u8; 32]; + + fn evonode_pro_tx_hash() -> ProTxHash { + ProTxHash::from_byte_array(EVONODE_PRO_TX_HASH) + } + + /// An evonode with platform ports 36656 (P2P) and 443 (HTTPS), as Core + /// lists it with `-deprecatedrpc=service`: the legacy port fields carry + /// the live ports and `addresses` repeats them. + fn evonode() -> MasternodeListItem { + let mut item = masternode(evonode_pro_tx_hash()); + item.node_type = MasternodeType::Evo; + item.state.platform_node_id = Some([7u8; 20]); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = Some(36656); + item.state.legacy_platform_http_port = Some(443); + } + item.state.addresses = Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:443".to_string()], + }); + item + } + + /// The same evonode in a state whose legacy ports are absent: the ports + /// are only in `addresses`. + fn evonode_without_legacy_ports() -> MasternodeListItem { + let mut item = evonode(); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = None; + item.state.legacy_platform_http_port = None; + } + item + } + + /// `item` as a node that restarted holds it: loaded back from storage. + fn reloaded(item: MasternodeListItem) -> MasternodeListItem { + MasternodeV0::from(item).into() + } + + /// Runs one masternode list update per diff, in order, for a state + /// holding `evonode` as a member of one validator set, with Core reporting + /// the diffs for it, and returns the state afterwards. + fn state_after_diffs(evonode: MasternodeListItem, diffs: Vec) -> PlatformState { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + + let pro_tx_hash = evonode.pro_tx_hash; + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + state.insert_masternode(evonode); + let validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: SERVICE_IP.to_string(), + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: false, + }; + let mut rng = StdRng::seed_from_u64(1); + state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: 1, + members: BTreeMap::from([(pro_tx_hash, validator)]), + threshold_public_key: SecretKey::::random(&mut rng).public_key(), + }), + ); + state.mark_saved(); + + let block_count = diffs.len() as u32; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, diffs[block as usize - 1].clone())], + }) + }); + + for block in 1..=block_count { + platform + .update_state_masternode_list_v0(&mut state, block, false) + .expect("update must succeed"); + } + + state + } + + /// The evonode's member of the validator set. + fn validator_in(state: &PlatformState) -> ValidatorV0 { + state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)].members() + [&evonode_pro_tx_hash()] + .clone() + } + + /// The validator a quorum Drive learns about later gets for the evonode, + /// once from the state as held in memory and once as a node that + /// restarted loads it back. Validator-set membership decides quorum + /// rotation, so every node must build the same one. + fn validators_for_a_new_quorum(state: &PlatformState) -> [Option; 2] { + let pro_tx_hash = evonode_pro_tx_hash(); + let mut restarted = state.clone(); + restarted.insert_masternode(reloaded(state.full_masternode_list()[&pro_tx_hash].clone())); + [state, &restarted].map(|state| { + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state) + }) + } + + /// Asserts that the evonode's member of the existing validator set has + /// the given ports, and that a new quorum gets the same member. + fn assert_validator_ports(state: &PlatformState, p2p_port: u16, http_port: u16) { + let validator = validator_in(state); + assert_eq!(validator.node_ip, SERVICE_IP); + assert_eq!(validator.platform_p2p_port, p2p_port); + assert_eq!(validator.platform_http_port, http_port); + for new_quorum_validator in validators_for_a_new_quorum(state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// The network info of the evonode after moving to platform ports 36657 + /// (P2P) and 8443 (HTTPS), as Core prints an ExtAddr one. + fn moved_platform_ports() -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36657".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + } + } + + /// A node that restarts reloads its masternode lists from storage, and + /// validator-set membership, which decides quorum rotation, must not + /// depend on whether it did. So after every masternode list update the + /// lists held in memory must be exactly what storage gives back. The + /// updates: an ExtAddr evonode added with its platform ports in + /// `addresses` and a payout list, a port move that only `addresses` + /// carries, then a revoke that zeroes the legacy ports and carries no + /// `addresses`. + #[test] + fn should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads() { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig { + testing_configs: PlatformTestConfig { + store_platform_state: true, + ..PlatformTestConfig::default_minimal_verifications() + }, + ..Default::default() + }) + .build_with_mock_rpc() + .set_genesis_state(); + + let mut added = evonode(); + added.collateral_address = Some([0x21; 20]); + added.state.owner_address = Some([0x22; 20]); + added.state.payout_address = None; + added.state.payouts = Some(vec![DMNPayout { + address: [0x23; 20], + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array([0x23; 20])), + reward: 10000, + }]); + let pro_tx_hash = added.pro_tx_hash; + let port_move = DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }; + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(3)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + let (added_mns, updated_mns) = match block { + 1 => (vec![added.clone()], vec![]), + 2 => (vec![], vec![(pro_tx_hash, port_move.clone())]), + _ => (vec![], vec![(pro_tx_hash, revoke.clone())]), + }; + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns, + removed_mns: vec![], + updated_mns, + }) + }); + + let transaction = platform.drive.grove.start_transaction(); + let mut state = platform.state.load().as_ref().clone(); + // The ports and ban each update leaves the evonode with. + let expected = [ + (Some(36656), Some(443), false), + (Some(36657), Some(8443), false), + (Some(0), Some(0), true), + ]; + for (block, (p2p, http, is_banned)) in (1..).zip(expected) { + platform + .update_state_masternode_list_v0(&mut state, block, false) + .expect("update must succeed"); + platform + .store_platform_state(&state, Some(&transaction), platform_version) + .expect("store platform state"); + state.mark_saved(); + let reloaded = Platform::::fetch_platform_state( + &platform.drive, + Some(&transaction), + platform_version, + ) + .expect("fetch platform state") + .expect("a state was stored"); + + assert_eq!( + reloaded.full_masternode_list(), + state.full_masternode_list(), + "the full list after block {block}" + ); + assert_eq!( + reloaded.hpmn_masternode_list(), + state.hpmn_masternode_list(), + "the evonode list after block {block}" + ); + let evonode = &reloaded.hpmn_masternode_list()[&pro_tx_hash].state; + assert_eq!( + ( + PlatformPorts::of(evonode), + evonode.pose_ban_height.is_some() + ), + (PlatformPorts { p2p, http }, is_banned), + "the evonode after block {block}" + ); + } + } + + /// An ExtAddr evonode keeps its platform ports in its network info and its + /// legacy port fields at zero, so a ProUpServTx that only moves its ports + /// reaches Drive as a diff whose only network field is `addresses` + /// (`service` too only with `-deprecatedrpc=service`). Tenderdash must be + /// told the new port, or it keeps dialling the old one. + #[test] + fn should_update_validator_ports_from_an_addresses_only_diff() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// The same port move as Core prints it with `-deprecatedrpc=service`: + /// `service` repeats because the network info changed, while the legacy + /// port fields are absent because they did not change. + #[test] + fn should_update_validator_ports_from_addresses_when_the_diff_also_carries_service() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// An ExtAddr evonode that moves only its HTTPS port lists its whole + /// network info again, the unchanged P2P entry included. + #[test] + fn should_update_the_validator_https_port_when_only_it_moved() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 8443); + } + + /// When a legacy evonode changes only a platform port, the diff does not + /// carry the node's IP, so Core pairs the new port in `addresses` with the + /// placeholder host 255.255.255.255. Only the port may be taken from it. + #[test] + fn should_not_take_the_placeholder_host_from_a_legacy_evonode_port_diff() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 443); + } + + /// A legacy evonode's diff lists in `addresses` only the entries that + /// changed, next to the changed legacy port field. A later diff that + /// changes the other port must not undo an earlier one's port. + #[test] + fn should_keep_each_platform_port_from_diffs_that_list_one_entry_each() { + let state = state_after_diffs( + evonode(), + vec![ + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }, + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_http_port: Some(8443), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec![], + platform_https: vec!["255.255.255.255:8443".to_string()], + })), + ..empty_state_diff() + }, + ], + ); + + assert_eq!(validator_in(&state).platform_p2p_port, 36657); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + let new_quorum_validator = new_quorum_validator.expect("the evonode has ports"); + assert_eq!(new_quorum_validator.platform_p2p_port, 36657); + assert_eq!(new_quorum_validator.platform_http_port, 8443); + } + } + + /// A diff of a legacy evonode's core address carries `addresses` with the + /// core entry only, so the platform ports must not be read as cleared. + #[test] + fn should_keep_validator_ports_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("5.6.7.8:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["5.6.7.8:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + let validator = validator_in(&state); + assert_eq!(validator.node_ip, "5.6.7.8"); + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// A node that restarted holds the evonode without `addresses`. A diff + /// that lists only the core entry must leave its platform ports as they + /// are, as on a node that kept its state in memory. + #[test] + fn should_keep_validator_ports_after_a_restart_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + reloaded(evonode()), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 443); + } + + /// After V24 a legacy evonode's revoke, or an operator key change, makes + /// it ExtAddr with an empty network info: Core prints its platform ports + /// as 0 and no `addresses`. Earlier binaries accept that diff, so every + /// node must build the validator they build from the legacy fields: a + /// banned member on the unspecified IP with ports 0. A node that kept the + /// evonode's `addresses` in memory and a node that restarted must both + /// build it, because validator-set membership decides quorum rotation. + #[test] + fn should_agree_on_the_validator_after_a_legacy_evonode_is_revoked_whether_or_not_the_node_restarted( + ) { + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + let kept = state_after_diffs(evonode(), vec![revoke.clone()]); + let restarted = state_after_diffs(reloaded(evonode()), vec![revoke]); + + let pro_tx_hash = evonode_pro_tx_hash(); + let new_quorum_validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 0, + platform_p2p_port: 0, + is_banned: true, + }; + // A refresh of an existing member takes the ban, the IP and the ports + // from the diff, and keeps its node id and core port. + let refreshed_member = ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + }; + for state in [&kept, &restarted] { + assert_eq!( + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), + Some(new_quorum_validator.clone()) + ); + assert_eq!(validator_in(state), refreshed_member); + } + let stored = |state: &PlatformState| { + MasternodeV0::from(state.full_masternode_list()[&pro_tx_hash].clone()).state + }; + assert_eq!(stored(&kept), stored(&restarted)); + } + + /// When Core resets an ExtAddr evonode's operator fields (ProUpRevTx, or + /// a ProUpRegTx that changes the operator key), its network info becomes + /// empty: the diff has `service` `[::]:0` and no `addresses` key, so the + /// platform ports keep resolving to the old endpoint. That endpoint is + /// never advertised, because Core bans the masternode in the same + /// transition and only a ProUpServTx, which sends new `addresses`, revives + /// it. The evonode stays a member, banned, with the ports it last had. + #[test] + fn should_not_advertise_an_evonode_whose_network_info_core_emptied() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + ..empty_state_diff() + }], + ); + + let validator_set = + &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; + let new_quorum_validator = ValidatorV0 { + pro_tx_hash: evonode_pro_tx_hash(), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: true, + }; + assert_eq!( + validator_set.members()[&evonode_pro_tx_hash()], + ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + } + ); + assert!( + validator_set.to_update().validator_updates.is_empty(), + "a banned validator is left out of the validator set update" + ); + for validator in validators_for_a_new_quorum(&state) { + assert_eq!( + validator, + Some(new_quorum_validator.clone()), + "a validator set built later gets the same banned member" + ); + } + } } diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index c67b646feb8..c3ad7d54420 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -2,7 +2,7 @@ pub mod accessors; use dpp::bincode::{Decode, Encode}; -use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeType}; +use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, DMNStateDiff, MasternodeType}; use dpp::dashcore_rpc::json::MasternodeListItem; use std::fmt::{Debug, Formatter}; @@ -154,9 +154,34 @@ pub struct MasternodeStateV0 { pub platform_http_port: Option, } +/// `state` in the form Drive keeps masternode states in, in memory as in +/// storage: `MasternodeStateV0` has no `addresses`, so the platform ports +/// resolved from them move into the legacy port fields and `addresses` is +/// dropped. It has no payout list either, so `payouts` is dropped too. +/// +/// A node that restarted loads its masternode states in this form. Keeping +/// them in it in memory too means every node derives the same validators +/// from the same Core diffs, however long it has run. +pub(crate) fn in_stored_form(state: DMNState) -> DMNState { + MasternodeStateV0::from(state).into() +} + +/// `state`, kept in stored form, with a Core state diff applied, in stored +/// form again. +pub(crate) fn apply_diff_in_stored_form(state: &DMNState, diff: &DMNStateDiff) -> DMNState { + let mut updated = state.clone(); + updated.apply_diff(diff.clone()); + in_stored_form(updated) +} + impl From for MasternodeStateV0 { fn from(value: DMNState) -> Self { - // The payout list and the nested addresses are not stored. + // The payout list and the nested addresses are not stored. The stored state keeps the + // platform ports resolved from the addresses instead: an ExtAddr evonode's legacy port + // fields stay zero and go stale when its ports move. Loaded back as legacy ports, they + // resolve to the same ports. Without a nested port the legacy field is stored as it is. + let resolved_platform_p2p_port = value.platform_p2p_address().map(|(_, port)| port); + let resolved_platform_http_port = value.platform_http_address().map(|(_, port)| port); #[allow(deprecated)] let DMNState { service, @@ -188,8 +213,8 @@ impl From for MasternodeStateV0 { pub_key_operator, operator_payout_address, platform_node_id, - platform_p2p_port, - platform_http_port, + platform_p2p_port: resolved_platform_p2p_port.or(platform_p2p_port), + platform_http_port: resolved_platform_http_port.or(platform_http_port), } } } @@ -235,9 +260,11 @@ impl From for DMNState { #[cfg(test)] mod tests { + use super::*; use crate::platform_types::platform_state::platform_state_for_saving::v2::{ deserialize_masternode_entry, serialize_masternode_entry, }; + use crate::platform_types::validator::v0::PlatformPorts; use dpp::dashcore::hashes::Hash; use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ @@ -358,4 +385,186 @@ mod tests { assert_eq!(reloaded.state.owner_address, None); assert_eq!(reloaded.state.payout_address, None); } + + /// An evonode registered at 1.2.3.4 with the given legacy platform ports + /// and nested addresses. + fn evonode_state( + legacy_platform_p2p_port: Option, + legacy_platform_http_port: Option, + addresses: Option, + ) -> DMNState { + #[allow(deprecated)] + DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: Some([7u8; 20]), + legacy_platform_p2p_port, + legacy_platform_http_port, + addresses, + } + } + + fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![p2p.to_string()], + platform_https: vec![https.to_string()], + } + } + + /// The stored state keeps no `addresses`, so it must keep the ports they + /// carry: an ExtAddr evonode's legacy port fields stay zero, and after a + /// port move they are stale. + #[test] + fn should_store_the_platform_ports_from_addresses() { + let stored = MasternodeStateV0::from(evonode_state( + Some(36656), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + )); + + assert_eq!(stored.platform_p2p_port, Some(36657)); + assert_eq!(stored.platform_http_port, Some(8443)); + } + + /// A node that restarts must resolve the same platform ports as one that + /// kept its state in memory, or it advertises stale validator ports. + #[test] + fn should_resolve_the_same_platform_ports_after_a_store_and_load() { + let in_memory = evonode_state( + Some(36656), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ); + + let loaded = DMNState::from(MasternodeStateV0::from(in_memory.clone())); + + assert_eq!(PlatformPorts::of(&loaded), PlatformPorts::of(&in_memory)); + assert_eq!( + in_stored_form(loaded.clone()), + loaded, + "a loaded state is already in stored form" + ); + } + + /// Without nested platform ports the legacy fields are stored as they are, + /// zero included, and so is a value no port can have: that is what every + /// earlier binary stores, and a state loaded back is then already in + /// stored form. + #[test] + fn should_store_legacy_ports_unchanged_without_nested_ports() { + for (p2p, http) in [ + (Some(26656), Some(443)), + (Some(0), Some(0)), + (None, None), + (Some(70000), Some(70000)), + ] { + let stored = MasternodeStateV0::from(evonode_state(p2p, http, None)); + assert_eq!( + (stored.platform_p2p_port, stored.platform_http_port), + (p2p, http) + ); + let loaded = DMNState::from(stored); + assert_eq!(in_stored_form(loaded.clone()), loaded); + + let core_entry_only = MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![], + platform_https: vec![], + }; + let stored = MasternodeStateV0::from(evonode_state(p2p, http, Some(core_entry_only))); + assert_eq!( + (stored.platform_p2p_port, stored.platform_http_port), + (p2p, http) + ); + } + } + + /// Masternode states are kept in memory in stored form so that a node + /// that restarted holds the same state as one that did not. That only + /// holds while the stored form is exactly what the store writes and + /// reads back. + #[test] + fn should_hold_in_stored_form_exactly_what_the_store_reads_back() { + let revoked = |addresses: Option| DMNState { + service: "[::]:0".parse().expect("socket address"), + pose_ban_height: Some(200), + revocation_reason: 1, + platform_node_id: Some([0u8; 20]), + ..evonode_state(Some(26656), Some(443), addresses) + }; + let placeholder_host = MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + }; + let on_ipv6 = DMNState { + service: "[2001:db8::1]:9999".parse().expect("socket address"), + ..evonode_state( + None, + None, + Some(platform_addresses( + "[2001:db8::1]:36656", + "[2001:db8::1]:443", + )), + ) + }; + let states = [ + evonode_state(Some(26656), Some(443), None), + evonode_state(Some(0), Some(0), None), + evonode_state(None, None, None), + evonode_state( + Some(36656), + Some(443), + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + ), + evonode_state( + Some(0), + Some(0), + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ), + evonode_state( + None, + None, + Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), + ), + evonode_state(Some(36657), Some(443), Some(placeholder_host)), + on_ipv6, + revoked(None), + revoked(Some(MasternodeAddresses::default())), + ]; + + for state in states { + let masternode = MasternodeListItem { + node_type: MasternodeType::Evo, + pro_tx_hash: ProTxHash::from_byte_array([0x77u8; 32]), + collateral_hash: Txid::from_byte_array([0x11u8; 32]), + collateral_index: 1, + collateral_address: Some([0x22u8; 20]), + operator_reward: 0.0, + state: state.clone(), + }; + let entry = serialize_masternode_entry(&masternode, PlatformVersion::latest()) + .expect("serializable masternode"); + let loaded = deserialize_masternode_entry(&entry).expect("its own entry"); + + assert_eq!( + loaded, + MasternodeListItem { + state: in_stored_form(state.clone()), + ..masternode + }, + "state: {state:?}" + ); + } + } } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs index 73a1990a4ea..4cf479b6431 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs @@ -1,5 +1,6 @@ use crate::error::execution::ExecutionError; use crate::error::Error; +use crate::platform_types::masternode::v0::{apply_diff_in_stored_form, in_stored_form}; use crate::platform_types::platform_state::masternode_list_changes::MasternodeListChanges; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::signature_verification_quorum_set::SignatureVerificationQuorumSet; @@ -138,10 +139,12 @@ pub trait PlatformStateV0Methods { /// Sets the current instant lock validating quorums. fn set_instant_lock_validating_quorums(&mut self, quorums: SignatureVerificationQuorumSet); - /// Sets the full masternode list. + /// Sets the full masternode list. Its states must be in stored form (see + /// `in_stored_form`), as `insert_masternode` keeps them. fn set_full_masternode_list(&mut self, list: BTreeMap); - /// Sets the list of high performance masternodes. + /// Sets the list of high performance masternodes. Its states must be in + /// stored form (see `in_stored_form`), as `insert_masternode` keeps them. fn set_hpmn_masternode_list(&mut self, list: BTreeMap); /// Sets the platform initialization information. fn set_genesis_block_info(&mut self, info: Option); @@ -170,19 +173,23 @@ pub trait PlatformStateV0Methods { /// Returns a mutable reference to the instant lock validating quorums. fn instant_lock_validating_quorums_mut(&mut self) -> &mut SignatureVerificationQuorumSet; - /// Returns a mutable reference to the full masternode list. + /// Returns a mutable reference to the full masternode list. States written + /// through it must be in stored form (see `in_stored_form`). fn full_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Returns a mutable reference to the list of high performance masternodes. + /// States written through it must be in stored form (see `in_stored_form`). fn hpmn_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Adds or replaces a masternode in the full list, and in the HPMN list when - /// it is an Evo node. Records the change for the per-entry store. + /// it is an Evo node. Records the change for the per-entry store. The + /// state is kept in the form it is stored in (see `in_stored_form`). fn insert_masternode(&mut self, masternode: MasternodeListItem); /// Applies a Core state diff to a listed masternode in both lists, recording - /// an entry change only when its stored state changes. Returns false, - /// changing nothing, when the masternode is not listed. + /// an entry change only when its stored state changes. The state stays in + /// the form it is stored in. Returns false, changing nothing, when the + /// masternode is not listed. fn apply_masternode_state_diff( &mut self, pro_tx_hash: &ProTxHash, @@ -556,7 +563,8 @@ impl PlatformStateV0Methods for PlatformState { &mut self.hpmn_masternode_list } - fn insert_masternode(&mut self, masternode: MasternodeListItem) { + fn insert_masternode(&mut self, mut masternode: MasternodeListItem) { + masternode.state = in_stored_form(masternode.state); let pro_tx_hash = masternode.pro_tx_hash; if masternode.node_type == MasternodeType::Evo { self.hpmn_masternode_list @@ -577,11 +585,11 @@ impl PlatformStateV0Methods for PlatformState { let Some(masternode) = self.full_masternode_list.get_mut(pro_tx_hash) else { return false; }; - let previous_state = masternode.state.clone(); - masternode.state.apply_diff(state_diff.clone()); - let entry_changed = masternode.state != previous_state; + let updated_state = apply_diff_in_stored_form(&masternode.state, state_diff); + let entry_changed = updated_state != masternode.state; + masternode.state = updated_state; if let Some(hpmn) = self.hpmn_masternode_list.get_mut(pro_tx_hash) { - hpmn.state.apply_diff(state_diff.clone()); + hpmn.state = apply_diff_in_stored_form(&hpmn.state, state_diff); } self.heavy_fields_dirty = true; // A mixed Core diff can include payment-only updates alongside real diff --git a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs index 2fbab063bd1..5bc88cfa733 100644 --- a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs @@ -22,31 +22,260 @@ impl NewValidatorIfMasternodeInState for ValidatorV0 { ) -> Option { let MasternodeListItem { state, .. } = state.hpmn_masternode_list().get(&pro_tx_hash)?; - #[allow(deprecated)] + let platform_ports = PlatformPorts::of(state); + let PlatformPorts { + p2p: Some(platform_p2p_port), + http: Some(platform_http_port), + } = platform_ports + else { + tracing::debug!( + pro_tx_hash = %pro_tx_hash, + ?platform_ports, + "evonode is not a validator: its platform ports do not resolve" + ); + return None; + }; let DMNState { service, platform_node_id, pose_ban_height, - legacy_platform_p2p_port: platform_p2p_port, - legacy_platform_http_port: platform_http_port, .. } = state; - let Some(platform_http_port) = platform_http_port else { - return None; - }; - let Some(platform_p2p_port) = platform_p2p_port else { - return None; - }; let platform_node_id = (*platform_node_id)?; + // Only the ports come from the platform entries: the validator is + // advertised on the core IP from `service`. Some(ValidatorV0 { pro_tx_hash, public_key, node_ip: service.ip().to_string(), node_id: PubkeyHash::from_byte_array(platform_node_id), core_port: service.port(), - platform_http_port: *platform_http_port as u16, - platform_p2p_port: *platform_p2p_port as u16, + platform_http_port, + platform_p2p_port, is_banned: pose_ban_height.is_some(), }) } } + +/// The platform ports a masternode's state gives its validator. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct PlatformPorts { + /// The port Tenderdash dials the validator on. + pub p2p: Option, + /// The platform HTTPS port. + pub http: Option, +} + +impl PlatformPorts { + /// Resolves each port from the nested `addresses` entry first, else from + /// the legacy port field as it is, zero included: rust-dashcore's + /// `platform_{p2p,http}_address()` fall back that way. A port outside the + /// `u16` range resolves to none; Core stores these ports as 16-bit values, + /// so it never prints one. + /// + /// A zero legacy port is kept rather than read as "not set": whether an + /// evonode is a validator decides quorum rotation, so it must not depend + /// on how a port is read. + pub(crate) fn of(state: &DMNState) -> Self { + Self { + p2p: state + .platform_p2p_address() + .and_then(|(_host, port)| u16::try_from(port).ok()), + http: state + .platform_http_address() + .and_then(|(_host, port)| u16::try_from(port).ok()), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::PlatformConfig; + use dpp::dashcore::{Network, Txid}; + use dpp::dashcore_rpc::json::{MasternodeAddresses, MasternodeType}; + use dpp::version::PlatformVersion; + + const PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + + /// An evonode registered at 1.2.3.4 with the given legacy platform ports + /// and nested addresses. + fn evonode_state( + legacy_platform_p2p_port: Option, + legacy_platform_http_port: Option, + addresses: Option, + ) -> DMNState { + #[allow(deprecated)] + DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: Some([7u8; 20]), + legacy_platform_p2p_port, + legacy_platform_http_port, + addresses, + } + } + + fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![p2p.to_string()], + platform_https: vec![https.to_string()], + } + } + + fn validator_for(dmn_state: DMNState) -> Option { + let platform_version = PlatformVersion::latest(); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + let pro_tx_hash = ProTxHash::from_byte_array(PRO_TX_HASH); + state.insert_masternode(MasternodeListItem { + node_type: MasternodeType::Evo, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0u8; 32]), + collateral_index: 0, + collateral_address: Some([0u8; 20]), + operator_reward: 0.0, + state: dmn_state, + }); + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + } + + /// An ExtAddr evonode keeps its platform ports in its network info; its + /// legacy port fields stay zero. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_zero() { + let validator = validator_for(evonode_state( + Some(0), + Some(0), + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// A state whose legacy ports are absent takes its ports from + /// `addresses`. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_absent() { + let validator = validator_for(evonode_state( + None, + None, + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// The validator is advertised on the core IP from `service`, whatever + /// host `addresses` names for the platform services: a legacy evonode's + /// port-only diff pairs the port with the placeholder host + /// 255.255.255.255. + #[test] + fn should_advertise_the_service_ip_whatever_host_addresses_name() { + let validator = validator_for(evonode_state( + Some(36656), + Some(443), + Some(platform_addresses( + "255.255.255.255:36656", + "203.0.113.7:443", + )), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.core_port, 9999); + } + + /// Core before v23 lists no nested addresses; the legacy ports apply. + #[test] + fn should_take_legacy_ports_without_addresses() { + let validator = validator_for(evonode_state(Some(26656), Some(443), None)) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.platform_p2p_port, 26656); + assert_eq!(validator.platform_http_port, 443); + } + + /// Validator-set membership decides quorum rotation, so it must not + /// depend on how a node reads a port: an evonode whose legacy ports are 0 + /// and that lists no nested ports stays a validator, with those ports. + #[test] + fn should_keep_an_evonode_with_zero_legacy_ports_as_a_validator() { + let validator = validator_for(evonode_state(Some(0), Some(0), None)) + .expect("membership does not depend on the port value"); + + assert_eq!(validator.platform_p2p_port, 0); + assert_eq!(validator.platform_http_port, 0); + } + + /// A revoked legacy evonode as Core lists it with + /// `-deprecatedrpc=service`: its network info is empty, so `service` is + /// `[::]:0` and `addresses` has no platform entry, while the legacy port + /// fields keep the ports it was registered with. + fn revoked_legacy_evonode_state(addresses: &str) -> DMNState { + serde_json::from_str(&format!( + r#"{{ + "service": "[::]:0", + "addresses": {addresses}, + "registeredHeight": 100, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": 200, + "revocationReason": 1, + "ownerAddress": "yPBWCdMRY5PsS3hJzs7csbdWQVRR85yxUz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "payoutAddress": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", + "pubKeyOperator": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "platformNodeID": "0000000000000000000000000000000000000000", + "platformP2PPort": 26656, + "platformHTTPPort": 443 + }}"# + )) + .expect("Core's JSON for a revoked evonode") + } + + /// Whether an evonode is a validator decides quorum rotation, so every + /// node must build the same member for it. For an evonode whose listed + /// `addresses` hold no platform entry, that is the member the legacy + /// port fields alone give: banned, on the unspecified IP, with its + /// registered ports. + #[test] + fn should_build_a_revoked_legacy_evonode_from_its_legacy_ports() { + for addresses in [r#"{}"#, r#"{"core_p2p": []}"#] { + let validator = validator_for(revoked_legacy_evonode_state(addresses)); + + assert_eq!( + validator, + Some(ValidatorV0 { + pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 26656, + is_banned: true, + }), + "addresses: {addresses}" + ); + } + } +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json new file mode 100644 index 00000000000..7611ce1aa1c --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/1260-1270.json @@ -0,0 +1,68 @@ +{ + "baseHeight": 1260, + "blockHeight": 1270, + "addedMNs": [ + { + "type": "Evo", + "proTxHash": "99e719c608d603658f296acd00f271345a29e391e2d717d3615062f9a961920c", + "collateralHash": "89b952e1ac0ecdf8245dc368c955a866538031c6685a562e579adeda25f7a8f3", + "collateralIndex": 0, + "collateralAddress": "yRuFRszcgUcV8jMEdg8MJ7Yync7GkNkp7d", + "operatorReward": 0, + "state": { + "version": 3, + "service": "4zmxpqirulb4blkyeufp3nd37wvcvpz6fibfwvgeybtfttkkni7h3mdd.onion:9999", + "addresses": { + "core_p2p": [ + "4zmxpqirulb4blkyeufp3nd37wvcvpz6fibfwvgeybtfttkkni7h3mdd.onion:9999" + ], + "platform_p2p": [ + "192.0.2.50:26656" + ], + "platform_https": [ + "192.0.2.50:443" + ] + }, + "registeredHeight": 1265, + "lastPaidHeight": 0, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMWTGVez6o7YXsaj2cEGT9bU3a1jHbmDtJ", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "9cea9116b333eba7631804e1be7efd62c56d5b58", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payouts": [ + { + "address": "yfzihVQvxhkkp4rTSw3sLYQzxbaifaTUTZ", + "script": "76a914d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d7d788ac", + "reward": 10000 + } + ], + "pubKeyOperator": "b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6b6" + } + } + ], + "removedMNs": [], + "updatedMNs": [ + { + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0": { + "service": "192.0.2.40:9999", + "addresses": { + "core_p2p": [ + "192.0.2.40:9999" + ], + "platform_p2p": [ + "192.0.2.40:36657" + ], + "platform_https": [ + "192.0.2.40:8443" + ] + } + } + } + ] +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json new file mode 100644 index 00000000000..311ade66dfd --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json @@ -0,0 +1,45 @@ +{ + "baseHeight": 1, + "blockHeight": 1000, + "addedMNs": [ + { + "type": "Evo", + "proTxHash": "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76", + "collateralHash": "b881e375496b24e492820639e8dbe466b86af721eba6fcfdd5c9acf9d3e0afff", + "collateralIndex": 0, + "collateralAddress": "yRzZJpqMjbUdXCyCqHFUeU3vnJ2Z5Pzs3Q", + "operatorReward": 0, + "state": { + "version": 2, + "service": "192.0.2.60:9999", + "addresses": { + "core_p2p": [ + "192.0.2.60:9999" + ], + "platform_https": [ + "192.0.2.60:443" + ], + "platform_p2p": [ + "192.0.2.60:26656" + ] + }, + "registeredHeight": 900, + "lastPaidHeight": 990, + "consecutivePayments": 0, + "PoSePenalty": 0, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": -1, + "revocationReason": 0, + "ownerAddress": "yMbm9SVj9uygvMChEDMPoW6R3Fw1fLRaRz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "platformNodeID": "416e5b04d3047def070bea5175431ca7732b4b7b", + "platformP2PPort": 26656, + "platformHTTPPort": 443, + "payoutAddress": "yaQv7p9egV1wB22MEzZ8vyyAHcYWUTNndN", + "pubKeyOperator": "b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7" + } + } + ], + "removedMNs": [], + "updatedMNs": [] +} diff --git a/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json new file mode 100644 index 00000000000..25b973d90b7 --- /dev/null +++ b/packages/rs-drive-abci/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1000-1010.json @@ -0,0 +1,18 @@ +{ + "baseHeight": 1000, + "blockHeight": 1010, + "addedMNs": [], + "removedMNs": [], + "updatedMNs": [ + { + "86e4a4274da5610322340e2f34adf8f6f2899f78b2ce1c31281b6d1754a88d76": { + "platformP2PPort": 36657, + "addresses": { + "platform_p2p": [ + "255.255.255.255:36657" + ] + } + } + } + ] +} From 214ce69929346cd5d6f097514d5b5f136835eaa7 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 1 Oct 2026 00:10:07 +0700 Subject: [PATCH 09/17] fix(drive-abci): bracket IPv6 hosts in validator node addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drive built each validator's Tenderdash node address as `tcp://{node_id}@{node_ip}:{port}`, which leaves an IPv6 host unbracketed (`tcp://…@2001:db8::1:26656`). A URL authority needs IPv6 hosts in brackets (RFC 3986). Core v24 lets an ExtAddr evonode's primary address be IPv6. Format the host and port through `SocketAddr`, so IPv6 is written as `[2001:db8::1]:26656`; IPv4 addresses are formatted exactly as before. All four places that build a node address (to_update, to_update_owned, and both branches of update_difference) now share one helper. Tenderdash v1.8.1 (the version dashmate runs) already reads both forms to the same address, because Go's net/url enforces strict colons only for http(s) URLs, and it writes the bracketed form itself (net.JoinHostPort). So this changes no Tenderdash behaviour today; it stops depending on that leniency. Validator node addresses are not part of Tenderdash's validator-set hash, so the change is not consensus- affecting. Test would have caught this in CI: the IPv6 node-address test fails before the fix (`…@2001:db8::1:30`), passes after; the IPv4 test pins the unchanged format for every builder. validator_set tests: 12 passed; clippy -D warnings clean. End to end, from Core v24 `protx listdiff` JSON through update_masternode_list to the Tenderdash validator update, the Core v24 fixture test's IPv6 evonode gets `tcp://…@[2001:db8::4]:26656` and the Tor-primary evonode `tcp://…@[::]:26656`; before the fix they fail with `…@2001:db8::4:26656` and `…@:::26656`. Co-Authored-By: Claude Opus 5.5 --- .../update_masternode_list/mod.rs | 41 ++++++- .../platform_types/validator_set/v0/mod.rs | 104 +++++++++++++----- 2 files changed, 119 insertions(+), 26 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index 3c4750508ec..5600d5a6a1d 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -79,7 +79,7 @@ mod test { use crate::platform_types::platform_state::PlatformStateV0Methods; use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; - use crate::platform_types::validator_set::ValidatorSet; + use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; use crate::test::helpers::setup::TestPlatformBuilder; use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; use dpp::core_types::validator_set::v0::ValidatorSetV0; @@ -643,6 +643,17 @@ mod test { assert_eq!(legacy_evo_validator.platform_p2p_port, 36656); assert_eq!(legacy_evo_validator.platform_http_port, 1443); + // An Evo on an IPv6 primary address is advertised to Tenderdash with + // its host in brackets, as a URL authority requires. + let ipv6_evo_validator = + validator("aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783") + .expect("expected the IPv6 Evo to be a validator"); + assert_eq!(ipv6_evo_validator.node_ip, "2001:db8::4"); + assert_eq!( + node_addresses([ipv6_evo_validator]), + ["tcp://4bcc85253e395ec272998a0722ac3ee1dd3965f7@[2001:db8::4]:26656"] + ); + // Every parsed masternode survives a store and reload, except for the payout list and // nested addresses, which are not stored. for masternode in block_platform_state.full_masternode_list().values() { @@ -796,6 +807,30 @@ mod test { /// The quorum of the validator set `state_after_core_v24_lists` adds. const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55; 32]; + /// The node addresses Drive hands Tenderdash for `validators`, as the + /// members of one validator set. + fn node_addresses(validators: impl IntoIterator) -> Vec { + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: 1, + members: validators + .into_iter() + .map(|validator| (validator.pro_tx_hash, validator)) + .collect(), + threshold_public_key: SecretKey::::random(&mut StdRng::seed_from_u64( + 1, + )) + .public_key(), + }) + .to_update() + .validator_updates + .into_iter() + .map(|validator_update| validator_update.node_address) + .collect() + } + /// Runs one masternode list update per `(core height, fixture)`, in order, /// as Drive runs one per block, with Core reporting the fixture's /// `protx listdiff` JSON from `core_v24_protx_list_diffs` for that height. @@ -971,6 +1006,10 @@ mod test { is_banned: false, }) ); + assert_eq!( + node_addresses(new_quorum_validator), + ["tcp://9cea9116b333eba7631804e1be7efd62c56d5b58@[::]:26656"] + ); } /// Before V24, a ProUpServTx that only moves a legacy evonode's P2P port diff --git a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs index b63af8af882..77dfff64371 100644 --- a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs @@ -2,7 +2,7 @@ use crate::error::execution::ExecutionError; use crate::error::Error; use dpp::dashcore::hashes::Hash; -use dpp::dashcore::ProTxHash; +use dpp::dashcore::{ProTxHash, PubkeyHash}; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::validator::v0::NewValidatorIfMasternodeInState; @@ -11,6 +11,7 @@ use dpp::core_types::validator::v0::ValidatorV0; pub use dpp::core_types::validator_set::v0::*; use dpp::dashcore_rpc::json::QuorumInfoResult; use std::collections::BTreeMap; +use std::net::{IpAddr, SocketAddr}; use tenderdash_abci::proto::abci::ValidatorSetUpdate; use tenderdash_abci::proto::crypto::public_key::Sum::Bls12381; use tenderdash_abci::proto::{abci, crypto}; @@ -91,12 +92,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { None } else { - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(node_id, node_ip, *platform_p2p_port); Some(Ok(abci::ValidatorUpdate { pub_key: (*public_key).map(|public_key| crypto::PublicKey { @@ -121,12 +118,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { None } else { - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(node_id, node_ip, *platform_p2p_port); Some(Ok(abci::ValidatorUpdate { pub_key: (*public_key).map(|public_key| crypto::PublicKey { @@ -178,12 +171,7 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if *is_banned { return None; } - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = validator_node_address(node_id, node_ip, *platform_p2p_port); Some(abci::ValidatorUpdate { pub_key: public_key.as_ref().map(|public_key| crypto::PublicKey { sum: Some(Bls12381(public_key.0.to_compressed().to_vec())), @@ -224,12 +212,8 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { if is_banned { return None; } - let node_address = format!( - "tcp://{}@{}:{}", - hex::encode(node_id.to_byte_array()), - node_ip, - platform_p2p_port - ); + let node_address = + validator_node_address(&node_id, &node_ip, platform_p2p_port); Some(abci::ValidatorUpdate { pub_key: public_key.map(|public_key| crypto::PublicKey { @@ -309,6 +293,23 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { } } +/// The address Tenderdash dials a validator on: `tcp://@:`. +/// +/// Formatting the host and port as a socket address writes an IPv6 host in +/// brackets, as a URL authority requires. `node_ip` always holds a formatted +/// IP address; anything else is written as it is. +fn validator_node_address(node_id: &PubkeyHash, node_ip: &str, platform_p2p_port: u16) -> String { + let host_port = match node_ip.parse::() { + Ok(ip) => SocketAddr::new(ip, platform_p2p_port).to_string(), + Err(_) => format!("{}:{}", node_ip, platform_p2p_port), + }; + format!( + "tcp://{}@{}", + hex::encode(node_id.to_byte_array()), + host_port + ) +} + #[cfg(test)] mod tests { use super::*; @@ -546,4 +547,57 @@ mod tests { // threshold_public_key is still present assert!(update.threshold_public_key.is_some()); } + + /// A set holding one validator, on `node_ip`. + fn set_with_validator_on(pro_tx_hash: ProTxHash, node_ip: &str) -> ValidatorSetV0 { + let validator = ValidatorV0 { + node_ip: node_ip.to_string(), + ..make_validator(pro_tx_hash, false) + }; + // Same seeds, so every such set shares its threshold key. + make_set(1, 100, 1, BTreeMap::from([(pro_tx_hash, validator)])) + } + + /// Every node address Drive hands Tenderdash for a validator on `node_ip`: + /// from `to_update`, `to_update_owned`, and `update_difference` both when + /// the validator is unchanged and when it just moved there. + fn node_addresses_for_validator_on(node_ip: &str) -> Vec { + let pro_tx_hash = ProTxHash::from_byte_array([9; 32]); + let set = set_with_validator_on(pro_tx_hash, node_ip); + let before_the_move = set_with_validator_on(pro_tx_hash, "192.168.0.2"); + [ + set.to_update(), + set.clone().to_update_owned(), + set.update_difference(&set).expect("same quorum"), + before_the_move + .update_difference(&set) + .expect("same quorum"), + ] + .into_iter() + .map(|update| { + assert_eq!(update.validator_updates.len(), 1); + update.validator_updates[0].node_address.clone() + }) + .collect() + } + + /// A URL authority needs an IPv6 host in brackets (RFC 3986). Tenderdash + /// writes validator addresses that way; it only reads the unbracketed form + /// because Go's URL parser does not enforce this outside http(s). + #[test] + fn should_bracket_an_ipv6_host_in_the_validator_node_address() { + let expected = format!("tcp://{}@[2001:db8::1]:30", "07".repeat(20)); + for node_address in node_addresses_for_validator_on("2001:db8::1") { + assert_eq!(node_address, expected); + } + } + + /// An IPv4 host takes no brackets. + #[test] + fn should_write_an_ipv4_validator_node_address_without_brackets() { + let expected = format!("tcp://{}@192.168.0.1:30", "07".repeat(20)); + for node_address in node_addresses_for_validator_on("192.168.0.1") { + assert_eq!(node_address, expected); + } + } } From 8b1c9c80a7ddd6fbfa119ec2dc44aea6c88beb6c Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 3 Oct 2026 16:04:09 +0700 Subject: [PATCH 10/17] test(dashmate): drop the core template test The rendered-dash.conf test for `deprecatedrpc=service` is not needed; the template change stays. Co-Authored-By: Claude Opus 5.5 --- .../test/unit/templates/coreTemplate.spec.js | 33 ------------------- 1 file changed, 33 deletions(-) delete mode 100644 packages/dashmate/test/unit/templates/coreTemplate.spec.js diff --git a/packages/dashmate/test/unit/templates/coreTemplate.spec.js b/packages/dashmate/test/unit/templates/coreTemplate.spec.js deleted file mode 100644 index 559505ccbc0..00000000000 --- a/packages/dashmate/test/unit/templates/coreTemplate.spec.js +++ /dev/null @@ -1,33 +0,0 @@ -import { expect } from 'chai'; -import HomeDir from '../../../src/config/HomeDir.js'; -import getBaseConfigFactory from '../../../configs/defaults/getBaseConfigFactory.js'; -import renderTemplateFactory from '../../../src/templates/renderTemplateFactory.js'; -import renderServiceTemplatesFactory from '../../../src/templates/renderServiceTemplatesFactory.js'; - -describe('Core template', () => { - let config; - let renderServiceTemplates; - - beforeEach(() => { - config = getBaseConfigFactory(HomeDir.createTemp())(); - renderServiceTemplates = renderServiceTemplatesFactory(renderTemplateFactory()); - }); - - // Drive and DAPI read `service`, `platformP2PPort` and `platformHTTPPort` from - // Core's masternode list RPCs. Core v24 leaves them out unless the deprecated - // fields are requested, and Platform cannot process a masternode list without - // them. - ['mainnet', 'testnet', 'devnet', 'local'].forEach((network) => { - it(`should keep Core's deprecated masternode service fields in RPC output on ${network}`, () => { - config.set('network', network); - - const dashConf = renderServiceTemplates(config)['core/dash.conf']; - - // Options under a [section] header apply to that chain only, so the - // option has to be in the default section to cover every network. - const [defaultSection] = dashConf.split(/^\[/m); - - expect(defaultSection).to.match(/^deprecatedrpc=service$/m); - }); - }); -}); From d55d9e4e9d28fbe744b36822cc99027896101e9a Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 3 Oct 2026 16:09:56 +0700 Subject: [PATCH 11/17] refactor(drive-abci): keep shipped masternode identity generations unchanged Shipped generations may change in place only where the new code cannot run under the protocol versions that select them. A node at protocol version 13 running against Dash Core v24 does receive shared and extended-address masternodes, so the branches the previous commit added to them were reachable and would have created state where every released binary stops. Restore create_owner_identity v0 (protocol versions 1-3) and v1 (4-13), update_masternode_identities v0 and transition_to_version_4 to the mechanical adaptation of the rust-dashcore bump: a masternode without the payout or owner address they read fails with DashCoreBadResponseError, the same halt, without state, as a release that cannot parse the list. The three create_owner_identity and update_masternode_identities files are byte-identical to that commit again; transition_to_version_4 differs only in its test. The stored masternode conversion (absent address <-> zero bytes) stays: it is aux storage, not consensus state. Tests now pin the shipped behaviour: - create_owner_identity at protocol versions 3 and 13: shared, single payout list, several payouts (and, at 13, no owner address) fail with DashCoreBadResponseError; a legacy masternode gets its identity. - The Core v24 listdiff fixture at protocol version 13 fails with DashCoreBadResponseError ("... has no payout address"). - transition_to_version_4 gives a masternode with an owner address its OWNER key after its TRANSFER key, skips a masternode without an owner identity, and fails on one with an owner identity but no owner address. - The payout-list-diff test moves to the update_masternode_identities dispatcher and runs at protocol version 13 and the latest version. The tests for the new owner identities move to the new generations in the next commit. Red to green: the three new error tests fail on the parent commit ("expected DashCoreBadResponseError, got Ok(None)" / "got Ok(())") and pass here. Co-Authored-By: Claude Opus 5.5 --- .../create_owner_identity/mod.rs | 274 ++++------------- .../create_owner_identity/v0/mod.rs | 39 +-- .../create_owner_identity/v1/mod.rs | 55 ++-- .../update_masternode_identities/mod.rs | 213 +++++++++++++ .../update_masternode_identities/v0/mod.rs | 206 +------------ .../update_masternode_list/mod.rs | 282 +++--------------- .../v0/mod.rs | 107 ++++--- 7 files changed, 401 insertions(+), 775 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs index 6b715c9e469..d520d7f43f4 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs @@ -5,7 +5,7 @@ use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; -use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeListItem}; +use dpp::dashcore_rpc::dashcore_rpc_json::MasternodeListItem; use dpp::identity::Identity; use dpp::version::PlatformVersion; @@ -25,13 +25,12 @@ where /// /// # Returns /// - /// * Result, Error> - The owner identity, or `None` when the masternode - /// has no key an owner identity of this version holds: a shared masternode has no owner - /// and no payout address. Otherwise, returns an error. + /// * Result - Returns the constructed identity for the owner if successful. + /// Otherwise, returns an error. pub(crate) fn create_owner_identity( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result, Error> { + ) -> Result { match platform_version .drive_abci .methods @@ -50,49 +49,22 @@ where } } -/// The one address the owner reward of a masternode is paid to, which its owner identity's -/// TRANSFER key holds: the payout address, or the only entry of a payout list with one entry -/// (Core turns a payout address into such a list when a masternode moves to extended -/// addresses). `None` for a shared masternode, which has neither, and for a reward split -/// between several payouts. -/// -/// The stored masternode list does not keep payout lists, so this is only meaningful for a -/// masternode as Core reported it, never for one read from the stored list. -/// -/// Shared by the shipped `create_owner_identity` v0 and v1: changing it changes both. -fn effective_payout_address(state: &DMNState) -> Option<[u8; 20]> { - match (state.payout_address, state.payouts.as_deref()) { - (Some(payout_address), _) => Some(payout_address), - (None, Some([payout])) => Some(payout.address), - _ => None, - } -} - #[cfg(test)] mod tests { + use crate::error::execution::ExecutionError; + use crate::error::Error; + use crate::platform_types::platform::Platform; use crate::rpc::core::MockCoreRPCLike; - use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; - use dpp::block::block_info::BlockInfo; use dpp::dashcore::hashes::Hash; use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ - DMNPayout, DMNState, MasternodeListDiff, MasternodeListItem, MasternodeType, + DMNPayout, DMNState, MasternodeListItem, MasternodeType, }; - use dpp::identifier::MasternodeIdentifiers; - use dpp::identity::accessors::IdentityGettersV0; - use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; - use dpp::identity::{Identity, KeyID, Purpose}; - use dpp::prelude::Identifier; - use dpp::serialization::PlatformSerializable; use dpp::version::PlatformVersion; - use std::collections::BTreeMap; - const PRO_TX_HASH: [u8; 32] = [0x21; 32]; const OWNER_ADDRESS: [u8; 20] = [0x22; 20]; - const VOTING_ADDRESS: [u8; 20] = [0x23; 20]; const PAYOUT_ADDRESS: [u8; 20] = [0x24; 20]; const SECOND_PAYOUT_ADDRESS: [u8; 20] = [0x25; 20]; - const PUB_KEY_OPERATOR: [u8; 48] = [0x26; 48]; /// A Regular masternode as Core lists it: a shared one has neither owner, payout nor /// collateral address; an extended-address one has `payouts` instead of `payout_address`. @@ -103,7 +75,7 @@ mod tests { ) -> MasternodeListItem { MasternodeListItem { node_type: MasternodeType::Regular, - pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + pro_tx_hash: ProTxHash::from_byte_array([0x21; 32]), collateral_hash: Txid::from_byte_array([0x27; 32]), collateral_index: 0, collateral_address: owner_address.map(|_| [0x28; 20]), @@ -115,10 +87,10 @@ mod tests { pose_ban_height: None, revocation_reason: 0, owner_address, - voting_address: VOTING_ADDRESS, + voting_address: [0x23; 20], payout_address, payouts, - pub_key_operator: PUB_KEY_OPERATOR.to_vec(), + pub_key_operator: vec![0x26; 48], operator_payout_address: None, platform_node_id: None, #[allow(deprecated)] @@ -138,193 +110,63 @@ mod tests { } } - fn shared_masternode() -> MasternodeListItem { - masternode(None, None, None) - } - - fn legacy_masternode() -> MasternodeListItem { - masternode(Some(OWNER_ADDRESS), Some(PAYOUT_ADDRESS), None) - } - - fn single_payout_masternode() -> MasternodeListItem { - masternode( + /// Generation 0 (protocol versions 1 to 3) builds the owner identity from the payout + /// address, and generation 1 (4 to 13) from the payout and the owner address. A masternode + /// Core lists without one of them fails with `DashCoreBadResponseError`, so its block + /// fails instead of the masternode getting an owner identity these generations do not + /// define: a shared masternode has neither address, and an extended-address masternode + /// has a payout list instead of a payout address. + #[test] + fn should_fail_for_a_masternode_without_a_payout_or_owner_address_up_to_protocol_version_13() { + let legacy = masternode(Some(OWNER_ADDRESS), Some(PAYOUT_ADDRESS), None); + let shared = masternode(None, None, None); + let single_payout = masternode( Some(OWNER_ADDRESS), None, Some(vec![p2pkh_payout(PAYOUT_ADDRESS, 10000)]), - ) - } - - fn multi_payout_masternode() -> MasternodeListItem { - masternode( + ); + let several_payouts = masternode( Some(OWNER_ADDRESS), None, Some(vec![ p2pkh_payout(PAYOUT_ADDRESS, 7000), p2pkh_payout(SECOND_PAYOUT_ADDRESS, 3000), ]), - ) - } - - /// A platform on `platform_version` whose masternode identities were created for - /// `masternode`, added on the first block. - fn platform_with_added_masternode( - masternode: MasternodeListItem, - platform_version: &PlatformVersion, - ) -> TempPlatform { - let platform = TestPlatformBuilder::new() - .with_initial_protocol_version(platform_version.protocol_version) - .build_with_mock_rpc() - .set_genesis_state(); - - let transaction = platform.drive.grove.start_transaction(); - platform - .update_masternode_identities( - MasternodeListDiff { - base_height: 0, - block_height: 1, - added_mns: vec![masternode], - removed_mns: vec![], - updated_mns: vec![], - }, - &BTreeMap::new(), - &BlockInfo::default(), - None, - &transaction, - platform_version, - ) - .expect("expected to create the masternode identities"); - platform - .drive - .grove - .commit_transaction(transaction) - .unwrap() - .expect("expected to commit"); - platform - } - - fn fetch_identity( - platform: &TempPlatform, - identity_id: Identifier, - platform_version: &PlatformVersion, - ) -> Option { - platform - .drive - .fetch_full_identity(identity_id.to_buffer(), None, platform_version) - .expect("expected to fetch an identity") - } - - fn owner_identity( - platform: &TempPlatform, - platform_version: &PlatformVersion, - ) -> Option { - fetch_identity(platform, PRO_TX_HASH.into(), platform_version) - } - - fn assert_voter_and_operator_identities_exist( - platform: &TempPlatform, - platform_version: &PlatformVersion, - ) { - let voter_identifier = Identifier::create_voter_identifier(&PRO_TX_HASH, &VOTING_ADDRESS); - let operator_identifier = - Identifier::create_operator_identifier(&PRO_TX_HASH, &PUB_KEY_OPERATOR); - assert!( - fetch_identity(platform, voter_identifier, platform_version).is_some(), - "the voter identity must be created" - ); - assert!( - fetch_identity(platform, operator_identifier, platform_version).is_some(), - "the operator identity must be created" ); - } - - /// Both generations: the owner identity carries the owner's keys, and a shared - /// masternode has no owner key (its owners are its share holders), so it gets no owner - /// identity. Its voter and operator identities are created as for any masternode. - #[test] - fn should_create_no_owner_identity_for_a_shared_masternode() { - for platform_version in [PlatformVersion::first(), PlatformVersion::latest()] { - let platform = platform_with_added_masternode(shared_masternode(), platform_version); - - assert_eq!( - owner_identity(&platform, platform_version), - None, - "protocol version {}", - platform_version.protocol_version - ); - assert_voter_and_operator_identities_exist(&platform, platform_version); + let without_owner_address = masternode(None, Some(PAYOUT_ADDRESS), None); + + for protocol_version in [3, 13] { + let platform_version = + PlatformVersion::get(protocol_version).expect("expected a known protocol version"); + + Platform::::create_owner_identity(&legacy, platform_version) + .expect("a masternode with an owner and a payout address has an owner identity"); + + let mut cases = vec![ + (&shared, "has no payout address"), + (&single_payout, "has no payout address"), + (&several_payouts, "has no payout address"), + ]; + if protocol_version == 13 { + cases.push((&without_owner_address, "has no owner address")); + } + for (masternode, expected_message) in cases { + match Platform::::create_owner_identity( + masternode, + platform_version, + ) { + Err(Error::Execution(ExecutionError::DashCoreBadResponseError(message))) => { + assert!( + message.ends_with(expected_message), + "protocol version {protocol_version}: unexpected message {message}" + ) + } + other => panic!( + "protocol version {protocol_version}: expected DashCoreBadResponseError \ + for {masternode:?}, got {other:?}" + ), + } + } } } - - /// Both generations: Core turns a payout address into a payout list with one entry - /// when a masternode moves to extended addresses, so a single payout must give the - /// owner identity a payout address gives, byte for byte. - #[test] - fn should_create_the_same_owner_identity_for_a_single_payout_as_for_a_payout_address() { - for platform_version in [PlatformVersion::first(), PlatformVersion::latest()] { - let legacy_owner_identity = owner_identity( - &platform_with_added_masternode(legacy_masternode(), platform_version), - platform_version, - ) - .expect("expected an owner identity for a payout address"); - let single_payout_owner_identity = owner_identity( - &platform_with_added_masternode(single_payout_masternode(), platform_version), - platform_version, - ) - .expect("expected an owner identity for a single payout"); - - assert_eq!( - single_payout_owner_identity - .serialize_to_bytes() - .expect("expected to serialize"), - legacy_owner_identity - .serialize_to_bytes() - .expect("expected to serialize"), - "protocol version {}", - platform_version.protocol_version - ); - } - } - - /// Several payouts have no single address for the TRANSFER key id 0, so the owner - /// identity gets only the OWNER key id 1 a payout address's owner identity has, and key - /// id 0 stays free. - #[test] - fn should_create_an_owner_identity_with_only_the_owner_key_for_several_payouts() { - let platform_version = PlatformVersion::latest(); - let multi_payout_owner_identity = owner_identity( - &platform_with_added_masternode(multi_payout_masternode(), platform_version), - platform_version, - ) - .expect("expected an owner identity for several payouts"); - let legacy_owner_identity = owner_identity( - &platform_with_added_masternode(legacy_masternode(), platform_version), - platform_version, - ) - .expect("expected an owner identity for a payout address"); - - let owner_key = legacy_owner_identity - .public_keys() - .get(&1) - .expect("expected the OWNER key id 1") - .clone(); - assert_eq!(owner_key.purpose(), Purpose::OWNER); - assert_eq!(owner_key.data().as_slice(), OWNER_ADDRESS.as_slice()); - assert_eq!( - multi_payout_owner_identity.public_keys(), - &BTreeMap::from([(1 as KeyID, owner_key)]) - ); - } - - /// The first generation's owner identity holds only the TRANSFER key of the payout - /// address, so several payouts leave it with no key: no owner identity is created, as - /// for a shared masternode. - #[test] - fn should_create_no_owner_identity_for_several_payouts_in_protocol_versions_without_owner_keys() - { - let platform_version = PlatformVersion::first(); - let platform = platform_with_added_masternode(multi_payout_masternode(), platform_version); - - assert_eq!(owner_identity(&platform, platform_version), None); - assert_voter_and_operator_identities_exist(&platform, platform_version); - } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs index dcb7634b183..7bdd4753d5c 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v0/mod.rs @@ -1,4 +1,4 @@ -use super::effective_payout_address; +use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -15,21 +15,13 @@ where pub(super) fn create_owner_identity_v0( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result, Error> { - // The identity holds only the payout address's TRANSFER key, so a masternode without a - // single payout address (shared, or paid to several payouts) gets none. Only a Core v24 - // masternode list has such a masternode, and no earlier binary can parse one, so no - // committed block reached this branch. A chain on Core v24 with V24 active must start at - // protocol version 4 or later: an Evo paid to several payouts gets no owner identity - // here, so paying it as a block proposer would fail. - let Some(payout_address) = effective_payout_address(&masternode.state) else { - tracing::debug!( - pro_tx_hash = %masternode.pro_tx_hash, - method = "create_owner_identity_v0", - "no owner identity: the masternode has no single payout address" - ); - return Ok(None); - }; + ) -> Result { + let payout_address = masternode.state.payout_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no payout address", + masternode.pro_tx_hash + ))) + })?; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; identity.add_public_keys([Self::get_owner_identity_withdrawal_key( @@ -37,7 +29,7 @@ where 0, platform_version, )?]); - Ok(Some(identity)) + Ok(identity) } pub(crate) fn get_owner_identifier( @@ -113,8 +105,7 @@ mod tests { let mn = make_masternode(pro_tx, [0x12u8; 20]); let identity = Platform::::create_owner_identity_v0(&mn, platform_version) - .expect("create_owner_identity_v0 must succeed") - .expect("a masternode with a payout address has an owner identity"); + .expect("create_owner_identity_v0 must succeed"); assert_eq!(identity.id(), Identifier::from(pro_tx)); assert_eq!( @@ -131,12 +122,10 @@ mod tests { let a = make_masternode([0x01u8; 32], [0u8; 20]); let b = make_masternode([0x02u8; 32], [0u8; 20]); - let ia = Platform::::create_owner_identity_v0(&a, platform_version) - .expect("a") - .expect("a has an owner identity"); - let ib = Platform::::create_owner_identity_v0(&b, platform_version) - .expect("b") - .expect("b has an owner identity"); + let ia = + Platform::::create_owner_identity_v0(&a, platform_version).expect("a"); + let ib = + Platform::::create_owner_identity_v0(&b, platform_version).expect("b"); assert_ne!(ia.id(), ib.id()); } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs index 0eb2ddadece..597f4a287e3 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v1/mod.rs @@ -1,4 +1,4 @@ -use super::effective_payout_address; +use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::rpc::core::CoreRPCLike; @@ -14,42 +14,25 @@ where pub(super) fn create_owner_identity_v1( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result, Error> { - // Only a Core v24 masternode list has a masternode without an owner address or without - // a single payout address, and no earlier binary can parse one, so no committed block - // reached the branches below that skip a key. - // - // A shared masternode has no owner key: its owners are its share holders. - let Some(owner_address) = masternode.state.owner_address else { - tracing::debug!( - pro_tx_hash = %masternode.pro_tx_hash, - method = "create_owner_identity_v1", - "no owner identity: the masternode has no owner address" - ); - return Ok(None); - }; + ) -> Result { + let payout_address = masternode.state.payout_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no payout address", + masternode.pro_tx_hash + ))) + })?; + let owner_address = masternode.state.owner_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no owner address", + masternode.pro_tx_hash + ))) + })?; let owner_identifier = Self::get_owner_identifier(masternode)?; let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; - // Several payouts have no single address for the TRANSFER key, so the identity holds - // only the OWNER key, under the same id as next to a TRANSFER key. - if let Some(payout_address) = effective_payout_address(&masternode.state) { - identity.add_public_keys([Self::get_owner_identity_withdrawal_key( - payout_address, - 0, - platform_version, - )?]); - } else { - tracing::debug!( - pro_tx_hash = %masternode.pro_tx_hash, - method = "create_owner_identity_v1", - "owner identity with only the OWNER key: no single payout address" - ); - } - identity.add_public_keys([Self::get_owner_identity_owner_key( - owner_address, - 1, - platform_version, - )?]); - Ok(Some(identity)) + identity.add_public_keys([ + Self::get_owner_identity_withdrawal_key(payout_address, 0, platform_version)?, + Self::get_owner_identity_owner_key(owner_address, 1, platform_version)?, + ]); + Ok(identity) } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs index 169f1f4cd06..1b635109277 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs @@ -49,3 +49,216 @@ where } } } + +#[cfg(test)] +mod tests { + use crate::config::PlatformConfig; + use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods}; + use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::block::block_info::BlockInfo; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{KeyID, Purpose}; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + /// A payout change of an extended-address masternode reaches Drive only as `payouts`, + /// never as `payout_address`. Every protocol version rotates the owner identity's TRANSFER + /// key only on a `payout_address` change; acting on `payouts` as well would change what a + /// block does to owner identities. Protocol version 13 is pinned because a node running it + /// against Dash Core v24 receives such diffs. + #[test] + fn should_not_change_owner_identity_keys_on_a_payout_list_change() { + for platform_version in [ + PlatformVersion::get(13).expect("expected protocol version 13"), + PlatformVersion::latest(), + ] { + assert_owner_identity_keys_unchanged_by_payout_list_changes(platform_version); + } + } + + fn assert_owner_identity_keys_unchanged_by_payout_list_changes( + platform_version: &PlatformVersion, + ) { + let protocol_version = platform_version.protocol_version; + let platform = TestPlatformBuilder::new() + .with_initial_protocol_version(protocol_version) + .build_with_mock_rpc() + .set_genesis_state(); + + let pro_tx_hash = ProTxHash::from_byte_array([0x51; 32]); + let owner_address = [0x52; 20]; + let payout_address = [0x53; 20]; + let masternode = MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0x54; 32]), + collateral_index: 0, + collateral_address: Some([0x55; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some(owner_address), + voting_address: [0x56; 20], + payout_address: Some(payout_address), + payouts: None, + pub_key_operator: vec![0x57; 48], + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + }; + + let transaction = platform.drive.grove.start_transaction(); + platform + .update_masternode_identities( + MasternodeListDiff { + base_height: 0, + block_height: 1, + added_mns: vec![masternode.clone()], + removed_mns: vec![], + updated_mns: vec![], + }, + &BTreeMap::new(), + &BlockInfo::default(), + None, + &transaction, + platform_version, + ) + .expect("expected to create the masternode identities"); + + let mut platform_state = PlatformState::default_with_protocol_versions( + protocol_version, + protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + platform_state.insert_masternode(masternode); + + let apply_update = |state_diff: DMNStateDiff| { + platform + .update_masternode_identities( + MasternodeListDiff { + base_height: 1, + block_height: 2, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, state_diff)], + }, + &BTreeMap::new(), + &BlockInfo::default(), + Some(&platform_state), + &transaction, + platform_version, + ) + .expect("expected to process the masternode update"); + }; + let owner_identity_keys = || -> BTreeMap, bool)> { + platform + .drive + .fetch_full_identity( + pro_tx_hash.to_byte_array(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch the owner identity") + .expect("expected the owner identity to exist") + .public_keys() + .iter() + .map(|(key_id, key)| { + ( + *key_id, + (key.purpose(), key.data().to_vec(), key.is_disabled()), + ) + }) + .collect() + }; + let keys_before = BTreeMap::from([ + (0, (Purpose::TRANSFER, payout_address.to_vec(), false)), + (1, (Purpose::OWNER, owner_address.to_vec(), false)), + ]); + assert_eq!( + owner_identity_keys(), + keys_before, + "protocol version {protocol_version}" + ); + + let payout = |address: [u8; 20], reward: u16| DMNPayout { + address, + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(address)), + reward, + }; + for payouts in [ + // The payout address moved into a list, as Core does when a masternode moves to + // extended addresses. + vec![payout(payout_address, 10000)], + // Another single payout address. + vec![payout([0x58; 20], 10000)], + // Several payouts. + vec![payout([0x58; 20], 2500), payout([0x59; 20], 7500)], + ] { + apply_update(DMNStateDiff { + payouts: Some(payouts.clone()), + ..empty_state_diff() + }); + assert_eq!( + owner_identity_keys(), + keys_before, + "protocol version {protocol_version}, payouts {payouts:?}" + ); + } + + // The same processing does rotate the TRANSFER key on a payout address change. + apply_update(DMNStateDiff { + payout_address: Some([0x5a; 20]), + ..empty_state_diff() + }); + assert_eq!( + owner_identity_keys(), + BTreeMap::from([ + (0, (Purpose::TRANSFER, payout_address.to_vec(), true)), + (1, (Purpose::OWNER, owner_address.to_vec(), false)), + (2, (Purpose::TRANSFER, vec![0x5a; 20], false)), + ]), + "protocol version {protocol_version}" + ); + } + + fn empty_state_diff() -> DMNStateDiff { + DMNStateDiff { + service: None, + registered_height: None, + last_paid_height: None, + consecutive_payments: None, + pose_penalty: None, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: None, + owner_address: None, + voting_address: None, + payout_address: None, + payouts: None, + pub_key_operator: None, + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + } + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs index 06458c123a9..6cdff83e2f1 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs @@ -81,17 +81,10 @@ where "create operator identity" ); - // A masternode with none of the keys an owner identity holds, such as a shared - // masternode, has no owner identity. Only an added masternode without an owner - // address, or with `payouts` instead of `payoutAddress`, gets none, and no earlier - // binary can deserialize a masternode list holding one, so no committed block - // skipped an owner identity here. - if let Some(owner_identity) = owner_identity { - drive_operations.push(IdentityOperation(AddNewIdentity { - identity: owner_identity, - is_masternode_identity: true, - })); - } + drive_operations.push(IdentityOperation(AddNewIdentity { + identity: owner_identity, + is_masternode_identity: true, + })); drive_operations.push(IdentityOperation(AddNewIdentity { identity: voter_identity, @@ -173,194 +166,3 @@ where Ok(()) } } - -#[cfg(test)] -mod tests { - use crate::config::PlatformConfig; - use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods}; - use crate::test::helpers::setup::TestPlatformBuilder; - use dpp::block::block_info::BlockInfo; - use dpp::dashcore::hashes::Hash; - use dpp::dashcore::{Network, ProTxHash, PubkeyHash, ScriptBuf, Txid}; - use dpp::dashcore_rpc::dashcore_rpc_json::{ - DMNPayout, DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, - }; - use dpp::identity::accessors::IdentityGettersV0; - use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; - use dpp::identity::{KeyID, Purpose}; - use dpp::version::PlatformVersion; - use std::collections::BTreeMap; - - /// A payout change of an extended-address masternode reaches Drive only as `payouts`, - /// never as `payout_address`. Only a `payout_address` change rotates the owner identity's - /// TRANSFER key; acting on `payouts` as well would change what a block does to owner - /// identities, which needs a new method version. - #[test] - fn should_not_change_owner_identity_keys_on_a_payout_list_change() { - let platform_version = PlatformVersion::latest(); - let platform = TestPlatformBuilder::new() - .build_with_mock_rpc() - .set_genesis_state(); - - let pro_tx_hash = ProTxHash::from_byte_array([0x51; 32]); - let owner_address = [0x52; 20]; - let payout_address = [0x53; 20]; - let masternode = MasternodeListItem { - node_type: MasternodeType::Regular, - pro_tx_hash, - collateral_hash: Txid::from_byte_array([0x54; 32]), - collateral_index: 0, - collateral_address: Some([0x55; 20]), - operator_reward: 0.0, - state: DMNState { - service: "1.2.3.4:9999".parse().expect("socket address"), - registered_height: 0, - pose_revived_height: None, - pose_ban_height: None, - revocation_reason: 0, - owner_address: Some(owner_address), - voting_address: [0x56; 20], - payout_address: Some(payout_address), - payouts: None, - pub_key_operator: vec![0x57; 48], - operator_payout_address: None, - platform_node_id: None, - #[allow(deprecated)] - legacy_platform_p2p_port: None, - #[allow(deprecated)] - legacy_platform_http_port: None, - addresses: None, - }, - }; - - let transaction = platform.drive.grove.start_transaction(); - platform - .update_masternode_identities( - MasternodeListDiff { - base_height: 0, - block_height: 1, - added_mns: vec![masternode.clone()], - removed_mns: vec![], - updated_mns: vec![], - }, - &BTreeMap::new(), - &BlockInfo::default(), - None, - &transaction, - platform_version, - ) - .expect("expected to create the masternode identities"); - - let mut platform_state = PlatformState::default_with_protocol_versions( - platform_version.protocol_version, - platform_version.protocol_version, - &PlatformConfig::default_for_network(Network::Testnet), - ) - .expect("platform state"); - platform_state.insert_masternode(masternode); - - let apply_update = |state_diff: DMNStateDiff| { - platform - .update_masternode_identities( - MasternodeListDiff { - base_height: 1, - block_height: 2, - added_mns: vec![], - removed_mns: vec![], - updated_mns: vec![(pro_tx_hash, state_diff)], - }, - &BTreeMap::new(), - &BlockInfo::default(), - Some(&platform_state), - &transaction, - platform_version, - ) - .expect("expected to process the masternode update"); - }; - let owner_identity_keys = || -> BTreeMap, bool)> { - platform - .drive - .fetch_full_identity( - pro_tx_hash.to_byte_array(), - Some(&transaction), - platform_version, - ) - .expect("expected to fetch the owner identity") - .expect("expected the owner identity to exist") - .public_keys() - .iter() - .map(|(key_id, key)| { - ( - *key_id, - (key.purpose(), key.data().to_vec(), key.is_disabled()), - ) - }) - .collect() - }; - let keys_before = BTreeMap::from([ - (0, (Purpose::TRANSFER, payout_address.to_vec(), false)), - (1, (Purpose::OWNER, owner_address.to_vec(), false)), - ]); - assert_eq!(owner_identity_keys(), keys_before); - - let payout = |address: [u8; 20], reward: u16| DMNPayout { - address, - script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(address)), - reward, - }; - for payouts in [ - // The payout address moved into a list, as Core does when a masternode moves to - // extended addresses. - vec![payout(payout_address, 10000)], - // Another single payout address. - vec![payout([0x58; 20], 10000)], - // Several payouts. - vec![payout([0x58; 20], 2500), payout([0x59; 20], 7500)], - ] { - apply_update(DMNStateDiff { - payouts: Some(payouts.clone()), - ..empty_state_diff() - }); - assert_eq!(owner_identity_keys(), keys_before, "payouts {payouts:?}"); - } - - // The same processing does rotate the TRANSFER key on a payout address change. - apply_update(DMNStateDiff { - payout_address: Some([0x5a; 20]), - ..empty_state_diff() - }); - assert_eq!( - owner_identity_keys(), - BTreeMap::from([ - (0, (Purpose::TRANSFER, payout_address.to_vec(), true)), - (1, (Purpose::OWNER, owner_address.to_vec(), false)), - (2, (Purpose::TRANSFER, vec![0x5a; 20], false)), - ]) - ); - } - - fn empty_state_diff() -> DMNStateDiff { - DMNStateDiff { - service: None, - registered_height: None, - last_paid_height: None, - consecutive_payments: None, - pose_penalty: None, - pose_revived_height: None, - pose_ban_height: None, - revocation_reason: None, - owner_address: None, - voting_address: None, - payout_address: None, - payouts: None, - pub_key_operator: None, - operator_payout_address: None, - platform_node_id: None, - #[allow(deprecated)] - legacy_platform_p2p_port: None, - #[allow(deprecated)] - legacy_platform_http_port: None, - addresses: None, - } - } -} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index ef114d68582..8494a69df19 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -73,26 +73,13 @@ where mod test { use super::*; use crate::config::PlatformConfig; - use crate::platform_types::platform_state::platform_state_for_saving::v2::{ - deserialize_masternode_entry, serialize_masternode_entry, - }; - use crate::platform_types::platform_state::PlatformStateV0Methods; - use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; - use crate::test::helpers::setup::TestPlatformBuilder; - use dpp::dashcore::hashes::Hash; - use dpp::dashcore::ProTxHash; + use crate::rpc::core::MockCoreRPCLike; + use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; use dpp::dashcore_rpc::json::MasternodeListDiff; - use dpp::identifier::MasternodeIdentifiers; - use dpp::identity::accessors::IdentityGettersV0; - use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; - use dpp::identity::{KeyID, Purpose}; - use dpp::prelude::Identifier; - use std::collections::BTreeMap; use std::env; use std::fs::File; use std::io::BufReader; use std::path::PathBuf; - use std::str::FromStr; #[test] fn test_update_masternode_list() { @@ -213,34 +200,25 @@ mod test { .expect("expected to commit"); } - /// Dash Core v24 prints masternodes that differ from every earlier list: a shared - /// masternode has no owner, payout or collateral address; an extended-address masternode - /// has a `payouts` list instead of `payoutAddress`; a Tor primary address is not an - /// `ip:port` `service`; an Evo without addresses has `-1` platform ports. The list must be - /// processed rather than fail the block, and the masternode identities must be: - /// - shared: voter and operator identities, no owner identity (it has no owner key); - /// - one payout: the same owner identity as a legacy payout address (TRANSFER key 0, - /// OWNER key 1); - /// - several payouts: an owner identity with only the OWNER key 1; - /// - a later payout change through `payouts`: no key change, since only a `payoutAddress` - /// change rotates the TRANSFER key. - #[test] - fn should_update_masternode_identities_from_a_core_v24_masternode_list() { - let platform_version = PlatformVersion::latest(); - + /// A platform at `platform_version` whose mock Core returns Dash Core v24 `protx listdiff` + /// output: the diff from height 1 to 1250 adds a legacy Evo, a shared masternode, Regular + /// masternodes with one and with two payouts (one of them with a Tor primary address), an + /// Evo without addresses and an IPv6 Evo with two payouts; the diff to 1260 moves the + /// legacy Evo to extended addresses and changes two payout lists. + fn platform_with_core_v24_masternode_list( + platform_version: &PlatformVersion, + ) -> TempPlatform { let mut platform = TestPlatformBuilder::new() .with_config(PlatformConfig::default()) + .with_initial_protocol_version(platform_version.protocol_version) .build_with_mock_rpc() .set_genesis_state(); - let genesis_core_block_height = 1250; - let first_block_core_block_height = 1260; - platform .core_rpc .expect_get_protx_diff_with_masternodes() .returning(move |_base_block, block| { - let file_name = if block == genesis_core_block_height { + let file_name = if block == 1250 { "1-1250.json" } else { "1250-1260.json" @@ -256,217 +234,41 @@ mod test { Ok(serde_json::from_str(&json)?) }); - let transaction = platform.drive.grove.start_transaction(); - - let mut init_chain_platform_state = platform.state.load().as_ref().clone(); - - platform - .update_masternode_list( - None, - &mut init_chain_platform_state, - genesis_core_block_height, - true, - &BlockInfo { - height: 1, - core_height: genesis_core_block_height, - time_ms: 1, - ..Default::default() - }, - &transaction, - platform_version, - ) - .expect("expected to process the Core v24 masternode list"); - - let platform_state = init_chain_platform_state.clone(); - let mut block_platform_state = platform_state.clone(); - platform - .update_masternode_list( - Some(&platform_state), - &mut block_platform_state, - first_block_core_block_height, - false, - &BlockInfo { - height: 1, - core_height: first_block_core_block_height, - time_ms: 2, - ..Default::default() - }, - &transaction, - platform_version, - ) - .expect("expected to process the Core v24 masternode list diff"); - - assert_eq!(block_platform_state.full_masternode_list().len(), 7); - assert_eq!(block_platform_state.hpmn_masternode_list().len(), 3); - - // An Evo without addresses prints `-1` platform ports, which read as no port, so it - // cannot be a validator, like any Evo without ports. The legacy Evo is one, with the - // ports and address the diff gave it. - let validator = |pro_tx_hash: &str| { - ValidatorV0::new_validator_if_masternode_in_state( - ProTxHash::from_str(pro_tx_hash).expect("expected a pro tx hash"), - None, - &block_platform_state, - ) - }; - assert!( - validator("6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706").is_none() - ); - let legacy_evo_validator = - validator("ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0") - .expect("expected the legacy Evo to be a validator"); - assert_eq!(legacy_evo_validator.node_ip, "192.0.2.40"); - assert_eq!(legacy_evo_validator.platform_p2p_port, 36656); - assert_eq!(legacy_evo_validator.platform_http_port, 1443); - - // Every parsed masternode survives a store and reload, except for the payout list and - // nested addresses, which are not stored. - for masternode in block_platform_state.full_masternode_list().values() { - let bytes = serialize_masternode_entry(masternode, platform_version) - .expect("expected to serialize the masternode entry"); - let mut expected = masternode.clone(); - expected.state.payouts = None; - expected.state.addresses = None; - assert_eq!( - deserialize_masternode_entry(&bytes) - .expect("expected to deserialize the masternode entry"), - expected - ); - } - - let hash160 = |hex: &str| -> Vec { hex::decode(hex).expect("expected hex") }; - - // The keys of an identity, none of them disabled; `None` if there is no identity. - let identity_keys = - |identity_id: Identifier| -> Option)>> { - platform - .drive - .fetch_full_identity( - identity_id.to_buffer(), - Some(&transaction), - platform_version, - ) - .expect("expected to fetch an identity") - .map(|identity| { - identity - .public_keys() - .iter() - .map(|(key_id, key)| { - assert!(key.disabled_at().is_none(), "key {key_id} is disabled"); - (*key_id, (key.purpose(), key.data().to_vec())) - }) - .collect() - }) - }; + } - let owner_key = |hex: &str| (Purpose::OWNER, hash160(hex)); - let transfer_key = |hex: &str| (Purpose::TRANSFER, hash160(hex)); + /// Up to protocol version 13 an added masternode needs a payout and an owner address for + /// its owner identity. A Dash Core v24 list adds shared and extended-address masternodes + /// that have neither or no payout address, so the update fails with + /// `DashCoreBadResponseError` and the block with it, as on every release that cannot parse + /// such a list. + #[test] + fn should_fail_on_a_core_v24_masternode_list_up_to_protocol_version_13() { + let platform_version = PlatformVersion::get(13).expect("expected protocol version 13"); + let platform = platform_with_core_v24_masternode_list(platform_version); - let voting_421c = "421c03add2c804421451c4e022258778175e60d8"; - let voting_064c = "064cd21ff210c1a92adaea49a4768a41c2aef1bc"; + let transaction = platform.drive.grove.start_transaction(); + let mut init_chain_platform_state = platform.state.load().as_ref().clone(); - // (pro_tx_hash, voting key hash, operator public key, owner identity keys) - let expected = [ - ( - // Legacy Evo whose payout address the diff moves into `payouts` - "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", - voting_421c, - "8ed3f0c208efbcfc815cbfb94490dc68cf2e29d44dd9f8a91e20e06057aa110d7062c8ab7ccc85a9ff0c88760157f563".to_string(), - Some(BTreeMap::from([ - (0, transfer_key("75d57974b6e29a4a70df57a3b11195ce0a0dc817")), - (1, owner_key("1f67d90f35e3c5070c368ae6f3635aac357e47df")), - ])), - ), - ( - // One payout, which the diff replaces with two - "27978dd892b7c876c238be1a6141461c2824f3497dd0058e160979bc8f0a0bef", - voting_421c, - "a792ce1af5f7bb9281053b3934cb8b08d00d075a56498e1a525388ce467f188e8a80911fd96a20982baa9b9678452534".to_string(), - Some(BTreeMap::from([ - (0, transfer_key("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")), - (1, owner_key("b740a2ab3f631e4dfe15debf746364f1d8352c50")), - ])), - ), - ( - // Shared - "a4d26868017c0ccffe2efe50944ef4211834660cca834c6e9f86dec6a88246fa", - voting_421c, - "b1".repeat(48), - None, + match platform.update_masternode_list( + None, + &mut init_chain_platform_state, + 1250, + true, + &BlockInfo { + height: 1, + core_height: 1250, + time_ms: 1, + ..Default::default() + }, + &transaction, + platform_version, + ) { + Err(Error::Execution(ExecutionError::DashCoreBadResponseError(message))) => assert!( + message.ends_with("has no payout address"), + "unexpected message: {message}" ), - ( - // One payout, which the diff replaces with another single payout - "813a7c3f28817988a8e6ce66e07e43e261e78398373bfbaae94c898645111d6b", - voting_421c, - "b2".repeat(48), - Some(BTreeMap::from([ - (0, transfer_key("5555555555555555555555555555555555555555")), - (1, owner_key("0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a")), - ])), - ), - ( - // Tor primary address, two payouts; the owner and voting keys are the same - "3023ffd989974768b0dfc347410ad923fa6d3f1eee90180bd0c435e81cb1a82f", - voting_064c, - "b3".repeat(48), - Some(BTreeMap::from([(1, owner_key(voting_064c))])), - ), - ( - // Evo without addresses, one payout - "6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706", - voting_421c, - "b4".repeat(48), - Some(BTreeMap::from([ - (0, transfer_key("9999999999999999999999999999999999999999")), - (1, owner_key("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")), - ])), - ), - ( - // Evo with an IPv6 primary address, two payouts - "aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783", - voting_421c, - "b5".repeat(48), - Some(BTreeMap::from([( - 1, - owner_key("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"), - )])), - ), - ]; - - for (pro_tx_hash, voting_key_hash, operator_public_key, owner_identity_keys) in expected { - let pro_tx_hash = ProTxHash::from_str(pro_tx_hash) - .expect("expected a pro tx hash") - .to_byte_array(); - let voting_key_hash: [u8; 20] = hash160(voting_key_hash) - .try_into() - .expect("expected 20 bytes"); - let operator_public_key = hex::decode(operator_public_key).expect("expected hex"); - - assert_eq!( - identity_keys(pro_tx_hash.into()), - owner_identity_keys, - "owner identity of {}", - hex::encode(pro_tx_hash) - ); - assert!( - identity_keys(Identifier::create_voter_identifier( - &pro_tx_hash, - &voting_key_hash - )) - .is_some(), - "voter identity of {}", - hex::encode(pro_tx_hash) - ); - assert!( - identity_keys(Identifier::create_operator_identifier( - &pro_tx_hash, - &operator_public_key - )) - .is_some(), - "operator identity of {}", - hex::encode(pro_tx_hash) - ); + other => panic!("expected DashCoreBadResponseError, got {other:?}"), } } } 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 13e7b5a13bf..76eacdf65a3 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 @@ -186,22 +186,18 @@ impl Platform { continue; } - // A shared masternode has no owner key to add. Only a Core v24 masternode list has - // one, and no earlier binary can parse it, so no committed block reached this branch. - let Some(owner_address) = masternode.state.owner_address else { - tracing::debug!( - pro_tx_hash = %masternode.pro_tx_hash, - method = "transition_to_version_4", - "no OWNER key added: the masternode has no owner address" - ); - continue; - }; - let last_key_id = *old_owner_identity_keys .keys() .max() .expect("there must be keys, we already checked"); + let owner_address = masternode.state.owner_address.ok_or_else(|| { + Error::Execution(ExecutionError::DashCoreBadResponseError(format!( + "masternode {} has no owner address", + masternode.pro_tx_hash + ))) + })?; + let new_owner_key = Self::get_owner_identity_owner_key( owner_address, last_key_id + 1, @@ -1047,11 +1043,14 @@ mod tests { assert!(result.is_ok()); } - /// Version 4 gives every owner identity the OWNER key of its masternode's owner address. - /// A shared masternode has no owner address, so its identity, if one exists under its - /// ProTx hash, gets no key; the owner identities of other masternodes still get theirs. + /// Version 4 gives every owner identity the OWNER key of its masternode's owner address, + /// after the keys it has. A masternode without an owner identity is skipped before its + /// owner address is read. One with an owner identity but no owner address (a shared + /// masternode) has no key to give, so the transition fails with `DashCoreBadResponseError` + /// instead of leaving the identity without an OWNER key. #[test] - fn should_add_no_owner_key_for_a_masternode_without_owner_address_on_transition_to_version_4() { + fn should_fail_transition_to_version_4_for_an_owner_identity_whose_masternode_has_no_owner_address( + ) { use dpp::dashcore::{ProTxHash, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeListItem, MasternodeType}; use dpp::identity::accessors::IdentityGettersV0; @@ -1064,6 +1063,8 @@ mod tests { .build_with_mock_rpc() .set_genesis_state(); + // A masternode as Core lists it; a shared one has no owner, payout or collateral + // address. let masternode = |pro_tx_hash: [u8; 32], owner_address: Option<[u8; 20]>| MasternodeListItem { node_type: MasternodeType::Regular, @@ -1092,23 +1093,13 @@ mod tests { addresses: None, }, }; - let owner_address = [0x46; 20]; - let with_owner = [0x47; 32]; - let shared_with_identity = [0x48; 32]; - let shared_without_identity = [0x49; 32]; - - let mut platform_state = platform.state.load().as_ref().clone(); - for item in [ - masternode(with_owner, Some(owner_address)), - masternode(shared_with_identity, None), - masternode(shared_without_identity, None), - ] { - platform_state.insert_masternode(item); - } + let with_owner_address = [0x47; 32]; + let shared_without_owner_identity = [0x48; 32]; + let shared_with_owner_identity = [0x49; 32]; // Owner identities as created before version 4: only the TRANSFER key id 0. - for pro_tx_hash in [with_owner, shared_with_identity] { + for pro_tx_hash in [with_owner_address, shared_with_owner_identity] { let mut identity = Identity::create_basic_identity(pro_tx_hash.into(), platform_version) .expect("expected an identity"); @@ -1134,49 +1125,53 @@ mod tests { } let transaction = platform.drive.grove.start_transaction(); + let platform_state_with = |masternodes: Vec| { + let mut platform_state = platform.state.load().as_ref().clone(); + for masternode in masternodes { + platform_state.insert_masternode(masternode); + } + platform_state + }; + platform .transition_to_version_4( - &platform_state, + &platform_state_with(vec![ + masternode(with_owner_address, Some(owner_address)), + masternode(shared_without_owner_identity, None), + ]), &BlockInfo::default(), &transaction, platform_version, ) - .expect("expected the transition to version 4 to succeed"); - - let identity_keys = |pro_tx_hash: [u8; 32]| -> BTreeMap)> { + .expect("expected a masternode without an owner identity to be skipped"); + assert_eq!( platform .drive - .fetch_full_identity(pro_tx_hash, Some(&transaction), platform_version) - .expect("expected to fetch an identity") - .expect("expected the identity to exist") + .fetch_full_identity(with_owner_address, Some(&transaction), platform_version) + .expect("expected to fetch the owner identity") + .expect("expected the owner identity to exist") .public_keys() .iter() .map(|(key_id, key)| (*key_id, (key.purpose(), key.data().to_vec()))) - .collect() - }; - - assert_eq!( - identity_keys(with_owner), + .collect::)>>(), BTreeMap::from([ (0, (Purpose::TRANSFER, vec![0x44; 20])), (1, (Purpose::OWNER, owner_address.to_vec())), ]) ); - assert_eq!( - identity_keys(shared_with_identity), - BTreeMap::from([(0, (Purpose::TRANSFER, vec![0x44; 20]))]) - ); - assert_eq!( - platform - .drive - .fetch_full_identity( - shared_without_identity, - Some(&transaction), - platform_version - ) - .expect("expected to fetch an identity"), - None - ); + + match platform.transition_to_version_4( + &platform_state_with(vec![masternode(shared_with_owner_identity, None)]), + &BlockInfo::default(), + &transaction, + platform_version, + ) { + Err(Error::Execution(ExecutionError::DashCoreBadResponseError(message))) => assert!( + message.ends_with("has no owner address"), + "unexpected message: {message}" + ), + other => panic!("expected DashCoreBadResponseError, got {other:?}"), + } } #[test] From b442df4fffeac19ab1ff468ea3aabfa4802fc9a2 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sat, 3 Oct 2026 16:15:54 +0700 Subject: [PATCH 12/17] feat(drive-abci)!: create owner identities for Core v24 shared and multi-payout masternodes at protocol version 14 From v24 on, Dash Core lists shared masternodes, which have no owner, payout or collateral address, and extended-address masternodes, which have a `payouts` list instead of `payoutAddress`. create_owner_identity 1 fails on both with DashCoreBadResponseError, which fails the block. New generations, selected only by protocol version 14 (DRIVE_ABCI_METHOD_VERSIONS_V10, amended in place; only PLATFORM_V14 references it): - create_owner_identity 2, a copy of 1: no owner address gives no owner identity; an owner address and a single payout address (payoutAddress, else the only entry of a one-entry payout list) give the version 1 identity byte for byte (TRANSFER key 0, OWNER key 1); several payouts give an owner identity with only the OWNER key 1, key id 0 free. The effective-payout helper lives in v2, its only user. - update_masternode_identities 1, a copy of 0 that adds no owner identity when create_owner_identity gives none. Updates are processed as in 0: only a payoutAddress change rotates the TRANSFER key, and a `payouts` change is not acted on. The create_owner_identity dispatcher now returns Option. Versions 0 and 1 are unchanged and wrapped in Some; the only change to update_masternode_identities 0 unwraps it and treats None as CorruptedCodeExecution, unreachable because every protocol version that selects it selects create_owner_identity 0 or 1. transition_to_version_4 is unchanged; it only runs when crossing protocol version 4. Item 66 of PLATFORM_V14's doc records the change: protocol version 14 has to be active on a network before its Dash Core activates V24, since earlier versions keep failing on these masternodes. Tests at the latest version, through the dispatchers: - create_owner_identity v2: shared gives None; a single payout gives the payout address's identity, which equals version 1's at protocol version 13, byte for byte; several payouts give OWNER key 1 only. - update_masternode_identities v1: a shared masternode gets voter and operator identities and no owner identity; a change of its voting address and operator key, and its removal, succeed without an owner identity and leave its voter and operator keys disabled. - update_masternode_list with the Core v24 listdiff fixture: processed, with the identities above and no key change on payout-list diffs (protocol version 13 keeps failing on it, pinned in the previous commit). - The shipped-generation test now also checks the key layout a legacy masternode gets at protocol versions 3 (TRANSFER 0) and 13 (TRANSFER 0, OWNER 1), and the payout-list-diff test describes both generations. Red to green: with create_owner_identity 1 and update_masternode_identities 0 left in DRIVE_ABCI_METHOD_VERSIONS_V10 the six new-generation tests fail with DashCoreBadResponseError ("... has no payout address"); with the table change they pass. Co-Authored-By: Claude Opus 5.5 --- .../create_owner_identity/mod.rs | 53 ++- .../create_owner_identity/v2/mod.rs | 244 ++++++++++ .../update_masternode_identities/mod.rs | 23 +- .../update_masternode_identities/v0/mod.rs | 9 +- .../update_masternode_identities/v1/mod.rs | 417 ++++++++++++++++++ .../update_masternode_list/mod.rs | 249 +++++++++++ .../drive_abci_method_versions/v10.rs | 8 +- .../rs-platform-version/src/version/v14.rs | 18 + 8 files changed, 1000 insertions(+), 21 deletions(-) create mode 100644 packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v2/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v1/mod.rs diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs index d520d7f43f4..df59d420863 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/mod.rs @@ -1,5 +1,6 @@ mod v0; mod v1; +mod v2; use crate::error::execution::ExecutionError; use crate::error::Error; @@ -18,6 +19,11 @@ where /// This function constructs an identity for an owner using details from the masternode. /// It delegates to a version-specific method depending on the platform version. /// + /// Versions 0 and 1 always create an identity and fail on a masternode without the payout + /// address (and, in version 1, the owner address) they read. Version 2 (from protocol + /// version 14) creates none for a masternode without an owner address, such as a shared + /// masternode, and only the OWNER key for one paid to several payouts. + /// /// # Arguments /// /// * masternode - A reference to the masternode list item. @@ -25,12 +31,12 @@ where /// /// # Returns /// - /// * Result - Returns the constructed identity for the owner if successful. - /// Otherwise, returns an error. + /// * Result, Error> - The owner identity, or `None` when the masternode + /// has none (only version 2 returns `None`). Otherwise, returns an error. pub(crate) fn create_owner_identity( masternode: &MasternodeListItem, platform_version: &PlatformVersion, - ) -> Result { + ) -> Result, Error> { match platform_version .drive_abci .methods @@ -38,11 +44,12 @@ where .masternode_updates .create_owner_identity { - 0 => Self::create_owner_identity_v0(masternode, platform_version), - 1 => Self::create_owner_identity_v1(masternode, platform_version), + 0 => Self::create_owner_identity_v0(masternode, platform_version).map(Some), + 1 => Self::create_owner_identity_v1(masternode, platform_version).map(Some), + 2 => Self::create_owner_identity_v2(masternode, platform_version), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "create_owner_identity".to_string(), - known_versions: vec![0, 1], + known_versions: vec![0, 1, 2], received: version, })), } @@ -60,7 +67,11 @@ mod tests { use dpp::dashcore_rpc::dashcore_rpc_json::{ DMNPayout, DMNState, MasternodeListItem, MasternodeType, }; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{KeyID, Purpose}; use dpp::version::PlatformVersion; + use std::collections::BTreeMap; const OWNER_ADDRESS: [u8; 20] = [0x22; 20]; const PAYOUT_ADDRESS: [u8; 20] = [0x24; 20]; @@ -111,11 +122,12 @@ mod tests { } /// Generation 0 (protocol versions 1 to 3) builds the owner identity from the payout - /// address, and generation 1 (4 to 13) from the payout and the owner address. A masternode - /// Core lists without one of them fails with `DashCoreBadResponseError`, so its block - /// fails instead of the masternode getting an owner identity these generations do not - /// define: a shared masternode has neither address, and an extended-address masternode - /// has a payout list instead of a payout address. + /// address (TRANSFER key 0), and generation 1 (4 to 13) from the payout and the owner + /// address (TRANSFER key 0, OWNER key 1). A masternode Core lists without one of them + /// fails with `DashCoreBadResponseError`, so its block fails instead of the masternode + /// getting an owner identity these generations do not define: a shared masternode has + /// neither address, and an extended-address masternode has a payout list instead of a + /// payout address. #[test] fn should_fail_for_a_masternode_without_a_payout_or_owner_address_up_to_protocol_version_13() { let legacy = masternode(Some(OWNER_ADDRESS), Some(PAYOUT_ADDRESS), None); @@ -139,8 +151,23 @@ mod tests { let platform_version = PlatformVersion::get(protocol_version).expect("expected a known protocol version"); - Platform::::create_owner_identity(&legacy, platform_version) - .expect("a masternode with an owner and a payout address has an owner identity"); + let legacy_owner_identity_keys: BTreeMap)> = + Platform::::create_owner_identity(&legacy, platform_version) + .expect("expected to create the owner identity") + .expect("a masternode with an owner and a payout address has an owner identity") + .public_keys() + .iter() + .map(|(key_id, key)| (*key_id, (key.purpose(), key.data().to_vec()))) + .collect(); + let mut expected_keys = + BTreeMap::from([(0, (Purpose::TRANSFER, PAYOUT_ADDRESS.to_vec()))]); + if protocol_version == 13 { + expected_keys.insert(1, (Purpose::OWNER, OWNER_ADDRESS.to_vec())); + } + assert_eq!( + legacy_owner_identity_keys, expected_keys, + "protocol version {protocol_version}" + ); let mut cases = vec![ (&shared, "has no payout address"), diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v2/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v2/mod.rs new file mode 100644 index 00000000000..f3df1718813 --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/create_owner_identity/v2/mod.rs @@ -0,0 +1,244 @@ +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::rpc::core::CoreRPCLike; +use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeListItem}; +use dpp::identity::accessors::IdentityGettersV0; +use dpp::identity::Identity; +use dpp::version::PlatformVersion; + +impl Platform +where + C: CoreRPCLike, +{ + /// Creates the owner identity of a masternode like version 1, and also decides it for the + /// masternodes version 1 fails on. + /// + /// Version 1 reads a payout address and an owner address and fails with + /// `DashCoreBadResponseError` when either is absent. Dash Core lists such masternodes from + /// v24 on: a shared masternode has no owner, payout or collateral address (its owners are + /// its share holders), and an extended-address masternode has a payout list instead of a + /// payout address. This version gives: + /// + /// - no owner address: no owner identity (`None`), as there is no owner key to hold; the + /// voter and operator identities of the masternode do not depend on it; + /// - an owner address and a single payout address (see `effective_payout_address`): the + /// identity of version 1, TRANSFER key id 0 and OWNER key id 1, byte for byte; + /// - an owner address and several payouts: an identity with only the OWNER key id 1, as no + /// single address can hold the TRANSFER key; key id 0 stays free. + pub(super) fn create_owner_identity_v2( + masternode: &MasternodeListItem, + platform_version: &PlatformVersion, + ) -> Result, Error> { + let Some(owner_address) = masternode.state.owner_address else { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "create_owner_identity_v2", + "no owner identity: the masternode has no owner address" + ); + return Ok(None); + }; + let owner_identifier = Self::get_owner_identifier(masternode)?; + let mut identity = Identity::create_basic_identity(owner_identifier, platform_version)?; + match effective_payout_address(&masternode.state) { + Some(payout_address) => identity.add_public_keys([ + Self::get_owner_identity_withdrawal_key(payout_address, 0, platform_version)?, + Self::get_owner_identity_owner_key(owner_address, 1, platform_version)?, + ]), + None => { + tracing::debug!( + pro_tx_hash = %masternode.pro_tx_hash, + method = "create_owner_identity_v2", + "owner identity with only the OWNER key: no single payout address" + ); + identity.add_public_keys([Self::get_owner_identity_owner_key( + owner_address, + 1, + platform_version, + )?]); + } + } + Ok(Some(identity)) + } +} + +/// The one address the owner reward of a masternode is paid to, which its owner identity's +/// TRANSFER key holds: the payout address, or the only entry of a payout list with one entry +/// (Core turns a payout address into such a list when a masternode moves to extended +/// addresses). `None` for a shared masternode, which has neither, and for a reward split +/// between several payouts. +/// +/// The stored masternode list does not keep payout lists, so this is only meaningful for a +/// masternode as Core reported it, never for one read from the stored list. +fn effective_payout_address(state: &DMNState) -> Option<[u8; 20]> { + match (state.payout_address, state.payouts.as_deref()) { + (Some(payout_address), _) => Some(payout_address), + (None, Some([payout])) => Some(payout.address), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use crate::platform_types::platform::Platform; + use crate::rpc::core::MockCoreRPCLike; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, MasternodeListItem, MasternodeType, + }; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{Identity, KeyID, Purpose}; + use dpp::prelude::Identifier; + use dpp::serialization::PlatformSerializable; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + const OWNER_ADDRESS: [u8; 20] = [0x22; 20]; + const PAYOUT_ADDRESS: [u8; 20] = [0x24; 20]; + const SECOND_PAYOUT_ADDRESS: [u8; 20] = [0x25; 20]; + + /// A Regular masternode as Core lists it: a shared one has neither owner, payout nor + /// collateral address; an extended-address one has `payouts` instead of `payout_address`. + fn masternode( + owner_address: Option<[u8; 20]>, + payout_address: Option<[u8; 20]>, + payouts: Option>, + ) -> MasternodeListItem { + MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash: ProTxHash::from_byte_array([0x21; 32]), + collateral_hash: Txid::from_byte_array([0x27; 32]), + collateral_index: 0, + collateral_address: owner_address.map(|_| [0x28; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address, + voting_address: [0x23; 20], + payout_address, + payouts, + pub_key_operator: vec![0x26; 48], + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + } + } + + fn p2pkh_payout(key_hash: [u8; 20], reward: u16) -> DMNPayout { + DMNPayout { + address: key_hash, + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array(key_hash)), + reward, + } + } + + fn legacy_masternode() -> MasternodeListItem { + masternode(Some(OWNER_ADDRESS), Some(PAYOUT_ADDRESS), None) + } + + fn create_owner_identity( + masternode: &MasternodeListItem, + platform_version: &PlatformVersion, + ) -> Option { + Platform::::create_owner_identity(masternode, platform_version) + .expect("expected to decide the owner identity") + } + + fn serialized(identity: Option) -> Vec { + identity + .expect("expected an owner identity") + .serialize_to_bytes() + .expect("expected to serialize the owner identity") + } + + /// The owner identity holds the owner's keys, and a shared masternode has no owner key + /// (its owners are its share holders), so it gets no owner identity. + #[test] + fn should_create_no_owner_identity_for_a_shared_masternode() { + let shared = masternode(None, None, None); + + assert_eq!( + create_owner_identity(&shared, PlatformVersion::latest()), + None + ); + } + + /// Core turns a payout address into a payout list with one entry when a masternode moves + /// to extended addresses, so a single payout must give the owner identity a payout address + /// gives, which is the identity version 1 creates, byte for byte. + #[test] + fn should_create_the_same_owner_identity_for_a_single_payout_as_for_a_payout_address() { + let platform_version = PlatformVersion::latest(); + let single_payout = masternode( + Some(OWNER_ADDRESS), + None, + Some(vec![p2pkh_payout(PAYOUT_ADDRESS, 10000)]), + ); + + let legacy_owner_identity = serialized(create_owner_identity( + &legacy_masternode(), + platform_version, + )); + + assert_eq!( + serialized(create_owner_identity(&single_payout, platform_version)), + legacy_owner_identity + ); + assert_eq!( + serialized(create_owner_identity( + &legacy_masternode(), + PlatformVersion::get(13).expect("expected protocol version 13"), + )), + legacy_owner_identity, + "a payout address must give the identity version 1 gives" + ); + } + + /// Several payouts have no single address for the TRANSFER key id 0, so the owner identity + /// gets only the OWNER key id 1 a payout address's owner identity has, and key id 0 stays + /// free. + #[test] + fn should_create_an_owner_identity_with_only_the_owner_key_for_several_payouts() { + let platform_version = PlatformVersion::latest(); + let several_payouts = masternode( + Some(OWNER_ADDRESS), + None, + Some(vec![ + p2pkh_payout(PAYOUT_ADDRESS, 7000), + p2pkh_payout(SECOND_PAYOUT_ADDRESS, 3000), + ]), + ); + + let owner_key = create_owner_identity(&legacy_masternode(), platform_version) + .expect("expected an owner identity for a payout address") + .public_keys() + .get(&1) + .expect("expected the OWNER key id 1") + .clone(); + assert_eq!(owner_key.purpose(), Purpose::OWNER); + assert_eq!(owner_key.data().as_slice(), OWNER_ADDRESS.as_slice()); + + let several_payouts_owner_identity = + create_owner_identity(&several_payouts, platform_version) + .expect("expected an owner identity for several payouts"); + assert_eq!( + several_payouts_owner_identity.id(), + Identifier::from([0x21; 32]), + "the owner identity id is the ProTx hash" + ); + assert_eq!( + several_payouts_owner_identity.public_keys(), + &BTreeMap::from([(1 as KeyID, owner_key)]) + ); + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs index 1b635109277..69ce72d94fa 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/mod.rs @@ -11,12 +11,17 @@ use drive::grovedb::Transaction; use std::collections::BTreeMap; mod v0; +mod v1; impl Platform where C: CoreRPCLike, { /// Update of the masternode identities + /// + /// Version 0 adds an owner identity for every added masternode. Version 1 (from protocol + /// version 14) adds none for a masternode `create_owner_identity` gives none, such as a + /// shared masternode. pub(in crate::execution) fn update_masternode_identities( &self, masternode_diff: MasternodeListDiff, @@ -41,9 +46,17 @@ where transaction, platform_version, ), + 1 => self.update_masternode_identities_v1( + masternode_diff, + removed_masternodes, + block_info, + platform_state, + transaction, + platform_version, + ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "update_masternode_identities".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -68,10 +81,10 @@ mod tests { use std::collections::BTreeMap; /// A payout change of an extended-address masternode reaches Drive only as `payouts`, - /// never as `payout_address`. Every protocol version rotates the owner identity's TRANSFER - /// key only on a `payout_address` change; acting on `payouts` as well would change what a - /// block does to owner identities. Protocol version 13 is pinned because a node running it - /// against Dash Core v24 receives such diffs. + /// never as `payout_address`. Both generations rotate the owner identity's TRANSFER key + /// only on a `payout_address` change and leave the owner identity's keys as they are on a + /// `payouts` change. Protocol version 13 is pinned because a node running it against Dash + /// Core v24 receives such diffs. #[test] fn should_not_change_owner_identity_keys_on_a_payout_list_change() { for platform_version in [ diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs index 6cdff83e2f1..a4182e82e64 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v0/mod.rs @@ -1,3 +1,4 @@ +use crate::error::execution::ExecutionError; use crate::error::Error; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; @@ -54,7 +55,13 @@ where let mut drive_operations = vec![]; for masternode in added_mns { - let owner_identity = Self::create_owner_identity(&masternode, platform_version)?; + // Protocol versions 1 to 13, the ones that select this generation, select + // create_owner_identity 0 or 1, which create an identity or fail, and never return + // `None`. + let owner_identity = Self::create_owner_identity(&masternode, platform_version)? + .ok_or(Error::Execution(ExecutionError::CorruptedCodeExecution( + "create_owner_identity 0 and 1 always create an owner identity", + )))?; tracing::trace!( identity = ?owner_identity, diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v1/mod.rs new file mode 100644 index 00000000000..72d61c97c0f --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_identities/update_masternode_identities/v1/mod.rs @@ -0,0 +1,417 @@ +use crate::error::Error; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use crate::rpc::core::CoreRPCLike; + +use dpp::block::block_info::BlockInfo; +use dpp::dashcore::ProTxHash; +use dpp::dashcore_rpc::dashcore_rpc_json::MasternodeListDiff; +use dpp::dashcore_rpc::json::MasternodeListItem; + +use dpp::version::PlatformVersion; + +use drive::util::batch::DriveOperation::IdentityOperation; +use drive::util::batch::IdentityOperationType::AddNewIdentity; + +use crate::platform_types::platform_state::PlatformStateV0Methods; + +use dpp::dashcore::hashes::Hash; +use drive::grovedb::Transaction; +use std::collections::BTreeMap; +use tracing::Level; + +impl Platform +where + C: CoreRPCLike, +{ + /// Updates the masternode identities like version 0, except that an added masternode may + /// have no owner identity. + /// + /// Version 0 is paired with `create_owner_identity` 0 and 1, which create an owner identity + /// for every masternode or fail. `create_owner_identity` 2 creates none for a masternode + /// without an owner address, such as a shared masternode, and this version then adds only + /// its voter and operator identities. Updates and removals are processed exactly as in + /// version 0: only a `payout_address` change rotates the owner identity's TRANSFER key, and + /// a `payouts` change is not acted on. + pub(super) fn update_masternode_identities_v1( + &self, + masternode_diff: MasternodeListDiff, + removed_masternodes: &BTreeMap, + block_info: &BlockInfo, + platform_state: Option<&PlatformState>, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + let MasternodeListDiff { + mut added_mns, + mut updated_mns, + .. + } = masternode_diff; + + let span = tracing::span!(Level::TRACE, "update_masternode_identities"); + let _enter = span.enter(); + + // We should don't trust the order of added mns or updated mns + + // Sort added_mns based on pro_tx_hash + added_mns.sort_by_key(|mn| mn.pro_tx_hash); + + // Sort updated_mns based on pro_tx_hash (the first element of the tuple) + updated_mns.sort_by_key(|mn| mn.0); + + let mut drive_operations = vec![]; + + for masternode in added_mns { + let owner_identity = Self::create_owner_identity(&masternode, platform_version)?; + + tracing::trace!( + identity = ?owner_identity, + method = "update_masternode_identities_v1", + "create owner identity" + ); + + let voter_identity = Self::create_voter_identity_from_masternode_list_item( + &masternode, + platform_version, + )?; + + tracing::trace!( + identity = ?voter_identity, + method = "update_masternode_identities_v1", + "create voter identity" + ); + + let operator_identity = Self::create_operator_identity(&masternode, platform_version)?; + + tracing::trace!( + identity = ?operator_identity, + method = "update_masternode_identities_v1", + "create operator identity" + ); + + if let Some(owner_identity) = owner_identity { + drive_operations.push(IdentityOperation(AddNewIdentity { + identity: owner_identity, + is_masternode_identity: true, + })); + } + + drive_operations.push(IdentityOperation(AddNewIdentity { + identity: voter_identity, + is_masternode_identity: true, + })); + + drive_operations.push(IdentityOperation(AddNewIdentity { + identity: operator_identity, + is_masternode_identity: true, + })); + } + + if let Some(platform_state) = platform_state { + // On initialization there is no platform state, but we also don't need to update + // masternode identities. + for update in updated_mns.iter() { + let (pro_tx_hash, state_diff) = update; + if let Some(new_withdrawal_address) = state_diff.payout_address { + let owner_identifier: [u8; 32] = pro_tx_hash.to_byte_array(); + self.update_owner_withdrawal_address( + owner_identifier, + new_withdrawal_address, + block_info, + transaction, + &mut drive_operations, + platform_version, + )?; + }; + + self.update_voter_identity( + update, + block_info, + platform_state, + transaction, + &mut drive_operations, + platform_version, + )?; + if state_diff.platform_node_id.is_some() + || state_diff.operator_payout_address.is_some() + || state_diff.pub_key_operator.is_some() + { + self.update_operator_identity( + pro_tx_hash, + state_diff.pub_key_operator.as_ref(), + state_diff.operator_payout_address, + state_diff.platform_node_id, + platform_state, + transaction, + &mut drive_operations, + platform_version, + )?; + } + } + + for masternode in removed_masternodes.values() { + self.disable_identity_keys( + masternode, + block_info, + transaction, + &mut drive_operations, + platform_version, + )?; + } + } + + let previous_fee_versions = platform_state.map(|state| state.previous_fee_versions()); + self.drive.apply_drive_operations( + drive_operations, + true, + block_info, + Some(transaction), + platform_version, + previous_fee_versions, + )?; + + let height = block_info.height; + tracing::debug!(height, "Updated masternode identities"); + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use crate::config::PlatformConfig; + use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods}; + use crate::test::helpers::setup::TestPlatformBuilder; + use dpp::block::block_info::BlockInfo; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{Network, ProTxHash, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, + }; + use dpp::identifier::MasternodeIdentifiers; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::Identity; + use dpp::prelude::Identifier; + use dpp::version::PlatformVersion; + use std::collections::BTreeMap; + + const PRO_TX_HASH: [u8; 32] = [0x61; 32]; + const VOTING_ADDRESS: [u8; 20] = [0x62; 20]; + const PUB_KEY_OPERATOR: [u8; 48] = [0x63; 48]; + + /// A shared masternode as Core lists it: no owner, payout or collateral address. + fn shared_masternode() -> MasternodeListItem { + MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + collateral_hash: Txid::from_byte_array([0x64; 32]), + collateral_index: 0, + collateral_address: None, + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: None, + voting_address: VOTING_ADDRESS, + payout_address: None, + payouts: None, + pub_key_operator: PUB_KEY_OPERATOR.to_vec(), + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + } + } + + fn masternode_list_diff( + added_mns: Vec, + removed_mns: Vec, + updated_mns: Vec<(ProTxHash, DMNStateDiff)>, + ) -> MasternodeListDiff { + MasternodeListDiff { + base_height: 0, + block_height: 1, + added_mns, + removed_mns, + updated_mns, + } + } + + /// A shared masternode has no owner address, so `create_owner_identity` gives it no owner + /// identity. Its voter and operator identities do not depend on the owner and are added as + /// for any masternode. + #[test] + fn should_add_the_voter_and_operator_identities_but_no_owner_identity_for_a_shared_masternode() + { + let platform_version = PlatformVersion::latest(); + let platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + + let transaction = platform.drive.grove.start_transaction(); + platform + .update_masternode_identities( + masternode_list_diff(vec![shared_masternode()], vec![], vec![]), + &BTreeMap::new(), + &BlockInfo::default(), + None, + &transaction, + platform_version, + ) + .expect("expected to add the masternode identities"); + + let identity_exists = |identity_id: Identifier| { + platform + .drive + .fetch_full_identity( + identity_id.to_buffer(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch an identity") + .is_some() + }; + assert!( + !identity_exists(PRO_TX_HASH.into()), + "a shared masternode has no owner identity" + ); + assert!( + identity_exists(Identifier::create_voter_identifier( + &PRO_TX_HASH, + &VOTING_ADDRESS + )), + "the voter identity must be added" + ); + assert!( + identity_exists(Identifier::create_operator_identifier( + &PRO_TX_HASH, + &PUB_KEY_OPERATOR + )), + "the operator identity must be added" + ); + } + + /// Only a `payout_address` change reads the owner identity, and Core prints none for a + /// shared masternode. A change of its voting address and operator key, and its removal, + /// update its voter and operator identities as for any masternode and do not fail for want + /// of an owner identity. + #[test] + fn should_update_and_remove_a_shared_masternode_without_an_owner_identity() { + let platform_version = PlatformVersion::latest(); + let platform = TestPlatformBuilder::new() + .build_with_mock_rpc() + .set_genesis_state(); + + let transaction = platform.drive.grove.start_transaction(); + let masternode = shared_masternode(); + let pro_tx_hash = masternode.pro_tx_hash; + platform + .update_masternode_identities( + masternode_list_diff(vec![masternode.clone()], vec![], vec![]), + &BTreeMap::new(), + &BlockInfo::default(), + None, + &transaction, + platform_version, + ) + .expect("expected to add the masternode identities"); + + let mut platform_state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + platform_state.insert_masternode(masternode.clone()); + + let new_voting_address = [0x65; 20]; + let new_pub_key_operator = [0x66; 48]; + let state_diff = DMNStateDiff { + service: None, + registered_height: None, + last_paid_height: None, + consecutive_payments: None, + pose_penalty: None, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: None, + owner_address: None, + voting_address: Some(new_voting_address), + payout_address: None, + payouts: None, + pub_key_operator: Some(new_pub_key_operator.to_vec()), + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }; + platform + .update_masternode_identities( + masternode_list_diff(vec![], vec![], vec![(pro_tx_hash, state_diff.clone())]), + &BTreeMap::new(), + &BlockInfo::default(), + Some(&platform_state), + &transaction, + platform_version, + ) + .expect("expected to update the identities of the shared masternode"); + + let mut updated_masternode = masternode; + updated_masternode.state.apply_diff(state_diff); + platform_state.insert_masternode(updated_masternode.clone()); + platform + .update_masternode_identities( + masternode_list_diff(vec![], vec![pro_tx_hash], vec![]), + &BTreeMap::from([(pro_tx_hash, updated_masternode)]), + &BlockInfo::default(), + Some(&platform_state), + &transaction, + platform_version, + ) + .expect("expected to remove the shared masternode"); + + let fetch_identity = |identity_id: Identifier| -> Option { + platform + .drive + .fetch_full_identity( + identity_id.to_buffer(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch an identity") + }; + let has_an_enabled_key = |identity_id: Identifier| { + fetch_identity(identity_id) + .expect("expected the identity to exist") + .public_keys() + .values() + .any(|key| !key.is_disabled()) + }; + assert_eq!(fetch_identity(PRO_TX_HASH.into()), None); + assert!( + !has_an_enabled_key(Identifier::create_voter_identifier( + &PRO_TX_HASH, + &new_voting_address + )), + "the removed masternode's voter identity keys must be disabled" + ); + assert!( + !has_an_enabled_key(Identifier::create_operator_identifier( + &PRO_TX_HASH, + &new_pub_key_operator + )), + "the removed masternode's operator identity keys must be disabled" + ); + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index 8494a69df19..1071fd1cf6c 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -73,13 +73,27 @@ where mod test { use super::*; use crate::config::PlatformConfig; + use crate::platform_types::platform_state::platform_state_for_saving::v2::{ + deserialize_masternode_entry, serialize_masternode_entry, + }; + use crate::platform_types::platform_state::PlatformStateV0Methods; + use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::ProTxHash; use dpp::dashcore_rpc::json::MasternodeListDiff; + use dpp::identifier::MasternodeIdentifiers; + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; + use dpp::identity::{KeyID, Purpose}; + use dpp::prelude::Identifier; + use std::collections::BTreeMap; use std::env; use std::fs::File; use std::io::BufReader; use std::path::PathBuf; + use std::str::FromStr; #[test] fn test_update_masternode_list() { @@ -271,4 +285,239 @@ mod test { other => panic!("expected DashCoreBadResponseError, got {other:?}"), } } + + /// Dash Core v24 prints masternodes that differ from every earlier list: a shared + /// masternode has no owner, payout or collateral address; an extended-address masternode + /// has a `payouts` list instead of `payoutAddress`; a Tor primary address is not an + /// `ip:port` `service`; an Evo without addresses has `-1` platform ports. From protocol + /// version 14 the list is processed rather than failing the block, and the masternode + /// identities are: + /// - shared: voter and operator identities, no owner identity (it has no owner key); + /// - one payout: the same owner identity as a legacy payout address (TRANSFER key 0, + /// OWNER key 1); + /// - several payouts: an owner identity with only the OWNER key 1; + /// - a later payout change through `payouts`: no key change, since only a `payoutAddress` + /// change rotates the TRANSFER key. + #[test] + fn should_update_masternode_identities_from_a_core_v24_masternode_list() { + let platform_version = PlatformVersion::latest(); + + let platform = platform_with_core_v24_masternode_list(platform_version); + + let genesis_core_block_height = 1250; + let first_block_core_block_height = 1260; + + let transaction = platform.drive.grove.start_transaction(); + + let mut init_chain_platform_state = platform.state.load().as_ref().clone(); + + platform + .update_masternode_list( + None, + &mut init_chain_platform_state, + genesis_core_block_height, + true, + &BlockInfo { + height: 1, + core_height: genesis_core_block_height, + time_ms: 1, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list"); + + let platform_state = init_chain_platform_state.clone(); + let mut block_platform_state = platform_state.clone(); + + platform + .update_masternode_list( + Some(&platform_state), + &mut block_platform_state, + first_block_core_block_height, + false, + &BlockInfo { + height: 1, + core_height: first_block_core_block_height, + time_ms: 2, + ..Default::default() + }, + &transaction, + platform_version, + ) + .expect("expected to process the Core v24 masternode list diff"); + + assert_eq!(block_platform_state.full_masternode_list().len(), 7); + assert_eq!(block_platform_state.hpmn_masternode_list().len(), 3); + + // An Evo without addresses prints `-1` platform ports, which read as no port, so it + // cannot be a validator, like any Evo without ports. The legacy Evo is one, with the + // ports and address the diff gave it. + let validator = |pro_tx_hash: &str| { + ValidatorV0::new_validator_if_masternode_in_state( + ProTxHash::from_str(pro_tx_hash).expect("expected a pro tx hash"), + None, + &block_platform_state, + ) + }; + assert!( + validator("6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706").is_none() + ); + let legacy_evo_validator = + validator("ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0") + .expect("expected the legacy Evo to be a validator"); + assert_eq!(legacy_evo_validator.node_ip, "192.0.2.40"); + assert_eq!(legacy_evo_validator.platform_p2p_port, 36656); + assert_eq!(legacy_evo_validator.platform_http_port, 1443); + + // Every parsed masternode survives a store and reload, except for the payout list and + // nested addresses, which are not stored. + for masternode in block_platform_state.full_masternode_list().values() { + let bytes = serialize_masternode_entry(masternode, platform_version) + .expect("expected to serialize the masternode entry"); + let mut expected = masternode.clone(); + expected.state.payouts = None; + expected.state.addresses = None; + assert_eq!( + deserialize_masternode_entry(&bytes) + .expect("expected to deserialize the masternode entry"), + expected + ); + } + + let hash160 = |hex: &str| -> Vec { hex::decode(hex).expect("expected hex") }; + + // The keys of an identity, none of them disabled; `None` if there is no identity. + let identity_keys = + |identity_id: Identifier| -> Option)>> { + platform + .drive + .fetch_full_identity( + identity_id.to_buffer(), + Some(&transaction), + platform_version, + ) + .expect("expected to fetch an identity") + .map(|identity| { + identity + .public_keys() + .iter() + .map(|(key_id, key)| { + assert!(key.disabled_at().is_none(), "key {key_id} is disabled"); + (*key_id, (key.purpose(), key.data().to_vec())) + }) + .collect() + }) + }; + + let owner_key = |hex: &str| (Purpose::OWNER, hash160(hex)); + let transfer_key = |hex: &str| (Purpose::TRANSFER, hash160(hex)); + + let voting_421c = "421c03add2c804421451c4e022258778175e60d8"; + let voting_064c = "064cd21ff210c1a92adaea49a4768a41c2aef1bc"; + + // (pro_tx_hash, voting key hash, operator public key, owner identity keys) + let expected = [ + ( + // Legacy Evo whose payout address the diff moves into `payouts` + "ca3d32c4f2ef62eaf736bf41bb3235a832ec1eb6d8dfaccd4d3555e70f6757b0", + voting_421c, + "8ed3f0c208efbcfc815cbfb94490dc68cf2e29d44dd9f8a91e20e06057aa110d7062c8ab7ccc85a9ff0c88760157f563".to_string(), + Some(BTreeMap::from([ + (0, transfer_key("75d57974b6e29a4a70df57a3b11195ce0a0dc817")), + (1, owner_key("1f67d90f35e3c5070c368ae6f3635aac357e47df")), + ])), + ), + ( + // One payout, which the diff replaces with two + "27978dd892b7c876c238be1a6141461c2824f3497dd0058e160979bc8f0a0bef", + voting_421c, + "a792ce1af5f7bb9281053b3934cb8b08d00d075a56498e1a525388ce467f188e8a80911fd96a20982baa9b9678452534".to_string(), + Some(BTreeMap::from([ + (0, transfer_key("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")), + (1, owner_key("b740a2ab3f631e4dfe15debf746364f1d8352c50")), + ])), + ), + ( + // Shared + "a4d26868017c0ccffe2efe50944ef4211834660cca834c6e9f86dec6a88246fa", + voting_421c, + "b1".repeat(48), + None, + ), + ( + // One payout, which the diff replaces with another single payout + "813a7c3f28817988a8e6ce66e07e43e261e78398373bfbaae94c898645111d6b", + voting_421c, + "b2".repeat(48), + Some(BTreeMap::from([ + (0, transfer_key("5555555555555555555555555555555555555555")), + (1, owner_key("0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a")), + ])), + ), + ( + // Tor primary address, two payouts; the owner and voting keys are the same + "3023ffd989974768b0dfc347410ad923fa6d3f1eee90180bd0c435e81cb1a82f", + voting_064c, + "b3".repeat(48), + Some(BTreeMap::from([(1, owner_key(voting_064c))])), + ), + ( + // Evo without addresses, one payout + "6f1757595185032c808321af3e2e8468fae10b8f91e2a657d7a4c7122f4b2706", + voting_421c, + "b4".repeat(48), + Some(BTreeMap::from([ + (0, transfer_key("9999999999999999999999999999999999999999")), + (1, owner_key("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")), + ])), + ), + ( + // Evo with an IPv6 primary address, two payouts + "aecd2830b843e6a84283ba290492a213e355cea7c5026e6118a21e1bfbc36783", + voting_421c, + "b5".repeat(48), + Some(BTreeMap::from([( + 1, + owner_key("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"), + )])), + ), + ]; + + for (pro_tx_hash, voting_key_hash, operator_public_key, owner_identity_keys) in expected { + let pro_tx_hash = ProTxHash::from_str(pro_tx_hash) + .expect("expected a pro tx hash") + .to_byte_array(); + let voting_key_hash: [u8; 20] = hash160(voting_key_hash) + .try_into() + .expect("expected 20 bytes"); + let operator_public_key = hex::decode(operator_public_key).expect("expected hex"); + + assert_eq!( + identity_keys(pro_tx_hash.into()), + owner_identity_keys, + "owner identity of {}", + hex::encode(pro_tx_hash) + ); + assert!( + identity_keys(Identifier::create_voter_identifier( + &pro_tx_hash, + &voting_key_hash + )) + .is_some(), + "voter identity of {}", + hex::encode(pro_tx_hash) + ); + assert!( + identity_keys(Identifier::create_operator_identifier( + &pro_tx_hash, + &operator_public_key + )) + .is_some(), + "operator identity of {}", + hex::encode(pro_tx_hash) + ); + } + } } diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs index 95493a875cf..9c84986f95a 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs @@ -22,6 +22,10 @@ use crate::version::drive_abci_versions::drive_abci_method_versions::{ /// `decode_raw_state_transitions` 1 refuses bytes left over after a raw state transition. /// `add_distribute_storage_fee_to_epochs_operations` 1 claws each pending storage refund back /// from the epochs it was priced for. +/// `create_owner_identity` 2 and `update_masternode_identities` 1 decide the owner identity of +/// the masternodes Dash Core lists from v24 on, on which version 1 fails: none without an owner +/// address (a shared masternode), the version 1 identity for a payout list with one entry, and +/// only the OWNER key for several payouts. /// Everything else matches `DRIVE_ABCI_METHOD_VERSIONS_V9`. pub const DRIVE_ABCI_METHOD_VERSIONS_V10: DriveAbciMethodVersions = DriveAbciMethodVersions { engine: DriveAbciEngineMethodVersions { @@ -47,10 +51,10 @@ pub const DRIVE_ABCI_METHOD_VERSIONS_V10: DriveAbciMethodVersions = DriveAbciMet get_voter_identifier_from_masternode_list_item: 0, get_operator_identifier_from_masternode_list_item: 0, create_operator_identity: 0, - create_owner_identity: 1, + create_owner_identity: 2, // changed in v14: no owner identity without an owner address; only the OWNER key for several payouts create_voter_identity: 0, disable_identity_keys: 0, - update_masternode_identities: 0, + update_masternode_identities: 1, // changed in v14: adds no owner identity where create_owner_identity gives none update_operator_identity: 0, update_owner_withdrawal_address: 1, update_voter_identity: 0, diff --git a/packages/rs-platform-version/src/version/v14.rs b/packages/rs-platform-version/src/version/v14.rs index adba75bd4c5..931b9c214af 100644 --- a/packages/rs-platform-version/src/version/v14.rs +++ b/packages/rs-platform-version/src/version/v14.rs @@ -1729,6 +1729,24 @@ pub const PROTOCOL_VERSION_14: ProtocolVersion = 14; /// with `==`. Every step is inert without a derived index property, which only generation 3 /// declares. /// +/// 66. **Owner identities for shared and extended-address masternodes**: from +/// v24 on, Dash Core lists shared masternodes, which have no owner, payout +/// or collateral address, and extended-address masternodes, which have a +/// `payouts` list instead of a `payoutAddress`. `create_owner_identity` 1 +/// needs both addresses and fails on such a masternode with +/// `DashCoreBadResponseError`, which fails the block. With +/// `create_owner_identity` 2 and `update_masternode_identities` 1 +/// (`DRIVE_ABCI_METHOD_VERSIONS_V10`), a masternode without an owner +/// address gets no owner identity, only its voter and operator identities; +/// one with an owner address and a single payout address (the payout +/// address, else the only entry of a one-entry payout list) gets the +/// version 1 identity, TRANSFER key id 0 and OWNER key id 1, byte for byte; +/// and one paid to several payouts gets an owner identity with only the +/// OWNER key id 1. Updates are unchanged: only a `payoutAddress` change +/// rotates the TRANSFER key, and a `payouts` change is not acted on. This +/// version must be active on a network before its Dash Core activates V24, +/// since earlier versions keep failing on these masternodes. +/// /// The app-connect system contract (`SystemDataContract::AppConnect`, schema v1) /// carries only the wallet's `loginKeyResponse`: a flat indexOnly entry keyed by /// the app's ephemeral key hash and the responding identity, with the wallet's From 279687b1b3c383c7b0a3646f3836f9de028006d7 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sun, 4 Oct 2026 14:01:23 +0700 Subject: [PATCH 13/17] test(drive-abci): verify owner-key transition at protocol version 4 Pin the historical transition regression and its initial storage to PV4 so the successful OWNER-key insertion checks the protocol that first shipped it. The targeted test passes. Test-only change; no production behavior delta. --- .../perform_events_on_first_block_of_protocol_change/v0/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 76eacdf65a3..26b3fbc38c2 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 @@ -1058,8 +1058,9 @@ mod tests { use dpp::identity::{Identity, KeyID, Purpose}; use std::collections::BTreeMap; - let platform_version = PlatformVersion::latest(); + let platform_version = PlatformVersion::get(4).expect("expected protocol version 4"); let platform = TestPlatformBuilder::new() + .with_initial_protocol_version(platform_version.protocol_version) .build_with_mock_rpc() .set_genesis_state(); From cab6b207d67e3fb5857b9906e498ec07ff870ad6 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sun, 4 Oct 2026 14:20:34 +0700 Subject: [PATCH 14/17] feat(drive-abci)!: resolve nested masternode ports at protocol version 14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Select update_masternode_list v1 only through PV14's existing method table. Resolve nested-first platform ports in that generation before inserting masternodes; keep shipped state update, storage conversion, accessors and validator construction on their historical flat-field behavior. Project the previous masternode state through its persisted form before applying a diff, so transient PV13 addresses cannot distinguish a running node from one restarted before activation. Compare projected states for no-op detection and per-entry writes. Preserve legacy fallback, zero ports and existing-validator refresh triggers, including the HTTPS-only lag. Test would have caught this in CI: ✖ before fix, ✔ after. The dispatcher regression observed PV13 ports 36656/8443 instead of its historical 26656/443 before the generation split; it now verifies additions, nested-only updates and flat-only updates on both sides of the gate. Validation: 59 targeted tests passed, none ignored, including a public proposal/finalize/commit activation with pre/post-update restarts and matching app hashes. Scoped rustfmt and drive-abci clippy with all features/targets and -D warnings passed. Two independent code reviews found no blockers. --- .../update_masternode_list/mod.rs | 231 +-- .../update_state_masternode_list/mod.rs | 3 +- .../update_state_masternode_list/v0/mod.rs | 650 +------- .../update_state_masternode_list/v1/mod.rs | 1325 +++++++++++++++++ .../update_masternode_list/v1/mod.rs | 93 ++ .../versioning_tests.rs | 170 +++ .../src/platform_types/masternode/v0/mod.rs | 217 +-- .../platform_state/accessors.rs | 30 +- .../src/platform_types/validator/v0/mod.rs | 251 +--- .../platform_types/validator_set/v0/mod.rs | 4 +- .../test_cases/upgrade_fork_tests.rs | 303 +++- .../drive_abci_method_versions/v10.rs | 2 +- .../rs-platform-version/src/version/v14.rs | 10 + 13 files changed, 2088 insertions(+), 1201 deletions(-) create mode 100644 packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v1/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs create mode 100644 packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/versioning_tests.rs diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs index f0e98a1885d..db6ffa4cbc8 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/mod.rs @@ -1,5 +1,6 @@ mod update_state_masternode_list; mod v0; +mod v1; use crate::error::execution::ExecutionError; use crate::error::Error; @@ -60,9 +61,18 @@ where transaction, platform_version, ), + 1 => self.update_masternode_list_v1( + platform_state, + block_platform_state, + core_block_height, + is_init_chain, + block_info, + transaction, + platform_version, + ), version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch { method: "update_masternode_list".to_string(), - known_versions: vec![0], + known_versions: vec![0, 1], received: version, })), } @@ -80,10 +90,10 @@ mod test { use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; - use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; - use dpp::core_types::validator_set::v0::ValidatorSetV0; use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash}; use dpp::dashcore_rpc::json::{ @@ -423,117 +433,127 @@ mod test { /// be exactly what those binaries build. #[test] fn should_build_the_validators_earlier_binaries_built_from_mainnet_masternode_lists() { - let platform_version = PlatformVersion::latest(); - let init_core_height = 2128896; - let next_core_height = 2129440; - let quorum_hash = QuorumHash::from_byte_array([0x55; 32]); + for platform_version in [ + PlatformVersion::get(13).expect("protocol version 13"), + PlatformVersion::latest(), + ] { + let init_core_height = 2128896; + let next_core_height = 2129440; + let quorum_hash = QuorumHash::from_byte_array([0x55; 32]); + + for core_23_addresses in [false, true] { + let context = if core_23_addresses { + "with Core 23 addresses" + } else { + "as listed" + }; + let mut evonodes = BTreeSet::new(); + let init_list = + mainnet_list_diff("1-2128896.json", core_23_addresses, &mut evonodes); + let list_diff = + mainnet_list_diff("2128896-2129440.json", core_23_addresses, &mut evonodes); + assert!( + list_diff + .updated_mns + .iter() + .any(|(pro_tx_hash, state_diff)| { + evonodes.contains(&pro_tx_hash.to_string()) + && state_diff.service.is_some() + }), + "the diff moves an evonode" + ); - for core_23_addresses in [false, true] { - let context = if core_23_addresses { - "with Core 23 addresses" - } else { - "as listed" - }; - let mut evonodes = BTreeSet::new(); - let init_list = mainnet_list_diff("1-2128896.json", core_23_addresses, &mut evonodes); - let list_diff = - mainnet_list_diff("2128896-2129440.json", core_23_addresses, &mut evonodes); - assert!( - list_diff - .updated_mns + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + let responses = [init_list.clone(), list_diff.clone()]; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_base_block, block| { + Ok(responses[usize::from(block != init_core_height)].clone()) + }); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Mainnet), + ) + .expect("platform state"); + + // The evonodes as every earlier binary holds them. + let is_evonode = + |masternode: &&MasternodeListItem| masternode.node_type == MasternodeType::Evo; + let mut listed: BTreeMap = init_list + .added_mns .iter() - .any(|(pro_tx_hash, state_diff)| { - evonodes.contains(&pro_tx_hash.to_string()) && state_diff.service.is_some() - }), - "the diff moves an evonode" - ); + .filter(is_evonode) + .map(|evonode| (evonode.pro_tx_hash, evonode.clone())) + .collect(); - let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); - let responses = [init_list.clone(), list_diff.clone()]; - platform - .core_rpc - .expect_get_protx_diff_with_masternodes() - .returning(move |_base_block, block| { - Ok(responses[usize::from(block != init_core_height)].clone()) - }); - let mut state = PlatformState::default_with_protocol_versions( - platform_version.protocol_version, - platform_version.protocol_version, - &PlatformConfig::default_for_network(Network::Mainnet), - ) - .expect("platform state"); - - // The evonodes as every earlier binary holds them. - let is_evonode = - |masternode: &&MasternodeListItem| masternode.node_type == MasternodeType::Evo; - let mut listed: BTreeMap = init_list - .added_mns - .iter() - .filter(is_evonode) - .map(|evonode| (evonode.pro_tx_hash, evonode.clone())) - .collect(); + let update = |state: &mut PlatformState, core_height, from_scratch| { + if platform_version.protocol_version == 13 { + platform.update_state_masternode_list_v0(state, core_height, from_scratch) + } else { + platform.update_state_masternode_list_v1(state, core_height, from_scratch) + } + .expect("expected to apply the masternode list") + }; + update(&mut state, init_core_height, true); + assert_validators_from_legacy_ports(&state, &listed, context); - platform - .update_state_masternode_list_v0(&mut state, init_core_height, true) - .expect("expected to apply the masternode list"); - assert_validators_from_legacy_ports(&state, &listed, context); - - let members: BTreeMap = listed - .iter() - .filter_map(|(pro_tx_hash, evonode)| { - validator_from_legacy_ports(evonode).map(|member| (*pro_tx_hash, member)) - }) - .collect(); - assert_eq!( - members.len(), - listed.len(), - "{context}: every evonode is a member" - ); - state.insert_validator_set( - quorum_hash, - ValidatorSet::V0(ValidatorSetV0 { + let members: BTreeMap = listed + .iter() + .filter_map(|(pro_tx_hash, evonode)| { + validator_from_legacy_ports(evonode).map(|member| (*pro_tx_hash, member)) + }) + .collect(); + assert_eq!( + members.len(), + listed.len(), + "{context}: every evonode is a member" + ); + state.insert_validator_set( quorum_hash, - quorum_index: None, - core_height: init_core_height, - members: members.clone(), - threshold_public_key: SecretKey::::random( - &mut StdRng::seed_from_u64(1), - ) - .public_key(), - }), - ); + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: init_core_height, + members: members.clone(), + threshold_public_key: SecretKey::::random( + &mut StdRng::seed_from_u64(1), + ) + .public_key(), + }), + ); - platform - .update_state_masternode_list_v0(&mut state, next_core_height, false) - .expect("expected to apply the masternode list diff"); + update(&mut state, next_core_height, false); - let mut refreshed_members = members.clone(); - for evonode in list_diff.added_mns.iter().filter(is_evonode) { - listed.insert(evonode.pro_tx_hash, evonode.clone()); - } - for (pro_tx_hash, state_diff) in &list_diff.updated_mns { - if let Some(evonode) = listed.get_mut(pro_tx_hash) { - evonode.state.apply_diff(state_diff.clone()); + let mut refreshed_members = members.clone(); + for evonode in list_diff.added_mns.iter().filter(is_evonode) { + listed.insert(evonode.pro_tx_hash, evonode.clone()); + } + for (pro_tx_hash, state_diff) in &list_diff.updated_mns { + if let Some(evonode) = listed.get_mut(pro_tx_hash) { + evonode.state.apply_diff(state_diff.clone()); + } + if let Some(member) = refreshed_members.get_mut(pro_tx_hash) { + refresh_from_legacy_ports(member, state_diff); + } } - if let Some(member) = refreshed_members.get_mut(pro_tx_hash) { - refresh_from_legacy_ports(member, state_diff); + for pro_tx_hash in &list_diff.removed_mns { + listed.remove(pro_tx_hash); + refreshed_members.remove(pro_tx_hash); } - } - for pro_tx_hash in &list_diff.removed_mns { - listed.remove(pro_tx_hash); - refreshed_members.remove(pro_tx_hash); - } - assert_validators_from_legacy_ports(&state, &listed, context); - assert_ne!( - refreshed_members, members, - "{context}: the diff refreshes members" - ); - assert_eq!( - state.validator_sets()[&quorum_hash].members(), - &refreshed_members, - "{context}: the refreshed members" - ); + assert_validators_from_legacy_ports(&state, &listed, context); + assert_ne!( + refreshed_members, members, + "{context}: the diff refreshes members" + ); + assert_eq!( + state.validator_sets()[&quorum_hash].members(), + &refreshed_members, + "{context}: the refreshed members" + ); + } } } @@ -1101,3 +1121,6 @@ mod test { ); } } + +#[cfg(test)] +mod versioning_tests; diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/mod.rs index 383eddf5abd..d0ad9c9e79e 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/mod.rs @@ -1,3 +1,4 @@ mod v0; +mod v1; -//internal method, doesn't need to be be versioned off of platform version +// Private helpers are selected by the update_masternode_list generation. diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs index 97f41c08ea5..e5518c423d3 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v0/mod.rs @@ -1,11 +1,9 @@ use crate::error::Error; use crate::execution::types::update_state_masternode_list_outcome; -use crate::platform_types::masternode::v0::apply_diff_in_stored_form; use crate::platform_types::platform::Platform; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; -use crate::platform_types::validator::v0::PlatformPorts; use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; use crate::rpc::core::CoreRPCLike; use dpp::dashcore::{ProTxHash, QuorumHash}; @@ -44,21 +42,19 @@ where /// Updates a masternode in the validator sets. /// /// This function updates the properties of the masternode that matches the given `pro_tx_hash`. - /// The ban status and service address are updated based on the provided `dmn_state_diff` - /// information, the platform P2P and HTTP ports from `new_platform_ports`. + /// The properties are updated based on the provided `dmn_state_diff` information. + /// If a matching masternode is found, the function updates its ban status, service address, + /// platform P2P port, and platform HTTP port accordingly. /// /// # Arguments /// /// * `pro_tx_hash` - The `ProTxHash` of the masternode to be updated /// * `dmn_state_diff` - The `DMNStateDiff` containing the updated masternode information - /// * `new_platform_ports` - The platform ports that resolve to a new value in the updated - /// masternode state /// * `state` - The platform state whose validator sets are updated; only the /// sets that list the masternode are touched fn update_masternode_in_validator_sets( pro_tx_hash: &ProTxHash, dmn_state_diff: &DMNStateDiff, - new_platform_ports: PlatformPorts, state: &mut PlatformState, ) { for quorum_hash in Self::validator_sets_with_member(state, pro_tx_hash) { @@ -76,28 +72,18 @@ where validator.node_ip = address.ip().to_string(); } - if let Some(p2p_port) = new_platform_ports.p2p { - validator.platform_p2p_port = p2p_port; + #[allow(deprecated)] + if let Some(p2p_port) = dmn_state_diff.legacy_platform_p2p_port { + validator.platform_p2p_port = p2p_port as u16; } - if let Some(http_port) = new_platform_ports.http { - validator.platform_http_port = http_port; + #[allow(deprecated)] + if let Some(http_port) = dmn_state_diff.legacy_platform_http_port { + validator.platform_http_port = http_port as u16; } } } - /// The platform ports an evonode's state resolves to, or `None` for a - /// masternode that is not an evonode. - fn evonode_platform_ports( - state: &PlatformState, - pro_tx_hash: &ProTxHash, - ) -> Option { - state - .hpmn_masternode_list() - .get(pro_tx_hash) - .map(|evonode| PlatformPorts::of(&evonode.state)) - } - /// Whether applying `updated_mns` would change any item of `masternode_list`. /// /// Core reports a masternode as updated whenever any field of its state @@ -111,7 +97,9 @@ where ) -> bool { updated_mns.iter().any(|(pro_tx_hash, state_diff)| { masternode_list.get(pro_tx_hash).is_some_and(|item| { - apply_diff_in_stored_form(&item.state, state_diff) != item.state + let mut updated = item.state.clone(); + updated.apply_diff(state_diff.clone()); + updated != item.state }) }) } @@ -176,44 +164,22 @@ where state.insert_masternode(masternode.clone()); } - // Every protocol version runs this code. Validator node addresses are - // not consensus state: Tenderdash hashes a validator set as - // merkle(threshold public key, quorum hash). Membership is, because - // quorum rotation keys off the set's members, and it stays what - // earlier binaries built for every masternode list they accept: an - // evonode is a member when it has a platform node id and both ports, a - // port resolves whenever its legacy field is present and within the - // port range (every legacy port Core can print fits a u16), and every - // evonode in such a list has both legacy fields. for (pro_tx_hash, state_diff) in updated_mns { - let platform_ports_before = Self::evonode_platform_ports(state, pro_tx_hash); + let is_hpmn = state.hpmn_masternode_list().contains_key(pro_tx_hash); if !state.apply_masternode_state_diff(pro_tx_hash, state_diff) { continue; } - // Only evonodes are validators. - let (Some(platform_ports_before), Some(platform_ports)) = ( - platform_ports_before, - Self::evonode_platform_ports(state, pro_tx_hash), - ) else { - continue; - }; - // The ports are read from the updated state, not from the diff: an - // ExtAddr evonode's port move reaches us only in `addresses`, and a - // legacy evonode's diff lists there just the entries that changed. - let new_platform_ports = changed_platform_ports(platform_ports_before, platform_ports); - // the ban status, the IP and the platform P2P port are the only fields that are useful for - // validators. If they change we need to update validator sets - if state_diff.pose_ban_height.is_some() - || state_diff.service.is_some() - || new_platform_ports.p2p.is_some() + // these 3 fields are the only fields that are useful for validators. If they change we need to update + // validator sets + #[allow(deprecated)] + let platform_p2p_port_changed = state_diff.legacy_platform_p2p_port.is_some(); + if is_hpmn + && (state_diff.pose_ban_height.is_some() + || state_diff.service.is_some() + || platform_p2p_port_changed) { // we updated the ban status the IP or the platform port, we need to update the validator in the validator list - Self::update_masternode_in_validator_sets( - pro_tx_hash, - state_diff, - new_platform_ports, - state, - ); + Self::update_masternode_in_validator_sets(pro_tx_hash, state_diff, state); } } @@ -240,42 +206,19 @@ where } } -/// The ports of `after` that resolve to a value other than in `before`. -/// -/// A port that stops resolving is not reported, so the validator keeps its -/// last known port. -fn changed_platform_ports(before: PlatformPorts, after: PlatformPorts) -> PlatformPorts { - PlatformPorts { - p2p: after.p2p.filter(|_| after.p2p != before.p2p), - http: after.http.filter(|_| after.http != before.http), - } -} - #[cfg(test)] mod tests { - use crate::config::{PlatformConfig, PlatformTestConfig}; - use crate::platform_types::masternode::v0::MasternodeV0; - use crate::platform_types::platform::Platform; + use crate::config::PlatformConfig; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::platform_state::PlatformStateV0Methods; - use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, PlatformPorts}; - use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; - use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; - use crate::rpc::core::MockCoreRPCLike; use crate::test::helpers::setup::TestPlatformBuilder; - use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; - use dpp::core_types::validator::v0::ValidatorV0; - use dpp::core_types::validator_set::v0::ValidatorSetV0; use dpp::dashcore::hashes::Hash; - use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash, ScriptBuf, Txid}; + use dpp::dashcore::{Network, ProTxHash, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ - DMNPayout, DMNState, DMNStateDiff, MasternodeAddresses, MasternodeListDiff, - MasternodeListItem, MasternodeType, + DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, MasternodeType, }; use dpp::version::PlatformVersion; - use rand::rngs::StdRng; - use rand::SeedableRng; - use std::collections::{BTreeMap, BTreeSet}; + use std::collections::BTreeSet; fn masternode(pro_tx_hash: ProTxHash) -> MasternodeListItem { MasternodeListItem { @@ -343,7 +286,7 @@ mod tests { PlatformState, ProTxHash, ) { - let platform_version = PlatformVersion::latest(); + let platform_version = PlatformVersion::get(13).expect("protocol version 13"); let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); let pro_tx_hash = ProTxHash::from_byte_array([0x77u8; 32]); @@ -353,7 +296,9 @@ mod tests { &PlatformConfig::default_for_network(Network::Testnet), ) .expect("platform state"); - state.insert_masternode(masternode(pro_tx_hash)); + state + .full_masternode_list_mut() + .insert(pro_tx_hash, masternode(pro_tx_hash)); state.mark_saved(); platform @@ -436,7 +381,7 @@ mod tests { #[test] fn should_only_upsert_changed_entries_in_a_mixed_masternode_diff() { - let platform_version = PlatformVersion::latest(); + let platform_version = PlatformVersion::get(13).expect("protocol version 13"); let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); let mut state = PlatformState::default_with_protocol_versions( platform_version.protocol_version, @@ -507,537 +452,4 @@ mod tests { assert!(state.masternode_changes.removed.is_empty()); assert!(!state.masternode_changes.rewrite_all); } - - /// The IP every test evonode is registered with. - const SERVICE_IP: &str = "1.2.3.4"; - const EVONODE_PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; - const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55u8; 32]; - - fn evonode_pro_tx_hash() -> ProTxHash { - ProTxHash::from_byte_array(EVONODE_PRO_TX_HASH) - } - - /// An evonode with platform ports 36656 (P2P) and 443 (HTTPS), as Core - /// lists it with `-deprecatedrpc=service`: the legacy port fields carry - /// the live ports and `addresses` repeats them. - fn evonode() -> MasternodeListItem { - let mut item = masternode(evonode_pro_tx_hash()); - item.node_type = MasternodeType::Evo; - item.state.platform_node_id = Some([7u8; 20]); - #[allow(deprecated)] - { - item.state.legacy_platform_p2p_port = Some(36656); - item.state.legacy_platform_http_port = Some(443); - } - item.state.addresses = Some(MasternodeAddresses { - core_p2p: vec!["1.2.3.4:1234".to_string()], - platform_p2p: vec!["1.2.3.4:36656".to_string()], - platform_https: vec!["1.2.3.4:443".to_string()], - }); - item - } - - /// The same evonode in a state whose legacy ports are absent: the ports - /// are only in `addresses`. - fn evonode_without_legacy_ports() -> MasternodeListItem { - let mut item = evonode(); - #[allow(deprecated)] - { - item.state.legacy_platform_p2p_port = None; - item.state.legacy_platform_http_port = None; - } - item - } - - /// `item` as a node that restarted holds it: loaded back from storage. - fn reloaded(item: MasternodeListItem) -> MasternodeListItem { - MasternodeV0::from(item).into() - } - - /// Runs one masternode list update per diff, in order, for a state - /// holding `evonode` as a member of one validator set, with Core reporting - /// the diffs for it, and returns the state afterwards. - fn state_after_diffs(evonode: MasternodeListItem, diffs: Vec) -> PlatformState { - let platform_version = PlatformVersion::latest(); - let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); - - let pro_tx_hash = evonode.pro_tx_hash; - let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); - let mut state = PlatformState::default_with_protocol_versions( - platform_version.protocol_version, - platform_version.protocol_version, - &PlatformConfig::default_for_network(Network::Testnet), - ) - .expect("platform state"); - state.insert_masternode(evonode); - let validator = ValidatorV0 { - pro_tx_hash, - public_key: None, - node_ip: SERVICE_IP.to_string(), - node_id: PubkeyHash::from_byte_array([7u8; 20]), - core_port: 1234, - platform_http_port: 443, - platform_p2p_port: 36656, - is_banned: false, - }; - let mut rng = StdRng::seed_from_u64(1); - state.insert_validator_set( - quorum_hash, - ValidatorSet::V0(ValidatorSetV0 { - quorum_hash, - quorum_index: None, - core_height: 1, - members: BTreeMap::from([(pro_tx_hash, validator)]), - threshold_public_key: SecretKey::::random(&mut rng).public_key(), - }), - ); - state.mark_saved(); - - let block_count = diffs.len() as u32; - platform - .core_rpc - .expect_get_protx_diff_with_masternodes() - .returning(move |base_block, block| { - Ok(MasternodeListDiff { - base_height: base_block.unwrap_or_default(), - block_height: block, - added_mns: vec![], - removed_mns: vec![], - updated_mns: vec![(pro_tx_hash, diffs[block as usize - 1].clone())], - }) - }); - - for block in 1..=block_count { - platform - .update_state_masternode_list_v0(&mut state, block, false) - .expect("update must succeed"); - } - - state - } - - /// The evonode's member of the validator set. - fn validator_in(state: &PlatformState) -> ValidatorV0 { - state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)].members() - [&evonode_pro_tx_hash()] - .clone() - } - - /// The validator a quorum Drive learns about later gets for the evonode, - /// once from the state as held in memory and once as a node that - /// restarted loads it back. Validator-set membership decides quorum - /// rotation, so every node must build the same one. - fn validators_for_a_new_quorum(state: &PlatformState) -> [Option; 2] { - let pro_tx_hash = evonode_pro_tx_hash(); - let mut restarted = state.clone(); - restarted.insert_masternode(reloaded(state.full_masternode_list()[&pro_tx_hash].clone())); - [state, &restarted].map(|state| { - ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state) - }) - } - - /// Asserts that the evonode's member of the existing validator set has - /// the given ports, and that a new quorum gets the same member. - fn assert_validator_ports(state: &PlatformState, p2p_port: u16, http_port: u16) { - let validator = validator_in(state); - assert_eq!(validator.node_ip, SERVICE_IP); - assert_eq!(validator.platform_p2p_port, p2p_port); - assert_eq!(validator.platform_http_port, http_port); - for new_quorum_validator in validators_for_a_new_quorum(state) { - assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); - } - } - - /// The network info of the evonode after moving to platform ports 36657 - /// (P2P) and 8443 (HTTPS), as Core prints an ExtAddr one. - fn moved_platform_ports() -> MasternodeAddresses { - MasternodeAddresses { - core_p2p: vec!["1.2.3.4:1234".to_string()], - platform_p2p: vec!["1.2.3.4:36657".to_string()], - platform_https: vec!["1.2.3.4:8443".to_string()], - } - } - - /// A node that restarts reloads its masternode lists from storage, and - /// validator-set membership, which decides quorum rotation, must not - /// depend on whether it did. So after every masternode list update the - /// lists held in memory must be exactly what storage gives back. The - /// updates: an ExtAddr evonode added with its platform ports in - /// `addresses` and a payout list, a port move that only `addresses` - /// carries, then a revoke that zeroes the legacy ports and carries no - /// `addresses`. - #[test] - fn should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads() { - let platform_version = PlatformVersion::latest(); - let mut platform = TestPlatformBuilder::new() - .with_config(PlatformConfig { - testing_configs: PlatformTestConfig { - store_platform_state: true, - ..PlatformTestConfig::default_minimal_verifications() - }, - ..Default::default() - }) - .build_with_mock_rpc() - .set_genesis_state(); - - let mut added = evonode(); - added.collateral_address = Some([0x21; 20]); - added.state.owner_address = Some([0x22; 20]); - added.state.payout_address = None; - added.state.payouts = Some(vec![DMNPayout { - address: [0x23; 20], - script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array([0x23; 20])), - reward: 10000, - }]); - let pro_tx_hash = added.pro_tx_hash; - let port_move = DMNStateDiff { - addresses: Some(Some(moved_platform_ports())), - ..empty_state_diff() - }; - #[allow(deprecated)] - let revoke = DMNStateDiff { - service: Some("[::]:0".parse().expect("socket address")), - pose_ban_height: Some(Some(3)), - revocation_reason: Some(1), - platform_node_id: Some([0u8; 20]), - pub_key_operator: Some(vec![1u8; 48]), - legacy_platform_p2p_port: Some(0), - legacy_platform_http_port: Some(0), - ..empty_state_diff() - }; - platform - .core_rpc - .expect_get_protx_diff_with_masternodes() - .returning(move |base_block, block| { - let (added_mns, updated_mns) = match block { - 1 => (vec![added.clone()], vec![]), - 2 => (vec![], vec![(pro_tx_hash, port_move.clone())]), - _ => (vec![], vec![(pro_tx_hash, revoke.clone())]), - }; - Ok(MasternodeListDiff { - base_height: base_block.unwrap_or_default(), - block_height: block, - added_mns, - removed_mns: vec![], - updated_mns, - }) - }); - - let transaction = platform.drive.grove.start_transaction(); - let mut state = platform.state.load().as_ref().clone(); - // The ports and ban each update leaves the evonode with. - let expected = [ - (Some(36656), Some(443), false), - (Some(36657), Some(8443), false), - (Some(0), Some(0), true), - ]; - for (block, (p2p, http, is_banned)) in (1..).zip(expected) { - platform - .update_state_masternode_list_v0(&mut state, block, false) - .expect("update must succeed"); - platform - .store_platform_state(&state, Some(&transaction), platform_version) - .expect("store platform state"); - state.mark_saved(); - let reloaded = Platform::::fetch_platform_state( - &platform.drive, - Some(&transaction), - platform_version, - ) - .expect("fetch platform state") - .expect("a state was stored"); - - assert_eq!( - reloaded.full_masternode_list(), - state.full_masternode_list(), - "the full list after block {block}" - ); - assert_eq!( - reloaded.hpmn_masternode_list(), - state.hpmn_masternode_list(), - "the evonode list after block {block}" - ); - let evonode = &reloaded.hpmn_masternode_list()[&pro_tx_hash].state; - assert_eq!( - ( - PlatformPorts::of(evonode), - evonode.pose_ban_height.is_some() - ), - (PlatformPorts { p2p, http }, is_banned), - "the evonode after block {block}" - ); - } - } - - /// An ExtAddr evonode keeps its platform ports in its network info and its - /// legacy port fields at zero, so a ProUpServTx that only moves its ports - /// reaches Drive as a diff whose only network field is `addresses` - /// (`service` too only with `-deprecatedrpc=service`). Tenderdash must be - /// told the new port, or it keeps dialling the old one. - #[test] - fn should_update_validator_ports_from_an_addresses_only_diff() { - let state = state_after_diffs( - evonode_without_legacy_ports(), - vec![DMNStateDiff { - addresses: Some(Some(moved_platform_ports())), - ..empty_state_diff() - }], - ); - - assert_validator_ports(&state, 36657, 8443); - } - - /// The same port move as Core prints it with `-deprecatedrpc=service`: - /// `service` repeats because the network info changed, while the legacy - /// port fields are absent because they did not change. - #[test] - fn should_update_validator_ports_from_addresses_when_the_diff_also_carries_service() { - let state = state_after_diffs( - evonode(), - vec![DMNStateDiff { - service: Some("1.2.3.4:1234".parse().expect("socket address")), - addresses: Some(Some(moved_platform_ports())), - ..empty_state_diff() - }], - ); - - assert_validator_ports(&state, 36657, 8443); - } - - /// An ExtAddr evonode that moves only its HTTPS port lists its whole - /// network info again, the unchanged P2P entry included. - #[test] - fn should_update_the_validator_https_port_when_only_it_moved() { - let state = state_after_diffs( - evonode(), - vec![DMNStateDiff { - service: Some("1.2.3.4:1234".parse().expect("socket address")), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec!["1.2.3.4:1234".to_string()], - platform_p2p: vec!["1.2.3.4:36656".to_string()], - platform_https: vec!["1.2.3.4:8443".to_string()], - })), - ..empty_state_diff() - }], - ); - - assert_validator_ports(&state, 36656, 8443); - } - - /// When a legacy evonode changes only a platform port, the diff does not - /// carry the node's IP, so Core pairs the new port in `addresses` with the - /// placeholder host 255.255.255.255. Only the port may be taken from it. - #[test] - fn should_not_take_the_placeholder_host_from_a_legacy_evonode_port_diff() { - let state = state_after_diffs( - evonode(), - vec![DMNStateDiff { - #[allow(deprecated)] - legacy_platform_p2p_port: Some(36657), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec![], - platform_p2p: vec!["255.255.255.255:36657".to_string()], - platform_https: vec![], - })), - ..empty_state_diff() - }], - ); - - assert_validator_ports(&state, 36657, 443); - } - - /// A legacy evonode's diff lists in `addresses` only the entries that - /// changed, next to the changed legacy port field. A later diff that - /// changes the other port must not undo an earlier one's port. - #[test] - fn should_keep_each_platform_port_from_diffs_that_list_one_entry_each() { - let state = state_after_diffs( - evonode(), - vec![ - DMNStateDiff { - #[allow(deprecated)] - legacy_platform_p2p_port: Some(36657), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec![], - platform_p2p: vec!["255.255.255.255:36657".to_string()], - platform_https: vec![], - })), - ..empty_state_diff() - }, - DMNStateDiff { - #[allow(deprecated)] - legacy_platform_http_port: Some(8443), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec![], - platform_p2p: vec![], - platform_https: vec!["255.255.255.255:8443".to_string()], - })), - ..empty_state_diff() - }, - ], - ); - - assert_eq!(validator_in(&state).platform_p2p_port, 36657); - for new_quorum_validator in validators_for_a_new_quorum(&state) { - let new_quorum_validator = new_quorum_validator.expect("the evonode has ports"); - assert_eq!(new_quorum_validator.platform_p2p_port, 36657); - assert_eq!(new_quorum_validator.platform_http_port, 8443); - } - } - - /// A diff of a legacy evonode's core address carries `addresses` with the - /// core entry only, so the platform ports must not be read as cleared. - #[test] - fn should_keep_validator_ports_when_a_diff_moves_only_the_core_address() { - let state = state_after_diffs( - evonode(), - vec![DMNStateDiff { - service: Some("5.6.7.8:1234".parse().expect("socket address")), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec!["5.6.7.8:1234".to_string()], - platform_p2p: vec![], - platform_https: vec![], - })), - ..empty_state_diff() - }], - ); - - let validator = validator_in(&state); - assert_eq!(validator.node_ip, "5.6.7.8"); - assert_eq!(validator.platform_p2p_port, 36656); - assert_eq!(validator.platform_http_port, 443); - for new_quorum_validator in validators_for_a_new_quorum(&state) { - assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); - } - } - - /// A node that restarted holds the evonode without `addresses`. A diff - /// that lists only the core entry must leave its platform ports as they - /// are, as on a node that kept its state in memory. - #[test] - fn should_keep_validator_ports_after_a_restart_when_a_diff_moves_only_the_core_address() { - let state = state_after_diffs( - reloaded(evonode()), - vec![DMNStateDiff { - service: Some("1.2.3.4:1234".parse().expect("socket address")), - addresses: Some(Some(MasternodeAddresses { - core_p2p: vec!["1.2.3.4:1234".to_string()], - platform_p2p: vec![], - platform_https: vec![], - })), - ..empty_state_diff() - }], - ); - - assert_validator_ports(&state, 36656, 443); - } - - /// After V24 a legacy evonode's revoke, or an operator key change, makes - /// it ExtAddr with an empty network info: Core prints its platform ports - /// as 0 and no `addresses`. Earlier binaries accept that diff, so every - /// node must build the validator they build from the legacy fields: a - /// banned member on the unspecified IP with ports 0. A node that kept the - /// evonode's `addresses` in memory and a node that restarted must both - /// build it, because validator-set membership decides quorum rotation. - #[test] - fn should_agree_on_the_validator_after_a_legacy_evonode_is_revoked_whether_or_not_the_node_restarted( - ) { - #[allow(deprecated)] - let revoke = DMNStateDiff { - service: Some("[::]:0".parse().expect("socket address")), - pose_ban_height: Some(Some(2)), - revocation_reason: Some(1), - platform_node_id: Some([0u8; 20]), - pub_key_operator: Some(vec![1u8; 48]), - legacy_platform_p2p_port: Some(0), - legacy_platform_http_port: Some(0), - ..empty_state_diff() - }; - let kept = state_after_diffs(evonode(), vec![revoke.clone()]); - let restarted = state_after_diffs(reloaded(evonode()), vec![revoke]); - - let pro_tx_hash = evonode_pro_tx_hash(); - let new_quorum_validator = ValidatorV0 { - pro_tx_hash, - public_key: None, - node_ip: "::".to_string(), - node_id: PubkeyHash::from_byte_array([0u8; 20]), - core_port: 0, - platform_http_port: 0, - platform_p2p_port: 0, - is_banned: true, - }; - // A refresh of an existing member takes the ban, the IP and the ports - // from the diff, and keeps its node id and core port. - let refreshed_member = ValidatorV0 { - node_id: PubkeyHash::from_byte_array([7u8; 20]), - core_port: 1234, - ..new_quorum_validator.clone() - }; - for state in [&kept, &restarted] { - assert_eq!( - ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), - Some(new_quorum_validator.clone()) - ); - assert_eq!(validator_in(state), refreshed_member); - } - let stored = |state: &PlatformState| { - MasternodeV0::from(state.full_masternode_list()[&pro_tx_hash].clone()).state - }; - assert_eq!(stored(&kept), stored(&restarted)); - } - - /// When Core resets an ExtAddr evonode's operator fields (ProUpRevTx, or - /// a ProUpRegTx that changes the operator key), its network info becomes - /// empty: the diff has `service` `[::]:0` and no `addresses` key, so the - /// platform ports keep resolving to the old endpoint. That endpoint is - /// never advertised, because Core bans the masternode in the same - /// transition and only a ProUpServTx, which sends new `addresses`, revives - /// it. The evonode stays a member, banned, with the ports it last had. - #[test] - fn should_not_advertise_an_evonode_whose_network_info_core_emptied() { - let state = state_after_diffs( - evonode_without_legacy_ports(), - vec![DMNStateDiff { - service: Some("[::]:0".parse().expect("socket address")), - pose_ban_height: Some(Some(2)), - revocation_reason: Some(1), - platform_node_id: Some([0u8; 20]), - pub_key_operator: Some(vec![1u8; 48]), - ..empty_state_diff() - }], - ); - - let validator_set = - &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; - let new_quorum_validator = ValidatorV0 { - pro_tx_hash: evonode_pro_tx_hash(), - public_key: None, - node_ip: "::".to_string(), - node_id: PubkeyHash::from_byte_array([0u8; 20]), - core_port: 0, - platform_http_port: 443, - platform_p2p_port: 36656, - is_banned: true, - }; - assert_eq!( - validator_set.members()[&evonode_pro_tx_hash()], - ValidatorV0 { - node_id: PubkeyHash::from_byte_array([7u8; 20]), - core_port: 1234, - ..new_quorum_validator.clone() - } - ); - assert!( - validator_set.to_update().validator_updates.is_empty(), - "a banned validator is left out of the validator set update" - ); - for validator in validators_for_a_new_quorum(&state) { - assert_eq!( - validator, - Some(new_quorum_validator.clone()), - "a validator set built later gets the same banned member" - ); - } - } } diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v1/mod.rs new file mode 100644 index 00000000000..996608f515f --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/update_state_masternode_list/v1/mod.rs @@ -0,0 +1,1325 @@ +use crate::error::Error; +use crate::execution::types::update_state_masternode_list_outcome; +use crate::platform_types::masternode::v0::MasternodeStateV0; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use crate::platform_types::platform_state::PlatformStateV0Methods; + +use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; +use crate::rpc::core::CoreRPCLike; +use dpp::dashcore::{ProTxHash, QuorumHash}; +use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNState, DMNStateDiff, MasternodeListDiff, MasternodeListItem, +}; +use std::collections::{BTreeMap, BTreeSet}; + +impl Platform +where + C: CoreRPCLike, +{ + /// The quorum hashes of the validator sets the masternode is a member of. + fn validator_sets_with_member_v1( + state: &PlatformState, + pro_tx_hash: &ProTxHash, + ) -> Vec { + state + .validator_sets() + .iter() + .filter(|(_, validator_set)| validator_set.members().contains_key(pro_tx_hash)) + .map(|(quorum_hash, _)| *quorum_hash) + .collect() + } + + /// Remove a masternode from every validator set it is a member of. + /// + /// Only the validator sets that list the masternode are touched, so only + /// their entries are rewritten by the per-entry store. + fn remove_masternode_in_validator_sets_v1(pro_tx_hash: &ProTxHash, state: &mut PlatformState) { + for quorum_hash in Self::validator_sets_with_member_v1(state, pro_tx_hash) { + if let Some(validator_set) = state.validator_set_mut(&quorum_hash) { + validator_set.members_mut().remove(pro_tx_hash); + } + } + } + + /// Updates a masternode in the validator sets. + /// + /// This function updates the properties of the masternode that matches the given `pro_tx_hash`. + /// The ban status and service address are updated based on the provided `dmn_state_diff` + /// information, the platform P2P and HTTP ports from `new_platform_ports`. + /// + /// # Arguments + /// + /// * `pro_tx_hash` - The `ProTxHash` of the masternode to be updated + /// * `dmn_state_diff` - The `DMNStateDiff` containing the updated masternode information + /// * `new_platform_ports` - The platform ports that resolve to a new value in the updated + /// masternode state + /// * `state` - The platform state whose validator sets are updated; only the + /// sets that list the masternode are touched + fn update_masternode_in_validator_sets_v1( + pro_tx_hash: &ProTxHash, + dmn_state_diff: &DMNStateDiff, + new_platform_ports: PlatformPorts, + state: &mut PlatformState, + ) { + for quorum_hash in Self::validator_sets_with_member_v1(state, pro_tx_hash) { + let Some(validator_set) = state.validator_set_mut(&quorum_hash) else { + continue; + }; + let Some(validator) = validator_set.members_mut().get_mut(pro_tx_hash) else { + continue; + }; + if let Some(maybe_ban_height) = dmn_state_diff.pose_ban_height { + // the ban_height was changed + validator.is_banned = maybe_ban_height.is_some(); + } + if let Some(address) = dmn_state_diff.service { + validator.node_ip = address.ip().to_string(); + } + + if let Some(p2p_port) = new_platform_ports.p2p { + validator.platform_p2p_port = p2p_port; + } + + if let Some(http_port) = new_platform_ports.http { + validator.platform_http_port = http_port; + } + } + } + + /// Whether applying `updated_mns` would change any item of `masternode_list`. + /// + /// Core reports a masternode as updated whenever any field of its state + /// moved, including the payment and penalty fields — last paid height, + /// consecutive payments, PoSe penalty — that change on nearly every Core + /// block and that the stored item does not keep. Applying each diff to a + /// copy tells the two apart. + fn any_stored_masternode_changes_v1( + masternode_list: &BTreeMap, + updated_mns: &[(ProTxHash, DMNStateDiff)], + ) -> bool { + updated_mns.iter().any(|(pro_tx_hash, state_diff)| { + masternode_list.get(pro_tx_hash).is_some_and(|item| { + apply_diff_in_stored_form(&item.state, state_diff) + != legacy_stored_form(item.state.clone()) + }) + }) + } + + pub(crate) fn update_state_masternode_list_v1( + &self, + state: &mut PlatformState, + core_block_height: u32, + start_from_scratch: bool, + ) -> Result + { + let previous_core_height = if start_from_scratch { + // baseBlock must be a chain height and not 0 + None + } else { + let state_core_height = state.last_committed_core_height(); + if core_block_height == state_core_height { + return Ok(update_state_masternode_list_outcome::v0::UpdateStateMasternodeListOutcome::default()); + // no need to do anything + } + Some(state_core_height) + }; + + let masternode_diff = self + .core_rpc + .get_protx_diff_with_masternodes(previous_core_height, core_block_height)?; + + let MasternodeListDiff { + added_mns, + removed_mns, + updated_mns, + .. + } = &masternode_diff; + + // Core advances a block without any stored masternode field changing far + // more often than not: on mainnet almost every block's diff is only the + // payment fields of the masternode paid in it. Returning before the + // first mutable borrow keeps the platform state clean, which is what + // lets the block skip rewriting the full saved state (over a megabyte + // on mainnet) to disk. + if !start_from_scratch + && added_mns.is_empty() + && removed_mns.is_empty() + && !Self::any_stored_masternode_changes_v1(state.full_masternode_list(), updated_mns) + { + return Ok( + update_state_masternode_list_outcome::v0::UpdateStateMasternodeListOutcome { + masternode_list_diff: masternode_diff, + removed_masternodes: BTreeMap::new(), + }, + ); + } + + // Every change below goes through an accessor that records which + // masternode or validator set it touched, so the store writes only + // those entries. + if start_from_scratch { + state.clear_masternode_lists(); + } + + for masternode in added_mns { + let mut masternode = masternode.clone(); + masternode.state = in_stored_form(masternode.state); + state.insert_masternode(masternode); + } + + for (pro_tx_hash, state_diff) in updated_mns { + let Some(mut masternode) = state.full_masternode_list().get(pro_tx_hash).cloned() + else { + continue; + }; + // Old in-memory addresses were not persisted before this generation. Start + // from the stored state so a running node and a restarted node agree. + let previous_state = legacy_stored_form(masternode.state.clone()); + let updated_state = apply_diff_in_stored_form(&previous_state, state_diff); + let is_hpmn = state.hpmn_masternode_list().contains_key(pro_tx_hash); + let platform_ports_before = is_hpmn.then(|| PlatformPorts::of(&previous_state)); + let platform_ports = is_hpmn.then(|| PlatformPorts::of(&updated_state)); + if updated_state != previous_state { + masternode.state = updated_state; + state.insert_masternode(masternode); + } + // Only evonodes are validators. + let (Some(platform_ports_before), Some(platform_ports)) = + (platform_ports_before, platform_ports) + else { + continue; + }; + // The ports are read from the updated state, not from the diff: an + // ExtAddr evonode's port move reaches us only in `addresses`, and a + // legacy evonode's diff lists there just the entries that changed. + let new_platform_ports = changed_platform_ports(platform_ports_before, platform_ports); + // the ban status, the IP and the platform P2P port are the only fields that are useful for + // validators. If they change we need to update validator sets + if state_diff.pose_ban_height.is_some() + || state_diff.service.is_some() + || new_platform_ports.p2p.is_some() + { + // we updated the ban status the IP or the platform port, we need to update the validator in the validator list + Self::update_masternode_in_validator_sets_v1( + pro_tx_hash, + state_diff, + new_platform_ports, + state, + ); + } + } + + for pro_tx_hash in removed_mns { + Self::remove_masternode_in_validator_sets_v1(pro_tx_hash, state); + } + + let deleted_masternodes = removed_mns.iter().copied().collect::>(); + + let mut removed_masternodes = BTreeMap::new(); + + for key in deleted_masternodes { + if let Some(value) = state.remove_masternode(&key) { + removed_masternodes.insert(key, value); + } + } + + Ok( + update_state_masternode_list_outcome::v0::UpdateStateMasternodeListOutcome { + masternode_list_diff: masternode_diff, + removed_masternodes, + }, + ) + } +} + +/// The ports of `after` that resolve to a value other than in `before`. +/// +/// A port that stops resolving is not reported, so the validator keeps its +/// last known port. +fn changed_platform_ports(before: PlatformPorts, after: PlatformPorts) -> PlatformPorts { + PlatformPorts { + p2p: after.p2p.filter(|_| after.p2p != before.p2p), + http: after.http.filter(|_| after.http != before.http), + } +} + +/// The old persisted state, without transient address or payout lists. +fn legacy_stored_form(state: DMNState) -> DMNState { + MasternodeStateV0::from(state).into() +} + +/// Resolves nested ports into the unchanged persisted fields. The global +/// converters stay literal so earlier protocol generations keep their behavior. +fn in_stored_form(mut state: DMNState) -> DMNState { + let p2p = state.platform_p2p_address().map(|(_, port)| port); + let http = state.platform_http_address().map(|(_, port)| port); + #[allow(deprecated)] + { + state.legacy_platform_p2p_port = p2p.or(state.legacy_platform_p2p_port); + state.legacy_platform_http_port = http.or(state.legacy_platform_http_port); + } + legacy_stored_form(state) +} + +fn apply_diff_in_stored_form(state: &DMNState, diff: &DMNStateDiff) -> DMNState { + let mut updated = legacy_stored_form(state.clone()); + updated.apply_diff(diff.clone()); + in_stored_form(updated) +} + +/// Platform ports resolved for this generation's validator updates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct PlatformPorts { + p2p: Option, + http: Option, +} + +impl PlatformPorts { + fn of(state: &DMNState) -> Self { + Self { + p2p: state + .platform_p2p_address() + .and_then(|(_, port)| u16::try_from(port).ok()), + http: state + .platform_http_address() + .and_then(|(_, port)| u16::try_from(port).ok()), + } + } +} + +#[cfg(test)] +mod tests { + use super::{in_stored_form, PlatformPorts}; + use crate::config::{PlatformConfig, PlatformTestConfig}; + use crate::platform_types::masternode::v0::MasternodeV0; + use crate::platform_types::platform::Platform; + use crate::platform_types::platform_state::PlatformState; + use crate::platform_types::platform_state::PlatformStateV0Methods; + use crate::platform_types::validator::v0::NewValidatorIfMasternodeInState; + use crate::platform_types::validator_set::v0::ValidatorSetV0Getters; + use crate::platform_types::validator_set::{ValidatorSet, ValidatorSetExt}; + use crate::rpc::core::MockCoreRPCLike; + use crate::test::helpers::setup::{TempPlatform, TestPlatformBuilder}; + use dpp::bls_signatures::{Bls12381G2Impl, SecretKey}; + use dpp::core_types::validator::v0::ValidatorV0; + use dpp::core_types::validator_set::v0::ValidatorSetV0; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{Network, ProTxHash, PubkeyHash, QuorumHash, ScriptBuf, Txid}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNPayout, DMNState, DMNStateDiff, MasternodeAddresses, MasternodeListDiff, + MasternodeListItem, MasternodeType, + }; + use dpp::version::PlatformVersion; + use rand::rngs::StdRng; + use rand::SeedableRng; + use std::collections::{BTreeMap, BTreeSet}; + + fn masternode(pro_tx_hash: ProTxHash) -> MasternodeListItem { + MasternodeListItem { + node_type: MasternodeType::Regular, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0u8; 32]), + collateral_index: 0, + collateral_address: Some([0u8; 20]), + operator_reward: 0.0, + state: DMNState { + service: "1.2.3.4:1234".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + }, + } + } + + /// A diff with no field set: what Core reports for a masternode is added + /// on top of this. + fn empty_state_diff() -> DMNStateDiff { + DMNStateDiff { + service: None, + registered_height: None, + last_paid_height: None, + consecutive_payments: None, + pose_penalty: None, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: None, + owner_address: None, + voting_address: None, + payout_address: None, + payouts: None, + pub_key_operator: None, + operator_payout_address: None, + platform_node_id: None, + #[allow(deprecated)] + legacy_platform_p2p_port: None, + #[allow(deprecated)] + legacy_platform_http_port: None, + addresses: None, + } + } + + /// A clean state holding one masternode, and a platform whose Core reports + /// `state_diff` for it on the next block. + fn state_with_one_masternode_and_diff( + state_diff: DMNStateDiff, + ) -> (TempPlatform, PlatformState, ProTxHash) { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + + let pro_tx_hash = ProTxHash::from_byte_array([0x77u8; 32]); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + state.insert_masternode(masternode(pro_tx_hash)); + state.mark_saved(); + + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, state_diff.clone())], + }) + }); + + (platform, state, pro_tx_hash) + } + + /// Core reports the masternode paid in a block as updated, but the stored + /// item keeps none of the payment fields, so nothing changes and the state + /// must stay clean: this is what almost every mainnet block's diff is. + #[test] + fn payment_only_update_leaves_the_state_clean() { + let (platform, mut state, pro_tx_hash) = state_with_one_masternode_and_diff(DMNStateDiff { + last_paid_height: Some(2_129_183), + consecutive_payments: Some(1), + pose_penalty: Some(0), + ..empty_state_diff() + }); + let before = state.full_masternode_list().clone(); + + platform + .update_state_masternode_list_v1(&mut state, 1, false) + .expect("update must succeed"); + + assert!( + !state.heavy_fields_dirty, + "a diff that changes no stored field must not dirty the state" + ); + assert!( + state.masternode_changes.is_empty(), + "and no entry needs rewriting" + ); + assert_eq!(state.full_masternode_list(), &before); + assert!(state.full_masternode_list().contains_key(&pro_tx_hash)); + } + + /// Transient nested addresses from a previous generation do not constitute + /// a stored change when Core reports only payment metadata. + #[test] + fn should_keep_a_payment_only_diff_clean_with_old_transient_addresses() { + let (platform, mut state, pro_tx_hash) = state_with_one_masternode_and_diff(DMNStateDiff { + last_paid_height: Some(42), + ..empty_state_diff() + }); + let mut item = evonode(); + item.state.addresses = Some(moved_platform_ports()); + state.insert_masternode(item); + state.mark_saved(); + let before = state.full_masternode_list()[&pro_tx_hash].clone(); + + platform + .update_state_masternode_list_v1(&mut state, 1, false) + .expect("update"); + + assert!(!state.heavy_fields_dirty); + assert!(state.masternode_changes.is_empty()); + assert_eq!(state.full_masternode_list()[&pro_tx_hash], before); + } + + /// A diff that does change a stored field is applied and dirties the state, + /// so the check above does not swallow real changes. + #[test] + fn stored_field_update_is_applied_and_dirties_the_state() { + let new_service = "5.6.7.8:5678".parse().expect("socket address"); + let (platform, mut state, pro_tx_hash) = state_with_one_masternode_and_diff(DMNStateDiff { + last_paid_height: Some(2_129_183), + service: Some(new_service), + ..empty_state_diff() + }); + + platform + .update_state_masternode_list_v1(&mut state, 1, false) + .expect("update must succeed"); + + assert!(state.heavy_fields_dirty); + assert_eq!( + state.masternode_changes.upserted, + BTreeSet::from([pro_tx_hash]), + "exactly the changed masternode's entry is rewritten" + ); + assert!(!state.masternode_changes.rewrite_all); + assert_eq!( + state + .full_masternode_list() + .get(&pro_tx_hash) + .expect("masternode stays listed") + .state + .service, + new_service + ); + } + + #[test] + fn should_only_upsert_changed_entries_in_a_mixed_masternode_diff() { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + let changed_hash = ProTxHash::from_byte_array([0x11; 32]); + let payment_only_hash = ProTxHash::from_byte_array([0x22; 32]); + for pro_tx_hash in [changed_hash, payment_only_hash] { + let mut item = evonode(); + item.pro_tx_hash = pro_tx_hash; + // Transient addresses can survive in memory from the preceding + // protocol generation, but do not make payment metadata a write. + item.state.addresses = Some(moved_platform_ports()); + state.insert_masternode(item); + } + state.mark_saved(); + let unchanged = state.full_masternode_list()[&payment_only_hash].clone(); + let new_service = "5.6.7.8:5678".parse().expect("socket address"); + + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![ + ( + changed_hash, + DMNStateDiff { + service: Some(new_service), + ..empty_state_diff() + }, + ), + ( + payment_only_hash, + DMNStateDiff { + last_paid_height: Some(2_129_183), + consecutive_payments: Some(1), + pose_penalty: Some(0), + ..empty_state_diff() + }, + ), + ], + }) + }); + + platform + .update_state_masternode_list_v1(&mut state, 1, false) + .expect("update must succeed"); + + assert_eq!(state.full_masternode_list()[&payment_only_hash], unchanged); + assert_eq!(state.hpmn_masternode_list()[&payment_only_hash], unchanged); + assert_eq!( + state.full_masternode_list()[&changed_hash].state.service, + new_service + ); + assert_eq!( + state.hpmn_masternode_list()[&changed_hash].state.service, + new_service + ); + assert_eq!( + state.masternode_changes.upserted, + BTreeSet::from([changed_hash]), + "payment-only updates must not cause writes alongside a real change" + ); + assert!(state.masternode_changes.removed.is_empty()); + assert!(!state.masternode_changes.rewrite_all); + } + + /// The IP every test evonode is registered with. + const SERVICE_IP: &str = "1.2.3.4"; + const EVONODE_PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + const VALIDATOR_QUORUM_HASH: [u8; 32] = [0x55u8; 32]; + + fn evonode_pro_tx_hash() -> ProTxHash { + ProTxHash::from_byte_array(EVONODE_PRO_TX_HASH) + } + + /// An evonode with platform ports 36656 (P2P) and 443 (HTTPS), as Core + /// lists it with `-deprecatedrpc=service`: the legacy port fields carry + /// the live ports and `addresses` repeats them. + fn evonode() -> MasternodeListItem { + let mut item = masternode(evonode_pro_tx_hash()); + item.node_type = MasternodeType::Evo; + item.state.platform_node_id = Some([7u8; 20]); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = Some(36656); + item.state.legacy_platform_http_port = Some(443); + } + item.state.addresses = Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:443".to_string()], + }); + item + } + + /// The same evonode in a state whose legacy ports are absent: the ports + /// are only in `addresses`. + fn evonode_without_legacy_ports() -> MasternodeListItem { + let mut item = evonode(); + #[allow(deprecated)] + { + item.state.legacy_platform_p2p_port = None; + item.state.legacy_platform_http_port = None; + } + item + } + + /// `item` as a node that restarted holds it: loaded back from storage. + fn reloaded(item: MasternodeListItem) -> MasternodeListItem { + MasternodeV0::from(item).into() + } + + /// Runs one masternode list update per diff, in order, for a state + /// holding `evonode` as a member of one validator set, with Core reporting + /// the diffs for it, and returns the state afterwards. + fn state_after_diffs( + mut evonode: MasternodeListItem, + diffs: Vec, + ) -> PlatformState { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new().build_with_mock_rpc(); + + let pro_tx_hash = evonode.pro_tx_hash; + let quorum_hash = QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + evonode.state = in_stored_form(evonode.state); + state.insert_masternode(evonode); + let validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: SERVICE_IP.to_string(), + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: false, + }; + let mut rng = StdRng::seed_from_u64(1); + state.insert_validator_set( + quorum_hash, + ValidatorSet::V0(ValidatorSetV0 { + quorum_hash, + quorum_index: None, + core_height: 1, + members: BTreeMap::from([(pro_tx_hash, validator)]), + threshold_public_key: SecretKey::::random(&mut rng).public_key(), + }), + ); + state.mark_saved(); + + let block_count = diffs.len() as u32; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, diffs[block as usize - 1].clone())], + }) + }); + + for block in 1..=block_count { + platform + .update_state_masternode_list_v1(&mut state, block, false) + .expect("update must succeed"); + } + + state + } + + /// The evonode's member of the validator set. + fn validator_in(state: &PlatformState) -> ValidatorV0 { + state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)].members() + [&evonode_pro_tx_hash()] + .clone() + } + + /// The validator a quorum Drive learns about later gets for the evonode, + /// once from the state as held in memory and once as a node that + /// restarted loads it back. Validator-set membership decides quorum + /// rotation, so every node must build the same one. + fn validators_for_a_new_quorum(state: &PlatformState) -> [Option; 2] { + let pro_tx_hash = evonode_pro_tx_hash(); + let mut restarted = state.clone(); + restarted.insert_masternode(reloaded(state.full_masternode_list()[&pro_tx_hash].clone())); + [state, &restarted].map(|state| { + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state) + }) + } + + /// Asserts that the evonode's member of the existing validator set has + /// the given ports, and that a new quorum gets the same member. + fn assert_validator_ports(state: &PlatformState, p2p_port: u16, http_port: u16) { + let validator = validator_in(state); + assert_eq!(validator.node_ip, SERVICE_IP); + assert_eq!(validator.platform_p2p_port, p2p_port); + assert_eq!(validator.platform_http_port, http_port); + for new_quorum_validator in validators_for_a_new_quorum(state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// The network info of the evonode after moving to platform ports 36657 + /// (P2P) and 8443 (HTTPS), as Core prints an ExtAddr one. + fn moved_platform_ports() -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36657".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + } + } + + /// A node that restarts reloads its masternode lists from storage, and + /// validator-set membership, which decides quorum rotation, must not + /// depend on whether it did. So after every masternode list update the + /// lists held in memory must be exactly what storage gives back. The + /// updates: an ExtAddr evonode added with its platform ports in + /// `addresses` and a payout list, a port move that only `addresses` + /// carries, then a revoke that zeroes the legacy ports and carries no + /// `addresses`. + #[test] + fn should_hold_after_every_update_the_masternode_lists_a_restarted_node_loads() { + let platform_version = PlatformVersion::latest(); + let mut platform = TestPlatformBuilder::new() + .with_config(PlatformConfig { + testing_configs: PlatformTestConfig { + store_platform_state: true, + ..PlatformTestConfig::default_minimal_verifications() + }, + ..Default::default() + }) + .build_with_mock_rpc() + .set_genesis_state(); + + let mut added = evonode(); + added.collateral_address = Some([0x21; 20]); + added.state.owner_address = Some([0x22; 20]); + added.state.payout_address = None; + added.state.payouts = Some(vec![DMNPayout { + address: [0x23; 20], + script: ScriptBuf::new_p2pkh(&PubkeyHash::from_byte_array([0x23; 20])), + reward: 10000, + }]); + let pro_tx_hash = added.pro_tx_hash; + let port_move = DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }; + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(3)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |base_block, block| { + let (added_mns, updated_mns) = match block { + 1 => (vec![added.clone()], vec![]), + 2 => (vec![], vec![(pro_tx_hash, port_move.clone())]), + _ => (vec![], vec![(pro_tx_hash, revoke.clone())]), + }; + Ok(MasternodeListDiff { + base_height: base_block.unwrap_or_default(), + block_height: block, + added_mns, + removed_mns: vec![], + updated_mns, + }) + }); + + let transaction = platform.drive.grove.start_transaction(); + let mut state = platform.state.load().as_ref().clone(); + // The ports and ban each update leaves the evonode with. + let expected = [ + (Some(36656), Some(443), false), + (Some(36657), Some(8443), false), + (Some(0), Some(0), true), + ]; + for (block, (p2p, http, is_banned)) in (1..).zip(expected) { + platform + .update_state_masternode_list_v1(&mut state, block, false) + .expect("update must succeed"); + platform + .store_platform_state(&state, Some(&transaction), platform_version) + .expect("store platform state"); + state.mark_saved(); + let reloaded = Platform::::fetch_platform_state( + &platform.drive, + Some(&transaction), + platform_version, + ) + .expect("fetch platform state") + .expect("a state was stored"); + + assert_eq!( + reloaded.full_masternode_list(), + state.full_masternode_list(), + "the full list after block {block}" + ); + assert_eq!( + reloaded.hpmn_masternode_list(), + state.hpmn_masternode_list(), + "the evonode list after block {block}" + ); + let evonode = &reloaded.hpmn_masternode_list()[&pro_tx_hash].state; + assert_eq!( + ( + PlatformPorts::of(evonode), + evonode.pose_ban_height.is_some() + ), + (PlatformPorts { p2p, http }, is_banned), + "the evonode after block {block}" + ); + } + } + + /// An ExtAddr evonode keeps its platform ports in its network info and its + /// legacy port fields at zero, so a ProUpServTx that only moves its ports + /// reaches Drive as a diff whose only network field is `addresses` + /// (`service` too only with `-deprecatedrpc=service`). Tenderdash must be + /// told the new port, or it keeps dialling the old one. + #[test] + fn should_update_validator_ports_from_an_addresses_only_diff() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// The same port move as Core prints it with `-deprecatedrpc=service`: + /// `service` repeats because the network info changed, while the legacy + /// port fields are absent because they did not change. + #[test] + fn should_update_validator_ports_from_addresses_when_the_diff_also_carries_service() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(moved_platform_ports())), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 8443); + } + + /// An ExtAddr evonode that moves only its HTTPS port lists its whole + /// network info again, the unchanged P2P entry included. + #[test] + fn should_update_the_validator_https_port_when_only_it_moved() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 8443); + } + + /// HTTPS alone is not an existing-validator refresh trigger. New quorums + /// still use the updated stored HTTPS port. + #[test] + fn should_keep_the_existing_validator_https_port_without_a_refresh_trigger() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec![], + platform_https: vec!["1.2.3.4:8443".to_string()], + })), + ..empty_state_diff() + }], + ); + assert_eq!(validator_in(&state).platform_http_port, 443); + for validator in validators_for_a_new_quorum(&state) { + assert_eq!(validator.expect("new validator").platform_http_port, 8443); + } + } + + /// When a legacy evonode changes only a platform port, the diff does not + /// carry the node's IP, so Core pairs the new port in `addresses` with the + /// placeholder host 255.255.255.255. Only the port may be taken from it. + #[test] + fn should_not_take_the_placeholder_host_from_a_legacy_evonode_port_diff() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36657, 443); + } + + /// A legacy evonode's diff lists in `addresses` only the entries that + /// changed, next to the changed legacy port field. A later diff that + /// changes the other port must not undo an earlier one's port. + #[test] + fn should_keep_each_platform_port_from_diffs_that_list_one_entry_each() { + let state = state_after_diffs( + evonode(), + vec![ + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_p2p_port: Some(36657), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec!["255.255.255.255:36657".to_string()], + platform_https: vec![], + })), + ..empty_state_diff() + }, + DMNStateDiff { + #[allow(deprecated)] + legacy_platform_http_port: Some(8443), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec![], + platform_p2p: vec![], + platform_https: vec!["255.255.255.255:8443".to_string()], + })), + ..empty_state_diff() + }, + ], + ); + + assert_eq!(validator_in(&state).platform_p2p_port, 36657); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + let new_quorum_validator = new_quorum_validator.expect("the evonode has ports"); + assert_eq!(new_quorum_validator.platform_p2p_port, 36657); + assert_eq!(new_quorum_validator.platform_http_port, 8443); + } + } + + /// A diff of a legacy evonode's core address carries `addresses` with the + /// core entry only, so the platform ports must not be read as cleared. + #[test] + fn should_keep_validator_ports_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + evonode(), + vec![DMNStateDiff { + service: Some("5.6.7.8:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["5.6.7.8:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + let validator = validator_in(&state); + assert_eq!(validator.node_ip, "5.6.7.8"); + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + for new_quorum_validator in validators_for_a_new_quorum(&state) { + assert_eq!(new_quorum_validator.as_ref(), Some(&validator)); + } + } + + /// A node that restarted holds the evonode without `addresses`. A diff + /// that lists only the core entry must leave its platform ports as they + /// are, as on a node that kept its state in memory. + #[test] + fn should_keep_validator_ports_after_a_restart_when_a_diff_moves_only_the_core_address() { + let state = state_after_diffs( + reloaded(evonode()), + vec![DMNStateDiff { + service: Some("1.2.3.4:1234".parse().expect("socket address")), + addresses: Some(Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:1234".to_string()], + platform_p2p: vec![], + platform_https: vec![], + })), + ..empty_state_diff() + }], + ); + + assert_validator_ports(&state, 36656, 443); + } + + /// After V24 a legacy evonode's revoke, or an operator key change, makes + /// it ExtAddr with an empty network info: Core prints its platform ports + /// as 0 and no `addresses`. Earlier binaries accept that diff, so every + /// node must build the validator they build from the legacy fields: a + /// banned member on the unspecified IP with ports 0. A node that kept the + /// evonode's `addresses` in memory and a node that restarted must both + /// build it, because validator-set membership decides quorum rotation. + #[test] + fn should_agree_on_the_validator_after_a_legacy_evonode_is_revoked_whether_or_not_the_node_restarted( + ) { + #[allow(deprecated)] + let revoke = DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + legacy_platform_p2p_port: Some(0), + legacy_platform_http_port: Some(0), + ..empty_state_diff() + }; + let kept = state_after_diffs(evonode(), vec![revoke.clone()]); + let restarted = state_after_diffs(reloaded(evonode()), vec![revoke]); + + let pro_tx_hash = evonode_pro_tx_hash(); + let new_quorum_validator = ValidatorV0 { + pro_tx_hash, + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 0, + platform_p2p_port: 0, + is_banned: true, + }; + // A refresh of an existing member takes the ban, the IP and the ports + // from the diff, and keeps its node id and core port. + let refreshed_member = ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + }; + for state in [&kept, &restarted] { + assert_eq!( + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, state), + Some(new_quorum_validator.clone()) + ); + assert_eq!(validator_in(state), refreshed_member); + } + let stored = |state: &PlatformState| { + MasternodeV0::from(state.full_masternode_list()[&pro_tx_hash].clone()).state + }; + assert_eq!(stored(&kept), stored(&restarted)); + } + + /// When Core resets an ExtAddr evonode's operator fields (ProUpRevTx, or + /// a ProUpRegTx that changes the operator key), its network info becomes + /// empty: the diff has `service` `[::]:0` and no `addresses` key, so the + /// platform ports keep resolving to the old endpoint. That endpoint is + /// never advertised, because Core bans the masternode in the same + /// transition and only a ProUpServTx, which sends new `addresses`, revives + /// it. The evonode stays a member, banned, with the ports it last had. + #[test] + fn should_not_advertise_an_evonode_whose_network_info_core_emptied() { + let state = state_after_diffs( + evonode_without_legacy_ports(), + vec![DMNStateDiff { + service: Some("[::]:0".parse().expect("socket address")), + pose_ban_height: Some(Some(2)), + revocation_reason: Some(1), + platform_node_id: Some([0u8; 20]), + pub_key_operator: Some(vec![1u8; 48]), + ..empty_state_diff() + }], + ); + + let validator_set = + &state.validator_sets()[&QuorumHash::from_byte_array(VALIDATOR_QUORUM_HASH)]; + let new_quorum_validator = ValidatorV0 { + pro_tx_hash: evonode_pro_tx_hash(), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 36656, + is_banned: true, + }; + assert_eq!( + validator_set.members()[&evonode_pro_tx_hash()], + ValidatorV0 { + node_id: PubkeyHash::from_byte_array([7u8; 20]), + core_port: 1234, + ..new_quorum_validator.clone() + } + ); + assert!( + validator_set.to_update().validator_updates.is_empty(), + "a banned validator is left out of the validator set update" + ); + for validator in validators_for_a_new_quorum(&state) { + assert_eq!( + validator, + Some(new_quorum_validator.clone()), + "a validator set built later gets the same banned member" + ); + } + } +} + +#[cfg(test)] +mod port_resolution_tests { + use super::in_stored_form; + use crate::config::PlatformConfig; + use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods}; + use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; + use dpp::dashcore::hashes::Hash; + use dpp::dashcore::{Network, Txid}; + use dpp::dashcore::{ProTxHash, PubkeyHash}; + use dpp::dashcore_rpc::json::{DMNState, MasternodeListItem}; + use dpp::dashcore_rpc::json::{MasternodeAddresses, MasternodeType}; + use dpp::version::PlatformVersion; + + const PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; + + /// An evonode registered at 1.2.3.4 with the given legacy platform ports + /// and nested addresses. + fn evonode_state( + legacy_platform_p2p_port: Option, + legacy_platform_http_port: Option, + addresses: Option, + ) -> DMNState { + #[allow(deprecated)] + DMNState { + service: "1.2.3.4:9999".parse().expect("socket address"), + registered_height: 0, + pose_revived_height: None, + pose_ban_height: None, + revocation_reason: 0, + owner_address: Some([0u8; 20]), + voting_address: [0u8; 20], + payout_address: Some([0u8; 20]), + payouts: None, + pub_key_operator: vec![0u8; 48], + operator_payout_address: None, + platform_node_id: Some([7u8; 20]), + legacy_platform_p2p_port, + legacy_platform_http_port, + addresses, + } + } + + fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { + MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec![p2p.to_string()], + platform_https: vec![https.to_string()], + } + } + + fn validator_for(dmn_state: DMNState) -> Option { + let platform_version = PlatformVersion::latest(); + let mut state = PlatformState::default_with_protocol_versions( + platform_version.protocol_version, + platform_version.protocol_version, + &PlatformConfig::default_for_network(Network::Testnet), + ) + .expect("platform state"); + let pro_tx_hash = ProTxHash::from_byte_array(PRO_TX_HASH); + state.insert_masternode(MasternodeListItem { + node_type: MasternodeType::Evo, + pro_tx_hash, + collateral_hash: Txid::from_byte_array([0u8; 32]), + collateral_index: 0, + collateral_address: Some([0u8; 20]), + operator_reward: 0.0, + state: in_stored_form(dmn_state), + }); + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + } + + /// An ExtAddr evonode keeps its platform ports in its network info; its + /// legacy port fields stay zero. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_zero() { + let validator = validator_for(evonode_state( + Some(0), + Some(0), + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// A state whose legacy ports are absent takes its ports from + /// `addresses`. + #[test] + fn should_take_platform_ports_from_addresses_when_legacy_ports_are_absent() { + let validator = validator_for(evonode_state( + None, + None, + Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.platform_p2p_port, 36656); + assert_eq!(validator.platform_http_port, 443); + } + + /// The validator is advertised on the core IP from `service`, whatever + /// host `addresses` names for the platform services: a legacy evonode's + /// port-only diff pairs the port with the placeholder host + /// 255.255.255.255. + #[test] + fn should_advertise_the_service_ip_whatever_host_addresses_name() { + let validator = validator_for(evonode_state( + Some(36656), + Some(443), + Some(platform_addresses( + "255.255.255.255:36656", + "203.0.113.7:443", + )), + )) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.core_port, 9999); + } + + /// Core before v23 lists no nested addresses; the legacy ports apply. + #[test] + fn should_take_legacy_ports_without_addresses() { + let validator = validator_for(evonode_state(Some(26656), Some(443), None)) + .expect("the evonode has platform ports"); + + assert_eq!(validator.node_ip, "1.2.3.4"); + assert_eq!(validator.platform_p2p_port, 26656); + assert_eq!(validator.platform_http_port, 443); + } + + /// Validator-set membership decides quorum rotation, so it must not + /// depend on how a node reads a port: an evonode whose legacy ports are 0 + /// and that lists no nested ports stays a validator, with those ports. + #[test] + fn should_keep_an_evonode_with_zero_legacy_ports_as_a_validator() { + let validator = validator_for(evonode_state(Some(0), Some(0), None)) + .expect("membership does not depend on the port value"); + + assert_eq!(validator.platform_p2p_port, 0); + assert_eq!(validator.platform_http_port, 0); + } + + /// A revoked legacy evonode as Core lists it with + /// `-deprecatedrpc=service`: its network info is empty, so `service` is + /// `[::]:0` and `addresses` has no platform entry, while the legacy port + /// fields keep the ports it was registered with. + fn revoked_legacy_evonode_state(addresses: &str) -> DMNState { + serde_json::from_str(&format!( + r#"{{ + "service": "[::]:0", + "addresses": {addresses}, + "registeredHeight": 100, + "PoSeRevivedHeight": -1, + "PoSeBanHeight": 200, + "revocationReason": 1, + "ownerAddress": "yPBWCdMRY5PsS3hJzs7csbdWQVRR85yxUz", + "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", + "payoutAddress": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", + "pubKeyOperator": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "platformNodeID": "0000000000000000000000000000000000000000", + "platformP2PPort": 26656, + "platformHTTPPort": 443 + }}"# + )) + .expect("Core's JSON for a revoked evonode") + } + + /// Whether an evonode is a validator decides quorum rotation, so every + /// node must build the same member for it. For an evonode whose listed + /// `addresses` hold no platform entry, that is the member the legacy + /// port fields alone give: banned, on the unspecified IP, with its + /// registered ports. + #[test] + fn should_build_a_revoked_legacy_evonode_from_its_legacy_ports() { + for addresses in [r#"{}"#, r#"{"core_p2p": []}"#] { + let validator = validator_for(revoked_legacy_evonode_state(addresses)); + + assert_eq!( + validator, + Some(ValidatorV0 { + pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), + public_key: None, + node_ip: "::".to_string(), + node_id: PubkeyHash::from_byte_array([0u8; 20]), + core_port: 0, + platform_http_port: 443, + platform_p2p_port: 26656, + is_banned: true, + }), + "addresses: {addresses}" + ); + } + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs new file mode 100644 index 00000000000..837b16ddcff --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs @@ -0,0 +1,93 @@ +use crate::error::Error; +use crate::execution::types::update_state_masternode_list_outcome; +use crate::platform_types::platform::Platform; +use crate::platform_types::platform_state::PlatformState; +use crate::platform_types::platform_state::PlatformStateV0Methods; +use crate::rpc::core::CoreRPCLike; +use dpp::block::block_info::BlockInfo; +use dpp::block::extended_block_info::v0::ExtendedBlockInfoV0Getters; +use dpp::version::PlatformVersion; +use drive::grovedb::Transaction; + +impl Platform +where + C: CoreRPCLike, +{ + /// Updates the masternode list in the platform state based on changes in the masternode list + /// from Dash Core between two block heights. + /// + /// This function fetches the masternode list difference between the current core block height + /// and the previous core block height, then updates the full masternode list and the + /// HPMN (high performance masternode) list in the platform state accordingly. + /// + /// # Arguments + /// + /// * `state` - A mutable reference to the platform state to be updated. + /// * `core_block_height` - The current block height in the Dash Core. + /// * `transaction` - The current groveDB transaction. + /// + /// # Returns + /// + /// * `Result<(), Error>` - Returns `Ok(())` if the update is successful. Returns an error if + /// there is a problem fetching the masternode list difference or updating the state. + #[allow(clippy::too_many_arguments)] + pub(super) fn update_masternode_list_v1( + &self, + platform_state: Option<&PlatformState>, + block_platform_state: &mut PlatformState, + core_block_height: u32, + is_init_chain: bool, + block_info: &BlockInfo, + transaction: &Transaction, + platform_version: &PlatformVersion, + ) -> Result<(), Error> { + if let Some(last_committed_block_info) = + block_platform_state.last_committed_block_info().as_ref() + { + if core_block_height == last_committed_block_info.basic_info().core_height { + tracing::debug!( + method = "update_masternode_list_v1", + "no update mnl at height {}", + core_block_height, + ); + return Ok(()); // no need to do anything + } + } + tracing::debug!( + method = "update_masternode_list_v1", + "update mnl to height {} at block {}", + core_block_height, + block_platform_state.last_committed_core_height() + ); + + let update_state_masternode_list_outcome::v0::UpdateStateMasternodeListOutcome { + masternode_list_diff, + removed_masternodes, + } = self.update_state_masternode_list_v1( + block_platform_state, + core_block_height, + is_init_chain, + )?; + + self.update_masternode_identities( + masternode_list_diff, + &removed_masternodes, + block_info, + platform_state, + transaction, + platform_version, + )?; + + if !removed_masternodes.is_empty() { + self.drive.remove_validators_proposed_app_versions( + removed_masternodes + .into_keys() + .map(|pro_tx_hash| pro_tx_hash.into()), + Some(transaction), + &platform_version.drive, + )?; + } + + Ok(()) + } +} diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/versioning_tests.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/versioning_tests.rs new file mode 100644 index 00000000000..345db4744cd --- /dev/null +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/versioning_tests.rs @@ -0,0 +1,170 @@ +use crate::platform_types::platform_state::PlatformStateV0Methods; +use crate::platform_types::validator::v0::{NewValidatorIfMasternodeInState, ValidatorV0}; +use crate::test::helpers::setup::TestPlatformBuilder; +use dpp::block::block_info::BlockInfo; +use dpp::dashcore_rpc::json::{MasternodeAddresses, MasternodeListDiff}; +use dpp::version::PlatformVersion; +use serde_json::json; + +/// An RPC response with legacy identity fields so the port version gate is +/// exercised independently of the owner-identity version gate. +fn conflicting_ports() -> MasternodeListDiff { + let mut diff: MasternodeListDiff = serde_json::from_str(include_str!(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/supporting_files/core_v24_protx_list_diffs/before_v24/1-1000.json" + ))) + .expect("masternode fixture"); + assert_eq!(diff.added_mns.len(), 1); + let state = &mut diff.added_mns[0].state; + #[allow(deprecated)] + { + state.legacy_platform_p2p_port = Some(26656); + state.legacy_platform_http_port = Some(443); + } + state.addresses = Some(MasternodeAddresses { + core_p2p: vec![state.service.to_string()], + platform_p2p: vec!["192.0.2.40:36656".to_string()], + platform_https: vec!["192.0.2.40:8443".to_string()], + }); + diff +} + +#[test] +fn should_use_nested_ports_only_after_the_protocol_upgrade() { + for (platform_version, expected_ports) in [ + (PlatformVersion::get(13).expect("PV13"), (26656, 443)), + (PlatformVersion::latest(), (36656, 8443)), + ] { + let diff = conflicting_ports(); + let pro_tx_hash = diff.added_mns[0].pro_tx_hash; + let mut platform = TestPlatformBuilder::new() + .with_initial_protocol_version(platform_version.protocol_version) + .build_with_mock_rpc() + .set_genesis_state(); + let update = MasternodeListDiff { + base_height: 1000, + block_height: 2000, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![( + pro_tx_hash, + serde_json::from_value(json!({ + "addresses": { "platform_p2p": ["192.0.2.40:36657"] } + })) + .expect("nested-only diff"), + )], + }; + let flat_update = MasternodeListDiff { + base_height: 2000, + block_height: 2001, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![( + pro_tx_hash, + serde_json::from_value(json!({ + "platformP2PPort": 27656, + "platformHTTPPort": 1443 + })) + .expect("flat-only diff"), + )], + }; + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .returning(move |_, block| { + Ok(if block == 1000 { + diff.clone() + } else if block == 2000 { + update.clone() + } else { + flat_update.clone() + }) + }); + let mut state = platform.state.load().as_ref().clone(); + let transaction = platform.drive.grove.start_transaction(); + platform + .update_masternode_list( + None, + &mut state, + 1000, + true, + &BlockInfo::default(), + &transaction, + platform_version, + ) + .expect("update masternode list"); + let validator = + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + .expect("validator"); + assert_eq!( + (validator.platform_p2p_port, validator.platform_http_port), + expected_ports, + "protocol version {}", + platform_version.protocol_version + ); + #[allow(deprecated)] + let stored_ports = ( + state.hpmn_masternode_list()[&pro_tx_hash] + .state + .legacy_platform_p2p_port, + state.hpmn_masternode_list()[&pro_tx_hash] + .state + .legacy_platform_http_port, + ); + assert_eq!( + stored_ports, + (Some(expected_ports.0.into()), Some(expected_ports.1.into())) + ); + + let previous_state = state.clone(); + platform + .update_masternode_list( + Some(&previous_state), + &mut state, + 2000, + false, + &BlockInfo::default(), + &transaction, + platform_version, + ) + .expect("nested-only update"); + let validator = + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + .expect("updated validator"); + assert_eq!( + (validator.platform_p2p_port, validator.platform_http_port), + ( + if platform_version.protocol_version == 13 { + 26656 + } else { + 36657 + }, + expected_ports.1 + ), + "nested-only diff at protocol version {}", + platform_version.protocol_version, + ); + + let previous_state = state.clone(); + platform + .update_masternode_list( + Some(&previous_state), + &mut state, + 2001, + false, + &BlockInfo::default(), + &transaction, + platform_version, + ) + .expect("flat-only update"); + let validator = + ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) + .expect("updated validator"); + assert_eq!( + (validator.platform_p2p_port, validator.platform_http_port), + (27656, 1443), + "flat-only diff at protocol version {}", + platform_version.protocol_version, + ); + } +} diff --git a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs index c3ad7d54420..c67b646feb8 100644 --- a/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/masternode/v0/mod.rs @@ -2,7 +2,7 @@ pub mod accessors; use dpp::bincode::{Decode, Encode}; -use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, DMNStateDiff, MasternodeType}; +use dpp::dashcore_rpc::dashcore_rpc_json::{DMNState, MasternodeType}; use dpp::dashcore_rpc::json::MasternodeListItem; use std::fmt::{Debug, Formatter}; @@ -154,34 +154,9 @@ pub struct MasternodeStateV0 { pub platform_http_port: Option, } -/// `state` in the form Drive keeps masternode states in, in memory as in -/// storage: `MasternodeStateV0` has no `addresses`, so the platform ports -/// resolved from them move into the legacy port fields and `addresses` is -/// dropped. It has no payout list either, so `payouts` is dropped too. -/// -/// A node that restarted loads its masternode states in this form. Keeping -/// them in it in memory too means every node derives the same validators -/// from the same Core diffs, however long it has run. -pub(crate) fn in_stored_form(state: DMNState) -> DMNState { - MasternodeStateV0::from(state).into() -} - -/// `state`, kept in stored form, with a Core state diff applied, in stored -/// form again. -pub(crate) fn apply_diff_in_stored_form(state: &DMNState, diff: &DMNStateDiff) -> DMNState { - let mut updated = state.clone(); - updated.apply_diff(diff.clone()); - in_stored_form(updated) -} - impl From for MasternodeStateV0 { fn from(value: DMNState) -> Self { - // The payout list and the nested addresses are not stored. The stored state keeps the - // platform ports resolved from the addresses instead: an ExtAddr evonode's legacy port - // fields stay zero and go stale when its ports move. Loaded back as legacy ports, they - // resolve to the same ports. Without a nested port the legacy field is stored as it is. - let resolved_platform_p2p_port = value.platform_p2p_address().map(|(_, port)| port); - let resolved_platform_http_port = value.platform_http_address().map(|(_, port)| port); + // The payout list and the nested addresses are not stored. #[allow(deprecated)] let DMNState { service, @@ -213,8 +188,8 @@ impl From for MasternodeStateV0 { pub_key_operator, operator_payout_address, platform_node_id, - platform_p2p_port: resolved_platform_p2p_port.or(platform_p2p_port), - platform_http_port: resolved_platform_http_port.or(platform_http_port), + platform_p2p_port, + platform_http_port, } } } @@ -260,11 +235,9 @@ impl From for DMNState { #[cfg(test)] mod tests { - use super::*; use crate::platform_types::platform_state::platform_state_for_saving::v2::{ deserialize_masternode_entry, serialize_masternode_entry, }; - use crate::platform_types::validator::v0::PlatformPorts; use dpp::dashcore::hashes::Hash; use dpp::dashcore::{ProTxHash, PubkeyHash, ScriptBuf, Txid}; use dpp::dashcore_rpc::dashcore_rpc_json::{ @@ -385,186 +358,4 @@ mod tests { assert_eq!(reloaded.state.owner_address, None); assert_eq!(reloaded.state.payout_address, None); } - - /// An evonode registered at 1.2.3.4 with the given legacy platform ports - /// and nested addresses. - fn evonode_state( - legacy_platform_p2p_port: Option, - legacy_platform_http_port: Option, - addresses: Option, - ) -> DMNState { - #[allow(deprecated)] - DMNState { - service: "1.2.3.4:9999".parse().expect("socket address"), - registered_height: 0, - pose_revived_height: None, - pose_ban_height: None, - revocation_reason: 0, - owner_address: Some([0u8; 20]), - voting_address: [0u8; 20], - payout_address: Some([0u8; 20]), - payouts: None, - pub_key_operator: vec![0u8; 48], - operator_payout_address: None, - platform_node_id: Some([7u8; 20]), - legacy_platform_p2p_port, - legacy_platform_http_port, - addresses, - } - } - - fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { - MasternodeAddresses { - core_p2p: vec!["1.2.3.4:9999".to_string()], - platform_p2p: vec![p2p.to_string()], - platform_https: vec![https.to_string()], - } - } - - /// The stored state keeps no `addresses`, so it must keep the ports they - /// carry: an ExtAddr evonode's legacy port fields stay zero, and after a - /// port move they are stale. - #[test] - fn should_store_the_platform_ports_from_addresses() { - let stored = MasternodeStateV0::from(evonode_state( - Some(36656), - Some(0), - Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), - )); - - assert_eq!(stored.platform_p2p_port, Some(36657)); - assert_eq!(stored.platform_http_port, Some(8443)); - } - - /// A node that restarts must resolve the same platform ports as one that - /// kept its state in memory, or it advertises stale validator ports. - #[test] - fn should_resolve_the_same_platform_ports_after_a_store_and_load() { - let in_memory = evonode_state( - Some(36656), - Some(0), - Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), - ); - - let loaded = DMNState::from(MasternodeStateV0::from(in_memory.clone())); - - assert_eq!(PlatformPorts::of(&loaded), PlatformPorts::of(&in_memory)); - assert_eq!( - in_stored_form(loaded.clone()), - loaded, - "a loaded state is already in stored form" - ); - } - - /// Without nested platform ports the legacy fields are stored as they are, - /// zero included, and so is a value no port can have: that is what every - /// earlier binary stores, and a state loaded back is then already in - /// stored form. - #[test] - fn should_store_legacy_ports_unchanged_without_nested_ports() { - for (p2p, http) in [ - (Some(26656), Some(443)), - (Some(0), Some(0)), - (None, None), - (Some(70000), Some(70000)), - ] { - let stored = MasternodeStateV0::from(evonode_state(p2p, http, None)); - assert_eq!( - (stored.platform_p2p_port, stored.platform_http_port), - (p2p, http) - ); - let loaded = DMNState::from(stored); - assert_eq!(in_stored_form(loaded.clone()), loaded); - - let core_entry_only = MasternodeAddresses { - core_p2p: vec!["1.2.3.4:9999".to_string()], - platform_p2p: vec![], - platform_https: vec![], - }; - let stored = MasternodeStateV0::from(evonode_state(p2p, http, Some(core_entry_only))); - assert_eq!( - (stored.platform_p2p_port, stored.platform_http_port), - (p2p, http) - ); - } - } - - /// Masternode states are kept in memory in stored form so that a node - /// that restarted holds the same state as one that did not. That only - /// holds while the stored form is exactly what the store writes and - /// reads back. - #[test] - fn should_hold_in_stored_form_exactly_what_the_store_reads_back() { - let revoked = |addresses: Option| DMNState { - service: "[::]:0".parse().expect("socket address"), - pose_ban_height: Some(200), - revocation_reason: 1, - platform_node_id: Some([0u8; 20]), - ..evonode_state(Some(26656), Some(443), addresses) - }; - let placeholder_host = MasternodeAddresses { - core_p2p: vec![], - platform_p2p: vec!["255.255.255.255:36657".to_string()], - platform_https: vec![], - }; - let on_ipv6 = DMNState { - service: "[2001:db8::1]:9999".parse().expect("socket address"), - ..evonode_state( - None, - None, - Some(platform_addresses( - "[2001:db8::1]:36656", - "[2001:db8::1]:443", - )), - ) - }; - let states = [ - evonode_state(Some(26656), Some(443), None), - evonode_state(Some(0), Some(0), None), - evonode_state(None, None, None), - evonode_state( - Some(36656), - Some(443), - Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), - ), - evonode_state( - Some(0), - Some(0), - Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), - ), - evonode_state( - None, - None, - Some(platform_addresses("1.2.3.4:36657", "1.2.3.4:8443")), - ), - evonode_state(Some(36657), Some(443), Some(placeholder_host)), - on_ipv6, - revoked(None), - revoked(Some(MasternodeAddresses::default())), - ]; - - for state in states { - let masternode = MasternodeListItem { - node_type: MasternodeType::Evo, - pro_tx_hash: ProTxHash::from_byte_array([0x77u8; 32]), - collateral_hash: Txid::from_byte_array([0x11u8; 32]), - collateral_index: 1, - collateral_address: Some([0x22u8; 20]), - operator_reward: 0.0, - state: state.clone(), - }; - let entry = serialize_masternode_entry(&masternode, PlatformVersion::latest()) - .expect("serializable masternode"); - let loaded = deserialize_masternode_entry(&entry).expect("its own entry"); - - assert_eq!( - loaded, - MasternodeListItem { - state: in_stored_form(state.clone()), - ..masternode - }, - "state: {state:?}" - ); - } - } } diff --git a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs index 4cf479b6431..73a1990a4ea 100644 --- a/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs +++ b/packages/rs-drive-abci/src/platform_types/platform_state/accessors.rs @@ -1,6 +1,5 @@ use crate::error::execution::ExecutionError; use crate::error::Error; -use crate::platform_types::masternode::v0::{apply_diff_in_stored_form, in_stored_form}; use crate::platform_types::platform_state::masternode_list_changes::MasternodeListChanges; use crate::platform_types::platform_state::PlatformState; use crate::platform_types::signature_verification_quorum_set::SignatureVerificationQuorumSet; @@ -139,12 +138,10 @@ pub trait PlatformStateV0Methods { /// Sets the current instant lock validating quorums. fn set_instant_lock_validating_quorums(&mut self, quorums: SignatureVerificationQuorumSet); - /// Sets the full masternode list. Its states must be in stored form (see - /// `in_stored_form`), as `insert_masternode` keeps them. + /// Sets the full masternode list. fn set_full_masternode_list(&mut self, list: BTreeMap); - /// Sets the list of high performance masternodes. Its states must be in - /// stored form (see `in_stored_form`), as `insert_masternode` keeps them. + /// Sets the list of high performance masternodes. fn set_hpmn_masternode_list(&mut self, list: BTreeMap); /// Sets the platform initialization information. fn set_genesis_block_info(&mut self, info: Option); @@ -173,23 +170,19 @@ pub trait PlatformStateV0Methods { /// Returns a mutable reference to the instant lock validating quorums. fn instant_lock_validating_quorums_mut(&mut self) -> &mut SignatureVerificationQuorumSet; - /// Returns a mutable reference to the full masternode list. States written - /// through it must be in stored form (see `in_stored_form`). + /// Returns a mutable reference to the full masternode list. fn full_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Returns a mutable reference to the list of high performance masternodes. - /// States written through it must be in stored form (see `in_stored_form`). fn hpmn_masternode_list_mut(&mut self) -> &mut BTreeMap; /// Adds or replaces a masternode in the full list, and in the HPMN list when - /// it is an Evo node. Records the change for the per-entry store. The - /// state is kept in the form it is stored in (see `in_stored_form`). + /// it is an Evo node. Records the change for the per-entry store. fn insert_masternode(&mut self, masternode: MasternodeListItem); /// Applies a Core state diff to a listed masternode in both lists, recording - /// an entry change only when its stored state changes. The state stays in - /// the form it is stored in. Returns false, changing nothing, when the - /// masternode is not listed. + /// an entry change only when its stored state changes. Returns false, + /// changing nothing, when the masternode is not listed. fn apply_masternode_state_diff( &mut self, pro_tx_hash: &ProTxHash, @@ -563,8 +556,7 @@ impl PlatformStateV0Methods for PlatformState { &mut self.hpmn_masternode_list } - fn insert_masternode(&mut self, mut masternode: MasternodeListItem) { - masternode.state = in_stored_form(masternode.state); + fn insert_masternode(&mut self, masternode: MasternodeListItem) { let pro_tx_hash = masternode.pro_tx_hash; if masternode.node_type == MasternodeType::Evo { self.hpmn_masternode_list @@ -585,11 +577,11 @@ impl PlatformStateV0Methods for PlatformState { let Some(masternode) = self.full_masternode_list.get_mut(pro_tx_hash) else { return false; }; - let updated_state = apply_diff_in_stored_form(&masternode.state, state_diff); - let entry_changed = updated_state != masternode.state; - masternode.state = updated_state; + let previous_state = masternode.state.clone(); + masternode.state.apply_diff(state_diff.clone()); + let entry_changed = masternode.state != previous_state; if let Some(hpmn) = self.hpmn_masternode_list.get_mut(pro_tx_hash) { - hpmn.state = apply_diff_in_stored_form(&hpmn.state, state_diff); + hpmn.state.apply_diff(state_diff.clone()); } self.heavy_fields_dirty = true; // A mixed Core diff can include payment-only updates alongside real diff --git a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs index 5bc88cfa733..2fbab063bd1 100644 --- a/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator/v0/mod.rs @@ -22,260 +22,31 @@ impl NewValidatorIfMasternodeInState for ValidatorV0 { ) -> Option { let MasternodeListItem { state, .. } = state.hpmn_masternode_list().get(&pro_tx_hash)?; - let platform_ports = PlatformPorts::of(state); - let PlatformPorts { - p2p: Some(platform_p2p_port), - http: Some(platform_http_port), - } = platform_ports - else { - tracing::debug!( - pro_tx_hash = %pro_tx_hash, - ?platform_ports, - "evonode is not a validator: its platform ports do not resolve" - ); - return None; - }; + #[allow(deprecated)] let DMNState { service, platform_node_id, pose_ban_height, + legacy_platform_p2p_port: platform_p2p_port, + legacy_platform_http_port: platform_http_port, .. } = state; + let Some(platform_http_port) = platform_http_port else { + return None; + }; + let Some(platform_p2p_port) = platform_p2p_port else { + return None; + }; let platform_node_id = (*platform_node_id)?; - // Only the ports come from the platform entries: the validator is - // advertised on the core IP from `service`. Some(ValidatorV0 { pro_tx_hash, public_key, node_ip: service.ip().to_string(), node_id: PubkeyHash::from_byte_array(platform_node_id), core_port: service.port(), - platform_http_port, - platform_p2p_port, + platform_http_port: *platform_http_port as u16, + platform_p2p_port: *platform_p2p_port as u16, is_banned: pose_ban_height.is_some(), }) } } - -/// The platform ports a masternode's state gives its validator. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) struct PlatformPorts { - /// The port Tenderdash dials the validator on. - pub p2p: Option, - /// The platform HTTPS port. - pub http: Option, -} - -impl PlatformPorts { - /// Resolves each port from the nested `addresses` entry first, else from - /// the legacy port field as it is, zero included: rust-dashcore's - /// `platform_{p2p,http}_address()` fall back that way. A port outside the - /// `u16` range resolves to none; Core stores these ports as 16-bit values, - /// so it never prints one. - /// - /// A zero legacy port is kept rather than read as "not set": whether an - /// evonode is a validator decides quorum rotation, so it must not depend - /// on how a port is read. - pub(crate) fn of(state: &DMNState) -> Self { - Self { - p2p: state - .platform_p2p_address() - .and_then(|(_host, port)| u16::try_from(port).ok()), - http: state - .platform_http_address() - .and_then(|(_host, port)| u16::try_from(port).ok()), - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::config::PlatformConfig; - use dpp::dashcore::{Network, Txid}; - use dpp::dashcore_rpc::json::{MasternodeAddresses, MasternodeType}; - use dpp::version::PlatformVersion; - - const PRO_TX_HASH: [u8; 32] = [0x77u8; 32]; - - /// An evonode registered at 1.2.3.4 with the given legacy platform ports - /// and nested addresses. - fn evonode_state( - legacy_platform_p2p_port: Option, - legacy_platform_http_port: Option, - addresses: Option, - ) -> DMNState { - #[allow(deprecated)] - DMNState { - service: "1.2.3.4:9999".parse().expect("socket address"), - registered_height: 0, - pose_revived_height: None, - pose_ban_height: None, - revocation_reason: 0, - owner_address: Some([0u8; 20]), - voting_address: [0u8; 20], - payout_address: Some([0u8; 20]), - payouts: None, - pub_key_operator: vec![0u8; 48], - operator_payout_address: None, - platform_node_id: Some([7u8; 20]), - legacy_platform_p2p_port, - legacy_platform_http_port, - addresses, - } - } - - fn platform_addresses(p2p: &str, https: &str) -> MasternodeAddresses { - MasternodeAddresses { - core_p2p: vec!["1.2.3.4:9999".to_string()], - platform_p2p: vec![p2p.to_string()], - platform_https: vec![https.to_string()], - } - } - - fn validator_for(dmn_state: DMNState) -> Option { - let platform_version = PlatformVersion::latest(); - let mut state = PlatformState::default_with_protocol_versions( - platform_version.protocol_version, - platform_version.protocol_version, - &PlatformConfig::default_for_network(Network::Testnet), - ) - .expect("platform state"); - let pro_tx_hash = ProTxHash::from_byte_array(PRO_TX_HASH); - state.insert_masternode(MasternodeListItem { - node_type: MasternodeType::Evo, - pro_tx_hash, - collateral_hash: Txid::from_byte_array([0u8; 32]), - collateral_index: 0, - collateral_address: Some([0u8; 20]), - operator_reward: 0.0, - state: dmn_state, - }); - ValidatorV0::new_validator_if_masternode_in_state(pro_tx_hash, None, &state) - } - - /// An ExtAddr evonode keeps its platform ports in its network info; its - /// legacy port fields stay zero. - #[test] - fn should_take_platform_ports_from_addresses_when_legacy_ports_are_zero() { - let validator = validator_for(evonode_state( - Some(0), - Some(0), - Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), - )) - .expect("the evonode has platform ports"); - - assert_eq!(validator.platform_p2p_port, 36656); - assert_eq!(validator.platform_http_port, 443); - } - - /// A state whose legacy ports are absent takes its ports from - /// `addresses`. - #[test] - fn should_take_platform_ports_from_addresses_when_legacy_ports_are_absent() { - let validator = validator_for(evonode_state( - None, - None, - Some(platform_addresses("1.2.3.4:36656", "1.2.3.4:443")), - )) - .expect("the evonode has platform ports"); - - assert_eq!(validator.platform_p2p_port, 36656); - assert_eq!(validator.platform_http_port, 443); - } - - /// The validator is advertised on the core IP from `service`, whatever - /// host `addresses` names for the platform services: a legacy evonode's - /// port-only diff pairs the port with the placeholder host - /// 255.255.255.255. - #[test] - fn should_advertise_the_service_ip_whatever_host_addresses_name() { - let validator = validator_for(evonode_state( - Some(36656), - Some(443), - Some(platform_addresses( - "255.255.255.255:36656", - "203.0.113.7:443", - )), - )) - .expect("the evonode has platform ports"); - - assert_eq!(validator.node_ip, "1.2.3.4"); - assert_eq!(validator.core_port, 9999); - } - - /// Core before v23 lists no nested addresses; the legacy ports apply. - #[test] - fn should_take_legacy_ports_without_addresses() { - let validator = validator_for(evonode_state(Some(26656), Some(443), None)) - .expect("the evonode has platform ports"); - - assert_eq!(validator.node_ip, "1.2.3.4"); - assert_eq!(validator.platform_p2p_port, 26656); - assert_eq!(validator.platform_http_port, 443); - } - - /// Validator-set membership decides quorum rotation, so it must not - /// depend on how a node reads a port: an evonode whose legacy ports are 0 - /// and that lists no nested ports stays a validator, with those ports. - #[test] - fn should_keep_an_evonode_with_zero_legacy_ports_as_a_validator() { - let validator = validator_for(evonode_state(Some(0), Some(0), None)) - .expect("membership does not depend on the port value"); - - assert_eq!(validator.platform_p2p_port, 0); - assert_eq!(validator.platform_http_port, 0); - } - - /// A revoked legacy evonode as Core lists it with - /// `-deprecatedrpc=service`: its network info is empty, so `service` is - /// `[::]:0` and `addresses` has no platform entry, while the legacy port - /// fields keep the ports it was registered with. - fn revoked_legacy_evonode_state(addresses: &str) -> DMNState { - serde_json::from_str(&format!( - r#"{{ - "service": "[::]:0", - "addresses": {addresses}, - "registeredHeight": 100, - "PoSeRevivedHeight": -1, - "PoSeBanHeight": 200, - "revocationReason": 1, - "ownerAddress": "yPBWCdMRY5PsS3hJzs7csbdWQVRR85yxUz", - "votingAddress": "ySM11LUD65Bi4p1gm68XLkdWc65TBKRzvQ", - "payoutAddress": "yX4Ve7Q8Y4jscV4LZJD8HVCHKyePzR3MhA", - "pubKeyOperator": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "platformNodeID": "0000000000000000000000000000000000000000", - "platformP2PPort": 26656, - "platformHTTPPort": 443 - }}"# - )) - .expect("Core's JSON for a revoked evonode") - } - - /// Whether an evonode is a validator decides quorum rotation, so every - /// node must build the same member for it. For an evonode whose listed - /// `addresses` hold no platform entry, that is the member the legacy - /// port fields alone give: banned, on the unspecified IP, with its - /// registered ports. - #[test] - fn should_build_a_revoked_legacy_evonode_from_its_legacy_ports() { - for addresses in [r#"{}"#, r#"{"core_p2p": []}"#] { - let validator = validator_for(revoked_legacy_evonode_state(addresses)); - - assert_eq!( - validator, - Some(ValidatorV0 { - pro_tx_hash: ProTxHash::from_byte_array(PRO_TX_HASH), - public_key: None, - node_ip: "::".to_string(), - node_id: PubkeyHash::from_byte_array([0u8; 20]), - core_port: 0, - platform_http_port: 443, - platform_p2p_port: 26656, - is_banned: true, - }), - "addresses: {addresses}" - ); - } - } -} diff --git a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs index 77dfff64371..4d639f64c1a 100644 --- a/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs +++ b/packages/rs-drive-abci/src/platform_types/validator_set/v0/mod.rs @@ -297,7 +297,9 @@ impl ValidatorSetMethodsV0 for ValidatorSetV0 { /// /// Formatting the host and port as a socket address writes an IPv6 host in /// brackets, as a URL authority requires. `node_ip` always holds a formatted -/// IP address; anything else is written as it is. +/// IP address; anything else is written as it is. This only formats Tenderdash's +/// dialing text; persisted validator membership, keys and the validator-set hash +/// do not depend on it, so every protocol version can use the same formatting. fn validator_node_address(node_id: &PubkeyHash, node_ip: &str, platform_p2p_port: u16) -> String { let host_port = match node_ip.parse::() { Ok(ip) => SocketAddr::new(ip, platform_p2p_port).to_string(), diff --git a/packages/rs-drive-abci/tests/strategy_tests/test_cases/upgrade_fork_tests.rs b/packages/rs-drive-abci/tests/strategy_tests/test_cases/upgrade_fork_tests.rs index 3a91a25015f..32c52ba8b7f 100644 --- a/packages/rs-drive-abci/tests/strategy_tests/test_cases/upgrade_fork_tests.rs +++ b/packages/rs-drive-abci/tests/strategy_tests/test_cases/upgrade_fork_tests.rs @@ -3,16 +3,20 @@ mod tests { use crate::addresses_with_balance::AddressesWithBalance; use crate::execution::{continue_chain_for_strategy, run_chain_for_strategy, GENESIS_TIME_MS}; use crate::strategy::{ - ChainExecutionOutcome, ChainExecutionParameters, NetworkStrategy, StrategyRandomness, - UpgradingInfo, + ChainExecutionOutcome, ChainExecutionParameters, CoreHeightIncrease, NetworkStrategy, + StrategyRandomness, UpgradingInfo, }; use dash_platform_macros::stack_size; use dpp::block::epoch::Epoch; use dpp::block::extended_block_info::v0::ExtendedBlockInfoV0Getters; use dpp::block::extended_epoch_info::v0::ExtendedEpochInfoV0Getters; + use dpp::core_types::validator_set::v0::ValidatorSetV0Getters; use dpp::dashcore::hashes::Hash; use dpp::dashcore::Network::Regtest; - use dpp::dashcore::{BlockHash, ChainLock}; + use dpp::dashcore::{BlockHash, ChainLock, ProTxHash}; + use dpp::dashcore_rpc::dashcore_rpc_json::{ + DMNStateDiff, MasternodeAddresses, MasternodeListDiff, + }; use dpp::data_contract::accessors::v0::DataContractV0Getters; use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters; use dpp::data_contracts::SystemDataContract; @@ -26,6 +30,7 @@ mod tests { ValidatorSetConfig, }; use drive_abci::logging::LogLevel; + use drive_abci::platform_types::masternode::v0::MasternodeV0; use drive_abci::platform_types::platform::Platform; use drive_abci::platform_types::platform_state::PlatformStateV0Methods; use drive_abci::rpc::core::MockCoreRPCLike; @@ -34,6 +39,8 @@ mod tests { use platform_version::version::mocks::v3_test::TEST_PROTOCOL_VERSION_3; use platform_version::version::INITIAL_PROTOCOL_VERSION; use std::collections::BTreeMap; + use std::sync::atomic::{AtomicU32, Ordering}; + use std::sync::{Arc, Mutex}; use strategy_tests::{IdentityInsertInfo, StartAddresses, StartIdentities, Strategy}; #[stack_size(4 * 1024 * 1024)] @@ -1490,6 +1497,296 @@ mod tests { ); } + /// Nested addresses in an old node's memory are absent from its persisted masternode + /// entries. Activation and subsequent partial address diffs must therefore start from + /// the same stored ports whether the node kept running or restarted before activation. + #[stack_size(4 * 1024 * 1024)] + #[test] + async fn should_preserve_masternode_ports_across_activation_and_restart() { + const BLOCKS_PER_EPOCH: u64 = 60; + const NEW_P2P_PORT: u16 = 29999; + let previous_version = PlatformVersion::get(13).expect("protocol version 13"); + let latest_version = PlatformVersion::latest(); + let strategy = NetworkStrategy { + total_hpmns: 50, + validator_quorum_count: 24, + chain_lock_quorum_count: 24, + upgrading_info: Some(UpgradingInfo { + current_protocol_version: previous_version.protocol_version, + proposed_protocol_versions_with_weight: vec![(latest_version.protocol_version, 1)], + upgrade_three_quarters_life: 0.0, + }), + // Allocate mock Core block hashes through height 2; the explicit chain-lock + // expectation below controls when that second height becomes visible. + core_height_increase: CoreHeightIncrease::KnownCoreHeightIncreases(vec![1, 2]), + ..Default::default() + }; + let config = PlatformConfig { + validator_set: ValidatorSetConfig { + quorum_size: 30, + ..Default::default() + }, + chain_lock: ChainLockConfig::default_100_67(), + instant_lock: InstantLockConfig::default_100_67(), + execution: ExecutionConfig { + verify_sum_trees: true, + epoch_time_length_s: BLOCKS_PER_EPOCH, + ..Default::default() + }, + block_spacing_ms: 1_000, + testing_configs: PlatformTestConfig { + store_platform_state: true, + ..PlatformTestConfig::default_minimal_verifications() + }, + ..Default::default() + }; + let mut observations = Vec::new(); + for restart_before_activation in [false, true] { + let mut platform = TestPlatformBuilder::new() + .with_config(config.clone()) + .with_initial_protocol_version(previous_version.protocol_version) + .build_with_mock_rpc(); + let core_height = Arc::new(AtomicU32::new(1)); + let reported_core_height = Arc::clone(&core_height); + platform + .core_rpc + .expect_get_best_chain_lock() + .returning(move || { + Ok(ChainLock { + block_height: reported_core_height.load(Ordering::SeqCst), + block_hash: BlockHash::from_byte_array([1; 32]), + signature: [2; 96].into(), + }) + }); + let updated_masternode = Arc::new(Mutex::new(None::)); + let rpc_updated_masternode = Arc::clone(&updated_masternode); + platform + .core_rpc + .expect_get_protx_diff_with_masternodes() + .withf(|base_height, height| *base_height == Some(1) && *height == 2) + .returning(move |_, _| { + let pro_tx_hash = rpc_updated_masternode + .lock() + .expect("masternode fixture lock") + .expect("masternode fixture selected before Core advances"); + let state_diff: DMNStateDiff = serde_json::from_value(serde_json::json!({ + "addresses": { "platform_p2p": [format!("1.2.3.4:{NEW_P2P_PORT}")] } + })) + .expect("Core address diff"); + Ok(MasternodeListDiff { + base_height: 1, + block_height: 2, + added_mns: vec![], + removed_mns: vec![], + updated_mns: vec![(pro_tx_hash, state_diff)], + }) + }); + + let before_activation = run_chain_for_strategy( + &mut platform, + 2 * BLOCKS_PER_EPOCH, + strategy.clone(), + config.clone(), + 13, + &mut None, + &mut None, + ) + .await; + let committed_before_activation = + committed_upgrade_state(before_activation.abci_app.platform); + assert_eq!(committed_before_activation.epoch_index, 1); + assert_eq!(committed_before_activation.current_protocol_version, 13); + assert_eq!( + committed_before_activation.next_epoch_protocol_version, + latest_version.protocol_version + ); + let activation_parameters = continuation_parameters(&before_activation, 1); + let quorum_hash = before_activation.current_validator_quorum_hash; + let current_platform = before_activation.abci_app.platform; + let mut state = current_platform.state.load().as_ref().clone(); + let pro_tx_hash = *state.validator_sets()[&quorum_hash] + .members() + .keys() + .next() + .expect("populated validator set"); + *updated_masternode.lock().expect("masternode fixture lock") = Some(pro_tx_hash); + #[allow(deprecated)] + let original_ports = { + let masternode = &state.full_masternode_list()[&pro_tx_hash]; + ( + masternode + .state + .legacy_platform_p2p_port + .expect("legacy P2P port") as u16, + masternode + .state + .legacy_platform_http_port + .expect("legacy HTTPS port") as u16, + ) + }; + let transient_addresses = Some(MasternodeAddresses { + core_p2p: vec!["1.2.3.4:9999".to_string()], + platform_p2p: vec!["1.2.3.4:36656".to_string()], + platform_https: vec!["1.2.3.4:8443".to_string()], + }); + assert_ne!(original_ports, (36656, 8443)); + // Populate the raw old-protocol state deliberately: its RPC representation + // retains nested addresses, while the historical stored representation does not. + state + .full_masternode_list_mut() + .get_mut(&pro_tx_hash) + .expect("masternode") + .state + .addresses = transient_addresses.clone(); + state + .hpmn_masternode_list_mut() + .get_mut(&pro_tx_hash) + .expect("evonode") + .state + .addresses = transient_addresses; + let root_before_fixture = current_platform + .drive + .grove + .root_hash(None, &previous_version.drive.grove_version) + .unwrap() + .expect("stored root"); + current_platform + .store_platform_state(&state, None, previous_version) + .expect("persist old-protocol masternode fixture"); + assert_eq!( + current_platform + .drive + .grove + .root_hash(None, &previous_version.drive.grove_version) + .unwrap() + .expect("stored root after fixture"), + root_before_fixture, + "transient old-protocol addresses must not change persisted state" + ); + current_platform.state.store(Arc::new(state)); + drop(before_activation); + + if restart_before_activation { + let TempPlatform { + platform: mut old_platform, + tempdir, + } = platform; + let core_rpc = std::mem::take(&mut old_platform.core_rpc); + drop(old_platform); + platform = TempPlatform::open_with_tempdir(tempdir, config.clone()); + platform.platform.core_rpc = core_rpc; + assert!(platform.state.load().full_masternode_list()[&pro_tx_hash] + .state + .addresses + .is_none()); + } + assert_eq!( + committed_upgrade_state(&platform), + committed_before_activation + ); + + let activation = continue_chain_for_strategy( + FullAbciApplication::new(&platform), + activation_parameters, + strategy.clone(), + config.clone(), + StrategyRandomness::SeedEntropy(18), + ) + .await; + let activated = committed_upgrade_state(&platform); + assert_eq!(activated.epoch_index, 2); + assert_eq!( + activated.current_protocol_version, + latest_version.protocol_version + ); + assert_eq!( + activated.next_epoch_protocol_version, + latest_version.protocol_version + ); + assert_eq!( + platform.state.load().last_committed_core_height(), + 1, + "activation must also work without a Core-height advance" + ); + let ports = |platform: &Platform| { + let state = platform.state.load(); + let validator = &state.validator_sets()[&quorum_hash].members()[&pro_tx_hash]; + (validator.platform_p2p_port, validator.platform_http_port) + }; + assert_eq!(ports(&platform), original_ports); + let after_activation_parameters = continuation_parameters(&activation, 1); + drop(activation); + core_height.store(2, Ordering::SeqCst); + let after_update = continue_chain_for_strategy( + FullAbciApplication::new(&platform), + after_activation_parameters, + strategy.clone(), + config.clone(), + StrategyRandomness::SeedEntropy(19), + ) + .await; + assert_eq!(platform.state.load().last_committed_core_height(), 2); + assert_eq!( + ports(&platform), + (NEW_P2P_PORT, original_ports.1), + "a partial new-protocol diff must not restore transient pre-upgrade HTTPS ports" + ); + let state = platform.state.load(); + #[allow(deprecated)] + let stored_ports = { + let masternode = &state.full_masternode_list()[&pro_tx_hash]; + ( + masternode.state.legacy_platform_p2p_port, + masternode.state.legacy_platform_http_port, + ) + }; + assert_eq!( + stored_ports, + ( + Some(u32::from(NEW_P2P_PORT)), + Some(u32::from(original_ports.1)) + ) + ); + observations.push(( + committed_before_activation, + activated, + committed_upgrade_state(&platform), + state + .full_masternode_list() + .values() + .cloned() + .map(MasternodeV0::from) + .collect::>(), + state.validator_sets().clone(), + )); + drop(state); + drop(after_update); + let TempPlatform { + platform: old_platform, + tempdir, + } = platform; + drop(old_platform); + let reopened = TempPlatform::open_with_tempdir(tempdir, config.clone()); + let observed = observations.last().expect("recorded committed state"); + assert_eq!(committed_upgrade_state(&reopened), observed.2); + assert_eq!(ports(&reopened), (NEW_P2P_PORT, original_ports.1)); + let state = reopened.state.load(); + assert_eq!( + state + .full_masternode_list() + .values() + .cloned() + .map(MasternodeV0::from) + .collect::>(), + observed.3, + "post-activation restart must preserve all stored masternodes" + ); + assert_eq!(state.validator_sets(), &observed.4); + } + assert_eq!(observations[0], observations[1], + "live and restarted nodes must commit equal roots, stored masternodes and validator sets"); + } + #[stack_size(4 * 1024 * 1024)] #[test] #[ignore] // Long-running: runs in nightly CI only diff --git a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs index 0b264acdbbf..7c3d8e2571c 100644 --- a/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs +++ b/packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_method_versions/v10.rs @@ -41,7 +41,7 @@ pub const DRIVE_ABCI_METHOD_VERSIONS_V10: DriveAbciMethodVersions = DriveAbciMet }, core_based_updates: DriveAbciCoreBasedUpdatesMethodVersions { update_core_info: 0, - update_masternode_list: 0, + update_masternode_list: 1, // changed in v14: resolves nested platform ports before storing masternodes update_quorum_info: 0, masternode_updates: DriveAbciMasternodeIdentitiesUpdatesMethodVersions { get_voter_identity_key: 0, diff --git a/packages/rs-platform-version/src/version/v14.rs b/packages/rs-platform-version/src/version/v14.rs index 7501f835eac..9d7dedd7824 100644 --- a/packages/rs-platform-version/src/version/v14.rs +++ b/packages/rs-platform-version/src/version/v14.rs @@ -1973,6 +1973,16 @@ pub const PROTOCOL_VERSION_14: ProtocolVersion = 14; /// version must be active on a network before its Dash Core activates V24, /// since earlier versions keep failing on these masternodes. /// +/// 74. **Versioned Core masternode address resolution**: `update_masternode_list` 1 +/// resolves nested platform addresses first, then falls back to legacy ports, +/// before storing the masternode state. Earlier protocol versions keep their +/// flat-field interpretation. The stored layout and validator construction +/// remain unchanged: new validators read the resolved stored ports, and an +/// existing validator is refreshed on a ban, service or P2P-port change. +/// Each diff starts from the old persisted representation so transient address +/// data retained before activation cannot make a running node disagree with +/// a restarted one. Payout lists remain outside the persisted representation. +/// /// The app-connect system contract (`SystemDataContract::AppConnect`, schema v1) /// carries only the wallet's `loginKeyResponse`: a flat indexOnly entry keyed by /// the app's ephemeral key hash and the responding identity, with the wallet's From b95ed194b637f17072d25650047bd588a1eac64d Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Sun, 4 Oct 2026 21:46:40 +0700 Subject: [PATCH 15/17] docs(drive-abci): describe masternode updater parameters Documentation only; no behavior change. Regression tests omitted because only argument documentation changed. Verified cargo fmt and git diff --check. --- .../core_based_updates/update_masternode_list/v1/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs index 837b16ddcff..c25f87a1cc6 100644 --- a/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs +++ b/packages/rs-drive-abci/src/execution/platform_events/core_based_updates/update_masternode_list/v1/mod.rs @@ -22,9 +22,13 @@ where /// /// # Arguments /// - /// * `state` - A mutable reference to the platform state to be updated. + /// * `platform_state` - The optional committed platform state used for identity updates. + /// * `block_platform_state` - The mutable platform state for the block being executed. /// * `core_block_height` - The current block height in the Dash Core. + /// * `is_init_chain` - Whether the update is initializing the chain. + /// * `block_info` - The block information used for identity updates. /// * `transaction` - The current groveDB transaction. + /// * `platform_version` - The protocol version governing the update. /// /// # Returns /// From c30ea08580c427095122d60c11f0e9bd9c853b47 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 5 Oct 2026 23:23:50 +0700 Subject: [PATCH 16/17] test(swift-sdk): refresh identity fixture with verified V1 proof MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Record a current testnet identity using the SDK recorder and trusted quorum provider, then replay it through the unchanged offline mock. Preserve the positive Swift ID and public-key assertions and all production proof verification. Red: the existing fixture fails with UnsupportedGroveDBProofVersion(0) locally and in Swift CI after #5294. Green: the fresh protocol-13 recording at height 612272 verifies live and offline, returning the expected identity and six public keys. Test would have caught this in CI: ✖ before fixture repair, ✔ after in SDK replay; full Swift verification runs in CI. --- ...9bae70cfdf255d4db7c291e9db395a8aa528ba.json | Bin 0 -> 154852 bytes ...9a538e83bc4d610211c3c45fc176b7d3f06b4d.json | 1 + .../SwiftDashSDKTests/SDKMethodTests.swift | 8 ++++---- 3 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 packages/rs-sdk/tests/vectors/test_identity_read_v1/msg_IdentityRequest_df74c9ad751824ab66cbc44c0b9bae70cfdf255d4db7c291e9db395a8aa528ba.json create mode 100644 packages/rs-sdk/tests/vectors/test_identity_read_v1/quorum_pubkey-6-0000003238798a94d4ae5f44409a538e83bc4d610211c3c45fc176b7d3f06b4d.json diff --git a/packages/rs-sdk/tests/vectors/test_identity_read_v1/msg_IdentityRequest_df74c9ad751824ab66cbc44c0b9bae70cfdf255d4db7c291e9db395a8aa528ba.json b/packages/rs-sdk/tests/vectors/test_identity_read_v1/msg_IdentityRequest_df74c9ad751824ab66cbc44c0b9bae70cfdf255d4db7c291e9db395a8aa528ba.json new file mode 100644 index 0000000000000000000000000000000000000000..f99368fb16d61c50988936126843e9b0217210b0 GIT binary patch literal 154852 zcmeI2&yHsIab#z$rx3WVMOXD7s{H~3ulxXpcjiJEz#eEc1_pxOea^YRE3!%>iXEp$ z9}K}jvB~Q0tjvszh|GIG{^3`@{`GhN{fFQGTT@Bhzt|M2TS@LT-mfBf*f z|MlB{|F<81!{7Sw>N~#w?Z5K=FPXCKD_?+<-_yqZ@<3z zyPv+i{GSLFo`NQ{bzWp=w-Xwha{05QduWyQbTlwd=P5$OBv@dVEeE$CP zj|2RdpQ`!pfBx>@|KkUS{rlhj?;q~J@W<=l{`$vX{qa}-?N@*J?w|h0cmMFu&o3XJ zo`C>-YTUhwER|&3E7YbAI}16fP7N{y{8k z>C@Mz&mXR@&;H79elqS)h@PMJ|Ni>v=_|!Pi^oX=CHx(dT&0UM<(nED z`FqzN+LRUDfAR|jTG~fz^o#1gfBO9Vt<@b8Fx)?-{`6Dh{#<_6%M;@ffp6>gNB-PX zx_T3X&p-WS(c_0-wH{#4xA$*?GkN*-D(0K>+66UUV8h|NBfkY(%lHe-zW?GMGf*nIB7c@*&v@T2fFm4c|5F}`CC^^b`!C7* zFY);K`8B?R%HR5Bf&axHrObXD)$JdW{T|a_N%YmkKa0q1Z?AF!;x9n->Mx&g*uI9y z)oWmDU?>mOgbG6kcJjYTE1eTpA1_Dm& zky*g8u~eD)tkbT6lzO*0W;76F_;n1t*TEQdij+KKpvZjVQhUv9ta)deUoV)1=-0O6R#0I8u%mn4aw7}1!_mJ+oxK=O~5&R5_ zKnuYlu5>Yw*idI8*bE~W?z{>M$YCW?tsRvaH4rXI7JVJ+1~k5u4X#r7N_0ApVCx71 zqpT2b+}%TCWPItc(fs#7m+5XGhNL^n2Ahqa)xWPBGct?)W)RGDnwl}l;bL_Rgfoq7 zyN;yU9wjD3?Rcs_V%yxALdb|Owec~6Bqh>CNRiTBs(3=Spa`ZA@N9Z#3OmiBX7{FU z4EICGz{e6ycI|t?I?wB~@wq^1VMjZ=%t)zwpccV0YCRIwG@Bo_LW2xqKWm*rP|)tQ zARoY|wo)EY1OVaWwO;4j4@+<60QftSM|5kE68O5u*1%ghfg~50We^B_XL4^a3CAfU zTaeQG(B0s>1qovwIt`{Nv}0qXeQ!`agYI_4NiZ&l zWX@`Q7o8&jlk^~CwM|6yxR+`=PR(%sHL7Hn&Tqy-Ydck(cm+Q=m{a5G+qIwyKr&&X zh!UlJHV?-@%f%q}?rs(A8J&BDCG2He+db0|2(x!*RC+cv@Iq$o(R5*zkPBk($lP)W z8L{X+ov`rQe76?^lf7$7;Xu0ZjB5y`@5ltr3P>AV10+wJ^}0*%@VPX294V~^U3v#c za;Dw4nuCd$_|C3VVVpv{02z9dd8L^>=EWCu1kLuLdkO4XICGEPaVG6`%CSSX@wI?a zAkz-`OSwf9^tj(wmdZGG&YT8gM%Ld>HcQ3rlc0$8Yn-ENI?;yi0y09%*Deh)xhOX` zNUmn}AhXk3rls+TXQHS>Zw+#S?t?fq3*2_@EbG10Fohxn@DD;r?G)kcR4gD=^KrPl zGapI>*j3r?uBPKFA!$^Y^=<_R$p9wfZchpZwilcEAw9q2s!NC2X+Gm}6dATauw}@( zvqiy@ad)A^>KKURcjX?=tawVZ2dnQA#aib*g?)nhQX3y5*pIhUXN0uvJMhFpgCg4M zZ+d46JI!1uVdB@~Yu>Wb66DvAn<0UdV7MQLeF5#cY;BLat#S{H+(;vHTctqFr*XK6 z`nsly*65BNcaffZiCi~P31eV>{B?)969}SM&0xZ-?R2aGq(p|F@ zkl7(MWKU=fmiITAgnK#SbBtX zGd3{O|LJF63?g)gaCWvRghoyR`ATX9&v3utU@={i!##*_Indos7{Mf0o8UFXSwy=d z$;^BS?3bJA@{zL-&U8>^U+4zrBeT_sQSThH&PPQlpK(i4-WsAgheY8bMPlis36CB( zoe~dc0MXZ;NV7sI0eMCY2}8#Es`-Gq!XgzfQHMY|xj#ynE_t1SxWR5C4v|vBEJ(LE zC|eSw*2&jyTzYb%sCcI37@p~DE#)eMgCxQF5=qXi%bC{(-44-A_xSdGcMoFcw)evxWxC`=29j>Sx=Cm3 zPEq+87SM6Oz0Ol)s8dW*gGMpLoij#NWQxN8DN@=qo(9Ede6QO%gUJL7y*SeXv8ECT zouK++L(A80eJ52=iow-C)X~7n<{(YZMI?P=~b2F3{)Y6)>3z7+%VD^QGi4#m&-NUm5XfxCFdEV{i+s`u++sVWZ;NOd@#=((rM8Sj?W1=PJQMAKf|DWL7n$jHlfRj^d*Fd3 zx-NL;(TC?#kfFgn<4y07F`chMn%;HW8t`({zZ;OW-o6=86avHgnGZ3sW+7PvU?HQ9B^c>wJx$??@6x zkn!C4d}rKyRNA`=`UW}V43-X_y9+smJ=Ym}ah0SyXP0k3TqTSz4P97R$AM#5#6AV^ zZ@K0T0w*t8u7O>2M+Je;0Y*Y#$W9WwyO1fca1n)4QNhlxQ)g(lP%?A18`=QaYuI4heeUC`vWG3sTJHraPJ{87~66*1$YW5E=c{9tC+~f_87vk z5eykT!|Df?5LL-6nEM^2y4%Ccxl4ne@Az9JySoK4g5W}Rc9E9ME;v|N0WDEF(Vf~( zk1{NX5k{Pz{%i_`2INT>+#or*MI!2b1sK@gA7(JAGk}TtQ0m^`SvpOmMkSh+s7t3* zJFosD@O?SzKYP?om|f;N!w#nDXfUKBimzRO7D>{5^|-M1bges!9aZPhS@N5&|zdOyL`d!OT^aFVSfkir8?;J4$4Q) zogvmqU}JJc#<_>l(;dPI+G+O#vr9#aXA@C+6X*ot%+53e(*-Ea(jd&ZGfh0hZcQU_ zMTQEKDP$| zFtMGM2G%3Ti3ggc#|kK2#z$1cB)#1d%yeoR)y^9N_Xg2j0Ml6~az+zl1WgDCc3;Y> zAUIe6AD!-tKxz>f94zfe(>31%5J(d0tcQBjPGJbG0n5%wskH-gPdwN{I2cZA?FOd1 zAUD(#}zX!}PKGVU^eeMSC($kB= z62aI*6o$Dg=m5g6kzmat68>|A?WF9Y*m-s$tw`9jA=#Y_rsMb#q=eW~N6N z!E7s!s1z9e1Xz*~qN`k+t2HQL_LV2GuQfC)mnhQh+%_E)P5PDI`#2;vfl0q*7NJ6R zP>8|?!)1F0w~I&&sWWj*p0tcRPegLhRqzLmaBno5#fSnFM{7Gtob~ad^&J}E2Tw@P zBZrW)akZ&&sO&I$-luz8Gk;*9p4=>mz{HcAU_qxBrHjCkYD)()Ve|))c@aPRI;w|gM#yKQiZy{~@iWawe;cY{g1mKsbxo6N|uKoX55=}!E^MHG&c z^l<~V>3bu9r>SP_-d)MQjM{Z7`F9StB_b@O3$sHn$+)aDbaz>L^91TF8L4wufq0(1 z9QcX<1e(!ZT9LOujCG1hC^EhLG*^&Mu<`c@d<4*rSo&=(Vtu=IPR7+R4?Ti2C;=>G zHwSeZm`qe#2l<0V1isfPEZk8$EJbd6c@4vrR8n!*i&Sj1A9IBZuuP}Ef>R*-rj~dF zCIDMTS9~(zP^8>I{$VAdkr2h~b_rYIHHDS@01_sC014O29k|cV?kIH+q{tGLRA7;9 z?Ka=W>(bDTg4KJe@11))cMhZgw}4u-xXnCI!G*aw-?g|N+b3#we|-49e{{rq9tG!SRnI0 zaR*ng5p}m`)RcCa8^zoc$E-6k%?UOSWUcHJJ+qicPoj1&rNA%(`(gxPcJ;n;r#DOz zId2gqv5U;ku>ht;QsEBEExo-vMTdfR=~*gv9bmaKwjK{lXZ4*P8#reo;|8?O9T}q5 z*76kYzAP!)kuv?%~fyO*eT542P`w)c4W8G2MlN%E_qoe_k&+f;I0as_gl3qW?ZI0C>7 za(IVk166D=d5eLRx_h8tUP)KZ7?slfCy!DK;qYj-*={*LJ80__!)$)StoJ}g*EZJ` z802$&tORl+g$9vo7zCIkjvbA!pg|HNO7qO?qz5<>H zlfo8UeLsn%3f+Z$cQm^jyA2|TL~QE}<2y{trS4a*s5Yb6i!>J%?ywk`K3sMR`*CZ! z-2-D^X@AK~klb7IpxXUGK*+c|=RqJ8-ub0>yJDr+9{_Z12GU_mF$y%}

5))UU7oPkc?X6KPRLGv8{}&zzul!p zK)MsVB4eG&W6hYJLnTar6rF~PD4nJZPSYE;aL;bvR$kqqHC{XHW^SU8>+pF`C%ZVg zcQEB-Iuz|Lu%`5NjD7VJ4R*93 zLz2L4Y8UAZeyFp)GBsmR!F!f?@Pz#Og*ux#Pb(h|ae^`<2MG8|33c??n9X0$9Lv~C$nC$?d&U7obja>N8;L_O^#vS&f z^V+RI90=UC=AF<;#Yq!XlO$tndtgBWZSELaUV0WFS34;!GGRiLU;D((xJOxJvJnHZ z<^7H_upQgfx!VG?3>#m8?~B{k>e4x|v|BwqO5FV1$LH+!DcBuS3U&mZ?O&n)Nua?}t&i_Xn1v=mHCu}xITngU>0t!q3I|47 zQl+VDWrsnNs~Ifd2N=aLj0}cAt=$=JOWDoAbi2TfYU_Zfs%DrVv)3ssJhShxgxQ&k zA|JEF(>k`iomyw5w!RQf%y$rPG>;k0{^2hZYr*V+mhWlIu~4`r>vcGI#BLc09oH(gV53_SM@ zYsfW-)j1_h9fX(FZZE1YMD6=aFvD{zfb2X8XG0KaAVvdW#jO<9j^vB3p*+zJ8L{pV zg&*z|62l}YN1UH=+LlRRYEY!xwQnHu-{tHhKseGUa$niUNM~lIH6c~NWWOYDjwLS@ z%mj<2?i(!+{R9?pz%z(FvE{~Or?i1w^McZt5MkU^V1=BKftF{X?-}*mkdCP(W}0s231F6VZACJ;ed0PZv&;S*4s7oK zl+HT$*%_5A+KDrW9a1x5w)>Jj84EZ;K5dB{Ova(xSCA@E*Vk%dMj`WEL^{~Mef0B2 z6383|*KS&jC;);^)02fr?ix1)Kz1;lI2m_y2$+QOG=ye)J3TCK2e9yPS4zn8Um?N$ z){p`+l-?~}A*Abf?kGrr+OUy_u-Zf%lcC7n5KgXObRpz19R<3&9rk@x5;56*4TVI1 zPazK*O#$uL!v5%|*QKQ|8tn>7?-ulj_9}zGQ|kCQa3ixnA89Was_QyXlEFE7L~2EH zGbh5d?t~?OZ~&fdkEc)ZY>+3B-HJ8v9a6bn0|Q~~4h`qN&R7Gl=!F9izA^7u0>sd1Oo&~G1+H#!xE-&O5Fp^NK`7- z*x(H0x~9Jxm-pdbr>usedthPpE`-3Y(2Gv;fJb-~9T*ruBT*n;O7sZa_EX_8L#MpaNe+6gT0U|EoF;D`@zps zNL!AwWq%CGpHNMH8YfBny~4r|jL`gKHyz z%=;xTm;?svV0qKtBi1xcB(Ae^JdB&*F(T*E7Rp_+WrbeJ& zsBq_=fRipca8R)Zm5OY{IBpX%qO3A&z)X+5c83FpiC>AG;|W z6<6TOPyK2ifwN1Pn2S@SnPhw2RAC9{fl6;X6sqijg2^BPGtVPcaOq5Grm$1m?l7f2 zP!XjUDFnLrcSV?Pze4W@_X!$^00S}!7Spx2%He!vc6`<2JO+|6cr{1t9wnIRBs+gz&k_^zn zVVgUW2BwdRJ?C~L&9$Eda`Lj5^$rve4zqosm@e6%v}~{V4yAW+ER_Rh-?o-oFSH=N zKEd@}cWO#B0-qtt?n3v-8ZGHA)Jb>TNX?Pb6UyCV; z?h9Qn3b~FmBujA$En`|JrURCJK^sWMror??Oh&)TQE1BYc-9MO9GSOqvu$^R>0^!0 zbhlpxZub2V_~2R6;VNX@8#E*@&AmZY5Tcm)-kNI~h{TKZ)6%&x5x z8_e4|K$_&>`1TEZ_#6Y%<#}h^A4m^HXRn51;eNoVcM++D^sds@AXupL%1HPOOPXE5 zBqh3My#r!knz(_K+I(!+omAl~n8Pu^oty{Kp!BvE&LB#I_4aTt?65HL5M_7d3c6$H z?G-`3Ioozl+oZc6uEB_Ff9RwsAk z1O3)I+3CAc-{kz;KaHA*@^&vW}QTQY^B)$4k#ap87kxgfU+ouxnSV8QBQ! zsRTUtjBQbRQ|;P0yESxo!}7ohT57P(ARn4a8kf?0I+4WK6Ov~NtE-sq9i`6c+O<#A zt{nksFLS_T*Z9Fd1UiD0ktmYRBLI_~ndaKJs(tN)Gb%sP?{(T5=DZuG;9rX}AQM8| z1qsZ%&T)M2ox;vc>a@r9fRI^D(%o-iORO_#R%FsMmzO~3?#MQ5Sf;S+NT%(MBql-Z z3~tXQjCCV~L*e~n+aNn-_ZFk}&+<%{qGtTl zL#T>NtAwa9H5qc@KX;dn2!`ymw8)EKYn=vyOt8MTwusv8$O?B(8nQ0}0ZH#pgS4qI zM4bvlYiftD-AV3@jh0Dw6j>&Y1e#j&mb@TcMJK`9A^?`)euyrm1I$jdeUnhn6B=ubqeN61k-5=-1%%q{2^>f?!7(A zbor4my~J+m+?6|}okF|w!DI*13e($ir+^mrj0U!+gM-ZOoI&OmNxJa#+Kz|B=QF#w zN`m$6WCq60E-(rBC()fc{dHV<@?=M{C34U^`K>TL!Wf87_o2ThUBN$DkhBc!!V?hq ze$YB`GVZ17LX2;J0&-{V=^Q$NJEe&VRQK+>&NTSv>K9NZo#dBXk`U7^f2MP|cJJSqI-eNkwlIw=O5HN9Y z%~g_TiX`VsRYWLNa_;!&i5p19eOz}+HD zx7)RKhFnB7wp`t<#&Uxh>P((_s1mebAXC`IeuKF$6p`6Crb5`G>@JOuz=O;>ccb#7 z<&|{J*x#<~I^Mi#ou#f=a^AVJ^{(*UL=sgp$2{8G#M2{&UbHu8cY!CD zIh2XR6F9c>S?OIg3p>czoz+J1R_7u1ZfpA^o)xC?8HlR{wp=49f%$}k)yQF7m*N=) z$GC%u<<{C`FN52Qd?!^1pGUxxMIab_4&w%py+T8HgSQtcn0X4ovNvc(_?&u-T0lYc zxSP_0Pj|OTC~_ZxXzWWwcI`I+iCV7U41^Q_&jGLlB9oKiR{v2f>GL})e4xR4mo^yT z@J=neBje8h6Bwpoy8Xg5U0TO?gL}q)gVA6k3^EUOrn3Q9l*2yv=wO#Wm~_0{93Ktt zcv7b;a!l89+1JI-IbB*Jce>;f4t)pH?sBPQ+@J4M9~lEb&nDV$0DDZwn4AerJ9VZ8 zBP^p91ccSRYc}jJ$6bBQg_2GPGY0(AfpjY<({d-689_C<#8KbuW;NZ-w4I;L%`m{| zfoZefRu(*vI`{G2QVPW0Id-=e0^9!|1%~ZtIoo6h^A^!ATajU#>_ayrv)eCuS>0^# zLUk59_f>#}ogSj7@VZy$B60XO^c~5Ns4>LLw(*LNkTK(l^)-GX)!=({7Z%V}WI6xfSSXTR5Jo_*AfMgw@M3ZZGGWj)R)H5P(2wHU|ih&B~V3H?>6WNcJ-a8$gxYgp+Q}A2lJj?LZ%so#>*^re%^9u z<48JEePK@qlDnnzq)w2^8u!x&oyD7=8WVx!K zElQBtRbYeo!%oXvJ}o0wwnJ*EY4i?ahx9bK>dwA0n7eH%qIUT={4dX(F=1*6lh&F% zscU_1jSaF>%L?13+ZhsN zGW`$&$#h;uC$F^3NTTj2RVcly)uj`n^2v<~nti42oB|EDjOoZf8H*yQlZ?%!L3I~} zhu`in*7df=f2}9rAgPWxYVFpNEvM~F^zP# zx$ZK$OL)c$q}?rlk5SPrQTMJIZGl1WylQUg+Tv@Mo9V(XsOf}-i)H8kX>isWb+WT| zk=}JCWKL`-)xf%8*L3EhyB!$^vBP4b{lTQ=f?a`Nr^0|gMG9sc97yi4m<~s3fxGlf z$Iy2VJW*k785o?Cvvyxwy~yNa2h6C}ed71v_CV|0M?h3*EsuK_oq+I8thEM=t-&Po z_E=$qe1%H@Om?1F$5r`}6g?rWP=%OT5hbP%A6~oOvhYv7oSb3}g`m zGUE)7+#mEbkiEN;uCRsf_9tr-?X5`_hTa^kvL%x9SlPZ2Kqkj?r4RckGT>=CifkuDs?&_u z5N82GPQJn{(sPixg8E=}IFQJdr||fU>5+lCOS4^rhQOH}Rik#cP~`K6r;jhc{PlHG zH|ys4BmaEQKhON*_x$&VuTNh;^RG`&U;UHwpC4JHzp;R?{LODG<`i6n9JIfOGsA*p=ho2!(Cp>;QUL%LwK@={RiMN*!yz6#Y+BIlv%zf886$Zz?XU8DI z2v`uw&@46JnT)qREy~OQNbp<6ojB>>(gpIabo)slLnw70_lXLWKCWWkaf04F=x(=n zbERi?uU2>7FK=77;bdI&eyvk~yo>k4*|1;E-gW{4Ir5c!eG>|rTlQG>M zb&v@S`eYC&+KAn9aiQ;nlVH0=2xESBNsmfoT%Ge283HBbH;I7|d$+ZdF$V5ZGSPJ~|IOaQ`X<(oJpF+41Fv0q z+sg{QKH%K;3?GvE=`8>7^S*xNKfgbpp8AfL_q*rQ)BhhIq`Kpq?V_LHo=6V5?Auzc zUh^x$Yga{+Yf^&3OV3w=$r{^@?BE$~VjrJ%LYQ5&BGVuR&&1CXh2ieG#Du`M_K@u! zc)$!pH;|$47T+@_Ik;j2EQGYjfS|kdtYTk5JG)Mq*OBUKvqr(mbo?0`ki2I7H{0bu u|GzLj^U3OqYyO30{OXfc`v1jho?rgzhrmDk$A94u-u%}64gdO=KmK3P(UepG literal 0 HcmV?d00001 diff --git a/packages/rs-sdk/tests/vectors/test_identity_read_v1/quorum_pubkey-6-0000003238798a94d4ae5f44409a538e83bc4d610211c3c45fc176b7d3f06b4d.json b/packages/rs-sdk/tests/vectors/test_identity_read_v1/quorum_pubkey-6-0000003238798a94d4ae5f44409a538e83bc4d610211c3c45fc176b7d3f06b4d.json new file mode 100644 index 00000000000..b7c03b0d039 --- /dev/null +++ b/packages/rs-sdk/tests/vectors/test_identity_read_v1/quorum_pubkey-6-0000003238798a94d4ae5f44409a538e83bc4d610211c3c45fc176b7d3f06b4d.json @@ -0,0 +1 @@ +a16e64068375ca0a085305c2b9a409ed85733f9398dfd2569a5238de20a47dec9a9ade493a0c428beb056fbfbdc13e5a \ No newline at end of file diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SDKMethodTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SDKMethodTests.swift index 4c605fcbd64..4bb1d5a0c0d 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SDKMethodTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/SDKMethodTests.swift @@ -99,12 +99,12 @@ final class SDKMethodTests: XCTestCase { } /// Fetches an identity through `identityGet` from rs-sdk's recorded - /// `test_identity_read` vectors: the mock replays the recorded DAPI + /// `test_identity_read_v1` vectors: the mock replays the recorded DAPI /// response, the SDK verifies its proof against the recorded quorum key, /// and the identity is decoded into the Swift result. @MainActor func testSimpleIdentityFetch() async throws { - let vectors = Self.rsSdkVectors("test_identity_read") + let vectors = Self.rsSdkVectors("test_identity_read_v1") guard FileManager.default.fileExists(atPath: vectors) else { XCTFail("missing rs-sdk offline vectors at \(vectors)") return @@ -113,8 +113,8 @@ final class SDKMethodTests: XCTestCase { SDK.initialize() let sdk = try SDK(mockVectorsDirectory: vectors) - // The vectors record identity [1; 32], rs-sdk's IDENTITY_ID_1. - let identityId = Data(repeating: 1, count: 32).toBase58() + // The vectors contain a testnet identity with a GroveDB V1 proof. + let identityId = "5DbLwAxGBzUzo81VewMUwn4b5P4bpv9FNFybi25XB5Bk" let identity = try await sdk.identityGet(identityId: identityId) XCTAssertEqual(identity["id"] as? String, identityId) From c233c6b7ccab01e0003c52477550addc66763a58 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 5 Oct 2026 23:54:26 +0700 Subject: [PATCH 17/17] test(swift-sdk): snapshot historical fixture with its store still open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Populate a working store, copy its committed WAL data using the existing SQLite backup helper while retaining the ModelContainer, and validate all application rows before describing and attaching the standalone fixture. Preserve existing schema assertions; no production SQLite behavior changes. Test would have caught this in CI: ✖ source checkpoint repeatedly failed with database is locked, including the one retry; ✔ the actual Swift backup helper preserves committed rows and a standalone readable snapshot with the source owner retained. Full Swift CI validates the updated capture test. --- .../SwiftDashSDKTests/DashModelMigrationTests.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift index 1ac6248f4cc..4beee9638fa 100644 --- a/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift +++ b/packages/swift-sdk/SwiftTests/SwiftDashSDKTests/DashModelMigrationTests.swift @@ -643,10 +643,11 @@ final class DashModelMigrationTests: XCTestCase { try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: directory) } let url = directory.appendingPathComponent("historical-v2.store") + let workingURL = directory.appendingPathComponent("working.store") try autoreleasepool { let schema = Schema(versionedSchema: DashSchemaV2.self) let container = try ModelContainer(for: schema, configurations: [ - ModelConfiguration(schema: schema, url: url, cloudKitDatabase: .none) + ModelConfiguration(schema: schema, url: workingURL, cloudKitDatabase: .none) ]) let context = container.mainContext let wallet = DashSchemaV2.PersistentWallet( @@ -702,8 +703,12 @@ final class DashModelMigrationTests: XCTestCase { networkRaw: Network.testnet.rawValue, proTxHash: Data(repeating: 7, count: 32), label: "fixture", addedAt: 1, snapshotJSON: "{}")) try context.save() + // Snapshot committed data while SwiftData still owns the WAL store. + try withExtendedLifetime(container) { + try DashLegacyStoreSQLite.copy(from: workingURL, to: url) + try DashLegacyStoreSQLite.validatePreservation(from: workingURL, to: url) + } } - try DashLegacyStoreSQLite.checkpoint(url) let metadata = try DashSchemaFixtureSupport.describeStore(at: url, version: Schema.Version(2, 0, 0)) XCTAssertEqual(metadata.entity_hashes.count, 35) XCTAssertEqual(metadata.model_checksum, "RrRj/iNbS9izgLQvNb2APed4iwaR7pftEE2+4tea7K8=")