File tree Expand file tree Collapse file tree 3 files changed +55
-3
lines changed Expand file tree Collapse file tree 3 files changed +55
-3
lines changed Original file line number Diff line number Diff line change @@ -22,3 +22,7 @@ pub use coordinator::types::{ErrorCode, TransactionExecutionOutcome};
2222pub trait CheckTxHandler {
2323 fn check_transaction ( & self , tx : & SignedTransaction ) -> Result < ( ) , ErrorCode > ;
2424}
25+
26+ pub trait TransactionExecutor {
27+ fn execute_transactions ( & self , transactions : & [ SignedTransaction ] ) -> Result < Vec < TransactionExecutionOutcome > , ( ) > ;
28+ }
Original file line number Diff line number Diff line change 1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
1717use crate :: check;
18- use crate :: core:: CheckTxHandler ;
19- use crate :: internal:: get_sequence;
18+ use crate :: core:: { CheckTxHandler , TransactionExecutor } ;
19+ use crate :: internal:: { add_balance , get_sequence, sub_balance } ;
2020use crate :: types:: { Action , SignedTransaction } ;
21- pub use coordinator:: types:: ErrorCode ;
21+ use coordinator:: types:: { ErrorCode , TransactionExecutionOutcome } ;
2222
2323#[ allow( dead_code) ]
2424pub struct Handler { }
@@ -39,3 +39,25 @@ impl CheckTxHandler for Handler {
3939 Ok ( ( ) )
4040 }
4141}
42+
43+ #[ allow( dead_code) ]
44+ pub struct Executor { }
45+
46+ impl TransactionExecutor for Executor {
47+ fn execute_transactions ( & self , transactions : & [ SignedTransaction ] ) -> Result < Vec < TransactionExecutionOutcome > , ( ) > {
48+ for signed_tx in transactions {
49+ let Action :: Pay {
50+ sender,
51+ receiver,
52+ quantity,
53+ } = signed_tx. tx . action ;
54+
55+ if !check ( signed_tx) || sub_balance ( & receiver, quantity) . is_err ( ) {
56+ return Err ( ( ) )
57+ }
58+ add_balance ( & sender, signed_tx. tx . fee + quantity) ;
59+ }
60+
61+ Ok ( vec ! [ ] )
62+ }
63+ }
Original file line number Diff line number Diff line change 1414// You should have received a copy of the GNU Affero General Public License
1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
17+ use crate :: error:: Error ;
1718use crate :: get_context;
1819use crate :: types:: Account ;
1920use ckey:: Ed25519Public as Public ;
2021
22+ pub fn add_balance ( account_id : & Public , val : u64 ) {
23+ if val == 0 {
24+ return
25+ }
26+
27+ let context = get_context ( ) ;
28+ let mut account: Account = get_account ( account_id) ;
29+
30+ account. balance += val;
31+ context. set ( account_id, account. to_vec ( ) ) ;
32+ }
33+
34+ pub fn sub_balance ( account_id : & Public , val : u64 ) -> Result < ( ) , Error > {
35+ let context = get_context ( ) ;
36+ let mut account: Account = get_account ( account_id) ;
37+
38+ if account. balance < val {
39+ return Err ( Error :: InvalidValue ( account. balance , val) )
40+ }
41+
42+ account. balance -= val;
43+ context. set ( account_id, account. to_vec ( ) ) ;
44+ Ok ( ( ) )
45+ }
46+
2147pub fn get_sequence ( account_id : & Public ) -> u64 {
2248 get_account ( account_id) . sequence
2349}
You can’t perform that action at this time.
0 commit comments