Skip to content

Commit fbf6525

Browse files
SeungMin Leesgkim126
authored andcommitted
Implement the trait AccountManager
1 parent 9e7e1fa commit fbf6525

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

basic_module/account/src/core.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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::types::SignedTransaction;
1819
pub use ckey::{Ed25519Private as Private, Ed25519Public as Public, Error as KeyError, Password, Signature};
1920
pub use coordinator::context::SubStorageAccess;
@@ -26,3 +27,13 @@ pub trait CheckTxHandler {
2627
pub trait TransactionExecutor {
2728
fn execute_transactions(&self, transactions: &[SignedTransaction]) -> Result<Vec<TransactionExecutionOutcome>, ()>;
2829
}
30+
31+
pub trait AccountManager {
32+
fn add_balance(&self, account_id: &Public, val: u64);
33+
34+
fn sub_balance(&self, account_id: &Public, val: u64) -> Result<(), Error>;
35+
36+
fn set_balance(&self, account_id: &Public, val: u64);
37+
38+
fn increment_sequence(&self, account_id: &Public);
39+
}

basic_module/account/src/impls.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
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::check;
18-
use crate::core::{CheckTxHandler, TransactionExecutor};
19-
use crate::internal::{add_balance, get_sequence, sub_balance};
17+
use crate::core::{AccountManager, CheckTxHandler, TransactionExecutor};
18+
use crate::error::Error;
19+
use crate::internal::{add_balance, get_account, get_sequence, sub_balance};
2020
use crate::types::{Action, SignedTransaction};
21+
use crate::{check, get_context};
22+
use ckey::Ed25519Public as Public;
2123
use coordinator::types::{ErrorCode, TransactionExecutionOutcome};
2224

2325
#[allow(dead_code)]
@@ -61,3 +63,32 @@ impl TransactionExecutor for Executor {
6163
Ok(vec![])
6264
}
6365
}
66+
67+
#[allow(dead_code)]
68+
pub struct Manager {}
69+
70+
impl AccountManager for Manager {
71+
fn add_balance(&self, account_id: &Public, val: u64) {
72+
add_balance(account_id, val)
73+
}
74+
75+
fn sub_balance(&self, account_id: &Public, val: u64) -> Result<(), Error> {
76+
sub_balance(account_id, val)
77+
}
78+
79+
fn set_balance(&self, account_id: &Public, val: u64) {
80+
let context = get_context();
81+
let mut account = get_account(account_id);
82+
83+
account.balance = val;
84+
context.set(account_id, account.to_vec());
85+
}
86+
87+
fn increment_sequence(&self, account_id: &Public) {
88+
let context = get_context();
89+
let mut account = get_account(account_id);
90+
91+
account.sequence += 1;
92+
context.set(account_id, account.to_vec());
93+
}
94+
}

0 commit comments

Comments
 (0)