From b40080580419529e01dd1d4a8cafad69b74bb85c Mon Sep 17 00:00:00 2001 From: Peter John Bushnell Date: Wed, 6 Sep 2023 01:41:27 +0100 Subject: [PATCH] Pass Gov var value as vector to Rust (#2397) --- lib/ain-rs-exports/src/evm.rs | 6 +-- lib/ain-rs-exports/src/lib.rs | 15 ++++++-- src/masternodes/govvariables/attributes.cpp | 41 ++++++++------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/ain-rs-exports/src/evm.rs b/lib/ain-rs-exports/src/evm.rs index 45a46fcc5a..d50ee36c53 100644 --- a/lib/ain-rs-exports/src/evm.rs +++ b/lib/ain-rs-exports/src/evm.rs @@ -690,11 +690,11 @@ pub fn evm_try_disconnect_latest_block(result: &mut ffi::CrossBoundaryResult) { } } -pub fn evm_try_set_attribute( +pub fn evm_try_handle_attribute_apply( result: &mut ffi::CrossBoundaryResult, _queue_id: u64, - _attribute_type: u32, - _value: u64, + _attribute_type: ffi::GovVarKeyDataStructure, + _value: Vec, ) -> bool { cross_boundary_success_return(result, true) } diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index f60db29a3f..8e2a0e6194 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -41,6 +41,15 @@ pub mod ffi { pub data: Vec, } + // ========== Governance Variable ========== + #[derive(Default)] + pub struct GovVarKeyDataStructure { + pub category: u8, + pub category_id: u32, + pub key: u32, + pub key_id: u32, + } + // ========= Core ========== pub struct CrossBoundaryResult { pub ok: bool, @@ -164,11 +173,11 @@ pub mod ffi { mnview_ptr: usize, ) -> FinalizeBlockCompletion; fn evm_unsafe_try_commit_queue(result: &mut CrossBoundaryResult, queue_id: u64); - fn evm_try_set_attribute( + fn evm_try_handle_attribute_apply( result: &mut CrossBoundaryResult, queue_id: u64, - attribute_type: u32, - value: u64, + attribute_type: GovVarKeyDataStructure, + value: Vec, ) -> bool; fn evm_try_create_and_sign_tx( result: &mut CrossBoundaryResult, diff --git a/src/masternodes/govvariables/attributes.cpp b/src/masternodes/govvariables/attributes.cpp index 8b0c2e4c07..a17a3f4795 100644 --- a/src/masternodes/govvariables/attributes.cpp +++ b/src/masternodes/govvariables/attributes.cpp @@ -15,6 +15,8 @@ #include /// GetDecimaleString #include /// ValueFromAmount #include + +#include #include enum class EVMAttributesTypes : uint32_t { @@ -2308,36 +2310,25 @@ Res ATTRIBUTES::Apply(CCustomCSView &mnview, const uint32_t height) { SetValue(lockKey, true); } } - } else if (attrV0->type == AttributeTypes::EVMType && attrV0->typeId == EVMIDs::Block) { - uint32_t attributeType{}; - if (attrV0->key == EVMKeys::Finalized) { - attributeType = static_cast(EVMAttributesTypes::Finalized); - } else if (attrV0->key == EVMKeys::GasLimit) { - attributeType = static_cast(EVMAttributesTypes::GasLimit); - } else if (attrV0->key == EVMKeys::GasTarget) { - attributeType = static_cast(EVMAttributesTypes::GasTarget); - } else { - return DeFiErrors::GovVarVariableUnsupportedEVMType(attrV0->key); - } + } - const auto number = std::get_if(&attribute.second); - if (!number) { - return DeFiErrors::GovVarUnsupportedValue(); - } + const auto govVarPtr = static_cast(static_cast(&attribute.second)); + const auto govVarVec = std::vector(govVarPtr, govVarPtr + sizeof(attribute.second)); - // TODO: Cut this out. - CrossBoundaryResult result; - if (!evm_try_set_attribute(result, evmQueueId, attributeType, *number)) { - return DeFiErrors::SettingEVMAttributeFailure(); - } - if (!result.ok) { - return DeFiErrors::SettingEVMAttributeFailure(result.reason.c_str()); - } + rust::Vec govVarValue{}; + govVarValue.reserve(govVarVec.size()); + std::copy(govVarVec.begin(), govVarVec.end(), govVarValue.begin()); + + CrossBoundaryResult result; + const auto rustKey = GovVarKeyDataStructure{attrV0->type, attrV0->typeId, attrV0->key, attrV0->keyId}; + if (!evm_try_handle_attribute_apply(result, evmQueueId, rustKey, govVarValue)) { + return DeFiErrors::SettingEVMAttributeFailure(); + } + if (!result.ok) { + return DeFiErrors::SettingEVMAttributeFailure(result.reason.c_str()); } } - // TODO: evm_try_handle_attribute_apply here. - // Pass the whole apply chain. On the rust side, pick and choose what needs to be handled return Res::Ok(); }