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

Fix counter notify #411

Merged
merged 5 commits into from
Apr 15, 2024
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
21 changes: 11 additions & 10 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,35 @@ jobs:
- concordium-std-derive/Cargo.toml
- concordium-std/Cargo.toml
- concordium-cis2/Cargo.toml
- examples/voting/Cargo.toml
abizjak marked this conversation as resolved.
Show resolved Hide resolved
- examples/eSealing/Cargo.toml
- examples/account-signature-checks/Cargo.toml
- examples/auction/Cargo.toml
- examples/bump-alloc-tests/Cargo.toml
- examples/cis2-dynamic-nft/Cargo.toml
- examples/cis2-multi/Cargo.toml
- examples/cis2-multi-royalties/Cargo.toml
- examples/cis2-nft/Cargo.toml
- examples/cis3-nft-sponsored-txs/Cargo.toml
- examples/cis2-wccd/Cargo.toml
- examples/cis3-nft-sponsored-txs/Cargo.toml
- examples/counter-notify/Cargo.toml
- examples/credential-registry/Cargo.toml
- examples/eSealing/Cargo.toml
- examples/factory/Cargo.toml
- examples/fib/Cargo.toml
- examples/icecream/Cargo.toml
- examples/memo/Cargo.toml
- examples/nametoken/Cargo.toml
- examples/offchain-transfers/Cargo.toml
- examples/piggy-bank/part1/Cargo.toml
- examples/piggy-bank/part2/Cargo.toml
- examples/proxy/Cargo.toml
- examples/recorder/Cargo.toml
- examples/signature-verifier/Cargo.toml
- examples/transfer-policy-check/Cargo.toml
- examples/two-step-transfer/Cargo.toml
- examples/smart-contract-upgrade/contract-version1/Cargo.toml
- examples/smart-contract-upgrade/contract-version2/Cargo.toml
- examples/offchain-transfers/Cargo.toml
- examples/account-signature-checks/Cargo.toml
- examples/sponsored-tx-enabled-auction/Cargo.toml
- examples/bump-alloc-tests/Cargo.toml
- examples/transfer-policy-check/Cargo.toml
- examples/two-step-transfer/Cargo.toml
- examples/voting/Cargo.toml

steps:
- name: Checkout sources
Expand Down Expand Up @@ -352,7 +353,7 @@ jobs:
sed -i "s/root/Concordium <[email protected]>/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/{version = \"10.0\", default-features = false}/{path = \"..\/..\/concordium-std\", default-features = false}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/{version = \"6.1\", default-features = false}/{path = \"..\/..\/concordium-cis2\", default-features = false}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-smart-contract-testing = \"4.0\"/concordium-smart-contract-testing = {path = \"..\/..\/contract-testing\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-smart-contract-testing = \"4.2\"/concordium-smart-contract-testing = {path = \"..\/..\/contract-testing\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-std-derive = \"6.0\"/concordium-std-derive = {path = \"..\/..\/concordium-std-derive\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
diff ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml examples/cis2-nft/Cargo.toml
diff ${{ runner.temp }}/$PROJECT_NAME/src/lib.rs examples/cis2-nft/src/lib.rs
Expand Down Expand Up @@ -401,7 +402,7 @@ jobs:
sed -i "s/root/Concordium <[email protected]>/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/version = \"10.0\", default-features = false/path = \"..\/..\/concordium-std\", version = \"10.0\", default-features = false/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/version = \"6.1\", default-features = false/path = \"..\/..\/concordium-cis2\", version = \"6.1\", default-features = false/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-smart-contract-testing = \"4.0\"/concordium-smart-contract-testing = {path = \"..\/..\/contract-testing\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-smart-contract-testing = \"4.2\"/concordium-smart-contract-testing = {path = \"..\/..\/contract-testing\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
sed -i "s/concordium-std-derive = \"6.0\"/concordium-std-derive = {path = \"..\/..\/concordium-std-derive\"}/g" ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml
diff ${{ runner.temp }}/$PROJECT_NAME/Cargo.toml examples/credential-registry/Cargo.toml
diff ${{ runner.temp }}/$PROJECT_NAME/src/lib.rs examples/credential-registry/src/lib.rs
Expand Down
2 changes: 1 addition & 1 deletion concordium-rust-sdk
4 changes: 3 additions & 1 deletion contract-testing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Unreleased changes

## 4.2.0

- Add support for querying the module reference and contract name of an instance.
This functionality is only available on-chain from protocol version 7.
- Bump minimum supported Rust version (MSRV) to `1.72`.
- Bump minimum supported Rust version (MSRV) to `1.73`.

## 4.1.0

Expand Down
23 changes: 19 additions & 4 deletions examples/counter-notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use concordium_std::*;

type State = u64;

#[derive(Serial, Deserial, PartialEq)]
pub enum ReentryOccurance {
NoReentryAttack,
ReentryAttack,
}

#[init(contract = "counter-notify")]
#[inline(always)]
fn contract_init(_ctx: &InitContext, _state_builder: &mut StateBuilder) -> InitResult<State> {
Expand All @@ -29,9 +35,13 @@ fn just_increment(_ctx: &ReceiveContext, host: &mut Host<State>) -> ReceiveResul
contract = "counter-notify",
name = "increment-and-notify",
mutable,
parameter = "(ContractAddress, OwnedEntrypointName)"
parameter = "(ContractAddress, OwnedEntrypointName)",
return_value = "ReentryOccurance"
)]
fn increment_and_notify(ctx: &ReceiveContext, host: &mut Host<State>) -> ReceiveResult<bool> {
fn increment_and_notify(
ctx: &ReceiveContext,
host: &mut Host<State>,
) -> ReceiveResult<ReentryOccurance> {
let (contract, entrypoint): (ContractAddress, OwnedEntrypointName) =
ctx.parameter_cursor().get()?;

Expand All @@ -48,8 +58,13 @@ fn increment_and_notify(ctx: &ReceiveContext, host: &mut Host<State>) -> Receive
)
.unwrap_abort();

// This will be false if the reentrancy has occurred.
Ok(preinvoke_count == *host.state())
let is_reentry = if preinvoke_count != *host.state() {
ReentryOccurance::ReentryAttack
} else {
ReentryOccurance::NoReentryAttack
};

Ok(is_reentry)
}

////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 3 additions & 2 deletions examples/counter-notify/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use concordium_smart_contract_testing::*;
use concordium_std_derive::account_address;
use counter_notify::ReentryOccurance;

const ACC_0: AccountAddress =
account_address!("2wkBET2rRgE8pahuaczxKbmv7ciehqsne57F9gtzf1PVdr2VP3");
Expand Down Expand Up @@ -112,6 +113,6 @@ fn tests() {
] if rcv_name_1 == "counter-notify.just-increment" && rcv_name_2 == "reentrancy-attacker.call-just-increment" && rcv_name_3 == "counter-notify.increment-and-notify"));

// Check that the contract observed the reentrancy attack.
let rv: bool = update.parse_return_value().unwrap();
assert!(rv, "Re-entrancy attack not observed.");
let rv: ReentryOccurance = update.parse_return_value().unwrap();
assert!(rv == ReentryOccurance::ReentryAttack, "Re-entrancy attack not observed.");
}
2 changes: 1 addition & 1 deletion templates/cis2-nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ concordium-std = {version = "10.0", default-features = false}
concordium-cis2 = {version = "6.1", default-features = false}

[dev-dependencies]
concordium-smart-contract-testing = "4.0"
concordium-smart-contract-testing = "4.2"
concordium-std-derive = "6.0"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion templates/credential-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ concordium-cis2 = {version = "6.1", default-features = false}
quickcheck = {version = "1"}

[dev-dependencies]
concordium-smart-contract-testing = "4.0"
concordium-smart-contract-testing = "4.2"
concordium-std-derive = "6.0"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion templates/default/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bump_alloc = ["concordium-std/bump_alloc"]
concordium-std = {version = "10.0", default-features = false}

[dev-dependencies]
concordium-smart-contract-testing = "4.0"
concordium-smart-contract-testing = "4.2"

[lib]
crate-type=["cdylib", "rlib"]
Expand Down
Loading