@@ -4,7 +4,9 @@ use sha2::{Digest, Sha256};
44use strata_primitives:: buf:: Buf32 ;
55use thiserror:: Error ;
66
7- use crate :: account:: { AccountId , AccountSerial , AccountState , SnarkAccountMessageEntry } ;
7+ use crate :: account:: {
8+ AccountId , AccountInnerState , AccountSerial , AccountState , SnarkAccountMessageEntry ,
9+ } ;
810
911#[ derive( Debug , Error ) ]
1012pub enum LedgerError { }
@@ -14,18 +16,21 @@ pub type LedgerResult<T> = Result<T, LedgerError>;
1416/// Interface for accessing and modifying accounts ledger
1517pub trait LedgerProvider {
1618 /// Root of the current accounts ledger. For example root of the accounts trie.
17- fn root ( & self ) -> LedgerResult < Buf32 > ;
19+ fn accounts_root ( & self ) -> LedgerResult < Buf32 > ;
1820
1921 /// Get account id from serial
20- fn account_id ( & self , serial : AccountSerial ) -> LedgerResult < Option < AccountId > > ;
22+ fn get_account_id ( & self , serial : AccountSerial ) -> LedgerResult < Option < AccountId > > ;
2123
2224 /// Get an account state
23- fn account_state ( & self , acct_id : & AccountId ) -> LedgerResult < Option < AccountState > > ;
25+ fn get_account_state ( & self , acct_id : & AccountId ) -> LedgerResult < Option < AccountState > > ;
2426
2527 /// Convenient method for accessing state via serial.
26- fn account_serial_state ( & self , serial : AccountSerial ) -> LedgerResult < Option < AccountState > > {
27- if let Some ( acct_id) = self . account_id ( serial) ? {
28- self . account_state ( & acct_id)
28+ fn get_account_state_by_serial (
29+ & self ,
30+ serial : AccountSerial ,
31+ ) -> LedgerResult < Option < AccountState > > {
32+ if let Some ( acct_id) = self . get_account_id ( serial) ? {
33+ self . get_account_state ( & acct_id)
2934 } else {
3035 Ok ( None )
3136 }
@@ -42,23 +47,14 @@ pub trait LedgerProvider {
4247 // TODO: message can be a bit generic instead of snark message?
4348 fn insert_message (
4449 & mut self ,
45- acct_id : AccountId ,
50+ acct_id : & AccountId ,
4651 message : SnarkAccountMessageEntry ,
4752 ) -> LedgerResult < ( ) > ;
48-
49- /// Consume input messages. Most likely updates some input index in state
50- fn consume_messages (
51- & mut self ,
52- acct_id : AccountId ,
53- from_idx : u64 ,
54- to_idx : u64 ,
55- ) -> LedgerResult < ( ) > ;
5653}
5754
5855/// Simplest in-memory ledger. All it has is an in-memory map of acct id to list of messages.
5956#[ derive( Debug , Clone ) ]
6057pub struct InMemoryVectorLedger {
61- pub acct_msgs : HashMap < AccountId , Vec < SnarkAccountMessageEntry > > ,
6258 pub serial_to_id : HashMap < AccountSerial , AccountId > ,
6359 pub account_states : HashMap < AccountId , AccountState > ,
6460 pub root_cache : Option < Buf32 > ,
@@ -67,7 +63,6 @@ pub struct InMemoryVectorLedger {
6763impl InMemoryVectorLedger {
6864 pub fn new ( ) -> Self {
6965 Self {
70- acct_msgs : HashMap :: new ( ) ,
7166 serial_to_id : HashMap :: new ( ) ,
7267 account_states : HashMap :: new ( ) ,
7368 root_cache : None ,
@@ -77,7 +72,6 @@ impl InMemoryVectorLedger {
7772 pub fn create_account ( & mut self , serial : AccountSerial , id : AccountId , state : AccountState ) {
7873 self . serial_to_id . insert ( serial, id) ;
7974 self . account_states . insert ( id, state) ;
80- self . acct_msgs . insert ( id, Vec :: new ( ) ) ;
8175 self . invalidate_root_cache ( ) ;
8276 }
8377
@@ -90,7 +84,6 @@ impl InMemoryVectorLedger {
9084 return Ok ( * cached) ;
9185 }
9286
93- use sha2:: { Digest , Sha256 } ;
9487 let mut hasher = Sha256 :: new ( ) ;
9588
9689 // Sort account IDs for deterministic ordering
@@ -100,9 +93,9 @@ impl InMemoryVectorLedger {
10093 for account_id in sorted_accounts {
10194 hasher. update ( account_id. as_ref ( ) ) ;
10295 if let Some ( state) = self . account_states . get ( account_id) {
103- hasher. update ( & state. serial . to_be_bytes ( ) ) ;
104- hasher. update ( & state. ty . to_be_bytes ( ) ) ;
105- hasher. update ( & state. balance . to_be_bytes ( ) ) ;
96+ hasher. update ( state. serial . to_be_bytes ( ) ) ;
97+ hasher. update ( state. ty . to_be_bytes ( ) ) ;
98+ hasher. update ( state. balance . to_be_bytes ( ) ) ;
10699 }
107100 }
108101
@@ -119,7 +112,7 @@ impl Default for InMemoryVectorLedger {
119112}
120113
121114impl LedgerProvider for InMemoryVectorLedger {
122- fn root ( & self ) -> LedgerResult < Buf32 > {
115+ fn accounts_root ( & self ) -> LedgerResult < Buf32 > {
123116 // Need mutable access to compute/cache root, but trait requires &self
124117 // For now, recompute every time (inefficient but correct)
125118 let mut hasher = Sha256 :: new ( ) ;
@@ -130,53 +123,33 @@ impl LedgerProvider for InMemoryVectorLedger {
130123 for account_id in sorted_accounts {
131124 hasher. update ( account_id. as_ref ( ) ) ;
132125 if let Some ( state) = self . account_states . get ( account_id) {
133- hasher. update ( & state. serial . to_be_bytes ( ) ) ;
134- hasher. update ( & state. ty . to_be_bytes ( ) ) ;
135- hasher. update ( & state. balance . to_be_bytes ( ) ) ;
126+ hasher. update ( state. serial . to_be_bytes ( ) ) ;
127+ hasher. update ( state. ty . to_be_bytes ( ) ) ;
128+ hasher. update ( state. balance . to_be_bytes ( ) ) ;
136129 }
137130 }
138131
139132 Ok ( Buf32 :: new ( hasher. finalize ( ) . into ( ) ) )
140133 }
141134
142- fn account_id ( & self , serial : AccountSerial ) -> LedgerResult < Option < AccountId > > {
135+ fn get_account_id ( & self , serial : AccountSerial ) -> LedgerResult < Option < AccountId > > {
143136 Ok ( self . serial_to_id . get ( & serial) . copied ( ) )
144137 }
145138
146- fn account_state ( & self , acct_id : & AccountId ) -> LedgerResult < Option < AccountState > > {
139+ fn get_account_state ( & self , acct_id : & AccountId ) -> LedgerResult < Option < AccountState > > {
147140 Ok ( self . account_states . get ( acct_id) . cloned ( ) )
148141 }
149142
150143 fn insert_message (
151144 & mut self ,
152- acct_id : AccountId ,
145+ acct_id : & AccountId ,
153146 message : SnarkAccountMessageEntry ,
154147 ) -> LedgerResult < ( ) > {
155- self . acct_msgs
156- . entry ( acct_id)
157- . or_insert_with ( Vec :: new)
158- . push ( message) ;
159- self . invalidate_root_cache ( ) ;
160- Ok ( ( ) )
161- }
162-
163- fn consume_messages (
164- & mut self ,
165- acct_id : AccountId ,
166- from_idx : u64 ,
167- to_idx : u64 ,
168- ) -> LedgerResult < ( ) > {
169- if let Some ( messages) = self . acct_msgs . get_mut ( & acct_id) {
170- let from_idx = from_idx as usize ;
171- let to_idx = to_idx as usize ;
172-
173- // Validate indices
174- if from_idx > to_idx || to_idx > messages. len ( ) {
175- return Ok ( ( ) ) ; // Invalid range, do nothing
176- }
177-
178- // Remove consumed messages by draining the range
179- messages. drain ( from_idx..to_idx) ;
148+ if let Some ( AccountInnerState :: Snark ( mut acct) ) =
149+ self . get_account_state ( acct_id) ?. map ( |a| a. inner_state )
150+ {
151+ // TODO: Will this actually update the hashmap?
152+ acct. input . push ( message) ;
180153 self . invalidate_root_cache ( ) ;
181154 }
182155 Ok ( ( ) )
0 commit comments