mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-08 22:28:12 -05:00
sdk: replaced db_get_slot_checkpoint with get_slot_checkpoint and moved to util
This commit is contained in:
1
Makefile
1
Makefile
@@ -50,6 +50,7 @@ $(PROOFS_BIN): zkas $(PROOFS_SRC)
|
||||
contracts: zkas
|
||||
$(MAKE) -C src/contract/money
|
||||
$(MAKE) -C src/contract/dao
|
||||
$(MAKE) -C src/contract/consensus
|
||||
|
||||
token_lists:
|
||||
$(MAKE) -C contrib/token all
|
||||
|
||||
@@ -26,10 +26,10 @@ use darkfi_sdk::{
|
||||
pasta_prelude::*, pedersen_commitment_base, pedersen_commitment_u64, poseidon_hash,
|
||||
ContractId, PublicKey, CONSENSUS_CONTRACT_ID, DARK_TOKEN_ID,
|
||||
},
|
||||
db::db_get_slot_checkpoint,
|
||||
error::{ContractError, ContractResult},
|
||||
msg,
|
||||
pasta::pallas,
|
||||
util::get_slot_checkpoint,
|
||||
ContractCall,
|
||||
};
|
||||
use darkfi_serial::{deserialize, Encodable, WriteExt};
|
||||
@@ -66,7 +66,7 @@ pub(crate) fn consensus_proposal_reward_get_metadata_v1(
|
||||
|
||||
// Grab the slot checkpoint to validate consensus parameters against
|
||||
let slot = ¶ms.slot;
|
||||
let Some(slot_checkpoint) = db_get_slot_checkpoint(*slot)? else {
|
||||
let Some(slot_checkpoint) = get_slot_checkpoint(*slot)? else {
|
||||
msg!("[ConsensusProposalRewardV1] Error: Missing slot checkpoint {} from db", slot);
|
||||
return Err(ConsensusError::ProposalMissingSlotCheckpoint.into())
|
||||
};
|
||||
|
||||
@@ -213,7 +213,7 @@ async fn alice2alice_random_amounts_multiplecoins() -> Result<()> {
|
||||
let mut minted_amounts = vec![];
|
||||
let mut owncoins = vec![];
|
||||
for i in 0..10 {
|
||||
let amount = rand::thread_rng().gen_range(1..1000);
|
||||
let amount = rand::thread_rng().gen_range(2..1000);
|
||||
info!(target: "money", "[Faucet] ===================================================");
|
||||
info!(target: "money", "[Faucet] Building Money::Mint params for Alice's mint for token {} and amount {}", i, amount);
|
||||
info!(target: "money", "[Faucet] ===================================================");
|
||||
|
||||
@@ -444,32 +444,6 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) -> i6
|
||||
(objects.len() - 1) as i64
|
||||
}
|
||||
|
||||
/// Will return requested slot checkpoint from `SlotCheckpointStore`.
|
||||
pub(crate) fn db_get_slot_checkpoint(ctx: FunctionEnvMut<Env>, slot: u64) -> i64 {
|
||||
let env = ctx.data();
|
||||
|
||||
if env.contract_section != ContractSection::Deploy &&
|
||||
env.contract_section != ContractSection::Exec &&
|
||||
env.contract_section != ContractSection::Metadata
|
||||
{
|
||||
error!(target: "runtime::db::db_get_slot_checkpoint()", "db_get_slot_checkpoint called in unauthorized section");
|
||||
return CALLER_ACCESS_DENIED.into()
|
||||
}
|
||||
|
||||
let ret = match env.blockchain.lock().unwrap().slot_checkpoints.get(slot) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
error!(target: "runtime::db::db_get_slot_checkpoint()", "Internal error getting from slot checkpoints tree: {}", e);
|
||||
return DB_GET_FAILED.into()
|
||||
}
|
||||
};
|
||||
|
||||
// Copy Vec<u8> to the VM
|
||||
let mut objects = env.objects.borrow_mut();
|
||||
objects.push(ret.to_vec());
|
||||
(objects.len() - 1) as i64
|
||||
}
|
||||
|
||||
/// Everyone can call this. Will check if a given db contains given key.
|
||||
pub(crate) fn db_contains_key(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) -> i32 {
|
||||
let env = ctx.data();
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use darkfi_sdk::db::{CALLER_ACCESS_DENIED, DB_GET_FAILED};
|
||||
use log::error;
|
||||
use wasmer::{FunctionEnvMut, WasmPtr};
|
||||
|
||||
@@ -149,14 +150,43 @@ pub(crate) fn get_object_size(ctx: FunctionEnvMut<Env>, idx: u32) -> i64 {
|
||||
obj.len() as i64
|
||||
}
|
||||
|
||||
/// Will return current epoch number.
|
||||
pub(crate) fn get_current_epoch(ctx: FunctionEnvMut<Env>) -> u64 {
|
||||
ctx.data().time_keeper.current_epoch()
|
||||
}
|
||||
|
||||
/// Will return current slot number.
|
||||
pub(crate) fn get_current_slot(ctx: FunctionEnvMut<Env>) -> u64 {
|
||||
ctx.data().time_keeper.current_slot()
|
||||
}
|
||||
|
||||
/// Will return requested slot checkpoint from `SlotCheckpointStore`.
|
||||
pub(crate) fn get_slot_checkpoint(ctx: FunctionEnvMut<Env>, slot: u64) -> i64 {
|
||||
let env = ctx.data();
|
||||
|
||||
if env.contract_section != ContractSection::Deploy &&
|
||||
env.contract_section != ContractSection::Exec &&
|
||||
env.contract_section != ContractSection::Metadata
|
||||
{
|
||||
error!(target: "runtime::db::db_get_slot_checkpoint()", "db_get_slot_checkpoint called in unauthorized section");
|
||||
return CALLER_ACCESS_DENIED.into()
|
||||
}
|
||||
|
||||
let ret = match env.blockchain.lock().unwrap().slot_checkpoints.get(slot) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
error!(target: "runtime::db::db_get_slot_checkpoint()", "Internal error getting from slot checkpoints tree: {}", e);
|
||||
return DB_GET_FAILED.into()
|
||||
}
|
||||
};
|
||||
|
||||
// Copy Vec<u8> to the VM
|
||||
let mut objects = env.objects.borrow_mut();
|
||||
objects.push(ret.to_vec());
|
||||
(objects.len() - 1) as i64
|
||||
}
|
||||
|
||||
/// Will return current blockchain timestamp.
|
||||
pub(crate) fn get_blockchain_time(ctx: FunctionEnvMut<Env>) -> u64 {
|
||||
ctx.data().time_keeper.blockchain_timestamp()
|
||||
}
|
||||
|
||||
@@ -210,12 +210,6 @@ impl Runtime {
|
||||
import::db::db_get,
|
||||
),
|
||||
|
||||
"db_get_slot_checkpoint_" => Function::new_typed_with_env(
|
||||
&mut store,
|
||||
&ctx,
|
||||
import::db::db_get_slot_checkpoint,
|
||||
),
|
||||
|
||||
"db_contains_key_" => Function::new_typed_with_env(
|
||||
&mut store,
|
||||
&ctx,
|
||||
@@ -276,6 +270,12 @@ impl Runtime {
|
||||
import::util::get_current_slot,
|
||||
),
|
||||
|
||||
"get_slot_checkpoint_" => Function::new_typed_with_env(
|
||||
&mut store,
|
||||
&ctx,
|
||||
import::util::get_slot_checkpoint,
|
||||
),
|
||||
|
||||
"get_blockchain_time_" => Function::new_typed_with_env(
|
||||
&mut store,
|
||||
&ctx,
|
||||
|
||||
@@ -21,7 +21,7 @@ use darkfi_serial::Encodable;
|
||||
use super::{
|
||||
crypto::ContractId,
|
||||
error::{ContractError, GenericResult},
|
||||
util::{get_object_bytes, get_object_size},
|
||||
util::parse_ret,
|
||||
};
|
||||
|
||||
pub type DbHandle = u32;
|
||||
@@ -96,36 +96,7 @@ pub fn db_get(db_handle: DbHandle, key: &[u8]) -> GenericResult<Option<Vec<u8>>>
|
||||
len += key.to_vec().encode(&mut buf)?;
|
||||
|
||||
let ret = unsafe { db_get_(buf.as_ptr(), len as u32) };
|
||||
db_ret(ret)
|
||||
}
|
||||
|
||||
/// Everyone can call this. Will return requested slot checkpoint from `SlotCheckpointStore`.
|
||||
///
|
||||
/// ```
|
||||
/// slot_checkpoint = db_get_slot(slot);
|
||||
/// ```
|
||||
pub fn db_get_slot_checkpoint(slot: u64) -> GenericResult<Option<Vec<u8>>> {
|
||||
let ret = unsafe { db_get_slot_checkpoint_(slot) };
|
||||
db_ret(ret)
|
||||
}
|
||||
|
||||
/// Auxiliary to parse db_get* calls.
|
||||
fn db_ret(ret: i64) -> GenericResult<Option<Vec<u8>>> {
|
||||
if ret < 0 {
|
||||
match ret as i32 {
|
||||
CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
|
||||
DB_GET_FAILED => return Err(ContractError::DbGetFailed),
|
||||
-127 => return Ok(None),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
let obj = ret as u32;
|
||||
let obj_size = get_object_size(obj);
|
||||
let mut buf = vec![0u8; obj_size as usize];
|
||||
get_object_bytes(&mut buf, obj);
|
||||
|
||||
Ok(Some(buf))
|
||||
parse_ret(ret)
|
||||
}
|
||||
|
||||
/// Everyone can call this. Checks if a key is contained in the key-value store.
|
||||
@@ -217,7 +188,6 @@ extern "C" {
|
||||
fn db_init_(ptr: *const u8, len: u32) -> i32;
|
||||
fn db_lookup_(ptr: *const u8, len: u32) -> i32;
|
||||
fn db_get_(ptr: *const u8, len: u32) -> i64;
|
||||
fn db_get_slot_checkpoint_(slot: u64) -> i64;
|
||||
fn db_contains_key_(ptr: *const u8, len: u32) -> i32;
|
||||
fn db_set_(ptr: *const u8, len: u32) -> i32;
|
||||
fn db_del_(ptr: *const u8, len: u32) -> i32;
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use super::error::ContractError;
|
||||
use super::{
|
||||
db::{CALLER_ACCESS_DENIED, DB_GET_FAILED},
|
||||
error::{ContractError, GenericResult},
|
||||
};
|
||||
|
||||
pub fn set_return_data(data: &[u8]) -> Result<(), ContractError> {
|
||||
unsafe {
|
||||
@@ -39,6 +42,25 @@ pub fn get_object_size(object_index: u32) -> i64 {
|
||||
unsafe { get_object_size_(object_index) }
|
||||
}
|
||||
|
||||
/// Auxiliary function to parse db_get and get_slot_checkpoint return value.
|
||||
pub(crate) fn parse_ret(ret: i64) -> GenericResult<Option<Vec<u8>>> {
|
||||
if ret < 0 {
|
||||
match ret as i32 {
|
||||
CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
|
||||
DB_GET_FAILED => return Err(ContractError::DbGetFailed),
|
||||
-127 => return Ok(None),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
let obj = ret as u32;
|
||||
let obj_size = get_object_size(obj);
|
||||
let mut buf = vec![0u8; obj_size as usize];
|
||||
get_object_bytes(&mut buf, obj);
|
||||
|
||||
Ok(Some(buf))
|
||||
}
|
||||
|
||||
/// Everyone can call this. Will return current epoch.
|
||||
///
|
||||
/// ```
|
||||
@@ -57,6 +79,16 @@ pub fn get_current_slot() -> u64 {
|
||||
unsafe { get_current_slot_() }
|
||||
}
|
||||
|
||||
/// Everyone can call this. Will return requested slot checkpoint from `SlotCheckpointStore`.
|
||||
///
|
||||
/// ```
|
||||
/// slot_checkpoint = get_slot_checkpoint(slot);
|
||||
/// ```
|
||||
pub fn get_slot_checkpoint(slot: u64) -> GenericResult<Option<Vec<u8>>> {
|
||||
let ret = unsafe { get_slot_checkpoint_(slot) };
|
||||
parse_ret(ret)
|
||||
}
|
||||
|
||||
/// Everyone can call this. Will return current blockchain timestamp.
|
||||
///
|
||||
/// ```
|
||||
@@ -74,5 +106,6 @@ extern "C" {
|
||||
|
||||
fn get_current_epoch_() -> u64;
|
||||
fn get_current_slot_() -> u64;
|
||||
fn get_slot_checkpoint_(slot: u64) -> i64;
|
||||
fn get_blockchain_time_() -> u64;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user