This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathclient.py
60 lines (47 loc) · 2.42 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import rlp
from web3.contract import ConciseContract
from web3 import HTTPProvider
from plasma.config import plasma_config
from plasma.root_chain.deployer import Deployer
from plasma.child_chain.transaction import Transaction, UnsignedTransaction
from .child_chain_service import ChildChainService
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.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,
blknum2=0, txindex2=0, oindex2=0,
newowner1=b'\x00' * 20, amount1=0,
newowner2=b'\x00' * 20, amount2=0,
fee=0):
return Transaction(blknum1, txindex1, oindex1,
blknum2, txindex2, oindex2,
newowner1, amount1,
newowner2, amount2,
fee)
def sign_transaction(self, transaction, key1=b'', key2=b''):
if key1:
transaction.sign1(key1)
if key2:
transaction.sign1(key2)
return transaction
def deposit(self, transaction):
self.root_chain.deposit(transact={'from': '0x' + transaction.newowner1.hex(), 'value': transaction.amount1})
def apply_transaction(self, transaction):
self.child_chain.apply_transaction(transaction)
def submit_block(self, block):
self.child_chain.submit_block(block)
def withdraw(self, txPos, tx, proof, sigs):
utxoPos = txPos[0] * 1000000000 + txPos[1] * 10000 + txPos[2] * 1
self.root_chain.startExit(utxoPos, rlp.encode(tx, UnsignedTransaction), proof, sigs, transact={'from': '0x' + tx.newowner1.hex()})
def get_transaction(self, blknum, txindex):
return self.child_chain.get_transaction(blknum, txindex)
def get_current_block(self):
return self.child_chain.get_current_block()
def get_block(self, blknum):
return self.child_chain.get_block(blknum)
def get_current_block_num(self):
return self.child_chain.get_current_block_num()