Skip to content

Commit

Permalink
Audit/randomness/status management (#86)
Browse files Browse the repository at this point in the history
* audit: cancellation update

* fix: fmt

* audit: adding status logics + tests

* fix: spell

* fix: spell

* fix: remove parameter
  • Loading branch information
JordyRo1 authored Nov 16, 2023
1 parent 1a036c1 commit a52b98f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/randomness/randomness.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ enum RequestStatus {
RECEIVED: (),
FULFILLED: (),
CANCELLED: (),
EXCESSIVE_GAS_NEEDED: (),
ERRORED: (),
}

#[starknet::interface]
Expand Down Expand Up @@ -133,6 +131,10 @@ mod Randomness {
status: RequestStatus
) {
assert_only_admin();
let status = self.request_status.read((requestor_address, request_id));
//The management is handled by the admin contract, he cannot change the status of a fulfilled or cancelled request
assert(status != RequestStatus::FULFILLED(()), 'request already fulfilled');
assert(status != RequestStatus::CANCELLED(()), 'request already cancelled');
self.request_status.write((requestor_address, request_id), status);
return ();
}
Expand Down Expand Up @@ -203,6 +205,11 @@ mod Randomness {
let stored_hash_ = self.request_hash.read((caller_address, request_id));
assert(_hashed_value == stored_hash_, 'invalid request configuration');
assert(requestor_address == caller_address, 'invalid request owner');
let status = self.request_status.read((requestor_address, request_id));
assert(status != RequestStatus::FULFILLED(()), 'request already fulfilled');
self
.request_status
.write((requestor_address, request_id), RequestStatus::CANCELLED(()));
self
.request_status
.write((requestor_address, request_id), RequestStatus::CANCELLED(()));
Expand All @@ -216,7 +223,6 @@ mod Randomness {
}
)
);

return ();
}

Expand All @@ -232,7 +238,9 @@ mod Randomness {
proof: Span<felt252>,
) {
assert_only_admin();

let status = self.request_status.read((requestor_address, request_id));
assert(status != RequestStatus::FULFILLED(()), 'request already fulfilled');
assert(status != RequestStatus::CANCELLED(()), 'request already cancelled');
let _hashed_value = hash_request(
request_id,
requestor_address,
Expand Down
84 changes: 84 additions & 0 deletions src/tests/test_randomness.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,90 @@ fn test_randomness_cancellation() {
assert(request_status == RequestStatus::CANCELLED, 'wrong request status');
}


#[test]
#[should_panic(expected: ('request already fulfilled', 'ENTRYPOINT_FAILED'))]
#[available_gas(100000000000)]
fn test_cancel_random_request_should_fail_if_fulflled() {
let requestor_address = contract_address_const::<0x1234>();
testing::set_contract_address(requestor_address);
let (randomness, example_randomness, randomness_address, example_randomness_address) = setup();
let seed = 1;
let callback_gas_limit = 0;
let callback_address = example_randomness_address;
let publish_delay = 1;
let num_words = 1;
let block_number = info::get_block_number();
let request_id = randomness
.request_random(seed, callback_address, callback_gas_limit, publish_delay, num_words);
let mut random_words = ArrayTrait::<felt252>::new();
let res = example_randomness.get_last_random();
assert(res == 0, 'wrong random');
let random_words = array![10000];
let block_hash = 123456789;
let proof = array![100, 200, 300];
let minimum_block_number = 2;
testing::set_block_number(4);
randomness
.submit_random(
0,
requestor_address,
seed,
1,
callback_address,
callback_gas_limit,
random_words.span(),
proof.span()
);
let res = example_randomness.get_last_random();
assert(res == 10000, 'wrong random');
randomness
.cancel_random_request(
0, requestor_address, seed, 1, callback_address, callback_gas_limit, 1,
);
}

#[test]
#[should_panic(expected: ('request already cancelled', 'ENTRYPOINT_FAILED'))]
#[available_gas(100000000000)]
fn test_submit_random_should_fail_if_request_cancelled() {
let requestor_address = contract_address_const::<0x1234>();
testing::set_contract_address(requestor_address);
let (randomness, example_randomness, randomness_address, example_randomness_address) = setup();
let seed = 1;
let callback_gas_limit = 0;
let callback_address = example_randomness_address;
let publish_delay = 1;
let num_words = 1;
let block_number = info::get_block_number();
let request_id = randomness
.request_random(seed, callback_address, callback_gas_limit, publish_delay, num_words);
let mut random_words = ArrayTrait::<felt252>::new();
let res = example_randomness.get_last_random();
assert(res == 0, 'wrong random');
let random_words = array![10000];
let block_hash = 123456789;
let proof = array![100, 200, 300];
let minimum_block_number = 2;
randomness
.cancel_random_request(
0, requestor_address, seed, 1, callback_address, callback_gas_limit, 1,
);
testing::set_block_number(4);
randomness
.submit_random(
0,
requestor_address,
seed,
1,
callback_address,
callback_gas_limit,
random_words.span(),
proof.span()
);
}


#[test]
#[available_gas(20000000000)]
fn test_randomness_id_incrementation() {
Expand Down

0 comments on commit a52b98f

Please sign in to comment.