Skip to content

Commit 03f677d

Browse files
authored
Merge pull request #411 from Concordium/fix-counter-notify
Fix counter notify
2 parents 49d9c78 + 607c80b commit 03f677d

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

.github/workflows/linter.yml

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,35 @@ jobs:
2626
- concordium-std-derive/Cargo.toml
2727
- concordium-std/Cargo.toml
2828
- concordium-cis2/Cargo.toml
29-
- examples/voting/Cargo.toml
30-
- examples/eSealing/Cargo.toml
29+
- examples/account-signature-checks/Cargo.toml
3130
- examples/auction/Cargo.toml
31+
- examples/bump-alloc-tests/Cargo.toml
3232
- examples/cis2-dynamic-nft/Cargo.toml
3333
- examples/cis2-multi/Cargo.toml
3434
- examples/cis2-multi-royalties/Cargo.toml
3535
- examples/cis2-nft/Cargo.toml
36-
- examples/cis3-nft-sponsored-txs/Cargo.toml
3736
- examples/cis2-wccd/Cargo.toml
37+
- examples/cis3-nft-sponsored-txs/Cargo.toml
38+
- examples/counter-notify/Cargo.toml
3839
- examples/credential-registry/Cargo.toml
40+
- examples/eSealing/Cargo.toml
3941
- examples/factory/Cargo.toml
4042
- examples/fib/Cargo.toml
4143
- examples/icecream/Cargo.toml
4244
- examples/memo/Cargo.toml
4345
- examples/nametoken/Cargo.toml
46+
- examples/offchain-transfers/Cargo.toml
4447
- examples/piggy-bank/part1/Cargo.toml
4548
- examples/piggy-bank/part2/Cargo.toml
4649
- examples/proxy/Cargo.toml
4750
- examples/recorder/Cargo.toml
4851
- examples/signature-verifier/Cargo.toml
49-
- examples/transfer-policy-check/Cargo.toml
50-
- examples/two-step-transfer/Cargo.toml
5152
- examples/smart-contract-upgrade/contract-version1/Cargo.toml
5253
- examples/smart-contract-upgrade/contract-version2/Cargo.toml
53-
- examples/offchain-transfers/Cargo.toml
54-
- examples/account-signature-checks/Cargo.toml
5554
- examples/sponsored-tx-enabled-auction/Cargo.toml
56-
- examples/bump-alloc-tests/Cargo.toml
55+
- examples/transfer-policy-check/Cargo.toml
56+
- examples/two-step-transfer/Cargo.toml
57+
- examples/voting/Cargo.toml
5758

5859
steps:
5960
- name: Checkout sources

examples/counter-notify/src/lib.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ use concordium_std::*;
1313

1414
type State = u64;
1515

16+
#[derive(Serial, Deserial, PartialEq)]
17+
pub enum ReentryOccurance {
18+
NoReentryAttack,
19+
ReentryAttack,
20+
}
21+
1622
#[init(contract = "counter-notify")]
1723
#[inline(always)]
1824
fn contract_init(_ctx: &InitContext, _state_builder: &mut StateBuilder) -> InitResult<State> {
@@ -29,9 +35,13 @@ fn just_increment(_ctx: &ReceiveContext, host: &mut Host<State>) -> ReceiveResul
2935
contract = "counter-notify",
3036
name = "increment-and-notify",
3137
mutable,
32-
parameter = "(ContractAddress, OwnedEntrypointName)"
38+
parameter = "(ContractAddress, OwnedEntrypointName)",
39+
return_value = "ReentryOccurance"
3340
)]
34-
fn increment_and_notify(ctx: &ReceiveContext, host: &mut Host<State>) -> ReceiveResult<bool> {
41+
fn increment_and_notify(
42+
ctx: &ReceiveContext,
43+
host: &mut Host<State>,
44+
) -> ReceiveResult<ReentryOccurance> {
3545
let (contract, entrypoint): (ContractAddress, OwnedEntrypointName) =
3646
ctx.parameter_cursor().get()?;
3747

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

51-
// This will be false if the reentrancy has occurred.
52-
Ok(preinvoke_count == *host.state())
61+
let is_reentry = if preinvoke_count != *host.state() {
62+
ReentryOccurance::ReentryAttack
63+
} else {
64+
ReentryOccurance::NoReentryAttack
65+
};
66+
67+
Ok(is_reentry)
5368
}
5469

5570
////////////////////////////////////////////////////////////////////////////////////////////////

examples/counter-notify/tests/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use concordium_smart_contract_testing::*;
22
use concordium_std_derive::account_address;
3+
use counter_notify::ReentryOccurance;
34

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

114115
// Check that the contract observed the reentrancy attack.
115-
let rv: bool = update.parse_return_value().unwrap();
116-
assert!(rv, "Re-entrancy attack not observed.");
116+
let rv: ReentryOccurance = update.parse_return_value().unwrap();
117+
assert!(rv == ReentryOccurance::ReentryAttack, "Re-entrancy attack not observed.");
117118
}

0 commit comments

Comments
 (0)