22#![ allow( non_snake_case) ]
33#![ feature( proc_macro_hygiene) ]
44
5- extern crate pwasm_std;
6- extern crate pwasm_ethereum;
75extern crate pwasm_abi;
86extern crate pwasm_abi_derive;
7+ extern crate pwasm_ethereum;
8+ extern crate pwasm_std;
99
10+ pub mod proc_table;
1011pub mod validator;
1112
13+ type ProcedureKey = [ u8 ; 24 ] ;
14+
1215pub mod token {
13- use pwasm_ethereum;
1416 use pwasm_abi:: types:: * ;
17+ use pwasm_ethereum;
1518
1619 // eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html
1720 use pwasm_abi_derive:: eth_abi;
@@ -27,87 +30,61 @@ pub mod token {
2730 ) ;
2831 }
2932
30- #[ eth_abi( TokenEndpoint , TokenClient ) ]
31- pub trait TokenInterface {
32- /// The constructor
33- fn constructor ( & mut self , _total_supply : U256 ) ;
34- /// Total amount of tokens
33+ #[ eth_abi( TokenEndpoint , KernelClient ) ]
34+ pub trait KernelInterface {
35+ /// The constructor set with Initial Entry Procedure
36+ fn constructor ( & mut self , _entry_proc_key : String , _entry_proc_address : Address ) ;
37+ /// Get Entry Procedure
3538 #[ constant]
36- fn totalSupply ( & mut self ) -> U256 ;
37- /// What is the balance of a particular account?
39+ fn entryProcedure ( & mut self ) -> String ;
40+ /// Get Current Executing Procedure
3841 #[ constant]
39- fn balanceOf ( & mut self , _owner : Address ) -> U256 ;
40- /// Transfer the balance from owner's account to another account
41- fn transfer ( & mut self , _to : Address , _amount : U256 ) -> bool ;
42- /// Event declaration
43- #[ event]
44- fn Transfer ( & mut self , indexed_from : Address , indexed_to : Address , _value : U256 ) ;
42+ fn currentProcedure ( & mut self ) -> String ;
43+
44+ /// Get Procedure Address By Key
45+ /// Returns 0 if Procedure Not Found
46+ fn getProcedureByKey ( & mut self , _proc_key : String ) -> Address ;
4547 }
4648
47- pub struct TokenContract ;
48-
49- impl TokenInterface for TokenContract {
50- fn constructor ( & mut self , total_supply : U256 ) {
51- let sender = pwasm_ethereum :: sender ( ) ;
52- // Set up the total supply for the token
53- pwasm_ethereum :: write ( & TOTAL_SUPPLY_KEY , & total_supply . into ( ) ) ;
54- // Give all tokens to the contract owner
55- pwasm_ethereum :: write ( & balance_key ( & sender ) , & total_supply . into ( ) ) ;
56- // Set the contract owner
57- pwasm_ethereum :: write ( & OWNER_KEY , & H256 :: from ( sender ) . into ( ) ) ;
49+ pub struct KernelContract ;
50+
51+ impl KernelInterface for KernelContract {
52+ fn constructor ( & mut self , _entry_proc_key : String , _entry_proc_address : Address ) {
53+ // // Set up the total supply for the token
54+ // pwasm_ethereum::write(&TOTAL_SUPPLY_KEY, &total_supply.into());
55+ // // Give all tokens to the contract owner
56+ // pwasm_ethereum::write(&balance_key(&sender), &total_supply.into());
57+ // // Set the contract owner
58+ // pwasm_ethereum::write(&OWNER_KEY, &H256::from(sender).into());
59+ unimplemented ! ( )
5860 }
5961
60- fn totalSupply ( & mut self ) -> U256 {
61- U256 :: from_big_endian ( & pwasm_ethereum :: read ( & TOTAL_SUPPLY_KEY ) )
62+ fn entryProcedure ( & mut self ) -> String {
63+ unimplemented ! ( )
6264 }
6365
64- fn balanceOf ( & mut self , owner : Address ) -> U256 {
65- read_balance_of ( & owner )
66+ fn currentProcedure ( & mut self ) -> String {
67+ unimplemented ! ( )
6668 }
6769
68- fn transfer ( & mut self , to : Address , amount : U256 ) -> bool {
69- let sender = pwasm_ethereum:: sender ( ) ;
70- let senderBalance = read_balance_of ( & sender) ;
71- let recipientBalance = read_balance_of ( & to) ;
72- if amount == 0 . into ( ) || senderBalance < amount || to == sender {
73- false
74- } else {
75- let new_sender_balance = senderBalance - amount;
76- let new_recipient_balance = recipientBalance + amount;
77- pwasm_ethereum:: write ( & balance_key ( & sender) , & new_sender_balance. into ( ) ) ;
78- pwasm_ethereum:: write ( & balance_key ( & to) , & new_recipient_balance. into ( ) ) ;
79- self . Transfer ( sender, to, amount) ;
80- true
81- }
70+ fn getProcedureByKey ( & mut self , _proc_key : String ) -> Address {
71+ unimplemented ! ( )
8272 }
8373 }
84-
85- // Reads balance by address
86- fn read_balance_of ( owner : & Address ) -> U256 {
87- U256 :: from_big_endian ( & pwasm_ethereum:: read ( & balance_key ( owner) ) )
88- }
89-
90- // Generates a balance key for some address.
91- // Used to map balances with their owners.
92- fn balance_key ( address : & Address ) -> H256 {
93- let mut key = H256 :: from ( * address) ;
94- key. as_bytes_mut ( ) [ 0 ] = 1 ; // just a naive "namespace";
95- key
96- }
9774}
9875// Declares the dispatch and dispatch_ctor methods
9976use pwasm_abi:: eth:: EndpointInterface ;
10077
10178#[ no_mangle]
10279pub fn call ( ) {
103- let mut endpoint = token:: TokenEndpoint :: new ( token:: TokenContract { } ) ;
80+ let mut endpoint = token:: TokenEndpoint :: new ( token:: KernelContract { } ) ;
10481 // Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details
10582 pwasm_ethereum:: ret ( & endpoint. dispatch ( & pwasm_ethereum:: input ( ) ) ) ;
10683}
10784
10885#[ no_mangle]
10986pub fn deploy ( ) {
110- let mut endpoint = token:: TokenEndpoint :: new ( token:: TokenContract { } ) ;
87+ let mut endpoint = token:: TokenEndpoint :: new ( token:: KernelContract { } ) ;
11188 endpoint. dispatch_ctor ( & pwasm_ethereum:: input ( ) ) ;
11289}
11390
@@ -116,41 +93,32 @@ pub fn deploy() {
11693mod tests {
11794 extern crate pwasm_test;
11895 extern crate std;
96+ use self :: pwasm_test:: { ext_get, ext_reset} ;
11997 use super :: * ;
12098 use core:: str:: FromStr ;
12199 use pwasm_abi:: types:: * ;
122- use self :: pwasm_test:: { ext_reset, ext_get} ;
123- use token:: TokenInterface ;
100+ use token:: KernelInterface ;
124101
125102 #[ test]
126- fn should_succeed_transfering_1000_from_owner_to_another_address ( ) {
127- let mut contract = token:: TokenContract { } ;
103+ #[ ignore]
104+ fn should_initialize_with_entry_procedure ( ) {
105+ let mut contract = token:: KernelContract { } ;
106+
128107 let owner_address = Address :: from_str ( "ea674fdde714fd979de3edf0f56aa9716b898ec8" ) . unwrap ( ) ;
129- let sam_address = Address :: from_str ( "db6fd484cfa46eeeb73c71edee823e4812f9e2e1" ) . unwrap ( ) ;
108+ let entry_proc_key = pwasm_abi:: types:: String :: from ( "init" ) ;
109+ let entry_proc_address =
110+ Address :: from_str ( "db6fd484cfa46eeeb73c71edee823e4812f9e2e1" ) . unwrap ( ) ;
111+
130112 // Here we're creating an External context using ExternalBuilder and set the `sender` to the `owner_address`
131- // so `pwasm_ethereum::sender()` in TokenInterface ::constructor() will return that `owner_address`
113+ // so `pwasm_ethereum::sender()` in KernelInterface ::constructor() will return that `owner_address`
132114 ext_reset ( |e| e. sender ( owner_address. clone ( ) ) ) ;
133- let total_supply = 10000 . into ( ) ;
134- contract. constructor ( total_supply) ;
135- assert_eq ! ( contract. balanceOf( owner_address) , total_supply) ;
136- assert_eq ! ( contract. transfer( sam_address, 1000 . into( ) ) , true ) ;
137- assert_eq ! ( contract. balanceOf( owner_address) , 9000 . into( ) ) ;
138- assert_eq ! ( contract. balanceOf( sam_address) , 1000 . into( ) ) ;
139- // 1 log entry should be created
140- assert_eq ! ( ext_get( ) . logs( ) . len( ) , 1 ) ;
141- }
142115
143- #[ test]
144- fn should_not_transfer_to_self ( ) {
145- let mut contract = token:: TokenContract { } ;
146- let owner_address = Address :: from_str ( "ea674fdde714fd979de3edf0f56aa9716b898ec8" ) . unwrap ( ) ;
147- ext_reset ( |e| e. sender ( owner_address. clone ( ) ) ) ;
148- let total_supply = 10000 . into ( ) ;
149- contract. constructor ( total_supply) ;
150- assert_eq ! ( contract. balanceOf( owner_address) , total_supply) ;
151- assert_eq ! ( contract. transfer( owner_address, 1000 . into( ) ) , false ) ;
152- assert_eq ! ( contract. balanceOf( owner_address) , 10000 . into( ) ) ;
153- assert_eq ! ( ext_get( ) . logs( ) . len( ) , 0 ) ;
116+ contract. constructor ( entry_proc_key. clone ( ) , entry_proc_address. clone ( ) ) ;
117+
118+ assert_eq ! ( contract. entryProcedure( ) , entry_proc_key) ;
119+ assert_eq ! ( contract. currentProcedure( ) , unsafe {
120+ String :: from_utf8_unchecked( [ 0 ; 32 ] . to_vec( ) )
121+ } ) ;
154122 }
155123
156- }
124+ }
0 commit comments