Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Byte array update #3221

Merged
merged 15 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,104 changes: 706 additions & 398 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions massa-client/src/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ impl Command {
let addr = parameters[0].parse::<Address>()?;
let target_addr = parameters[1].parse::<Address>()?;
let target_func = parameters[2].clone();
let param = parameters[3].clone();
let param = parameters[3].clone().into_bytes();
let max_gas = parameters[4].parse::<u64>()?;
let gas_price = parameters[5].parse::<Amount>()?;
let coins = parameters[6].parse::<Amount>()?;
Expand Down Expand Up @@ -995,7 +995,7 @@ impl Command {

let target_address = parameters[0].parse::<Address>()?;
let target_function = parameters[1].parse::<String>()?;
let parameter = parameters[2].parse::<String>()?;
let parameter = parameters[2].parse::<String>()?.into_bytes();
let max_gas = parameters[3].parse::<u64>()?;
let simulated_gas_price = parameters[4].parse::<Amount>()?;
let caller_address = if let Some(addr) = parameters.get(5) {
Expand Down
2 changes: 1 addition & 1 deletion massa-execution-exports/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub enum ReadOnlyExecutionTarget {
/// Target function
target_func: String,
/// Parameter to pass to the target function
parameter: String,
parameter: Vec<u8>,
},
}

Expand Down
19 changes: 16 additions & 3 deletions massa-execution-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ massa_execution_exports = { path = "../massa-execution-exports" }
massa_models = { path = "../massa-models" }
massa_storage = { path = "../massa-storage" }
massa_hash = { path = "../massa-hash" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", tag = "v0.8.2" }
massa-sc-runtime = { git = "https://github.com/massalabs/massa-sc-runtime", branch = "testnet_17" }
massa_signature = { path = "../massa-signature" }
massa_time = { path = "../massa-time" }
massa_ledger_exports = { path = "../massa-ledger-exports" }
Expand All @@ -33,8 +33,21 @@ serial_test = "0.9"
tempfile = "3.2"
# custom modules with testing enabled
massa_pos_worker = { path = "../massa-pos-worker" }
massa_ledger_worker = { path = "../massa-ledger-worker" }
massa_ledger_worker = { path = "../massa-ledger-worker", features = [
"testing",
] }
massa_execution_exports = { path = "../massa-execution-exports", features = [
"testing",
] }
massa_final_state = { path = "../massa-final-state", features = ["testing"] }

[features]
sandbox = ["massa_async_pool/sandbox"]
testing = ["massa_execution_exports/testing", "massa_ledger_exports/testing", "massa_pos_exports/testing", "massa_pos_worker/testing", "massa_ledger_worker/testing", "massa_final_state/testing"]
testing = [
"massa_execution_exports/testing",
"massa_ledger_exports/testing",
"massa_pos_exports/testing",
"massa_pos_worker/testing",
"massa_ledger_worker/testing",
"massa_final_state/testing",
]
19 changes: 11 additions & 8 deletions massa-execution-worker/src/active_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,18 @@ impl ActiveHistory {
}

/// Gets the deferred credits for a given address that will be credited at a given slot
pub(crate) fn get_adress_deferred_credit_for(&self, addr: &Address, slot: &Slot) -> Option<Amount> {
for hist_item in self.0
.iter()
.rev() {
pub(crate) fn get_adress_deferred_credit_for(
&self,
addr: &Address,
slot: &Slot,
) -> Option<Amount> {
for hist_item in self.0.iter().rev() {
if let Some(v) = hist_item
.state_changes
.pos_changes
.deferred_credits
.get_address_deferred_credit_for_slot(addr, slot) {
.state_changes
.pos_changes
.deferred_credits
.get_address_deferred_credit_for_slot(addr, slot)
{
return Some(v);
}
}
Expand Down
14 changes: 7 additions & 7 deletions massa-execution-worker/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ impl ExecutionState {
&bytecode,
max_gas,
target_func,
param,
&param,
&*self.execution_interface,
) {
Ok(_reamining_gas) => {}
Expand Down Expand Up @@ -630,7 +630,7 @@ impl ExecutionState {
) -> Result<(), ExecutionError> {
// prepare execution context
let context_snapshot;
let (bytecode, data): (Vec<u8>, &str) = {
let bytecode: Vec<u8> = {
let mut context = context_guard!(self);
context_snapshot = context.get_snapshot();
context.max_gas = message.max_gas;
Expand All @@ -653,9 +653,9 @@ impl ExecutionState {

// If there is no target bytecode or if message data is invalid,
// reimburse sender with coins and quit
let (bytecode, data) = match (bytecode, std::str::from_utf8(&message.data)) {
(Some(bc), Ok(d)) => (bc, d),
(bc, _d) => {
let bytecode = match bytecode {
Some(bc) => bc,
bc => {
let err = if bc.is_none() {
ExecutionError::RuntimeError("no target bytecode found".into())
} else {
Expand Down Expand Up @@ -683,15 +683,15 @@ impl ExecutionState {
return Err(err);
}

(bytecode, data)
bytecode
};

// run the target function
if let Err(err) = massa_sc_runtime::run_function(
&bytecode,
message.max_gas,
&message.handler,
data,
&message.data,
&*self.execution_interface,
) {
// execution failed: reset context to snapshot and reimburse sender
Expand Down
40 changes: 20 additions & 20 deletions massa-execution-worker/src/interface_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ impl Interface for InterfaceImpl {
///
/// # Returns
/// The datastore value matching the provided key, if found, otherwise an error.
fn raw_get_data_for(&self, address: &str, key: &str) -> Result<Vec<u8>> {
fn raw_get_data_for(&self, address: &str, key: &[u8]) -> Result<Vec<u8>> {
let addr = &massa_models::address::Address::from_str(address)?;
let context = context_guard!(self);
match context.get_data_entry(addr, key.as_bytes()) {
match context.get_data_entry(addr, &key) {
Some(value) => Ok(value),
_ => bail!("data entry not found"),
}
Expand All @@ -199,10 +199,10 @@ impl Interface for InterfaceImpl {
/// * address: string representation of the address
/// * key: string key of the datastore entry to set
/// * value: new value to set
fn raw_set_data_for(&self, address: &str, key: &str, value: &[u8]) -> Result<()> {
fn raw_set_data_for(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> {
let addr = massa_models::address::Address::from_str(address)?;
let mut context = context_guard!(self);
context.set_data_entry(&addr, key.as_bytes().to_vec(), value.to_vec())?;
context.set_data_entry(&addr, key.to_vec(), value.to_vec())?;
Ok(())
}

Expand All @@ -213,9 +213,9 @@ impl Interface for InterfaceImpl {
/// * address: string representation of the address
/// * key: string key of the datastore entry
/// * value: value to append
fn raw_append_data_for(&self, address: &str, key: &str, value: &[u8]) -> Result<()> {
fn raw_append_data_for(&self, address: &str, key: &[u8], value: &[u8]) -> Result<()> {
let addr = massa_models::address::Address::from_str(address)?;
context_guard!(self).append_data_entry(&addr, key.as_bytes().to_vec(), value.to_vec())?;
context_guard!(self).append_data_entry(&addr, key.to_vec(), value.to_vec())?;
Ok(())
}

Expand All @@ -225,9 +225,9 @@ impl Interface for InterfaceImpl {
/// # Arguments
/// * address: string representation of the address
/// * key: string key of the datastore entry to delete
fn raw_delete_data_for(&self, address: &str, key: &str) -> Result<()> {
fn raw_delete_data_for(&self, address: &str, key: &[u8]) -> Result<()> {
let addr = &massa_models::address::Address::from_str(address)?;
context_guard!(self).delete_data_entry(addr, key.as_bytes())?;
context_guard!(self).delete_data_entry(addr, &key)?;
Ok(())
}

Expand All @@ -239,10 +239,10 @@ impl Interface for InterfaceImpl {
///
/// # Returns
/// true if the address exists and has the entry matching the provided key in its datastore, otherwise false
fn has_data_for(&self, address: &str, key: &str) -> Result<bool> {
fn has_data_for(&self, address: &str, key: &[u8]) -> Result<bool> {
let addr = massa_models::address::Address::from_str(address)?;
let context = context_guard!(self);
Ok(context.has_data_entry(&addr, key.as_bytes()))
Ok(context.has_data_entry(&addr, &key))
}

/// Gets a datastore value by key for the current address (top of the call stack).
Expand All @@ -252,10 +252,10 @@ impl Interface for InterfaceImpl {
///
/// # Returns
/// The datastore value matching the provided key, if found, otherwise an error.
fn raw_get_data(&self, key: &str) -> Result<Vec<u8>> {
fn raw_get_data(&self, key: &[u8]) -> Result<Vec<u8>> {
let context = context_guard!(self);
let addr = context.get_current_address()?;
match context.get_data_entry(&addr, key.as_bytes()) {
match context.get_data_entry(&addr, &key) {
Some(data) => Ok(data),
_ => bail!("data entry not found"),
}
Expand All @@ -269,10 +269,10 @@ impl Interface for InterfaceImpl {
/// * address: string representation of the address
/// * key: string key of the datastore entry to set
/// * value: new value to set
fn raw_set_data(&self, key: &str, value: &[u8]) -> Result<()> {
fn raw_set_data(&self, key: &[u8], value: &[u8]) -> Result<()> {
let mut context = context_guard!(self);
let addr = context.get_current_address()?;
context.set_data_entry(&addr, key.as_bytes().to_vec(), value.to_vec())?;
context.set_data_entry(&addr, key.to_vec(), value.to_vec())?;
Ok(())
}

Expand All @@ -283,10 +283,10 @@ impl Interface for InterfaceImpl {
/// * address: string representation of the address
/// * key: string key of the datastore entry
/// * value: value to append
fn raw_append_data(&self, key: &str, value: &[u8]) -> Result<()> {
fn raw_append_data(&self, key: &[u8], value: &[u8]) -> Result<()> {
let mut context = context_guard!(self);
let addr = context.get_current_address()?;
context.append_data_entry(&addr, key.as_bytes().to_vec(), value.to_vec())?;
context.append_data_entry(&addr, key.to_vec(), value.to_vec())?;
Ok(())
}

Expand All @@ -295,10 +295,10 @@ impl Interface for InterfaceImpl {
///
/// # Arguments
/// * key: string key of the datastore entry to delete
fn raw_delete_data(&self, key: &str) -> Result<()> {
fn raw_delete_data(&self, key: &[u8]) -> Result<()> {
let mut context = context_guard!(self);
let addr = context.get_current_address()?;
context.delete_data_entry(&addr, key.as_bytes())?;
context.delete_data_entry(&addr, &key)?;
Ok(())
}

Expand All @@ -309,10 +309,10 @@ impl Interface for InterfaceImpl {
///
/// # Returns
/// true if the address exists and has the entry matching the provided key in its datastore, otherwise false
fn has_data(&self, key: &str) -> Result<bool> {
fn has_data(&self, key: &[u8]) -> Result<bool> {
let context = context_guard!(self);
let addr = context.get_current_address()?;
Ok(context.has_data_entry(&addr, key.as_bytes()))
Ok(context.has_data_entry(&addr, &key))
}

/// Get the operation datastore keys (aka entries).
Expand Down
31 changes: 18 additions & 13 deletions massa-execution-worker/src/speculative_roll_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,14 @@ impl SpeculativeRollState {
.saturating_add(roll_price.saturating_mul_u64(roll_count));

// Remove the rolls
self
.added_changes
self.added_changes
.roll_changes
.insert(*seller_addr, owned_count.saturating_sub(roll_count));

// Add deferred credits (reimbursement) corresponding to the sold rolls value
self.added_changes
.deferred_credits
.insert(*seller_addr, target_slot, new_deferred_credits);
.deferred_credits
.insert(*seller_addr, target_slot, new_deferred_credits);

Ok(())
}
Expand Down Expand Up @@ -277,26 +276,32 @@ impl SpeculativeRollState {

/// Gets the deferred credits for a given address that will be credited at a given slot
fn get_address_deferred_credit_for_slot(&self, addr: &Address, slot: &Slot) -> Option<Amount> {

// search in the added changes
if let Some(v) = self.added_changes
if let Some(v) = self
.added_changes
.deferred_credits
.get_address_deferred_credit_for_slot(addr, slot) {
.get_address_deferred_credit_for_slot(addr, slot)
{
return Some(v);
}

// search in the history
if let Some(v) = self.active_history
if let Some(v) = self
.active_history
.read()
.get_adress_deferred_credit_for(addr, slot) {
.get_adress_deferred_credit_for(addr, slot)
{
return Some(v);
}

// search in the final state
if let Some(v) = self.final_state
if let Some(v) = self
.final_state
.read()
.pos_state.deferred_credits
.get_address_deferred_credit_for_slot(addr, slot) {
.pos_state
.deferred_credits
.get_address_deferred_credit_for_slot(addr, slot)
{
return Some(v);
}

Expand Down Expand Up @@ -490,7 +495,7 @@ impl SpeculativeRollState {
credits.extend(
self.active_history
.read()
.get_all_deferred_credits_for(slot)
.get_all_deferred_credits_for(slot),
);

// added deferred credits
Expand Down
3 changes: 1 addition & 2 deletions massa-execution-worker/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ pub fn get_sample_state() -> Result<(Arc<RwLock<FinalState>>, NamedTempFile, Tem
};
let (_, selector_controller) = start_selector_worker(SelectorConfig::default())
.expect("could not start selector controller");
let mut final_state =
FinalState::new(cfg, Box::new(ledger), selector_controller).unwrap();
let mut final_state = FinalState::new(cfg, Box::new(ledger), selector_controller).unwrap();
final_state.compute_initial_draws().unwrap();
final_state.pos_state.create_initial_cycle();
Ok((Arc::new(RwLock::new(final_state)), tempfile, tempdir))
Expand Down
Loading