Skip to content

Commit 9e7e1fa

Browse files
SeungMin Leesgkim126
authored andcommitted
Impelment the trait TransactionExecutor
1 parent 10d4f28 commit 9e7e1fa

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

basic_module/account/src/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ pub use coordinator::types::{ErrorCode, TransactionExecutionOutcome};
2222
pub 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+
}

basic_module/account/src/impls.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
use 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};
2020
use crate::types::{Action, SignedTransaction};
21-
pub use coordinator::types::ErrorCode;
21+
use coordinator::types::{ErrorCode, TransactionExecutionOutcome};
2222

2323
#[allow(dead_code)]
2424
pub 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+
}

basic_module/account/src/internal.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,36 @@
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;
1718
use crate::get_context;
1819
use crate::types::Account;
1920
use 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+
2147
pub fn get_sequence(account_id: &Public) -> u64 {
2248
get_account(account_id).sequence
2349
}

0 commit comments

Comments
 (0)