mirror of https://github.com/nucypher/nucypher.git
Parametrize test_snapshot with values covering different byte ranges
parent
5c22845d9e
commit
a713d44f03
|
@ -283,12 +283,18 @@ contract ReEncryptionValidatorMock {
|
||||||
*/
|
*/
|
||||||
contract SnapshotMock {
|
contract SnapshotMock {
|
||||||
|
|
||||||
|
// Helpers
|
||||||
uint128[] public history;
|
uint128[] public history;
|
||||||
|
|
||||||
function length() public view returns(uint256){
|
function length() public view returns(uint256){
|
||||||
return history.length;
|
return history.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteHistory() public {
|
||||||
|
delete history;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mock functions
|
||||||
function encodeSnapshot(uint32 _time, uint96 _value) public pure returns(uint128) {
|
function encodeSnapshot(uint32 _time, uint96 _value) public pure returns(uint128) {
|
||||||
return Snapshot.encodeSnapshot(_time, _value);
|
return Snapshot.encodeSnapshot(_time, _value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ You should have received a copy of the GNU Affero General Public License
|
||||||
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from web3 import Web3
|
from web3 import Web3
|
||||||
|
|
||||||
|
@ -25,10 +27,20 @@ def snapshot(testerchain, deploy_contract):
|
||||||
return contract
|
return contract
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.slow
|
timestamps = (0x00000001,
|
||||||
def test_snapshot(testerchain, snapshot):
|
0x00001000,
|
||||||
|
0xff000000,
|
||||||
|
0xffff0001)
|
||||||
|
|
||||||
assert snapshot.functions.length().call() == 0
|
values = (0x000000000000000000000000,
|
||||||
|
0x000000000001000000000001,
|
||||||
|
0xff0000000000000000000000,
|
||||||
|
0xffff00000000000000000001)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.slow
|
||||||
|
@pytest.mark.parametrize('block_number, value', itertools.product(timestamps, values))
|
||||||
|
def test_snapshot(testerchain, snapshot, block_number, value):
|
||||||
|
|
||||||
# Testing basic encoding and decoding of snapshots
|
# Testing basic encoding and decoding of snapshots
|
||||||
def encode(_time, _value):
|
def encode(_time, _value):
|
||||||
|
@ -37,10 +49,6 @@ def test_snapshot(testerchain, snapshot):
|
||||||
def decode(_snapshot):
|
def decode(_snapshot):
|
||||||
return snapshot.functions.decodeSnapshot(_snapshot).call()
|
return snapshot.functions.decodeSnapshot(_snapshot).call()
|
||||||
|
|
||||||
# TODO: Parametrize this test with different types of inputs
|
|
||||||
block_number = 42
|
|
||||||
value = int(3.99e27) + 1234123412341234
|
|
||||||
|
|
||||||
encoded_snapshot = encode(block_number, value)
|
encoded_snapshot = encode(block_number, value)
|
||||||
assert decode(encoded_snapshot) == [block_number, value]
|
assert decode(encoded_snapshot) == [block_number, value]
|
||||||
expected_encoded_snapshot_as_bytes = block_number.to_bytes(4, "big") + value.to_bytes(12, "big")
|
expected_encoded_snapshot_as_bytes = block_number.to_bytes(4, "big") + value.to_bytes(12, "big")
|
||||||
|
@ -49,7 +57,7 @@ def test_snapshot(testerchain, snapshot):
|
||||||
# Testing adding new snapshots
|
# Testing adding new snapshots
|
||||||
account = testerchain.etherbase_account
|
account = testerchain.etherbase_account
|
||||||
|
|
||||||
data = [(block_number + i*10, value - i) for i in range(10)]
|
data = [(block_number + i*10, value + i) for i in range(10)]
|
||||||
for i, (block_i, value_i) in enumerate(data):
|
for i, (block_i, value_i) in enumerate(data):
|
||||||
tx = snapshot.functions.addSnapshot(block_i, value_i).transact({'from': account})
|
tx = snapshot.functions.addSnapshot(block_i, value_i).transact({'from': account})
|
||||||
receipt = testerchain.wait_for_receipt(tx)
|
receipt = testerchain.wait_for_receipt(tx)
|
||||||
|
@ -74,4 +82,6 @@ def test_snapshot(testerchain, snapshot):
|
||||||
last_block, last_value = snapshot.functions.lastSnapshot().call()
|
last_block, last_value = snapshot.functions.lastSnapshot().call()
|
||||||
assert snapshot.functions.getValueAt(last_block + 100).call() == last_value
|
assert snapshot.functions.getValueAt(last_block + 100).call() == last_value
|
||||||
|
|
||||||
|
# Clear history for next test
|
||||||
|
tx = snapshot.functions.deleteHistory().transact({'from': account})
|
||||||
|
testerchain.wait_for_receipt(tx)
|
||||||
|
|
Loading…
Reference in New Issue