Skip to content

Commit 7d40a1d

Browse files
committed
Refactor unpacked in_msg params
1 parent d462cbe commit 7d40a1d

File tree

6 files changed

+313
-248
lines changed

6 files changed

+313
-248
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ tracing = "0.1"
3939
tracing-subscriber = "0.3"
4040
tracing-test = "0.2"
4141

42-
everscale-asm-macros = { git = "https://github.com/broxus/everscale-asm.git", rev = "bbd284a72676300c89ab074bd39cd91fde21d597" }
42+
everscale-asm-macros = { git = "https://github.com/broxus/everscale-asm.git", rev = "1ca1675c0e9b7fa8dde3a5f7422ebd3bd169fb62" }
4343

4444
tycho-vm = { path = "./vm" }
4545

4646
[patch.crates-io]
47-
everscale-types = { git = "https://github.com/broxus/everscale-types.git", rev = "3b71c951d876213ad4e03f071182be0b1711d77c" }
47+
everscale-types = { git = "https://github.com/broxus/everscale-types.git", rev = "fee2eb43c3cbd0c5180a6d4c359e5bb415fb9d72" }

executor/src/phase/compute.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use anyhow::Result;
22
use everscale_types::models::{
33
AccountState, AccountStatus, ComputePhase, ComputePhaseSkipReason, CurrencyCollection,
4-
ExecutedComputePhase, SkippedComputePhase, StateInit, TickTock,
4+
ExecutedComputePhase, IntAddr, IntMsgInfo, MsgType, SkippedComputePhase, StateInit, TickTock,
55
};
66
use everscale_types::num::Tokens;
77
use everscale_types::prelude::*;
88
use num_bigint::{BigInt, Sign};
9-
use tycho_vm::{tuple, SafeRc, SmcInfoBase, Stack, VmState};
9+
use tycho_vm::{tuple, SafeRc, SmcInfoBase, Stack, Tuple, UnpackedInMsgSmcInfo, VmState};
1010

1111
use crate::phase::receive::{MsgStateInit, ReceivedMessage};
1212
use crate::util::{
@@ -147,6 +147,7 @@ impl ExecutorState<'_> {
147147
});
148148
return Ok(res);
149149
}
150+
150151
// Apply internal message state.
151152
let state_libs;
152153
let msg_libs;
@@ -180,7 +181,7 @@ impl ExecutorState<'_> {
180181
&self.address.address
181182
};
182183

183-
if from_msg.hash != *target_hash || from_msg.parsed.split_depth.is_some() {
184+
if from_msg.root_hash() != target_hash || from_msg.parsed.split_depth.is_some() {
184185
// State hash mismatch, cannot use this state.
185186
// We also forbid using `split_depth` (for now).
186187
res.compute_phase = ComputePhase::Skipped(SkippedComputePhase {
@@ -227,7 +228,7 @@ impl ExecutorState<'_> {
227228
}
228229
(Some(from_msg), AccountState::Active(StateInit { libraries, .. })) => {
229230
// Check if a state from the external message has the correct hash.
230-
if is_external && from_msg.hash != self.address.address {
231+
if is_external && from_msg.root_hash() != &self.address.address {
231232
res.compute_phase = ComputePhase::Skipped(SkippedComputePhase {
232233
reason: ComputePhaseSkipReason::BadState,
233234
});
@@ -243,6 +244,12 @@ impl ExecutorState<'_> {
243244
}
244245
}
245246

247+
// Unpack internal message.
248+
let unpacked_in_msg = match ctx.input.in_msg() {
249+
Some(msg) => msg.make_tuple()?,
250+
None => None,
251+
};
252+
246253
// Build VM stack and register info.
247254
let stack = self.prepare_vm_stack(ctx.input);
248255

@@ -262,9 +269,8 @@ impl ExecutorState<'_> {
262269
.with_storage_fees(ctx.storage_fee)
263270
.require_ton_v6()
264271
.with_unpacked_config(self.config.unpacked.as_tuple())
265-
.require_ton_v9()
266272
.require_ton_v11()
267-
.with_in_message(ctx.input.in_msg().map(|x| &x.root))?;
273+
.with_unpacked_in_msg(unpacked_in_msg);
268274

269275
let libraries = (msg_libs, state_libs, &self.params.libraries);
270276
let mut vm = VmState::builder()
@@ -364,16 +370,16 @@ impl ExecutorState<'_> {
364370
SafeRc::new(Stack::with_items(match input {
365371
TransactionInput::Ordinary(msg) => {
366372
tuple![
367-
int self.balance.tokens.into_inner(),
368-
int msg.balance_remaining.tokens.into_inner(),
373+
int self.balance.tokens,
374+
int msg.balance_remaining.tokens,
369375
cell msg.root.clone(),
370376
slice msg.body.clone(),
371377
int if msg.is_external { -1 } else { 0 },
372378
]
373379
}
374380
TransactionInput::TickTock(ty) => {
375381
tuple![
376-
int self.balance.tokens.into_inner(),
382+
int self.balance.tokens,
377383
int BigInt::from_bytes_be(Sign::Plus, self.address.address.as_array()),
378384
int match ty {
379385
TickTock::Tick => 0,
@@ -386,6 +392,42 @@ impl ExecutorState<'_> {
386392
}
387393
}
388394

395+
impl ReceivedMessage {
396+
fn make_tuple(&self) -> Result<Option<SafeRc<Tuple>>, everscale_types::error::Error> {
397+
let mut cs = self.root.as_slice()?;
398+
if MsgType::load_from(&mut cs)? != MsgType::Int {
399+
return Ok(None);
400+
}
401+
402+
// Get `src` addr range.
403+
let src_addr_slice = {
404+
let mut cs = cs;
405+
// Skip flags.
406+
cs.skip_first(3, 0)?;
407+
let mut addr_slice = cs;
408+
// Read `src`.
409+
IntAddr::load_from(&mut cs)?;
410+
addr_slice.skip_last(cs.size_bits(), cs.size_refs())?;
411+
addr_slice.range()
412+
};
413+
414+
let info = IntMsgInfo::load_from(&mut cs)?;
415+
416+
let unpacked = UnpackedInMsgSmcInfo {
417+
bounce: info.bounce,
418+
bounced: info.bounced,
419+
src_addr: (src_addr_slice, self.root.clone()).into(),
420+
fwd_fee: info.fwd_fee,
421+
created_lt: info.created_lt,
422+
created_at: info.created_at,
423+
original_value: info.value.tokens,
424+
remaining_value: self.balance_remaining.clone(),
425+
state_init: self.init.as_ref().map(|init| init.root.clone()),
426+
};
427+
Ok(Some(unpacked.into_tuple()))
428+
}
429+
}
430+
389431
#[cfg(test)]
390432
mod tests {
391433
use everscale_asm_macros::tvmasm;

executor/src/phase/receive.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl ExecutorState<'_> {
9797
let init = if slice.load_bit()? {
9898
Some(if slice.load_bit()? {
9999
// State init as reference.
100-
let state_root = slice.load_reference()?;
100+
let state_root = slice.load_reference_cloned()?;
101101
anyhow::ensure!(
102102
!state_root.is_exotic(),
103103
"state init must be an ordinary cell"
@@ -108,7 +108,7 @@ impl ExecutorState<'_> {
108108
anyhow::ensure!(slice.is_empty(), "state init contains extra data");
109109

110110
MsgStateInit {
111-
hash: *state_root.repr_hash(),
111+
root: state_root,
112112
parsed,
113113
}
114114
} else {
@@ -122,7 +122,7 @@ impl ExecutorState<'_> {
122122
let state_root = CellBuilder::build_from(state_init_cs)?;
123123

124124
MsgStateInit {
125-
hash: *state_root.repr_hash(),
125+
root: state_root,
126126
parsed,
127127
}
128128
})
@@ -194,12 +194,18 @@ pub struct ReceivedMessage {
194194
/// Message state init.
195195
#[derive(Debug, Clone)]
196196
pub struct MsgStateInit {
197-
/// [`StateInit`] hash.
198-
pub hash: HashBytes,
197+
/// Serialized [`StateInit`].
198+
pub root: Cell,
199199
/// Parsed [`StateInit`].
200200
pub parsed: StateInit,
201201
}
202202

203+
impl MsgStateInit {
204+
pub fn root_hash(&self) -> &HashBytes {
205+
self.root.repr_hash()
206+
}
207+
}
208+
203209
#[cfg(test)]
204210
mod tests {
205211
use everscale_types::models::{
@@ -245,7 +251,7 @@ mod tests {
245251
let target = StateInit::default();
246252
assert_eq!(init.parsed, target);
247253
let target_hash = *CellBuilder::build_from(&target).unwrap().repr_hash();
248-
assert_eq!(init.hash, target_hash);
254+
assert_eq!(init.root_hash(), &target_hash);
249255
}
250256
assert!(msg.body.0.is_empty());
251257
assert_eq!(msg.balance_remaining, CurrencyCollection::ZERO);

0 commit comments

Comments
 (0)