Skip to content

Commit

Permalink
Get protocol version from ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
gRoussac committed Feb 4, 2025
1 parent 683766b commit e9653a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 0 additions & 2 deletions contracts/contract/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub const PROTOCOL_VERSION: u32 = 2u32;

pub const PREFIX_ACCESS_KEY_NAME: &str = "cep78_contract_package_access";
pub const PREFIX_CEP78: &str = "cep78";
pub const PREFIX_CONTRACT_NAME: &str = "cep78_contract_hash";
Expand Down
22 changes: 6 additions & 16 deletions contracts/contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use casper_contract::{
use casper_types::{
account::AccountHash,
contract_messages::MessageTopicOperation,
contracts::{ContractHash, ContractPackageHash, ContractVersionKey, ProtocolVersionMajor},
contracts::{ContractHash, ContractPackageHash},
runtime_args, ApiError, CLType, CLValue, EntityAddr, EntryPoint, EntryPointAccess,
EntryPointPayment, EntryPointType, EntryPoints, Key, KeyTag, NamedKeys, PackageHash, Parameter,
RuntimeArgs, Tagged,
Expand Down Expand Up @@ -59,8 +59,8 @@ use constants::{
NUMBER_OF_MINTED_TOKENS, OPERATOR, OPERATORS, OPERATOR_BURN_MODE, OWNED_TOKENS, OWNERSHIP_MODE,
PACKAGE_OPERATOR_MODE, PAGE_LIMIT, PAGE_TABLE, PREFIX_ACCESS_KEY_NAME, PREFIX_CEP78,
PREFIX_CONTRACT_NAME, PREFIX_CONTRACT_VERSION, PREFIX_HASH_KEY_NAME, PREFIX_PAGE_DICTIONARY,
PROTOCOL_VERSION, RECEIPT_NAME, REPORTING_MODE, RLO_MFLAG, TOKEN_COUNT, TOKEN_ISSUERS,
TOKEN_OWNERS, TOTAL_TOKEN_SUPPLY, TRANSFER_FILTER_CONTRACT, TRANSFER_FILTER_CONTRACT_METHOD,
RECEIPT_NAME, REPORTING_MODE, RLO_MFLAG, TOKEN_COUNT, TOKEN_ISSUERS, TOKEN_OWNERS,
TOTAL_TOKEN_SUPPLY, TRANSFER_FILTER_CONTRACT, TRANSFER_FILTER_CONTRACT_METHOD,
UNMATCHED_HASH_COUNT, WHITELIST_MODE,
};
use core::convert::TryInto;
Expand All @@ -79,7 +79,7 @@ use modalities::{
NFTKind, NFTMetadataKind, NamedKeyConventionMode, OwnerReverseLookupMode, OwnershipMode,
Requirement, TokenIdentifier, TransferFilterContractResult, WhitelistMode,
};
use utils::get_holder_mode;
use utils::{get_contract_version_key, get_holder_mode};

#[no_mangle]
pub extern "C" fn init() {
Expand Down Expand Up @@ -2612,14 +2612,9 @@ fn install_contract() {
Key::contract_entity_key(contract_hash.into()),
);

let contract_version_key = ContractVersionKey::new(
ProtocolVersionMajor::from(PROTOCOL_VERSION),
contract_version,
);

runtime::put_key(
&format!("{PREFIX_CONTRACT_VERSION}_{collection_name}"),
storage::new_uref(contract_version_key.to_string()).into(),
storage::new_uref(get_contract_version_key(contract_version).to_string()).into(),
);

let nft_contract_package_hash: PackageHash = runtime::get_key(&hash_key_name)
Expand Down Expand Up @@ -2710,19 +2705,14 @@ fn migrate_contract(access_key_name: String, package_key_name: String) {
message_topics,
);

let contract_version_key = ContractVersionKey::new(
ProtocolVersionMajor::from(PROTOCOL_VERSION),
contract_version,
);

// Store contract_hash and contract_version under the keys CONTRACT_NAME and CONTRACT_VERSION
runtime::put_key(
&format!("{PREFIX_CONTRACT_NAME}_{collection_name}"),
Key::contract_entity_key(contract_hash.into()),
);
runtime::put_key(
&format!("{PREFIX_CONTRACT_VERSION}_{collection_name}"),
storage::new_uref(contract_version_key.to_string()).into(),
storage::new_uref(get_contract_version_key(contract_version).to_string()).into(),
);

let events_mode = utils::get_optional_named_arg_with_user_errors::<u8>(
Expand Down
19 changes: 15 additions & 4 deletions contracts/contract/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ use core::{convert::TryInto, mem::MaybeUninit};

use casper_contract::{
contract_api::{
self,
runtime::{self},
self, alloc_bytes,
runtime::{self, PROTOCOL_VERSION_FIELD_IDX, PROTOCOL_VERSION_LENGTH},
storage,
},
ext_ffi,
ext_ffi::{self, casper_get_block_info},
unwrap_or_revert::UnwrapOrRevert,
};
use casper_types::{
account::AccountHash,
api_error,
bytesrepr::{self, FromBytes, ToBytes},
contracts::ContractHash,
contracts::{ContractHash, ContractVersionKey, ProtocolVersionMajor},
system::CallStackElement,
AddressableEntityHash, ApiError, CLTyped, Key, PackageHash, URef,
};
Expand Down Expand Up @@ -349,6 +349,17 @@ pub fn get_immediate_caller() -> (Key, Option<Key>) {
}
}

pub fn get_contract_version_key(contract_version: u32) -> ContractVersionKey {
let dest_ptr = alloc_bytes(PROTOCOL_VERSION_LENGTH as usize);
unsafe {
casper_get_block_info(PROTOCOL_VERSION_FIELD_IDX, dest_ptr.as_ptr());
let protocol_version_major = ProtocolVersionMajor::from(u32::from_ne_bytes(
core::ptr::read_unaligned(dest_ptr.as_ptr() as *const [u8; 4]),
));
ContractVersionKey::new(protocol_version_major, contract_version)
}
}

pub fn get_token_identifier_from_runtime_args(
identifier_mode: &NFTIdentifierMode,
) -> TokenIdentifier {
Expand Down

0 comments on commit e9653a4

Please sign in to comment.