-
Notifications
You must be signed in to change notification settings - Fork 91
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
Implement SMO
instruction
#159
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,20 +88,25 @@ impl<S> Interpreter<S> { | |
|
||
// compute the initial free balances for each asset type | ||
pub(crate) fn initial_free_balances(&self) -> Result<HashMap<AssetId, Word>, InterpreterError> { | ||
let base_asset = AssetId::default(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Orthogonal to this PR, but I'm thinking the base asset's ID should be a parameter in the VM. It's not necessarily guaranteed to be |
||
let mut balances = HashMap::<AssetId, Word>::new(); | ||
|
||
// Add up all the inputs for each asset ID | ||
for (asset_id, amount) in self.tx.inputs().iter().filter_map(|input| match input { | ||
Input::CoinPredicate { asset_id, amount, .. } | Input::CoinSigned { asset_id, amount, .. } => { | ||
Some((asset_id, amount)) | ||
} | ||
_ => None, | ||
}) { | ||
*balances.entry(*asset_id).or_default() += amount; | ||
} | ||
self.tx | ||
.inputs() | ||
.iter() | ||
.filter_map(|i| match i { | ||
Input::CoinPredicate { asset_id, amount, .. } | Input::CoinSigned { asset_id, amount, .. } => { | ||
Some((*asset_id, *amount)) | ||
} | ||
Input::MessageSigned { amount, .. } | Input::MessagePredicate { amount, .. } => { | ||
Some((base_asset, *amount)) | ||
} | ||
_ => None, | ||
}) | ||
.for_each(|(asset_id, amount)| *balances.entry(asset_id).or_default() += amount); | ||
|
||
// Reduce by unavailable balances | ||
let base_asset = AssetId::default(); | ||
if let Some(base_asset_balance) = balances.get_mut(&base_asset) { | ||
// calculate the fee with used metered bytes + gas limit | ||
let factor = self.params.gas_price_factor as f64; | ||
|
@@ -124,19 +129,20 @@ impl<S> Interpreter<S> { | |
let fee = bytes.checked_add(gas).ok_or(ValidationError::ArithmeticOverflow)?; | ||
|
||
// subtract total fee from base asset balance | ||
*base_asset_balance = | ||
let deducted_balance = | ||
base_asset_balance | ||
.checked_sub(fee) | ||
.ok_or(ValidationError::InsufficientFeeAmount { | ||
expected: fee, | ||
provided: *base_asset_balance, | ||
})?; | ||
|
||
*base_asset_balance = deducted_balance; | ||
} | ||
|
||
// reduce free balances by coin and withdrawal outputs | ||
// reduce free balances by coin | ||
for (asset_id, amount) in self.tx.outputs().iter().filter_map(|output| match output { | ||
Output::Coin { asset_id, amount, .. } => Some((asset_id, amount)), | ||
Output::Message { .. } => todo!(), | ||
adlerjohn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ => None, | ||
}) { | ||
let balance = balances | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ impl<S> Interpreter<S> { | |
.tx | ||
.witnesses() | ||
.get(b as usize) | ||
.ok_or(PanicReason::OutputNotFound) | ||
.ok_or(PanicReason::WitnessNotFound) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an orthogonal bugfix. Ideally make a separate PR for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats true, I just spotted this nit while debugging |
||
.map(|witness| witness.serialized_size() as Word)?; | ||
|
||
self.inc_pc() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should use
Bytes32::LEN
like on line 316 for consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, they should be the same. The concrete type is in fact
Address
, instead of raw bytes. Better to use that so its clear what is expected of this register:7bd5789