Skip to content

Commit

Permalink
Merge pull request #1 from tvmlabs/feature-block_usage_detection
Browse files Browse the repository at this point in the history
Block usage detection
  • Loading branch information
SilkovAlexander authored Feb 28, 2024
2 parents 9212fb9 + c7bb6b8 commit 1babc83
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
edition = "2021"
name = "tvm_vm"
rust-version = "1.75.0"
version = "2.0.0"
version = "2.1.0"

[dependencies]
anyhow = "1.0.79"
Expand Down
11 changes: 11 additions & 0 deletions src/executor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(super) fn execute_balance(engine: &mut Engine) -> Status {

// ( - D 32)
pub(super) fn execute_config_dict(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
engine.load_instruction(Instruction::new("CONFIGDICT"))?;
let dict = engine.smci_param(9)?.clone();
engine.cc.stack.push(dict);
Expand All @@ -54,11 +55,13 @@ pub(super) fn execute_config_dict(engine: &mut Engine) -> Status {

/// (i - c?)
pub(super) fn execute_config_opt_param(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_config_param(engine, "CONFIGOPTPARAM", true)
}

/// (i - c -1 or 0)
pub(super) fn execute_config_ref_param(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_config_param(engine, "CONFIGPARAM", false)
}

Expand All @@ -71,26 +74,31 @@ fn extract_config(engine: &mut Engine, name: &'static str) -> Status {

// - D
pub(super) fn execute_config_root(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "CONFIGROOT")
}

// - x
pub(super) fn execute_getparam(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "GETPARAM")
}

// - integer
pub(super) fn execute_now(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "NOW")
}

// - integer
pub(super) fn execute_blocklt(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "BLOCKLT")
}

// - integer
pub(super) fn execute_ltime(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "LTIME")
}

Expand All @@ -107,6 +115,7 @@ pub(super) fn execute_my_code(engine: &mut Engine) -> Status {

// - x
pub(super) fn execute_randseed(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
extract_config(engine, "RANDSEED")
}

Expand All @@ -119,11 +128,13 @@ pub(super) fn execute_init_code_hash(engine: &mut Engine) -> Status {
// - integer
pub(super) fn execute_seq_no(engine: &mut Engine) -> Status {
engine.check_capability(GlobalCapabilities::CapDelections)?;
engine.mark_execution_as_block_related()?;
extract_config(engine, "SEQNO")
}

// - integer
pub(super) fn execute_storage_fees_collected(engine: &mut Engine) -> Status {
engine.check_capability(GlobalCapabilities::CapStorageFeeToTvm)?;
engine.mark_execution_as_block_related()?;
extract_config(engine, "STORAGEFEE")
}
30 changes: 30 additions & 0 deletions src/executor/engine/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub struct Engine {
block_version: u32,
#[cfg(feature = "signature_with_id")]
signature_id: i32,
vm_execution_is_block_related: Arc<Mutex<bool>>,
block_collation_was_finished: Arc<Mutex<bool>>,
}

#[cfg(feature = "signature_no_check")]
Expand Down Expand Up @@ -282,6 +284,34 @@ impl Engine {
block_version: 0,
#[cfg(feature = "signature_with_id")]
signature_id: 0,
vm_execution_is_block_related: Arc::new(Mutex::new(false)),
block_collation_was_finished: Arc::new(Mutex::new(false)),
}
}

pub fn set_block_related_flags(
&mut self,
vm_execution_is_block_related: Arc<Mutex<bool>>,
block_collation_was_finished: Arc<Mutex<bool>>,
) {
self.vm_execution_is_block_related = vm_execution_is_block_related;
self.block_collation_was_finished = block_collation_was_finished;
}

pub fn mark_execution_as_block_related(&mut self) -> Status {
log::trace!("Mark execution as block related");
{
let mut flag = self.vm_execution_is_block_related.lock().unwrap();
*flag = true;
}
let block_was_finalized = self.block_collation_was_finished.lock().unwrap();
if *block_was_finalized {
err!(
ExceptionCode::UnknownError,
"Transaction is block related and block was finalized before it's finalization."
)
} else {
Ok(())
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/executor/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,27 @@ fn execute_setget_globalvar(engine: &mut Engine, name: &'static str, how: u8) ->
// GETGLOBVAR (k–x), returns the k-th global variable for 0 ≤ k < 255.
// Equivalent to PUSH c7; SWAP; INDEXVARQ
pub(super) fn execute_getglobvar(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_setget_globalvar(engine, "GETGLOBVAR", STACK)
}

// GETGLOB k( –x), returns the k-th global variable for 1 ≤ k ≤ 31
// Equivalent to PUSH c7; INDEXQ k.
pub(super) fn execute_getglob(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_setget_globalvar(engine, "GETGLOB", CMD)
}

// SETGLOBVAR (x k– ), assigns x to the k-th global variable for 0 ≤ k <255.
// Equivalent to PUSH c7; ROTREV; SETINDEXVARQ; POP c7.
pub(super) fn execute_setglobvar(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_setget_globalvar(engine, "SETGLOBVAR", SET | STACK)
}

// SETGLOB k (x– ), assigns x to the k-th global variable for 1 ≤ k ≤ 31.
// Equivalent to PUSH c7; SWAP; SETINDEXQ k; POP c7
pub(super) fn execute_setglob(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
execute_setget_globalvar(engine, "SETGLOB", SET | CMD)
}
2 changes: 2 additions & 0 deletions src/executor/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) fn execute_addrand(engine: &mut Engine) -> Status {

// (y - z)
pub(crate) fn execute_rand(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
engine.load_instruction(Instruction::new("RAND"))?;
fetch_stack(engine, 1)?;
let sha512 =
Expand All @@ -52,6 +53,7 @@ pub(crate) fn execute_rand(engine: &mut Engine) -> Status {

// ( - x)
pub(crate) fn execute_randu256(engine: &mut Engine) -> Status {
engine.mark_execution_as_block_related()?;
engine.load_instruction(Instruction::new("RANDU256"))?;
let sha512 =
sha512_digest(engine.rand()?.as_builder::<UnsignedIntegerBigEndianEncoding>(256)?.data());
Expand Down

0 comments on commit 1babc83

Please sign in to comment.