Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Implement child chain and client #26

Merged
merged 14 commits into from Feb 28, 2018
3 changes: 1 addition & 2 deletions plasma/child_chain/child_chain.py
Expand Up @@ -54,7 +54,6 @@ def valid_input_tx(self, blknum, txindex, oindex):
spent = self.blocks[blknum].transaction_set[txindex].spent2
amount = self.blocks[blknum].transaction_set[txindex].amount2
assert spent is False
spent = True
return amount

def submit_block(self, block):
Expand All @@ -66,7 +65,7 @@ def submit_block(self, block):
self.current_block = Block()

def get_transaction(self, blknum, txindex):
return rlp.encode(self.block[blknum].transaction_set[txindex]).hex()
return rlp.encode(self.blocks[blknum].transaction_set[txindex]).hex()

def get_block(self, blknum):
return rlp.encode(self.blocks[blknum]).hex()
Expand Down
25 changes: 11 additions & 14 deletions plasma/cli/client.py
Expand Up @@ -20,12 +20,8 @@ def start_client_cmd(ctx):

class ClientParser():

def __init__(self):
self.client = Client()
self.db = plyvel.DB('/tmp/plasma_mvp_db/', create_if_missing=True)
self.current_block = self.client.get_current_block_num()
self.synced_block = 1
self.client_cmds = dict(
synced_block = 1
client_cmds = dict(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving client_cmds to a class variable means we don't have access to self so we need to change to:

    client_cmds = dict(
        sync=ClientParser.sync_child_chain,
        deposit=ClientParser.deposit,
        send_tx=ClientParser.send_tx,
        submit_block=ClientParser.submit_block,
        withdraw=ClientParser.withdraw,
        help=ClientParser.help,
    )

sync=self.sync_child_chain,
deposit=self.deposit,
send_tx=self.send_tx,
Expand All @@ -34,13 +30,18 @@ def __init__(self):
help=self.help,
)

def __init__(self):
self.client = Client()
self.db = plyvel.DB('/tmp/plasma_mvp_db/', create_if_missing=True)
self.current_block = self.client.get_current_block_num()

def process_input(self, inp):
self.inp = inp
command = self.inp[0]
if command not in self.client_cmds:
if command not in client_cmds:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need the self reference unless we make client_cmds a global here.

print("Please enter a valid command ('or enter help')")
else:
return self.client_cmds[command]()
return client_cmds[command]()

def sync_child_chain(self):
self.current_block = self.client.get_current_block_num()
Expand Down Expand Up @@ -77,9 +78,7 @@ def send_tx(self):
amount2 = int(self.inp[10])
fee = int(self.inp[11])
key1 = utils.normalize_key(self.inp[12])
key2 = b''
if len(self.inp) == 14:
key2 = utils.normalize_key(self.inp[13])
key2 = utils.normalize_key(self.inp[13]) if len(self.inp) == 14 else b''
tx = Transaction(blknum1, tx_pos1, utxo_pos1,
blknum2, tx_pos2, utxo_pos2,
newowner1, amount1,
Expand All @@ -106,9 +105,7 @@ def withdraw(self):
blknum, txindex, oindex = int(self.inp[1]), int(self.inp[2]), int(self.inp[3])
txPos = [blknum, txindex, oindex]
key1 = utils.normalize_key(self.inp[4])
key2 = b''
if len(self.inp) == 6:
key2 = utils.normalize_key(self.inp[5])
key2 = utils.normalize_key(self.inp[5]) if len(self.inp) == 6 else b''
block = self.client.get_block(blknum)
block = rlp.decode(utils.decode_hex(block), Block)
tx = block.transaction_set[txindex]
Expand Down
2 changes: 1 addition & 1 deletion plasma/cli/main.py
Expand Up @@ -7,7 +7,7 @@
)


@click.group(invoke_without_command=True)
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
@click.pass_context
def main(ctx):
pass
3 changes: 1 addition & 2 deletions plasma/client/child_chain_service.py
Expand Up @@ -22,8 +22,7 @@ def send_request(self, method, args):
"jsonrpc": "2.0",
"id": 0,
}
response = requests.post(self.url, data=json.dumps(payload),
headers=headers).json()
response = requests.post(self.url, data=payload).json()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be json=payload if we want to send as JSON w/o headers = {'content-type': 'application/json'}. Might as well remove headers = {'content-type': 'application/json'} if we're going this route.

return response["result"]

def submit_deposit(self, tx_hash):
Expand Down
3 changes: 1 addition & 2 deletions plasma/client/client.py
Expand Up @@ -13,8 +13,7 @@ class Client(object):
def __init__(self, root_chain_provider=HTTPProvider('http://localhost:8545'), child_chain_url="http://localhost:8546/jsonrpc"):
deployer = Deployer(root_chain_provider)
abi = json.load(open("contract_data/RootChain.json"))
self.w3 = deployer.w3
self.root_chain = self.w3.eth.contract(abi, plasma_config['ROOT_CHAIN_CONTRACT_ADDRESS'], ContractFactoryClass=ConciseContract)
self.root_chain = deployer.w3.eth.contract(abi, plasma_config['ROOT_CHAIN_CONTRACT_ADDRESS'], ContractFactoryClass=ConciseContract)
self.child_chain = ChildChainService(child_chain_url)

def create_transaction(self, blknum1=0, txindex1=0, oindex1=0,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Expand Up @@ -57,7 +57,7 @@ def u():
@pytest.fixture
def get_contract(t, u):
def create_contract(path, args=(), sender=t.k0):
abi, hexcode = Deployer().compile_contract(path, args)
abi, hexcode, _ = Deployer().compile_contract(path, args)
bytecode = u.decode_hex(hexcode)
ct = ContractTranslator(abi)
code = bytecode + (ct.encode_constructor_arguments(args) if args else b'')
Expand Down