-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from musalbas/cross-contract-clean
Cross contract clean
- Loading branch information
Showing
6 changed files
with
222 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
chainspacecontract/chainspacecontract/examples/increment_thrice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""An example smart contract to demonstrate three layer deep cross-contract | ||
calls.""" | ||
|
||
from chainspacecontract import ChainspaceContract | ||
from chainspacecontract.examples.increment_twice import contract as increment_twice_contract | ||
|
||
contract = ChainspaceContract('increment_thrice') | ||
contract.register_dependency(increment_twice_contract) | ||
|
||
|
||
@contract.method('init') | ||
def init(): | ||
return { | ||
'outputs': (0,) | ||
} | ||
|
||
|
||
@contract.method('increment') | ||
def increment(inputs, reference_inputs, parameters): | ||
increment_twice_contract.increment((reference_inputs[0],), (reference_inputs[1],), None) | ||
return { | ||
'outputs': (inputs[0] + 1,) | ||
} | ||
|
||
if __name__ == '__main__': | ||
contract.run() |
25 changes: 25 additions & 0 deletions
25
chainspacecontract/chainspacecontract/examples/increment_twice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""An example smart contract to demonstrate cross-contract calls.""" | ||
|
||
from chainspacecontract import ChainspaceContract | ||
from chainspacecontract.examples.increment import contract as increment_contract | ||
|
||
contract = ChainspaceContract('increment_twice') | ||
contract.register_dependency(increment_contract) | ||
|
||
|
||
@contract.method('init') | ||
def init(): | ||
return { | ||
'outputs': (0,) | ||
} | ||
|
||
|
||
@contract.method('increment') | ||
def increment(inputs, reference_inputs, parameters): | ||
increment_contract.increment((reference_inputs[0],), None, None) | ||
return { | ||
'outputs': (inputs[0] + 1,) | ||
} | ||
|
||
if __name__ == '__main__': | ||
contract.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
chainspacecontract/chainspacecontract/test/examples_bank_authenticated.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from multiprocessing import Process | ||
import time | ||
import unittest | ||
|
||
import requests | ||
|
||
from chainspacecontract.examples.bank_authenticated import contract as bank_authenticated_contract | ||
from chainspacecontract.examples import bank_authenticated | ||
|
||
|
||
class TestBankAuthenticated(unittest.TestCase): | ||
############################################################################################# | ||
# test an authenticated bank transfer | ||
############################################################################################# | ||
def test_bank_authenticated_checker_service(self): | ||
checker_service_process = Process(target=bank_authenticated_contract.run_checker_service) | ||
checker_service_process.start() | ||
time.sleep(0.1) | ||
|
||
# NOTE: export public key | ||
""" | ||
G = EcGroup() | ||
g = G.generator() | ||
priv = G.order().random() | ||
pub = priv * g | ||
byte_string = pub.export() | ||
print hexlify(byte_string) | ||
print EcPt.from_binary(byte_string, G) == pub | ||
""" | ||
|
||
response = requests.post('http://127.0.0.1:5000/auth_transfer', | ||
json=bank_authenticated.auth_transfer( | ||
[{'name': 'alice', 'balance': 10}, {'name': 'bob', 'balance': 10}], | ||
None, | ||
{'amount': 3}, | ||
'83C72CF7E1BA9F120C5A45135A0FE3DA59D7771BB9C670B63134A8B0' | ||
) | ||
) | ||
response_json = response.json() | ||
self.assertTrue(response_json['success']) | ||
|
||
checker_service_process.terminate() | ||
checker_service_process.join() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |