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

Make HoldBound errors reachable #1276

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/binaries/gasless-wasting/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/gasless-wasting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/proxy-with-gas/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/proxy-with-gas/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/proxy/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/waiting-proxy/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
2 changes: 1 addition & 1 deletion examples/binaries/waiting-proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of Gear.

// Copyright (C) 2021 Gear Technologies Inc.
// Copyright (C) 2022 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
Expand Down
6 changes: 5 additions & 1 deletion pallets/gear/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ where

// Validating duration.
if hold.expected_duration().is_zero() {
unreachable!("Failed to figure out correct wait hold bound");
// TODO: Replace with unreachable call after:
// - `HoldBound` safety usage stabilized;
// - Issue #1173 solved.
log::error!("Failed to figure out correct wait hold bound");
return;
techraed marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: remove, once duration-control added inside programs (#1173).
Expand Down
17 changes: 15 additions & 2 deletions pallets/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ pub struct GasInfo {
pub reserved: u64,
/// Contains number of gas burned during message processing.
pub burned: u64,
/// The value may be returned if a program happens to be executed the second or next time in a block.
/// The value may be returned if a program happens to be executed
/// the second or next time in a block.
pub may_be_returned: u64,
/// Was the message placed into waitlist at the end of calculating.
///
/// This flag shows, that `min_limit` makes sense and have some guarantees
/// only before insertion into waitlist.
pub waited: bool,
}

#[frame_support::pallet]
Expand Down Expand Up @@ -632,7 +638,10 @@ pub mod pallet {
) -> Result<GasInfo, String> {
log::debug!("\n===== CALCULATE GAS INFO =====\n");
log::debug!("\n--- FIRST TRY ---\n");
let GasInfo { min_limit, .. } = Self::run_with_ext_copy(|| {

let GasInfo {
min_limit, waited, ..
} = Self::run_with_ext_copy(|| {
let initial_gas = BlockGasLimitOf::<T>::get();
Self::calculate_gas_info_impl(
source,
Expand Down Expand Up @@ -670,6 +679,7 @@ pub mod pallet {
reserved,
burned,
may_be_returned,
waited,
},
)
.map_err(|e| {
Expand Down Expand Up @@ -926,11 +936,14 @@ pub mod pallet {
}
}

let waited = WaitlistOf::<T>::contains(&main_program_id, &main_message_id);

Ok(GasInfo {
min_limit,
reserved,
burned,
may_be_returned,
waited,
})
}

Expand Down
59 changes: 59 additions & 0 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,47 @@ fn claim_value_works() {
})
}

#[test]
fn uninitialized_program_zero_gas() {
use demo_init_wait::WASM_BINARY;

init_logger();
new_test_ext().execute_with(|| {
System::reset_events();

assert_ok!(GearPallet::<Test>::upload_program(
Origin::signed(USER_1),
WASM_BINARY.to_vec(),
vec![],
Vec::new(),
50_000_000_000u64,
0u128
));

let init_message_id = utils::get_last_message_id();
let program_id = utils::get_last_program_id();

assert!(!Gear::is_initialized(program_id));
assert!(!Gear::is_terminated(program_id));

run_to_block(2, None);

assert!(!Gear::is_initialized(program_id));
assert!(!Gear::is_terminated(program_id));
assert!(WaitlistOf::<Test>::contains(&program_id, &init_message_id));

assert_ok!(GearPallet::<Test>::send_message(
Origin::signed(1),
program_id,
vec![],
0, // that may trigger unreachable code
0,
));

run_to_block(3, None);
})
}

#[test]
fn distributor_initialize() {
init_logger();
Expand Down Expand Up @@ -4099,6 +4140,24 @@ fn test_reply_to_terminated_program() {
})
}

#[test]
breathx marked this conversation as resolved.
Show resolved Hide resolved
fn calculate_gas_info_for_wait_dispatch_works() {
init_logger();
new_test_ext().execute_with(|| {
// Test should still be valid once #1173 solved.
let GasInfo { waited, .. } = Gear::calculate_gas_info(
USER_1.into_origin(),
HandleKind::Init(demo_init_wait::WASM_BINARY.to_vec()),
EMPTY_PAYLOAD.to_vec(),
0,
true,
)
.unwrap();

assert!(waited);
});
}

#[test]
fn cascading_messages_with_value_do_not_overcharge() {
init_logger();
Expand Down
2 changes: 1 addition & 1 deletion runtime/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("gear-node"),
apis: RUNTIME_API_VERSIONS,
authoring_version: 1,
spec_version: 1580,
spec_version: 1590,
impl_version: 1,
transaction_version: 1,
state_version: 1,
Expand Down
2 changes: 1 addition & 1 deletion runtime/vara/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// The version of the runtime specification. A full node will not attempt to use its native
// runtime in substitute for the on-chain Wasm runtime unless all of `spec_name`,
// `spec_version`, and `authoring_version` are the same between Wasm and native.
spec_version: 1580,
spec_version: 1590,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down