2
2
#![ allow( non_snake_case) ]
3
3
#![ feature( proc_macro_hygiene) ]
4
4
5
- extern crate pwasm_std;
6
- extern crate pwasm_ethereum;
7
5
extern crate pwasm_abi;
8
6
extern crate pwasm_abi_derive;
7
+ extern crate pwasm_ethereum;
8
+ extern crate pwasm_std;
9
9
10
+ pub mod proc_table;
10
11
pub mod validator;
11
12
13
+ type ProcedureKey = [ u8 ; 24 ] ;
14
+
12
15
pub mod token {
13
- use pwasm_ethereum;
14
16
use pwasm_abi:: types:: * ;
17
+ use pwasm_ethereum;
15
18
16
19
// eth_abi is a procedural macros https://doc.rust-lang.org/book/first-edition/procedural-macros.html
17
20
use pwasm_abi_derive:: eth_abi;
@@ -27,87 +30,61 @@ pub mod token {
27
30
) ;
28
31
}
29
32
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
35
38
#[ 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
38
41
#[ 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 ;
45
47
}
46
48
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 ! ( )
58
60
}
59
61
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 ! ( )
62
64
}
63
65
64
- fn balanceOf ( & mut self , owner : Address ) -> U256 {
65
- read_balance_of ( & owner )
66
+ fn currentProcedure ( & mut self ) -> String {
67
+ unimplemented ! ( )
66
68
}
67
69
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 ! ( )
82
72
}
83
73
}
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
- }
97
74
}
98
75
// Declares the dispatch and dispatch_ctor methods
99
76
use pwasm_abi:: eth:: EndpointInterface ;
100
77
101
78
#[ no_mangle]
102
79
pub fn call ( ) {
103
- let mut endpoint = token:: TokenEndpoint :: new ( token:: TokenContract { } ) ;
80
+ let mut endpoint = token:: TokenEndpoint :: new ( token:: KernelContract { } ) ;
104
81
// Read http://solidity.readthedocs.io/en/develop/abi-spec.html#formal-specification-of-the-encoding for details
105
82
pwasm_ethereum:: ret ( & endpoint. dispatch ( & pwasm_ethereum:: input ( ) ) ) ;
106
83
}
107
84
108
85
#[ no_mangle]
109
86
pub fn deploy ( ) {
110
- let mut endpoint = token:: TokenEndpoint :: new ( token:: TokenContract { } ) ;
87
+ let mut endpoint = token:: TokenEndpoint :: new ( token:: KernelContract { } ) ;
111
88
endpoint. dispatch_ctor ( & pwasm_ethereum:: input ( ) ) ;
112
89
}
113
90
@@ -116,41 +93,32 @@ pub fn deploy() {
116
93
mod tests {
117
94
extern crate pwasm_test;
118
95
extern crate std;
96
+ use self :: pwasm_test:: { ext_get, ext_reset} ;
119
97
use super :: * ;
120
98
use core:: str:: FromStr ;
121
99
use pwasm_abi:: types:: * ;
122
- use self :: pwasm_test:: { ext_reset, ext_get} ;
123
- use token:: TokenInterface ;
100
+ use token:: KernelInterface ;
124
101
125
102
#[ 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
+
128
107
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
+
130
112
// 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`
132
114
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
- }
142
115
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
+ } ) ;
154
122
}
155
123
156
- }
124
+ }
0 commit comments