Skip to content

Commit

Permalink
Merge pull request #87 from NIC619/add_log_emission_tests
Browse files Browse the repository at this point in the history
Add log emission tests
  • Loading branch information
NIC619 committed May 14, 2018
2 parents caa4b0a + 94523ad commit cb8f5b5
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tests/contract/test_add_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ def test_add_header_wrong_period(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that collation record of shard 0 has not been updated and transaction consume all gas
# and no logs has been emitted
assert smc_handler.records_updated_period(0) == 0
assert smc_handler.get_collation_chunk_root(1, 0) == BLANK_CHUNK_ROOT
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0

# Second attempt to add collation record with wrong period specified
tx_hash = smc_handler.add_header(
Expand All @@ -87,9 +89,11 @@ def test_add_header_wrong_period(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that collation record of shard 0 has not been updated and transaction consume all gas
# and no logs has been emitted
assert smc_handler.records_updated_period(0) == 0
assert smc_handler.get_collation_chunk_root(1, 0) == BLANK_CHUNK_ROOT
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0

# Add correct collation record
smc_handler.add_header(1, 0, CHUNK_ROOT_1_0, private_key=NotaryAccount(0).private_key)
Expand Down Expand Up @@ -121,9 +125,11 @@ def test_add_header_wrong_shard(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that collation record of shard 0 has not been updated and transaction consume all gas
# and no logs has been emitted
assert smc_handler.records_updated_period(0) == 0
assert smc_handler.get_collation_chunk_root(1, 0) == BLANK_CHUNK_ROOT
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0

# Second attempt to add collation record with illegal shard_id specified
tx_hash = smc_handler.add_header(
Expand All @@ -134,9 +140,11 @@ def test_add_header_wrong_shard(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that collation record of shard 0 has not been updated and transaction consume all gas
# and no logs has been emitted
assert smc_handler.records_updated_period(0) == 0
assert smc_handler.get_collation_chunk_root(1, 0) == BLANK_CHUNK_ROOT
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0

# Add correct collation record
smc_handler.add_header(1, 0, CHUNK_ROOT_1_0, private_key=NotaryAccount(0).private_key)
Expand Down Expand Up @@ -171,8 +179,9 @@ def test_double_add_header(smc_handler): # noqa: F811
private_key=NotaryAccount(0).private_key
)
mine(web3, 1)
# Check that transaction consume all gas
# Check that transaction consume all gas and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0

# Attempt to add collation record again with different chunk root
tx_hash = smc_handler.add_header(
Expand All @@ -183,6 +192,8 @@ def test_double_add_header(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that collation record of shard 0 remains the same and transaction consume all gas
# and no logs has been emitted
assert smc_handler.records_updated_period(0) == 1
assert smc_handler.get_collation_chunk_root(1, 0) == CHUNK_ROOT_1_0
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
80 changes: 80 additions & 0 deletions tests/contract/test_log_emission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from sharding.handler.utils.web3_utils import (
mine,
)
from tests.handler.fixtures import ( # noqa: F401
smc_handler,
)
from tests.contract.utils.common_utils import (
fast_forward,
)
from tests.contract.utils.notary_account import (
NotaryAccount,
)
from tests.contract.utils.sample_helper import (
sampling,
)


def test_log_emission(smc_handler): # noqa: F811
web3 = smc_handler.web3
notary = NotaryAccount(0)

# Register
tx_hash = smc_handler.register_notary(private_key=notary.private_key)
mine(web3, 1)
# Check that log was successfully emitted
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
log = smc_handler.events.RegisterNotary().processReceipt(tx_receipt)[0]['args']
assert log['index_in_notary_pool'] == 0 and log['notary'] == notary.checksum_address
fast_forward(smc_handler, 1)

# Add header
CHUNK_ROOT_1_0 = b'\x10' * 32
tx_hash = smc_handler.add_header(
period=1,
shard_id=0,
chunk_root=CHUNK_ROOT_1_0,
private_key=notary.private_key
)
mine(web3, 1)
# Check that log was successfully emitted
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
log = smc_handler.events.AddHeader().processReceipt(tx_receipt)[0]['args']
assert log['period'] == 1 and log['shard_id'] == 0 and log['chunk_root'] == CHUNK_ROOT_1_0

# Submit vote
sample_index = 0
pool_index = sampling(smc_handler, 0)[sample_index]
tx_hash = smc_handler.submit_vote(
period=1,
shard_id=0,
chunk_root=CHUNK_ROOT_1_0,
index=sample_index,
private_key=NotaryAccount(pool_index).private_key
)
mine(web3, 1)
# Check that log was successfully emitted
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
log = smc_handler.events.SubmitVote().processReceipt(tx_receipt)[0]['args']
assert log['period'] == 1 and log['shard_id'] == 0 and log['chunk_root'] == CHUNK_ROOT_1_0 \
and log['notary'] == NotaryAccount(pool_index).checksum_address
fast_forward(smc_handler, 1)

# Deregister
tx_hash = smc_handler.deregister_notary(private_key=notary.private_key)
mine(web3, 1)
# Check that log was successfully emitted
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
log = smc_handler.events.DeregisterNotary().processReceipt(tx_receipt)[0]['args']
assert log['index_in_notary_pool'] == 0 and log['notary'] == notary.checksum_address \
and log['deregistered_period'] == 2
# Fast foward to end of lock up
fast_forward(smc_handler, smc_handler.config['NOTARY_LOCKUP_LENGTH'] + 1)

# Release
tx_hash = smc_handler.release_notary(private_key=notary.private_key)
mine(web3, 1)
# Check that log was successfully emitted
tx_receipt = web3.eth.getTransactionReceipt(tx_hash)
log = smc_handler.events.ReleaseNotary().processReceipt(tx_receipt)[0]['args']
assert log['index_in_notary_pool'] == 0 and log['notary'] == notary.checksum_address
6 changes: 6 additions & 0 deletions tests/contract/test_registry_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ def test_double_register(smc_handler): # noqa: F811
tx_hash = smc_handler.register_notary(private_key=notary_0.private_key)
mine(web3, 1)
# Check pool remain the same and the transaction consume all gas
# and no logs has been emitted
notary_pool_length = smc_handler.notary_pool_len()
assert notary_pool_length == 1
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0


def test_normal_deregister(smc_handler): # noqa: F811
Expand Down Expand Up @@ -170,9 +172,11 @@ def test_deregister_then_register(smc_handler): # noqa: F811
tx_hash = smc_handler.register_notary(private_key=notary_0.private_key)
mine(web3, 1)
# Check pool remain the same and the transaction consume all gas
# and no logs has been emitted
notary_pool_length = smc_handler.notary_pool_len()
assert notary_pool_length == 0
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0


def test_normal_release_notary(smc_handler): # noqa: F811
Expand Down Expand Up @@ -234,9 +238,11 @@ def test_instant_release_notary(smc_handler): # noqa: F811
tx_hash = smc_handler.release_notary(private_key=notary_0.private_key)
mine(web3, 1)
# Check registry remain the same and the transaction consume all gas
# and no logs has been emitted
does_notary_exist = smc_handler.does_notary_exist(notary_0.checksum_address)
assert does_notary_exist
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0


def test_deregister_and_new_notary_register(smc_handler): # noqa: F811
Expand Down
10 changes: 10 additions & 0 deletions tests/contract/test_submit_vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def test_double_submit_vote(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that transaction failed and vote count remains the same
# and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
assert smc_handler.get_vote_count(shard_id) == 1


Expand Down Expand Up @@ -229,7 +231,9 @@ def test_submit_vote_by_non_eligible_notary(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that transaction failed and vote count remains the same
# and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
assert smc_handler.get_vote_count(shard_id) == 0
assert not smc_handler.has_notary_voted(shard_id, sample_index)

Expand Down Expand Up @@ -259,7 +263,9 @@ def test_submit_vote_without_add_header_first(smc_handler): # noqa: F811
)
mine(web3, 1)
# Check that transaction failed and vote count remains the same
# and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
assert smc_handler.get_vote_count(shard_id) == 0
assert not smc_handler.has_notary_voted(shard_id, sample_index)

Expand Down Expand Up @@ -306,7 +312,9 @@ def test_submit_vote_with_invalid_args(smc_handler, period, shard_id, chunk_root
)
mine(web3, 1)
# Check that transaction failed and vote count remains the same
# and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
assert smc_handler.get_vote_count(shard_id) == 0
assert not smc_handler.has_notary_voted(shard_id, sample_index)

Expand Down Expand Up @@ -368,5 +376,7 @@ def test_submit_vote_then_deregister(smc_handler): # noqa: F811
mine(web3, 1)

# Check that transaction failed and vote count remains the same
# and no logs has been emitted
assert web3.eth.getTransactionReceipt(tx_hash)['gasUsed'] == default_gas
assert len(web3.eth.getTransactionReceipt(tx_hash)['logs']) == 0
assert smc_handler.get_vote_count(shard_id) == 1

0 comments on commit cb8f5b5

Please sign in to comment.