Skip to content

Commit bac4cce

Browse files
committed
add stuff
1 parent 717d701 commit bac4cce

File tree

5 files changed

+145
-41
lines changed

5 files changed

+145
-41
lines changed

target_chains/ethereum/contracts/contracts/entropy/Entropy.sol

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ abstract contract Entropy is IEntropy, EntropyState {
251251

252252
req.blockNumber = SafeCast.toUint64(block.number);
253253
req.useBlockhash = useBlockhash;
254-
req.status = isRequestWithCallback ? 1 : 0;
254+
req.status = isRequestWithCallback
255+
? EntropyConstants.STATUS_CALLBACK_NOT_STARTED
256+
: EntropyConstants.STATUS_NO_CALLBACK;
255257
}
256258

257259
// As a user, request a random number from `provider`. Prior to calling this method, the user should
@@ -471,7 +473,10 @@ abstract contract Entropy is IEntropy, EntropyState {
471473
sequenceNumber
472474
);
473475

474-
if (!(req.status == EntropyConstants.STATUS_CALLBACK_NOT_STARTED || req.status == EntropyConstants.STATUS_CALLBACK_FAILED)) {
476+
if (
477+
!(req.status == EntropyConstants.STATUS_CALLBACK_NOT_STARTED ||
478+
req.status == EntropyConstants.STATUS_CALLBACK_FAILED)
479+
) {
475480
revert EntropyErrors.InvalidRevealCall();
476481
}
477482

@@ -485,19 +490,18 @@ abstract contract Entropy is IEntropy, EntropyState {
485490

486491
address callAddress = req.requester;
487492

488-
// blockNumberOrGasLimit holds the gas limit in the callback case.
489-
// If the gas limit is 0, then the provider hasn't configured their default limit,
490-
// so we default to the prior entropy flow (where there is no failure state).
491-
// Similarly, if the request has already failed, we fall back to the prior flow so that
492-
// recovery attempts can provide more gas / directly see the revert reason.
493+
// Requests that haven't been invoked yet will be invoked safely (catching reverts), and
494+
// any reverts will be reported as an event. Any failing requests move to a failure state
495+
// at which point they can be recovered. The recovery flow invokes the callback directly
496+
// (no catching errors) which allows callers to easily see the revert reason.
493497
if (req.status == EntropyConstants.STATUS_CALLBACK_NOT_STARTED) {
494498
req.status = EntropyConstants.STATUS_CALLBACK_IN_PROGRESS;
495499
bool success;
496500
bytes memory ret;
497501
(success, ret) = callAddress.excessivelySafeCall(
498502
gasleft(), // TODO: providers need to be able to configure this in the future.
499503
0,
500-
256,
504+
256, // copy at most 256 bytes of the return value into ret.
501505
abi.encodeWithSelector(
502506
IEntropyConsumer._entropyCallback.selector,
503507
sequenceNumber,
@@ -517,10 +521,14 @@ abstract contract Entropy is IEntropy, EntropyState {
517521
);
518522
clearRequest(provider, sequenceNumber);
519523
} else if (ret.length > 0) {
524+
// Callback reverted for some reason that is *not* out-of-gas.
520525
emit CallbackFailed(
521526
provider,
522527
req.requester,
523528
sequenceNumber,
529+
userRandomNumber,
530+
providerRevelation,
531+
randomNumber,
524532
ret
525533
);
526534
req.status = EntropyConstants.STATUS_CALLBACK_FAILED;
@@ -530,6 +538,7 @@ abstract contract Entropy is IEntropy, EntropyState {
530538
require(false, "provider needs to send more gas");
531539
}
532540
} else {
541+
// This case uses the checks-effects-interactions pattern to avoid reentry attacks
533542
emit RevealedWithCallback(
534543
req,
535544
userRandomNumber,
@@ -545,16 +554,11 @@ abstract contract Entropy is IEntropy, EntropyState {
545554
len := extcodesize(callAddress)
546555
}
547556
if (len != 0) {
548-
// Setting the reentry guard here isn't strictly necessary because we've cleared the request above
549-
// (using the check-effects-interactions pattern). However, it seems like a good measure for consistency
550-
// across the two cases.
551-
req.status = EntropyConstants.STATUS_CALLBACK_IN_PROGRESS;
552557
IEntropyConsumer(callAddress)._entropyCallback(
553558
sequenceNumber,
554559
provider,
555560
randomNumber
556561
);
557-
req.status = EntropyConstants.STATUS_CALLBACK_NOT_STARTED;
558562
}
559563
}
560564
}

target_chains/ethereum/entropy_sdk/solidity/EntropyConstants.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
library EntropyConstants {
32
// Status values for Request.status //
43
// not a request with callback

target_chains/ethereum/entropy_sdk/solidity/EntropyEvents.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ interface EntropyEvents {
3333
address indexed provider,
3434
address indexed requestor,
3535
uint64 indexed sequenceNumber,
36+
bytes32 userRandomNumber,
37+
bytes32 providerRevelation,
38+
bytes32 randomNumber,
3639
bytes errorCode
3740
);
3841

target_chains/ethereum/entropy_sdk/solidity/abis/EntropyEvents.json

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
[
2+
{
3+
"anonymous": false,
4+
"inputs": [
5+
{
6+
"indexed": true,
7+
"internalType": "address",
8+
"name": "provider",
9+
"type": "address"
10+
},
11+
{
12+
"indexed": true,
13+
"internalType": "address",
14+
"name": "requestor",
15+
"type": "address"
16+
},
17+
{
18+
"indexed": true,
19+
"internalType": "uint64",
20+
"name": "sequenceNumber",
21+
"type": "uint64"
22+
},
23+
{
24+
"indexed": false,
25+
"internalType": "bytes32",
26+
"name": "userRandomNumber",
27+
"type": "bytes32"
28+
},
29+
{
30+
"indexed": false,
31+
"internalType": "bytes32",
32+
"name": "providerRevelation",
33+
"type": "bytes32"
34+
},
35+
{
36+
"indexed": false,
37+
"internalType": "bytes32",
38+
"name": "randomNumber",
39+
"type": "bytes32"
40+
},
41+
{
42+
"indexed": false,
43+
"internalType": "bytes",
44+
"name": "errorCode",
45+
"type": "bytes"
46+
}
47+
],
48+
"name": "CallbackFailed",
49+
"type": "event"
50+
},
251
{
352
"anonymous": false,
453
"inputs": [
@@ -215,9 +264,9 @@
215264
"type": "bool"
216265
},
217266
{
218-
"internalType": "bool",
219-
"name": "isRequestWithCallback",
220-
"type": "bool"
267+
"internalType": "uint8",
268+
"name": "status",
269+
"type": "uint8"
221270
}
222271
],
223272
"indexed": false,
@@ -294,9 +343,9 @@
294343
"type": "bool"
295344
},
296345
{
297-
"internalType": "bool",
298-
"name": "isRequestWithCallback",
299-
"type": "bool"
346+
"internalType": "uint8",
347+
"name": "status",
348+
"type": "uint8"
300349
}
301350
],
302351
"indexed": false,
@@ -349,9 +398,9 @@
349398
"type": "bool"
350399
},
351400
{
352-
"internalType": "bool",
353-
"name": "isRequestWithCallback",
354-
"type": "bool"
401+
"internalType": "uint8",
402+
"name": "status",
403+
"type": "uint8"
355404
}
356405
],
357406
"indexed": false,
@@ -428,9 +477,9 @@
428477
"type": "bool"
429478
},
430479
{
431-
"internalType": "bool",
432-
"name": "isRequestWithCallback",
433-
"type": "bool"
480+
"internalType": "uint8",
481+
"name": "status",
482+
"type": "uint8"
434483
}
435484
],
436485
"indexed": false,

target_chains/ethereum/entropy_sdk/solidity/abis/IEntropy.json

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
[
2+
{
3+
"anonymous": false,
4+
"inputs": [
5+
{
6+
"indexed": true,
7+
"internalType": "address",
8+
"name": "provider",
9+
"type": "address"
10+
},
11+
{
12+
"indexed": true,
13+
"internalType": "address",
14+
"name": "requestor",
15+
"type": "address"
16+
},
17+
{
18+
"indexed": true,
19+
"internalType": "uint64",
20+
"name": "sequenceNumber",
21+
"type": "uint64"
22+
},
23+
{
24+
"indexed": false,
25+
"internalType": "bytes32",
26+
"name": "userRandomNumber",
27+
"type": "bytes32"
28+
},
29+
{
30+
"indexed": false,
31+
"internalType": "bytes32",
32+
"name": "providerRevelation",
33+
"type": "bytes32"
34+
},
35+
{
36+
"indexed": false,
37+
"internalType": "bytes32",
38+
"name": "randomNumber",
39+
"type": "bytes32"
40+
},
41+
{
42+
"indexed": false,
43+
"internalType": "bytes",
44+
"name": "errorCode",
45+
"type": "bytes"
46+
}
47+
],
48+
"name": "CallbackFailed",
49+
"type": "event"
50+
},
251
{
352
"anonymous": false,
453
"inputs": [
@@ -215,9 +264,9 @@
215264
"type": "bool"
216265
},
217266
{
218-
"internalType": "bool",
219-
"name": "isRequestWithCallback",
220-
"type": "bool"
267+
"internalType": "uint8",
268+
"name": "status",
269+
"type": "uint8"
221270
}
222271
],
223272
"indexed": false,
@@ -294,9 +343,9 @@
294343
"type": "bool"
295344
},
296345
{
297-
"internalType": "bool",
298-
"name": "isRequestWithCallback",
299-
"type": "bool"
346+
"internalType": "uint8",
347+
"name": "status",
348+
"type": "uint8"
300349
}
301350
],
302351
"indexed": false,
@@ -349,9 +398,9 @@
349398
"type": "bool"
350399
},
351400
{
352-
"internalType": "bool",
353-
"name": "isRequestWithCallback",
354-
"type": "bool"
401+
"internalType": "uint8",
402+
"name": "status",
403+
"type": "uint8"
355404
}
356405
],
357406
"indexed": false,
@@ -428,9 +477,9 @@
428477
"type": "bool"
429478
},
430479
{
431-
"internalType": "bool",
432-
"name": "isRequestWithCallback",
433-
"type": "bool"
480+
"internalType": "uint8",
481+
"name": "status",
482+
"type": "uint8"
434483
}
435484
],
436485
"indexed": false,
@@ -735,9 +784,9 @@
735784
"type": "bool"
736785
},
737786
{
738-
"internalType": "bool",
739-
"name": "isRequestWithCallback",
740-
"type": "bool"
787+
"internalType": "uint8",
788+
"name": "status",
789+
"type": "uint8"
741790
}
742791
],
743792
"internalType": "struct EntropyStructs.Request",

0 commit comments

Comments
 (0)