1
1
use async_trait:: async_trait;
2
2
use namada_sdk:: {
3
3
borsh:: { BorshDeserialize , BorshSerialize } ,
4
- masp:: { ContextSyncStatus , DispatcherCache , ShieldedContext , ShieldedUtils } ,
4
+ masp:: { ContextSyncStatus , DispatcherCache , ShieldedUtils } ,
5
5
masp_proofs:: prover:: LocalTxProver ,
6
+ ShieldedWallet ,
6
7
} ;
7
8
use wasm_bindgen:: { prelude:: wasm_bindgen, JsValue } ;
8
9
@@ -21,6 +22,8 @@ const FILE_NAME: &str = "shielded.dat";
21
22
const TMP_FILE_NAME : & str = "shielded.tmp" ;
22
23
const SPECULATIVE_FILE_NAME : & str = "speculative_shielded.dat" ;
23
24
const SPECULATIVE_TMP_FILE_NAME : & str = "speculative_shielded.tmp" ;
25
+ const CACHE_FILE_NAME : & str = "shielded_sync.cache" ;
26
+ const CACHE_FILE_TMP_PREFIX : & str = "shielded_sync.cache.tmp" ;
24
27
25
28
/// Mostly copied from the Namada CLI
26
29
@@ -32,7 +35,7 @@ pub struct NodeShieldedUtils {
32
35
}
33
36
34
37
impl NodeShieldedUtils {
35
- pub async fn new ( context_dir : & str ) -> ShieldedContext < Self > {
38
+ pub async fn new ( context_dir : & str ) -> ShieldedWallet < Self > {
36
39
let context_dir = PathBuf :: from ( context_dir) ;
37
40
38
41
let spend_path = context_dir. join ( SPEND_NAME ) ;
@@ -56,7 +59,7 @@ impl NodeShieldedUtils {
56
59
57
60
let utils = Self { context_dir } ;
58
61
59
- ShieldedContext {
62
+ ShieldedWallet {
60
63
utils,
61
64
sync_status,
62
65
..Default :: default ( )
@@ -85,7 +88,7 @@ impl ShieldedUtils for NodeShieldedUtils {
85
88
86
89
async fn load < U : ShieldedUtils > (
87
90
& self ,
88
- ctx : & mut ShieldedContext < U > ,
91
+ ctx : & mut ShieldedWallet < U > ,
89
92
force_confirmed : bool ,
90
93
) -> std:: io:: Result < ( ) > {
91
94
let file_name = if force_confirmed {
@@ -101,14 +104,14 @@ impl ShieldedUtils for NodeShieldedUtils {
101
104
//TODO: change to_bytes to sth more descripive, add "from_bytes"
102
105
let bytes = to_bytes ( read_file_sync ( path) . unwrap ( ) . into ( ) ) ;
103
106
104
- * ctx = ShieldedContext {
107
+ * ctx = ShieldedWallet {
105
108
utils : ctx. utils . clone ( ) ,
106
- ..ShieldedContext :: < U > :: deserialize ( & mut & bytes[ ..] ) ?
109
+ ..ShieldedWallet :: < U > :: deserialize ( & mut & bytes[ ..] ) ?
107
110
} ;
108
111
Ok ( ( ) )
109
112
}
110
113
111
- async fn save < U : ShieldedUtils > ( & self , ctx : & ShieldedContext < U > ) -> std:: io:: Result < ( ) > {
114
+ async fn save < U : ShieldedUtils > ( & self , ctx : & ShieldedWallet < U > ) -> std:: io:: Result < ( ) > {
112
115
let ( tmp_file_name, file_name) = match ctx. sync_status {
113
116
ContextSyncStatus :: Confirmed => ( TMP_FILE_NAME , FILE_NAME ) ,
114
117
ContextSyncStatus :: Speculative => ( SPECULATIVE_TMP_FILE_NAME , SPECULATIVE_FILE_NAME ) ,
@@ -139,9 +142,20 @@ impl ShieldedUtils for NodeShieldedUtils {
139
142
140
143
/// Save a cache of data as part of shielded sync if that
141
144
/// process gets interrupted.
142
- async fn cache_save ( & self , _cache : & DispatcherCache ) -> std:: io:: Result < ( ) > {
143
- // TODO:
144
- todo ! ( )
145
+ async fn cache_save ( & self , cache : & DispatcherCache ) -> std:: io:: Result < ( ) > {
146
+ let tmp_path = path_buf_to_js_value ( self . context_dir . join ( CACHE_FILE_TMP_PREFIX ) ) ;
147
+ {
148
+ let mut bytes = Vec :: new ( ) ;
149
+ cache. serialize ( & mut bytes) . expect ( "cannot serialize cache" ) ;
150
+ let uint8_array = js_sys:: Uint8Array :: from ( & bytes[ ..] ) ;
151
+
152
+ write_file_sync ( tmp_path. clone ( ) , uint8_array. into ( ) ) . unwrap ( ) ;
153
+ }
154
+
155
+ let new_path = path_buf_to_js_value ( self . context_dir . join ( CACHE_FILE_NAME ) ) ;
156
+ renameSync ( tmp_path, new_path) . unwrap ( ) ;
157
+
158
+ Ok ( ( ) )
145
159
}
146
160
147
161
/// Load a cache of data as part of shielded sync if that
0 commit comments