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

Fix plasma-mvp error #212

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
8 changes: 7 additions & 1 deletion plasma/child_chain/child_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ def __init__(self, operator, root_chain):
self.root_chain = root_chain
self.chain = Chain(self.operator)
self.current_block = Block(number=self.chain.next_child_block)

print("self.current_block {0}".format(self.current_block.number))
# Listen for events
self.event_listener = RootEventListener(root_chain, confirmations=0)
self.event_listener.on('Deposit', self.apply_deposit)
self.event_listener.on('ExitStarted', self.apply_exit)
self.event_listener.on('Msgsender', self.msgsender)

def msgsender(self, event):
print("msgsender {0}".format(event['args']))

def apply_exit(self, event):
event_args = event['args']
utxo_id = event_args['utxoPos']
self.chain.mark_utxo_spent(utxo_id)

def apply_deposit(self, event):
print("apply deposit {0}".format(event['args']))
event_args = event['args']
owner = event_args['depositor']
amount = event_args['amount']
Expand All @@ -33,6 +38,7 @@ def apply_deposit(self, event):
self.chain.add_block(deposit_block)

def apply_transaction(self, tx):
print("spent_utxos {0}".format(self.current_block.spent_utxos))
self.chain.validate_transaction(tx, self.current_block.spent_utxos)
self.current_block.add_transaction(tx)
return encode_utxo_id(self.current_block.number, len(self.current_block.transaction_set) - 1, 0)
Expand Down
1 change: 1 addition & 0 deletions plasma/child_chain/root_event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, root_chain, w3=Web3(HTTPProvider('http://localhost:8545')), c

self.__listen_for_event('Deposit')
self.__listen_for_event('ExitStarted')
self.__listen_for_event('Msgsender')

def on(self, event_name, event_handler):
"""Registers an event handler to an event by name.
Expand Down
9 changes: 5 additions & 4 deletions plasma/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def sendtx(client,
blknum1, txindex1, oindex1,
blknum2, txindex2, oindex2,
cur12,
amount1, newowner1,
amount2, newowner2,
newowner1, amount1,
newowner2, amount2,
key1, key2):
if cur12 == "0x0":
cur12 = NULL_ADDRESS
Expand All @@ -68,7 +68,8 @@ def sendtx(client,
if newowner2 == "0x0":
newowner2 = NULL_ADDRESS

# Form a transaction
print("newowner1 {0}".format(newowner1))
print("amount1 {0}".format(amount1))
tx = Transaction(blknum1, txindex1, oindex1,
blknum2, txindex2, oindex2,
utils.normalize_address(cur12),
Expand All @@ -91,7 +92,7 @@ def submitblock(client, key):

# Get the current block, already decoded by client
block = client_call(client.get_current_block)

print("block number {0}".format(block.number))
# Sign the block
block.make_mutable()
normalized_key = utils.normalize_key(key)
Expand Down
9 changes: 7 additions & 2 deletions plasma/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def sign_transaction(self, transaction, key1=b'', key2=b''):
return transaction

def deposit(self, amount, owner):
print("Owner Address {0}".format(owner))
self.root_chain.deposit(transact={'from': owner, 'value': amount})

def apply_transaction(self, transaction):
Expand All @@ -51,10 +52,14 @@ def withdraw(self, blknum, txindex, oindex, tx, proof, sigs):
encoded_transaction = rlp.encode(tx, UnsignedTransaction)
owner = tx.newowner1 if oindex == 0 else tx.newowner2
owner_addr = address.to_checksum_address('0x' + owner.hex())
self.root_chain.startExit(utxo_pos, encoded_transaction, proof, sigs, transact={'from': owner_addr})
bond = 1234567890
self.root_chain.startExit(utxo_pos, encoded_transaction, proof, sigs, transact={'from': owner_addr, 'value': bond})

def withdraw_deposit(self, owner, deposit_pos, amount):
self.root_chain.startDepositExit(deposit_pos, NULL_ADDRESS, amount, transact={'from': owner})
print("wd utxo_pos {0}".format(deposit_pos))
print("wd amount {0}".format(amount))
bond = 1234567890
self.root_chain.startDepositExit(deposit_pos, NULL_ADDRESS, amount, transact={'from': owner, 'value': bond})

def get_transaction(self, blknum, txindex):
encoded_transaction = self.child_chain.get_transaction(blknum, txindex)
Expand Down
7 changes: 7 additions & 0 deletions plasma/root_chain/contracts/RootChain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ contract RootChain {
address token
);

event Msgsender(
address sender,
uint256 bond,
address token
);

/*
* Storage
Expand Down Expand Up @@ -159,6 +164,8 @@ contract RootChain {
{
uint256 blknum = _depositPos / 1000000000;

emit Msgsender(msg.sender, msg.value, _token);

// Check that the given UTXO is a deposit.
require(blknum % CHILD_BLOCK_INTERVAL != 0, "Referenced block must be a deposit block.");

Expand Down
1 change: 1 addition & 0 deletions plasma/root_chain/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def deploy_contract(self, contract_name, gas=5000000, args=(), concise=True):
contract_instance = self.w3.eth.contract(address=contract_address, abi=abi)

print("Successfully deployed {0} contract!".format(contract_name))
print("Address {0}".format(contract_address))

return ConciseContract(contract_instance) if concise else contract_instance

Expand Down
32 changes: 32 additions & 0 deletions plasma_core/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def add_block(self, block):

# Insert the block into the chain.
self._apply_block(block)
print("add_block self.blocks {0}".format(self.blocks))

# Update the head state.
if is_next_child_block:
Expand Down Expand Up @@ -60,6 +61,23 @@ def validate_transaction(self, tx, temp_spent={}):
if blknum == 0:
continue

print("blocknum {0}".format(blknum))
print("blocks {0}".format(self.blocks))

if not self.blocks:
print("blocks are empty")
return
if blknum not in self.blocks:
print("block num not in dictionary")
return
print("self blocks {0}".format(self.blocks[blknum]))

if not self.blocks[blknum].transaction_set:
print("transaction_set is empty")
return
print("transaction_set {0}".format(self.blocks[blknum].transaction_set))
print("transaction_set list {0}".format(self.blocks[blknum].transaction_set[txindex]))

input_tx = self.blocks[blknum].transaction_set[txindex]

if oindex == 0:
Expand Down Expand Up @@ -87,11 +105,25 @@ def get_block(self, blknum):

def get_transaction(self, utxo_id):
(blknum, txindex, _) = decode_utxo_id(utxo_id)
print("blocks {0}".format(self.blocks))
if not self.blocks:
print("blocks are empty")
return {}
if blknum not in self.blocks:
print("block num not in dictionary")
return {}
if not self.blocks[blknum].transaction_set:
print("transaction_set is empty")
return {}
return self.blocks[blknum].transaction_set[txindex]

def mark_utxo_spent(self, utxo_id):
(_, _, oindex) = decode_utxo_id(utxo_id)
tx = self.get_transaction(utxo_id)
print("tx {0}".format(tx))
if not tx:
print("tx is empty")
return
if oindex == 0:
tx.spent1 = True
else:
Expand Down