Skip to content

Commit

Permalink
Users/pingke/eip155 signer test (#926)
Browse files Browse the repository at this point in the history
* reformatted files

* reformatted files

* fix typo

* fix typo

* add content

* fix test case

* fix config bug

* fix issue

* add test case

* fix test

* fix test

* revert posw change

* revert posw change

* fix test

* change jsonrpc to support eip155 signer

* resolve comments

* reformat

* add validation for tx.version = 2

* resolve comments

* reformat

* resolve qi's comments

* fix estimatesGas (#924)

* fix tests

* fix test

* fix test

* fix test

* fix bug

* change Eth_Chain_Id = network_id

* fix bug

* fix note

* add from_shard_key and to_shard_key

* update ENABLE_EIP155_SIGNER_TIMESTAMP value

* update message

* fix bug

* fix bug

* fix but

* add test_eip155_signer_attachment test

* fix test case

* update eip155 test case

Co-authored-by: 516108736 <[email protected]>
  • Loading branch information
ping-ke and scf0220 authored Sep 2, 2021
1 parent f2da602 commit 3214f2b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
63 changes: 62 additions & 1 deletion quarkchain/cluster/tests/test_shard_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,7 @@ def test_enable_eip155_signer_timestamp(self):
b1 = state.create_block_to_mine()
self.assertEqual(len(b1.tx_list), 1)

state.evm_state.timestamp=b1.header.create_time
state.evm_state.timestamp = b1.header.create_time
env.quark_chain_config.ENABLE_EIP155_SIGNER_TIMESTAMP = (
b1.header.create_time + 100
)
Expand All @@ -2908,6 +2908,67 @@ def test_enable_eip155_signer_timestamp(self):
self.assertEqual(len(b3.tx_list), 1)
state.finalize_and_add_block(b3)

def test_eip155_signer_attack(self):
# use chain 0 signed tx to submit to chain 1
id0 = Identity.create_random_identity()
id1 = Identity.create_random_identity()
acc_0_1 = Address.create_from_identity(id0, full_shard_key=0)
acc_0_2 = Address.create_from_identity(id0, full_shard_key=0)
acc_1_1 = Address.create_from_identity(id1, full_shard_key=65536)
acc_1_2 = Address.create_from_identity(id1, full_shard_key=65536)

env = get_test_env(genesis_account=acc_0_1, genesis_minor_quarkash=10000000)
state0 = create_default_shard_state(env=env, shard_id=0)
state1 = create_default_shard_state(env=env, shard_id=1)

# Add a root block to have all the shards initialized
root_block = state0.root_tip.create_block_to_append().finalize()
state0.add_root_block(root_block)
state1.add_root_block(root_block)
env.quark_chain_config.ENABLE_TX_TIMESTAMP = 0
env.quark_chain_config.ENABLE_EIP155_SIGNER_TIMESTAMP = None

tx0 = create_transfer_transaction(
shard_state=state0,
key=id0.get_key(),
from_address=acc_0_1,
to_address=acc_0_2,
value=5000000,
gas=50000,
version=2,
)
self.assertTrue(state0.add_tx(tx0))

b0 = state0.create_block_to_mine()
self.assertEqual(len(b0.tx_list), 1)

state0.finalize_and_add_block(b0)

tx1 = create_transfer_transaction(
shard_state=state1,
key=id0.get_key(),
from_address=acc_1_1,
to_address=acc_1_2,
value=5000000,
gas=50000,
version=2,
)
evm_tx = tx0.tx.to_evm_tx()
tx1.tx.to_evm_tx().set_signature(evm_tx.v, evm_tx.r, evm_tx.s)
self.assertFalse(state1.add_tx(tx1))

tx2 = create_transfer_transaction(
shard_state=state1,
key=id1.get_key(),
from_address=acc_1_1,
to_address=acc_1_2,
value=5000000,
gas=50000,
version=2,
network_id=evm_tx.network_id,
)
self.assertFalse(state1.add_tx(tx2))

def test_enable_evm_timestamp_with_contract_call(self):
id1 = Identity.create_random_identity()
acc1 = Address.create_from_identity(id1, full_shard_key=0)
Expand Down
7 changes: 7 additions & 0 deletions quarkchain/evm/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ def sign(self, key, network_id=None):
self._sender = utils.privtoaddr(key)
return self

def set_signature(self, v, r, s):
"""
only use for test
"""
self._in_mutable_context = True
self.v, self.r, self.s = v, r, s

@property
def hash(self):
return sha3_256(rlp.encode(self))
Expand Down

0 comments on commit 3214f2b

Please sign in to comment.