Exemplo n.º 1
0
    def __init__(self):
        self.w3 = Web3(HTTPProvider('http://localhost:8545'))
        self.root_chain = Deployer().deploy_contract('RootChain', concise=False)
        self.child_chain = ChildChain(AUTHORITY['address'], self.root_chain)

        self.transactions = []
        self.accounts = []
Exemplo n.º 2
0
 def __init__(self):
     self.root_chain = Deployer().deploy_contract('RootChain',
                                                  concise=False)
     self.child_chain = ChildChain(AUTHORITY['address'], self.root_chain)
     print("child chain {0}".format(self.child_chain))
     print("init {0}".format(AUTHORITY['address']))
     self.confirmations = {}
     self.accounts = []
Exemplo n.º 3
0
def child_chain():
    child_chain = ChildChain(AUTHORITY, Mock())

    # Create some valid transations
    tx1 = Transaction(0, 0, 0, 0, 0, 0, newowner1, amount1, b'\x00' * 20, 0, 0)
    tx2 = Transaction(0, 0, 0, 0, 0, 0, newowner1, amount1, b'\x00' * 20, 0, 0)

    # Create a block with those transactions
    child_chain.blocks[1] = Block([tx1, tx2])

    return child_chain
Exemplo n.º 4
0
def child_chain(root_chain):
    child_chain = ChildChain(None, None, None)

    # Create some valid transations
    key = b'8b76243a95f959bf101248474e6bdacdedc8ad995d287c24616a41bd51642965'
    owner, amount1 = '0x8f2ecdd6103c0423f4c3e906666238936e81e538', 200
    tx1 = Transaction(0, 0, 0, 0, 0, 0, owner, amount1, b'\x00' * 20, 0, 0)
    tx2 = Transaction(0, 0, 0, 0, 0, 0, owner, amount1, b'\x00' * 20, 0, 0)

    # Create a block with those transactions
    child_chain.blocks[1] = Block([tx1, tx2])

    return child_chain
Exemplo n.º 5
0
    def __init__(self):
        self.w3 = Web3(HTTPProvider('http://localhost:8545'))
        self.root_chain = Deployer().deploy_contract('RootChain', concise=False)
        self.child_chain = ChildChain(bytes.fromhex(AUTHORITY['address'][2:]), self.root_chain)

        self.transactions = []
        self.accounts = []

        self.handlers = dict()

        # Register handlers
        self.register_handler('Deposit', self.deposit)
        self.register_handler('Transfer', self.transfer)
        self.register_handler('SubmitBlock', self.submit_block)
        self.register_handler('Confirm', self.confirm)
        self.register_handler('Withdraw', self.withdraw)
Exemplo n.º 6
0
def main(root_chain_address, eth_node_endpoint):
    global child_chain
    root_chain_address = Web3.toChecksumAddress(root_chain_address)

    root_chain = Deployer(eth_node_endpoint).get_contract_at_address(
        "RootChain", root_chain_address, concise=False)
    print("root_chain is %s" % root_chain)
    child_chain = ChildChain(root_chain, eth_node_endpoint)

    run_simple('0.0.0.0', 8546, application)
Exemplo n.º 7
0
import rlp
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from ethereum import utils
from plasma.child_chain.child_chain import ChildChain
from plasma.root_chain.deployer import Deployer
from plasma_core.constants import CONTRACT_ADDRESS, AUTHORITY
from plasma_core.block import Block
from plasma_core.transaction import Transaction
from plasma_core.utils.transactions import encode_utxo_id

root_chain = Deployer().get_contract_at_address("RootChain", CONTRACT_ADDRESS, concise=False)
child_chain = ChildChain(AUTHORITY['address'], root_chain)


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["submit_block"] = lambda block: child_chain.submit_block(rlp.decode(utils.decode_hex(block), Block))
    dispatcher["apply_transaction"] = lambda transaction: child_chain.apply_transaction(rlp.decode(utils.decode_hex(transaction), Transaction))
    dispatcher["get_transaction"] = lambda blknum, txindex: rlp.encode(child_chain.get_transaction(encode_utxo_id(blknum, txindex, 0)), Transaction).hex()
    dispatcher["get_current_block"] = lambda: rlp.encode(child_chain.get_current_block(), Block).hex()
    dispatcher["get_current_block_num"] = lambda: child_chain.get_current_block_num()
    dispatcher["get_block"] = lambda blknum: rlp.encode(child_chain.get_block(blknum), Block).hex()
    dispatcher["get_test"] = lambda: child_chain.get_test()
    dispatcher["set_dict"] = lambda **num: child_chain.set_dict(**num)
    dispatcher["set_test"] = lambda num: child_chain.set_test(num)
    dispatcher["hello"] = lambda: child_chain.hello()
    response = JSONRPCResponseManager.handle(
        request.data, dispatcher)
Exemplo n.º 8
0
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from plasma.child_chain.child_chain import ChildChain
from plasma.root_chain.deployer import Deployer
from plasma_core.constants import CONTRACT_ADDRESS, AUTHORITY

root_chain = Deployer().get_contract_at_address("RootChain", CONTRACT_ADDRESS, concise=False)
child_chain = ChildChain(bytes.fromhex(AUTHORITY['address'][2:]), root_chain)


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["submit_block"] = lambda block: child_chain.submit_block(block)
    dispatcher["apply_transaction"] = lambda transaction: child_chain.apply_transaction(transaction)
    dispatcher["get_transaction"] = lambda blknum, txindex: child_chain.get_transaction(blknum, txindex)
    dispatcher["get_current_block"] = lambda: child_chain.get_current_block()
    dispatcher["get_current_block_num"] = lambda: child_chain.get_current_block_num()
    dispatcher["get_block"] = lambda blknum: child_chain.get_block(blknum)
    response = JSONRPCResponseManager.handle(
        request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':
    run_simple('localhost', 8546, application)
Exemplo n.º 9
0
class TestingLanguage(object):

    def __init__(self):
        self.root_chain = Deployer().deploy_contract('RootChain', concise=False)
        self.child_chain = ChildChain(AUTHORITY['address'], self.root_chain)

        self.transactions = []
        self.accounts = []

    def get_account(self):
        account = ACCOUNTS[len(self.accounts)]
        self.accounts.append(account)
        return account

    def deposit(self, account, amount):
        amount = int(amount)

        self.root_chain.transact({
            'from': account['address'],
            'value': amount
        }).deposit()

        # Wait for the Deposit event to be detected
        time.sleep(1)

        tx = Transaction(0, 0, 0,
                         0, 0, 0,
                         NULL_ADDRESS,
                         account['address'], amount,
                         NULL_ADDRESS, 0)

        self.transactions.append({
            'tx': tx,
            'confirm_sigs': b'',
            'is_deposit': True
        })
        return len(self.transactions) - 1

    def transfer(self,
                 input1, oindex1, newowner1, amount1, signatory1,
                 input2=None, oindex2=0, newowner2=None, amount2=None, signatory2=None, cur12=NULL_ADDRESS):
        newowner_address1 = newowner1['address']
        amount1 = int(amount1)

        newowner_address2 = NULL_ADDRESS
        if newowner2 is not None:
            newowner_address2 = newowner2['address']
        amount2 = int(amount2) if amount2 is not None else 0

        encoded_input_tx1 = rlp.encode(self.transactions[input1]['tx']).hex()
        blknum1, txindex1 = self.child_chain.get_tx_pos(encoded_input_tx1)

        blknum2, txindex2 = 0, 0
        if input2 is not None:
            encoded_input_tx2 = rlp.encode(self.transactions[input2]['tx']).hex()
            blknum2, txindex2 = self.child_chain.get_tx_pos(encoded_input_tx2)

        tx = Transaction(blknum1, txindex1, oindex1,
                         blknum2, txindex2, oindex2,
                         cur12,
                         newowner_address1, amount1,
                         newowner_address2, amount2)

        if signatory1 is not None:
            key1 = signatory1['key']
            tx.sign1(key1)

        if signatory2 is not None:
            key2 = signatory2['key']
            tx.sign2(key2)

        encoded_tx = rlp.encode(tx).hex()

        self.child_chain.apply_transaction(encoded_tx)

        self.transactions.append({
            'tx': tx,
            'confirm_sigs': b'',
            'is_deposit': False
        })
        return len(self.transactions) - 1

    def submit_block(self, signatory=AUTHORITY):
        signing_key = None
        if signatory is not None:
            signing_key = signatory['key']

        block = self.child_chain.current_block
        block.make_mutable()
        if signing_key:
            block.sign(signing_key)

        self.child_chain.submit_block(rlp.encode(block).hex())

    def confirm(self, tx_id, signatory1, signatory2=None):
        transaction = self.transactions[tx_id]

        tx = transaction['tx']
        encoded_tx = rlp.encode(tx).hex()

        blknum, _ = self.child_chain.get_tx_pos(encoded_tx)
        block_root = self.child_chain.blocks[blknum].merkle.root

        confirm_sigs = b''
        for signatory in [x for x in [signatory1, signatory2] if x is not None]:
            confirm_sigs += tx.confirm(block_root, signatory['key'])

        self.transactions[tx_id]['confirm_sigs'] = confirm_sigs

    def withdraw(self, tx_id, oindex, exitor):
        transaction = self.transactions[tx_id]

        tx = transaction['tx']
        encoded_tx = rlp.encode(tx).hex()
        tx_bytes = rlp.encode(tx, UnsignedTransaction)
        sigs = tx.sig1 + tx.sig2 + transaction['confirm_sigs']

        blknum, txindex = self.child_chain.get_tx_pos(encoded_tx)
        utxo_pos = blknum * 1000000000 + txindex * 10000 + oindex * 1

        if transaction['is_deposit']:
            deposit_amount = tx.amount1

            self.root_chain.transact({
                'from': exitor['address']
            }).startDepositExit(utxo_pos + 1, NULL_ADDRESS_HEX, deposit_amount)
        else:
            output_block = self.child_chain.blocks[blknum]
            hashed_transaction_set = [transaction.merkle_hash for transaction in output_block.transaction_set]
            merkle = FixedMerkle(16, hashed_transaction_set, hashed=True)
            proof = merkle.create_membership_proof(tx.merkle_hash)

            self.root_chain.transact({
                'from': exitor['address']
            }).startExit(utxo_pos, tx_bytes, proof, sigs)
Exemplo n.º 10
0
    def __init__(self):
        self.root_chain = Deployer().deploy_contract('RootChain', concise=False)
        self.child_chain = ChildChain(AUTHORITY['address'], self.root_chain)

        self.transactions = []
        self.accounts = []
Exemplo n.º 11
0
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from plasma.child_chain.child_chain import ChildChain
from plasma.config import plasma_config
from plasma.root_chain.deployer import Deployer

child_chain = ChildChain(plasma_config['AUTHORITY'],
                         Deployer().get_contract("RootChain/RootChain.sol"))


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["submit_block"] = lambda block: child_chain.submit_block(block)
    dispatcher[
        "apply_transaction"] = lambda transaction: child_chain.apply_transaction(
            transaction)
    dispatcher[
        "get_transaction"] = lambda blknum, txindex: child_chain.get_transaction(
            blknum, txindex)
    dispatcher["get_current_block"] = lambda: child_chain.get_current_block()
    dispatcher[
        "get_current_block_num"] = lambda: child_chain.get_current_block_num()
    dispatcher["get_block"] = lambda blknum: child_chain.get_block(blknum)
    response = JSONRPCResponseManager.handle(request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':
    run_simple('localhost', 8546, application)
Exemplo n.º 12
0
class TestingLanguage(object):

    def __init__(self):
        self.root_chain = Deployer().deploy_contract('RootChain', concise=False)
        self.child_chain = ChildChain(AUTHORITY['address'], self.root_chain)
        self.confirmations = {}
        self.accounts = []

    def get_account(self):
        account = ACCOUNTS[len(self.accounts)]
        self.accounts.append(account)
        return account

    def deposit(self, account, amount):
        deposit_blknum = self.child_chain.chain.next_deposit_block

        self.root_chain.transact({
            'from': account['address'],
            'value': amount
        }).deposit()

        # Wait for the Deposit event to be detected
        time.sleep(1)

        return encode_utxo_id(deposit_blknum, 0, 0)

    def transfer(self,
                 input1, newowner1, amount1, signatory1,
                 input2=0, newowner2=None, amount2=0, signatory2=None, cur12=NULL_ADDRESS):
        newowner_address1 = newowner1['address']
        newowner_address2 = NULL_ADDRESS
        if newowner2 is not None:
            newowner_address2 = newowner2['address']

        tx = Transaction(*decode_utxo_id(input1),
                         *decode_utxo_id(input2),
                         cur12,
                         newowner_address1, amount1,
                         newowner_address2, amount2)

        if signatory1 is not None:
            key1 = signatory1['key']
            tx.sign1(key1)

        if signatory2 is not None:
            key2 = signatory2['key']
            tx.sign2(key2)

        spend_id = self.child_chain.apply_transaction(tx)
        self.submit_block()
        return spend_id

    def submit_block(self, signatory=AUTHORITY):
        signing_key = None
        if signatory is not None:
            signing_key = signatory['key']

        block = self.child_chain.current_block
        if signing_key:
            block.sign(signing_key)

        self.child_chain.submit_block(block)

    def confirm(self, tx_id, signatory1, signatory2=None):
        tx = self.child_chain.get_transaction(tx_id)
        (blknum, _, _) = decode_utxo_id(tx_id)
        block_root = self.child_chain.get_block(blknum).root

        confirm_sigs = b''
        for signatory in [x for x in [signatory1, signatory2] if x is not None]:
            confirm_sigs += confirm_tx(tx, block_root, signatory['key'])

        self.confirmations[tx_id] = confirm_sigs

    def start_deposit_exit(self, utxo_id, exitor):
        tx = self.child_chain.get_transaction(utxo_id)

        self.root_chain.transact({
            'from': exitor['address']
        }).startDepositExit(utxo_id, NULL_ADDRESS_HEX, tx.amount1)

    def start_exit(self, utxo_id, exitor):
        tx = self.child_chain.get_transaction(utxo_id)

        sigs = tx.sig1 + tx.sig2 + self.confirmations[utxo_id]
        (blknum, _, _) = decode_utxo_id(utxo_id)
        block = self.child_chain.get_block(blknum)
        proof = block.merkle.create_membership_proof(tx.merkle_hash)

        self.root_chain.transact({
            'from': exitor['address']
        }).startExit(utxo_id, tx.encoded, proof, sigs)
Exemplo n.º 13
0
import rlp
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from ethereum import utils
from plasma.child_chain.child_chain import ChildChain
from plasma.root_chain.deployer import Deployer
from plasma_core.constants import CONTRACT_ADDRESS, AUTHORITY
from plasma_core.block import Block
from plasma_core.transaction import Transaction

root_chain = Deployer().get_contract_at_address("RootChain", CONTRACT_ADDRESS, concise=False)
child_chain = ChildChain(AUTHORITY['address'], AUTHORITY['ecrecoveredAddr'], root_chain)


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["submit_block"] = lambda block: child_chain.submit_block(rlp.decode(utils.decode_hex(block), Block))
    dispatcher["apply_transaction"] = lambda transaction: child_chain.apply_transaction(rlp.decode(utils.decode_hex(transaction), Transaction))
    dispatcher["get_transaction"] = lambda utxo_id: rlp.encode(child_chain.get_transaction(utxo_id), Transaction).hex()
    dispatcher["get_current_block"] = lambda: rlp.encode(child_chain.get_current_block(), Block).hex()
    dispatcher["get_current_block_num"] = lambda: child_chain.get_current_block_num()
    dispatcher["get_block"] = lambda blknum: rlp.encode(child_chain.get_block(blknum), Block).hex()
    response = JSONRPCResponseManager.handle(
        request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':
    run_simple('localhost', 8546, application)
Exemplo n.º 14
0
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from plasma.child_chain.child_chain import ChildChain
from plasma.child_chain.partially_signed_transaction_pool import PartiallySignedTransactionPool
from plasma.child_chain.block_auto_submitter import BlockAutoSubmitter
from plasma.child_chain.finalize_exits_auto_submitter import FinalizeExitsAutoSubmitter
# from plasma.child_chain.liquidity_provider import LiquidilyProvider
from plasma.config import plasma_config
from plasma.root_chain.deployer import Deployer

deployer = Deployer()
root_chain = deployer.get_contract_at_address(
    "RootChain", plasma_config['ROOT_CHAIN_CONTRACT_ADDRESS'], concise=False)
partially_signed_transaction_pool = PartiallySignedTransactionPool()
child_chain = ChildChain(
    plasma_config['AUTHORITY'],
    root_chain,
    partially_signed_transaction_pool=partially_signed_transaction_pool)
BlockAutoSubmitter(child_chain,
                   plasma_config['BLOCK_AUTO_SUMBITTER_INTERVAL']).start()
FinalizeExitsAutoSubmitter(
    plasma_config['AUTHORITY'], root_chain,
    plasma_config['FINALIZE_EXITS_AUTO_SUBMITTER_INTERVAL']).start()
# liquidity_provider = LiquidilyProvider(child_chain)


def printKey(key):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            print("Dispatcher: " + key)
            return func(*args, **kwargs)
Exemplo n.º 15
0
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher
from plasma.child_chain.child_chain import ChildChain
from plasma.config import plasma_config
from plasma.root_chain.deployer import Deployer

root_chain = Deployer().get_contract_at_address(
    "RootChain", plasma_config['ROOT_CHAIN_CONTRACT_ADDRESS'], concise=False)
child_chain = ChildChain(plasma_config['AUTHORITY'], root_chain)


@Request.application
def application(request):
    # Dispatcher is dictionary {<method_name>: callable}
    dispatcher["submit_block"] = lambda block: child_chain.submit_block(block)
    dispatcher[
        "apply_transaction"] = lambda transaction: child_chain.apply_transaction(
            transaction)
    dispatcher[
        "get_transaction"] = lambda blknum, txindex: child_chain.get_transaction(
            blknum, txindex)
    dispatcher["get_current_block"] = lambda: child_chain.get_current_block()
    dispatcher[
        "get_current_block_num"] = lambda: child_chain.get_current_block_num()
    dispatcher["get_block"] = lambda blknum: child_chain.get_block(blknum)
    response = JSONRPCResponseManager.handle(request.data, dispatcher)
    return Response(response.json, mimetype='application/json')


if __name__ == '__main__':