Skip to content

Commit 1cc7120

Browse files
authored
Allow configuring zero delay (#1541)
* Fix genDelay to use 0 as lower bound for random delay genDelay used getRandomR(1, maxDelay) which caused undefined behavior when maxDelay was 0 (inverted bounds). In practice this leaked delay=1 even when the user configured maxTimeDelay or maxBlockDelay to 0. Change the lower bound to 0, which fixes the inverted bounds issue and also allows zero delays naturally (consecutive txs in same block/time), consistent with how genValue already uses getRandomR(0, ...). * tests: add no-delay test for maxTimeDelay/maxBlockDelay = 0 Test that with maxTimeDelay: 0 and maxBlockDelay: 0, block numbers and timestamps remain consistent across transactions (no delay leaks).
1 parent 6e589d4 commit 1cc7120

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

lib/Echidna/Transaction.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ genTx world deployedContracts = do
8989
genDelay :: MonadRandom m => W256 -> Set W256 -> m W256
9090
genDelay mv ds =
9191
join $ oftenUsually fromDict randValue
92-
where randValue = fromIntegral <$> getRandomR (1 :: Integer, fromIntegral mv)
92+
where randValue = fromIntegral <$> getRandomR (0 :: Integer, fromIntegral mv)
9393
fromDict = (`mod` (mv + 1)) <$> rElem' ds
9494

9595
genValue

src/test/Tests/Integration.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ integrationTests = testGroup "Solidity Integration Testing"
7070
, testContract "basic/delay.sol" Nothing
7171
[ ("echidna_block_number passed", solved "echidna_block_number")
7272
, ("echidna_timestamp passed", solved "echidna_timestamp") ]
73+
, testContract "basic/no-delay.sol" (Just "basic/no-delay.yaml")
74+
[ ("echidna_block_number failed", passed "echidna_block_number")
75+
, ("echidna_timestamp failed", passed "echidna_timestamp") ]
7376
, testContractV "basic/immutable.sol" (Just (>= solcV (0,6,0))) Nothing
7477
[ ("echidna_test passed", solved "echidna_test") ]
7578
, testContractV "basic/immutable-2.sol" (Just (>= solcV (0,6,0))) Nothing

tests/solidity/basic/no-delay.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
contract C {
2+
uint blockA;
3+
uint timeA;
4+
uint blockB;
5+
uint timeB;
6+
7+
constructor() public {
8+
blockA = block.number;
9+
timeA = block.timestamp;
10+
blockB = block.number;
11+
timeB = block.timestamp;
12+
}
13+
14+
function f() public {
15+
blockA = block.number;
16+
timeA = block.timestamp;
17+
}
18+
19+
function g() public {
20+
blockB = block.number;
21+
timeB = block.timestamp;
22+
}
23+
24+
function echidna_block_number() public returns (bool) {
25+
return blockA == blockB;
26+
}
27+
28+
function echidna_timestamp() public returns (bool) {
29+
return timeA == timeB;
30+
}
31+
}

tests/solidity/basic/no-delay.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
maxTimeDelay: 0
2+
maxBlockDelay: 0

0 commit comments

Comments
 (0)