Skip to content

Commit

Permalink
Pass Gov var value as vector to Rust (#2397)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar authored Sep 6, 2023
1 parent a783b1e commit b400805
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/ain-rs-exports/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
) -> bool {
cross_boundary_success_return(result, true)
}
Expand Down
15 changes: 12 additions & 3 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ pub mod ffi {
pub data: Vec<u8>,
}

// ========== 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,
Expand Down Expand Up @@ -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<u8>,
) -> bool;
fn evm_try_create_and_sign_tx(
result: &mut CrossBoundaryResult,
Expand Down
41 changes: 16 additions & 25 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <amount.h> /// GetDecimaleString
#include <core_io.h> /// ValueFromAmount
#include <util/strencodings.h>

#include <ain_rs_exports.h>
#include <ffi/ffihelpers.h>

enum class EVMAttributesTypes : uint32_t {
Expand Down Expand Up @@ -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<uint32_t>(EVMAttributesTypes::Finalized);
} else if (attrV0->key == EVMKeys::GasLimit) {
attributeType = static_cast<uint32_t>(EVMAttributesTypes::GasLimit);
} else if (attrV0->key == EVMKeys::GasTarget) {
attributeType = static_cast<uint32_t>(EVMAttributesTypes::GasTarget);
} else {
return DeFiErrors::GovVarVariableUnsupportedEVMType(attrV0->key);
}
}

const auto number = std::get_if<uint64_t>(&attribute.second);
if (!number) {
return DeFiErrors::GovVarUnsupportedValue();
}
const auto govVarPtr = static_cast<const uint8_t*>(static_cast<const void*>(&attribute.second));
const auto govVarVec = std::vector<uint8_t>(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<uint8_t> 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();
}

Expand Down

0 comments on commit b400805

Please sign in to comment.