Skip to content

Commit 5a4de23

Browse files
authored
Simplify block execution code; remove redundant arguments. (#3641)
## Motivation There are several methods in the block execution code with too many arguments (according to Clippy), and some with redundant arguments. ## Proposal Remove the redundant arguments; move more information into the `TransactionTracker`; add `TransactionTracker` test constructors to deduplicate some code. ## Test Plan CI ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links - [reviewer checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
1 parent b977ac6 commit 5a4de23

File tree

13 files changed

+222
-357
lines changed

13 files changed

+222
-357
lines changed

linera-chain/src/chain.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,8 @@ where
801801
None => None,
802802
};
803803
let mut txn_tracker = TransactionTracker::new(
804+
local_time,
805+
txn_index,
804806
next_message_index,
805807
next_application_index,
806808
maybe_responses,
@@ -817,8 +819,6 @@ where
817819
incoming_bundle,
818820
block,
819821
round,
820-
txn_index,
821-
local_time,
822822
&mut txn_tracker,
823823
&mut resource_controller,
824824
))
@@ -841,7 +841,6 @@ where
841841
};
842842
Box::pin(self.execution_state.execute_operation(
843843
context,
844-
local_time,
845844
operation.clone(),
846845
&mut txn_tracker,
847846
&mut resource_controller,
@@ -993,8 +992,6 @@ where
993992
incoming_bundle: &IncomingBundle,
994993
block: &ProposedBlock,
995994
round: Option<u32>,
996-
txn_index: u32,
997-
local_time: Timestamp,
998995
txn_tracker: &mut TransactionTracker,
999996
resource_controller: &mut ResourceController<Option<AccountOwner>>,
1000997
) -> Result<(), ChainError> {
@@ -1013,29 +1010,24 @@ where
10131010
let mut grant = posted_message.grant;
10141011
match incoming_bundle.action {
10151012
MessageAction::Accept => {
1013+
let chain_execution_context =
1014+
ChainExecutionContext::IncomingBundle(txn_tracker.transaction_index());
10161015
// Once a chain is closed, accepting incoming messages is not allowed.
10171016
ensure!(!self.is_closed(), ChainError::ClosedChain);
10181017

10191018
Box::pin(self.execution_state.execute_message(
10201019
context,
1021-
local_time,
10221020
posted_message.message.clone(),
10231021
(grant > Amount::ZERO).then_some(&mut grant),
10241022
txn_tracker,
10251023
resource_controller,
10261024
))
10271025
.await
1028-
.with_execution_context(ChainExecutionContext::IncomingBundle(txn_index))?;
1029-
if grant > Amount::ZERO {
1030-
if let Some(refund_grant_to) = posted_message.refund_grant_to {
1031-
self.execution_state
1032-
.send_refund(context, grant, refund_grant_to, txn_tracker)
1033-
.await
1034-
.with_execution_context(ChainExecutionContext::IncomingBundle(
1035-
txn_index,
1036-
))?;
1037-
}
1038-
}
1026+
.with_execution_context(chain_execution_context)?;
1027+
self.execution_state
1028+
.send_refund(context, grant, txn_tracker)
1029+
.await
1030+
.with_execution_context(chain_execution_context)?;
10391031
}
10401032
MessageAction::Reject => {
10411033
// If rejecting a message fails, the entire block proposal should be
@@ -1054,17 +1046,10 @@ where
10541046
.bounce_message(context, grant, posted_message.message.clone(), txn_tracker)
10551047
.await
10561048
.with_execution_context(ChainExecutionContext::Block)?;
1057-
} else if grant > Amount::ZERO {
1049+
} else {
10581050
// Nothing to do except maybe refund the grant.
1059-
let Some(refund_grant_to) = posted_message.refund_grant_to else {
1060-
// See OperationContext::refund_grant_to()
1061-
return Err(ChainError::InternalError(
1062-
"Messages with grants should have a non-empty `refund_grant_to`".into(),
1063-
));
1064-
};
1065-
// Refund grant.
10661051
self.execution_state
1067-
.send_refund(context, posted_message.grant, refund_grant_to, txn_tracker)
1052+
.send_refund(context, grant, txn_tracker)
10681053
.await
10691054
.with_execution_context(ChainExecutionContext::Block)?;
10701055
}

linera-core/src/unit_tests/wasm_worker_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ where
282282
creator_state
283283
.execute_operation(
284284
operation_context,
285-
Timestamp::from(3),
286285
Operation::User {
287286
application_id,
288287
bytes: user_operation,
289288
},
290289
&mut TransactionTracker::new(
290+
Timestamp::from(3),
291+
0,
291292
0,
292293
0,
293294
Some(vec![OracleResponse::Blob(application_description_blob_id)]),

linera-execution/src/execution.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::{mem, vec};
55

66
use futures::{FutureExt, StreamExt};
77
use linera_base::{
8-
data_types::{Amount, BlockHeight, Timestamp},
9-
identifiers::{Account, AccountOwner, BlobType, ChainId, Destination},
8+
data_types::{Amount, BlockHeight},
9+
identifiers::{Account, AccountOwner, BlobType, Destination},
1010
};
1111
use linera_views::{
1212
context::Context,
@@ -60,13 +60,14 @@ where
6060
pub async fn simulate_instantiation(
6161
&mut self,
6262
contract: UserContractCode,
63-
local_time: Timestamp,
63+
local_time: linera_base::data_types::Timestamp,
6464
application_description: ApplicationDescription,
6565
instantiation_argument: Vec<u8>,
6666
contract_blob: Blob,
6767
service_blob: Blob,
6868
) -> Result<(), ExecutionError> {
6969
let chain_id = application_description.creator_chain_id;
70+
assert_eq!(chain_id, self.context().extra().chain_id);
7071
let context = OperationContext {
7172
chain_id,
7273
authenticated_signer: None,
@@ -106,13 +107,16 @@ where
106107
tracker,
107108
account: None,
108109
};
109-
let mut txn_tracker =
110-
TransactionTracker::new(next_message_index, next_application_index, None);
110+
let mut txn_tracker = TransactionTracker::new(
111+
local_time,
112+
0,
113+
next_message_index,
114+
next_application_index,
115+
None,
116+
);
111117
txn_tracker.add_created_blob(Blob::new_application_description(&application_description));
112118
self.run_user_action(
113119
application_id,
114-
chain_id,
115-
local_time,
116120
action,
117121
context.refund_grant_to(),
118122
None,
@@ -163,12 +167,9 @@ where
163167
C: Context + Clone + Send + Sync + 'static,
164168
C::Extra: ExecutionRuntimeContext,
165169
{
166-
#[expect(clippy::too_many_arguments)]
167170
async fn run_user_action(
168171
&mut self,
169172
application_id: ApplicationId,
170-
chain_id: ChainId,
171-
local_time: Timestamp,
172173
action: UserAction,
173174
refund_grant_to: Option<Account>,
174175
grant: Option<&mut Amount>,
@@ -178,8 +179,6 @@ where
178179
let ExecutionRuntimeConfig {} = self.context().extra().execution_runtime_config();
179180
self.run_user_action_with_runtime(
180181
application_id,
181-
chain_id,
182-
local_time,
183182
action,
184183
refund_grant_to,
185184
grant,
@@ -189,18 +188,16 @@ where
189188
.await
190189
}
191190

192-
#[expect(clippy::too_many_arguments)]
193191
async fn run_user_action_with_runtime(
194192
&mut self,
195193
application_id: ApplicationId,
196-
chain_id: ChainId,
197-
local_time: Timestamp,
198194
action: UserAction,
199195
refund_grant_to: Option<Account>,
200196
grant: Option<&mut Amount>,
201197
txn_tracker: &mut TransactionTracker,
202198
resource_controller: &mut ResourceController<Option<AccountOwner>>,
203199
) -> Result<(), ExecutionError> {
200+
let chain_id = self.context().extra().chain_id();
204201
let mut cloned_grant = grant.as_ref().map(|x| **x);
205202
let initial_balance = resource_controller
206203
.with_state_and_grant(&mut self.system, cloned_grant.as_mut())
@@ -219,7 +216,6 @@ where
219216
let runtime = ContractSyncRuntime::new(
220217
execution_state_sender,
221218
chain_id,
222-
local_time,
223219
refund_grant_to,
224220
controller,
225221
&action,
@@ -257,7 +253,6 @@ where
257253
pub async fn execute_operation(
258254
&mut self,
259255
context: OperationContext,
260-
local_time: Timestamp,
261256
operation: Operation,
262257
txn_tracker: &mut TransactionTracker,
263258
resource_controller: &mut ResourceController<Option<AccountOwner>>,
@@ -273,8 +268,6 @@ where
273268
let user_action = UserAction::Instantiate(context, argument);
274269
self.run_user_action(
275270
application_id,
276-
context.chain_id,
277-
local_time,
278271
user_action,
279272
context.refund_grant_to(),
280273
None,
@@ -290,8 +283,6 @@ where
290283
} => {
291284
self.run_user_action(
292285
application_id,
293-
context.chain_id,
294-
local_time,
295286
UserAction::Operation(context, bytes),
296287
context.refund_grant_to(),
297288
None,
@@ -307,7 +298,6 @@ where
307298
pub async fn execute_message(
308299
&mut self,
309300
context: MessageContext,
310-
local_time: Timestamp,
311301
message: Message,
312302
grant: Option<&mut Amount>,
313303
txn_tracker: &mut TransactionTracker,
@@ -325,8 +315,6 @@ where
325315
} => {
326316
self.run_user_action(
327317
application_id,
328-
context.chain_id,
329-
local_time,
330318
UserAction::Message(context, bytes),
331319
context.refund_grant_to,
332320
grant,
@@ -362,10 +350,17 @@ where
362350
&self,
363351
context: MessageContext,
364352
amount: Amount,
365-
account: Account,
366353
txn_tracker: &mut TransactionTracker,
367354
) -> Result<(), ExecutionError> {
368355
assert_eq!(context.chain_id, self.context().extra().chain_id());
356+
if amount.is_zero() {
357+
return Ok(());
358+
}
359+
let Some(account) = context.refund_grant_to else {
360+
return Err(ExecutionError::InternalError(
361+
"Messages with grants should have a non-empty `refund_grant_to`",
362+
));
363+
};
369364
let message = SystemMessage::Credit {
370365
amount,
371366
source: context.authenticated_signer.unwrap_or(AccountOwner::CHAIN),

linera-execution/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ pub enum ExecutionError {
330330
InactiveChain,
331331
#[error("No recorded response for oracle query")]
332332
MissingOracleResponse,
333+
#[error("Internal error: {0}")]
334+
InternalError(&'static str),
333335
}
334336

335337
impl From<ViewError> for ExecutionError {

linera-execution/src/runtime.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ pub struct SyncRuntimeInternal<UserInstance> {
6969
height: BlockHeight,
7070
/// The current consensus round. Only available during block validation in multi-leader rounds.
7171
round: Option<u32>,
72-
/// The current local time.
73-
local_time: Timestamp,
7472
/// The authenticated signer of the operation or message, if any.
7573
#[debug(skip_if = Option::is_none)]
7674
authenticated_signer: Option<AccountOwner>,
@@ -293,7 +291,6 @@ impl<UserInstance> SyncRuntimeInternal<UserInstance> {
293291
chain_id: ChainId,
294292
height: BlockHeight,
295293
round: Option<u32>,
296-
local_time: Timestamp,
297294
authenticated_signer: Option<AccountOwner>,
298295
executing_message: Option<ExecutingMessage>,
299296
execution_state_sender: ExecutionStateSender,
@@ -306,7 +303,6 @@ impl<UserInstance> SyncRuntimeInternal<UserInstance> {
306303
chain_id,
307304
height,
308305
round,
309-
local_time,
310306
authenticated_signer,
311307
executing_message,
312308
execution_state_sender,
@@ -478,7 +474,7 @@ impl SyncRuntimeInternal<UserContractInstance> {
478474
let context = QueryContext {
479475
chain_id: self.chain_id,
480476
next_block_height: self.height,
481-
local_time: self.local_time,
477+
local_time: self.transaction_tracker.local_time(),
482478
};
483479
let sender = self.execution_state_sender.clone();
484480

@@ -886,11 +882,12 @@ where
886882
.replay_oracle_response(OracleResponse::Assert)?
887883
{
888884
// There are no recorded oracle responses, so we check the local time.
885+
let local_time = this.transaction_tracker.local_time();
889886
ensure!(
890-
this.local_time < timestamp,
887+
local_time < timestamp,
891888
ExecutionError::AssertBefore {
892889
timestamp,
893-
local_time: this.local_time,
890+
local_time,
894891
}
895892
);
896893
}
@@ -955,7 +952,6 @@ impl ContractSyncRuntime {
955952
pub(crate) fn new(
956953
execution_state_sender: ExecutionStateSender,
957954
chain_id: ChainId,
958-
local_time: Timestamp,
959955
refund_grant_to: Option<Account>,
960956
resource_controller: ResourceController,
961957
action: &UserAction,
@@ -966,7 +962,6 @@ impl ContractSyncRuntime {
966962
chain_id,
967963
action.height(),
968964
action.round(),
969-
local_time,
970965
action.signer(),
971966
if let UserAction::Message(context, _) = action {
972967
Some(context.into())
@@ -1488,12 +1483,9 @@ impl ContractRuntime for ContractSyncRuntimeHandle {
14881483
impl ServiceSyncRuntime {
14891484
/// Creates a new [`ServiceSyncRuntime`] ready to execute using a provided [`QueryContext`].
14901485
pub fn new(execution_state_sender: ExecutionStateSender, context: QueryContext) -> Self {
1491-
Self::new_with_txn_tracker(
1492-
execution_state_sender,
1493-
context,
1494-
None,
1495-
TransactionTracker::default(),
1496-
)
1486+
let mut txn_tracker = TransactionTracker::default();
1487+
txn_tracker.set_local_time(context.local_time);
1488+
Self::new_with_txn_tracker(execution_state_sender, context, None, txn_tracker)
14971489
}
14981490

14991491
/// Creates a new [`ServiceSyncRuntime`] ready to execute using a provided [`QueryContext`].
@@ -1508,7 +1500,6 @@ impl ServiceSyncRuntime {
15081500
context.chain_id,
15091501
context.next_block_height,
15101502
None,
1511-
context.local_time,
15121503
None,
15131504
None,
15141505
execution_state_sender,
@@ -1579,7 +1570,10 @@ impl ServiceSyncRuntime {
15791570
let execution_state_sender = self.handle_mut().inner().execution_state_sender.clone();
15801571
*self = ServiceSyncRuntime::new(execution_state_sender, new_context);
15811572
} else {
1582-
self.handle_mut().inner().local_time = new_context.local_time;
1573+
self.handle_mut()
1574+
.inner()
1575+
.transaction_tracker
1576+
.set_local_time(new_context.local_time);
15831577
}
15841578
}
15851579

@@ -1623,7 +1617,7 @@ impl ServiceRuntime for ServiceSyncRuntimeHandle {
16231617
let query_context = QueryContext {
16241618
chain_id: this.chain_id,
16251619
next_block_height: this.height,
1626-
local_time: this.local_time,
1620+
local_time: this.transaction_tracker.local_time(),
16271621
};
16281622
this.push_application(ApplicationStatus {
16291623
caller_id: None,

0 commit comments

Comments
 (0)