def _validate_block(self, block): # Check for a valid signature. if not block.is_deposit_block and (block.sig == NULL_SIGNATURE or address_to_hex(block.signer) != address_to_hex(self.operator)): raise InvalidBlockSignatureException('failed to validate block') for tx in block.transaction_set: self.validate_transaction(tx)
def sendtx(client, blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, cur12, amount1, newowner1, amount2, newowner2, key1, key2): if cur12 == "0x0": cur12 = NULL_ADDRESS if newowner1 == "0x0": newowner1 = NULL_ADDRESS if newowner2 == "0x0": newowner2 = NULL_ADDRESS # Form a transaction tx = Transaction(blknum1, txindex1, oindex1, blknum2, txindex2, oindex2, utils.normalize_address(cur12), utils.normalize_address(newowner1), amount1, utils.normalize_address(newowner2), amount2) # Sign it if key1: tx.sign1(utils.normalize_key(key1)) if key2: tx.sign2(utils.normalize_key(key2)) print("key1:", key1) print("sender1:", address_to_hex(tx.sender1)) client_call(client.apply_transaction, [tx], "Sent transaction")
def ethtester(): tester.chain = tester.Chain() tester.accounts = [] for i in range(10): address = getattr(tester, 'a{0}'.format(i)) key = getattr(tester, 'k{0}'.format(i)) tester.accounts.append(EthereumAccount(address_to_hex(address), key)) return tester
def test_challenge_exit(t, u, root_chain, assert_tx_failed): owner, value_1, key = t.a1, 100, t.k1 tx1 = Transaction(0, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) deposit_tx_hash = get_deposit_hash(owner, NULL_ADDRESS, value_1) utxo_pos1 = root_chain.getDepositBlock() * 1000000000 + 1 root_chain.deposit(value=value_1, sender=key) utxo_pos2 = root_chain.getDepositBlock() * 1000000000 root_chain.deposit(value=value_1, sender=key) merkle = FixedMerkle(16, [deposit_tx_hash], True) proof = merkle.create_membership_proof(deposit_tx_hash) confirmSig1 = tx1.confirm(root_chain.getChildChain(utxo_pos1)[0], key) sigs = tx1.sig1 + tx1.sig2 + confirmSig1 root_chain.startDepositExit(utxo_pos1, NULL_ADDRESS, tx1.amount1, sender=key) tx3 = Transaction(utxo_pos2, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) tx3.sign1(key) tx_bytes3 = rlp.encode(tx3, UnsignedTransaction) merkle = FixedMerkle(16, [tx3.merkle_hash], True) proof = merkle.create_membership_proof(tx3.merkle_hash) child_blknum = root_chain.currentChildBlock() root_chain.submitBlock(merkle.root) confirmSig = tx3.confirm(root_chain.getChildChain(child_blknum)[0], key) sigs = tx3.sig1 + tx3.sig2 utxo_pos3 = child_blknum * 1000000000 + 10000 * 0 + 0 tx4 = Transaction(utxo_pos1, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) tx4.sign1(key) tx_bytes4 = rlp.encode(tx4, UnsignedTransaction) merkle = FixedMerkle(16, [tx4.merkle_hash], True) proof = merkle.create_membership_proof(tx4.merkle_hash) child_blknum = root_chain.currentChildBlock() root_chain.submitBlock(merkle.root) confirmSig = tx4.confirm(root_chain.getChildChain(child_blknum)[0], key) sigs = tx4.sig1 + tx4.sig2 utxo_pos4 = child_blknum * 1000000000 + 10000 * 0 + 0 oindex1 = 0 assert root_chain.exits(utxo_pos1) == [ address_to_hex(owner), NULL_ADDRESS_HEX, 100 ] # Fails if transaction after exit doesn't reference the utxo being exited assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos3, utxo_pos1, tx_bytes3, proof, sigs, confirmSig)) # Fails if transaction proof is incorrect assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos4, utxo_pos1, tx_bytes4, proof[::-1], sigs, confirmSig)) # Fails if transaction confirmation is incorrect assert_tx_failed(lambda: root_chain.challengeExit( utxo_pos4, utxo_pos1, tx_bytes4, proof, sigs, confirmSig[::-1])) root_chain.challengeExit(utxo_pos4, oindex1, tx_bytes4, proof, sigs, confirmSig) assert root_chain.exits(utxo_pos1) == [ NULL_ADDRESS_HEX, NULL_ADDRESS_HEX, value_1 ]
def get_input(self, index): input_info = self.inputs.get(index) if not input_info: input_info = TransactionOutput( *self.root_chain.getInFlightExitOutput( self.in_flight_tx.encoded, index)) input_info.owner = address_to_hex(input_info.owner) self.inputs[index] = input_info return input_info
def submit_block(self, block): if isinstance(block, str): block = rlp.decode(utils.decode_hex(block), Block) self.chain.add_block(block) self.root_chain.transact({ 'from': utils.checksum_encode(address_to_hex(self.operator)) }).submitBlock(block.merkle.root) self.current_block = Block(number=self.chain.next_child_block)
def _validate_block(self, block): """Determines if a block is valid. Args: block (Block): Block to validate. """ # Check for a valid signature. if not block.is_deposit_block and ( block.signature == NULL_SIGNATURE or address_to_hex(block.signer) != self.operator): raise InvalidBlockSignatureException('failed to validate block') for tx in block.transactions: self.validate_transaction(tx)
def get_accounts(ethtester): """Converts ethereum.tools.tester accounts into a list. Args: ethtester (ethereum.tools.tester): Ethereum tester instance. Returns: EthereumAccount[]: A list of EthereumAccounts. """ accounts = [] for i in range(10): address = getattr(ethtester, 'a{0}'.format(i)) key = getattr(ethtester, 'k{0}'.format(i)) accounts.append(EthereumAccount(address_to_hex(address), key)) return accounts
def test_finalize_exits(t, u, root_chain): two_weeks = 60 * 60 * 24 * 14 owner, value_1, key = t.a1, 100, t.k1 tx1 = Transaction(0, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) dep1_blknum = root_chain.getDepositBlock() root_chain.deposit(value=value_1, sender=key) utxo_pos1 = dep1_blknum * 1000000000 + 10000 * 0 + 1 root_chain.startDepositExit(utxo_pos1, NULL_ADDRESS, tx1.amount1, sender=key) t.chain.head_state.timestamp += two_weeks * 2 assert root_chain.exits(utxo_pos1) == [ address_to_hex(owner), NULL_ADDRESS_HEX, 100 ] pre_balance = t.chain.head_state.get_balance(owner) root_chain.finalizeExits(sender=t.k2) post_balance = t.chain.head_state.get_balance(owner) assert post_balance == pre_balance + value_1 assert root_chain.exits(utxo_pos1) == [ NULL_ADDRESS_HEX, NULL_ADDRESS_HEX, value_1 ]
def test_start_exit(t, root_chain, assert_tx_failed): week_and_a_half = 60 * 60 * 24 * 13 owner, value_1, key = t.a1, 100, t.k1 tx1 = Transaction(0, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) deposit_tx_hash = get_deposit_hash(owner, NULL_ADDRESS, value_1) dep_blknum = root_chain.getDepositBlock() assert dep_blknum == 1 root_chain.deposit(value=value_1, sender=key) merkle = FixedMerkle(16, [deposit_tx_hash], True) proof = merkle.create_membership_proof(deposit_tx_hash) confirmSig1 = tx1.confirm(root_chain.getChildChain(dep_blknum)[0], key) priority1 = dep_blknum * 1000000000 + 10000 * 0 + 1 snapshot = t.chain.snapshot() sigs = tx1.sig1 + tx1.sig2 + confirmSig1 utxoId = dep_blknum * 1000000000 + 10000 * 0 + 1 # Deposit exit root_chain.startDepositExit(utxoId, NULL_ADDRESS, tx1.amount1, sender=key) t.chain.head_state.timestamp += week_and_a_half # Cannot exit twice off of the same utxo utxo_pos1 = dep_blknum * 1000000000 + 10000 * 0 + 1 assert_tx_failed(lambda: root_chain.startExit( utxo_pos1, deposit_tx_hash, proof, sigs, sender=key)) assert root_chain.getExit(priority1) == [ address_to_hex(owner), NULL_ADDRESS_HEX, 100 ] t.chain.revert(snapshot) tx2 = Transaction(dep_blknum, 0, 0, 0, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0) tx2.sign1(key) tx_bytes2 = rlp.encode(tx2, UnsignedTransaction) merkle = FixedMerkle(16, [tx2.merkle_hash], True) proof = merkle.create_membership_proof(tx2.merkle_hash) child_blknum = root_chain.currentChildBlock() assert child_blknum == 1000 root_chain.submitBlock(merkle.root) confirmSig1 = tx2.confirm(root_chain.getChildChain(child_blknum)[0], key) priority2 = child_blknum * 1000000000 + 10000 * 0 + 0 sigs = tx2.sig1 + tx2.sig2 + confirmSig1 snapshot = t.chain.snapshot() # # Single input exit utxo_pos2 = child_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(utxo_pos2, tx_bytes2, proof, sigs, sender=key) assert root_chain.getExit(priority2) == [ address_to_hex(owner), NULL_ADDRESS_HEX, 100 ] t.chain.revert(snapshot) dep2_blknum = root_chain.getDepositBlock() assert dep2_blknum == 1001 root_chain.deposit(value=value_1, sender=key) tx3 = Transaction(child_blknum, 0, 0, dep2_blknum, 0, 0, NULL_ADDRESS, owner, value_1, NULL_ADDRESS, 0, 0) tx3.sign1(key) tx3.sign2(key) tx_bytes3 = rlp.encode(tx3, UnsignedTransaction) merkle = FixedMerkle(16, [tx3.merkle_hash], True) proof = merkle.create_membership_proof(tx3.merkle_hash) child2_blknum = root_chain.currentChildBlock() assert child2_blknum == 2000 root_chain.submitBlock(merkle.root) confirmSig1 = tx3.confirm(root_chain.getChildChain(child2_blknum)[0], key) confirmSig2 = tx3.confirm(root_chain.getChildChain(child2_blknum)[0], key) priority3 = child2_blknum * 1000000000 + 10000 * 0 + 0 sigs = tx3.sig1 + tx3.sig2 + confirmSig1 + confirmSig2 # Double input exit utxoPos3 = child2_blknum * 1000000000 + 10000 * 0 + 0 root_chain.startExit(utxoPos3, tx_bytes3, proof, sigs, sender=key) assert root_chain.getExit(priority3) == [ address_to_hex(owner), NULL_ADDRESS_HEX, 100 ]
from plasma_core.utils.address import address_to_hex NULL_BYTE = b'\x00' NULL_ADDRESS = NULL_BYTE * 20 NULL_ADDRESS_HEX = address_to_hex(NULL_ADDRESS) NULL_HASH = NULL_BYTE * 32 NULL_SIGNATURE = NULL_BYTE * 65 WEEKS = 60 * 60 * 24 * 7
def priority_queue(get_contract, ethtester): pql = get_contract('PriorityQueueLib') return get_contract('PriorityQueue', args=[address_to_hex(ethtester.a0)], libraries={'PriorityQueueLib': pql.address})
def _encode_libs(libraries): return { libname + '.sol' + ':' + libname: address_to_hex(libaddress) for libname, libaddress in libraries.items() }