@@ -2,9 +2,10 @@ use crate::db::*;
22use anyhow:: { Context , Result , anyhow, bail} ;
33use rusqlite:: Transaction ;
44use shared:: bee_msg:: buddy_group:: CombinedTargetState ;
5+ use shared:: bee_msg:: node:: Nic ;
56use shared:: bee_msg:: quota:: QuotaEntry ;
67use shared:: bee_msg:: storage_pool:: StoragePool ;
7- use shared:: bee_serde:: { Deserializable , Deserializer } ;
8+ use shared:: bee_serde:: { BeeSerdeConversion , Deserializable , Deserializer } ;
89use shared:: types:: * ;
910use sqlite_check:: sql;
1011use std:: io:: Write ;
@@ -112,6 +113,23 @@ fn check_target_states(f: &Path) -> Result<()> {
112113 Ok ( ( ) )
113114}
114115
116+ fn node_nics ( tx : & Transaction , node_uid : Uid , nics : Vec < Nic > ) -> Result < ( ) > {
117+ let mut stmt = tx. prepare_cached ( sql ! (
118+ "INSERT INTO node_nics (node_uid, nic_type, addr, name) VALUES (?1, ?2, ?3, ?4)"
119+ ) ) ?;
120+
121+ for nic in nics {
122+ stmt. execute ( params ! [
123+ node_uid,
124+ nic. nic_type. sql_variant( ) ,
125+ nic. addr. to_string( ) ,
126+ String :: from_utf8_lossy( & nic. name)
127+ ] ) ?;
128+ }
129+
130+ Ok ( ( ) )
131+ }
132+
115133/// Imports meta nodes / targets. Intentionally ignores nics as they are refreshed on first contact
116134/// anyway.
117135fn meta_nodes ( tx : & Transaction , f : & Path ) -> Result < ( NodeId , bool ) > {
@@ -121,8 +139,9 @@ fn meta_nodes(tx: &Transaction, f: &Path) -> Result<(NodeId, bool)> {
121139 nodes,
122140 } = read_nodes ( f) ?;
123141
124- for ( num_id, port) in nodes {
125- node:: insert ( tx, num_id, None , NodeType :: Meta , port) ?;
142+ for ( num_id, port, nics) in nodes {
143+ let node = node:: insert ( tx, num_id, None , NodeType :: Meta , port) ?;
144+ node_nics ( tx, node. uid , nics) ?;
126145
127146 // A meta target has to be explicitly created with the same ID as the node.
128147 let Ok ( target_id) = TargetId :: try_from ( num_id) else {
@@ -151,8 +170,9 @@ fn meta_nodes(tx: &Transaction, f: &Path) -> Result<(NodeId, bool)> {
151170fn storage_nodes ( tx : & Transaction , f : & Path ) -> Result < ( ) > {
152171 let ReadNodesResult { nodes, .. } = read_nodes ( f) ?;
153172
154- for ( num_id, port) in nodes {
155- node:: insert ( tx, num_id, None , NodeType :: Storage , port) ?;
173+ for ( num_id, port, nics) in nodes {
174+ let node = node:: insert ( tx, num_id, None , NodeType :: Storage , port) ?;
175+ node_nics ( tx, node. uid , nics) ?;
156176 }
157177
158178 Ok ( ( ) )
@@ -161,7 +181,7 @@ fn storage_nodes(tx: &Transaction, f: &Path) -> Result<()> {
161181struct ReadNodesResult {
162182 root_id : NodeId ,
163183 root_mirrored : bool ,
164- nodes : Vec < ( NodeId , Port ) > ,
184+ nodes : Vec < ( NodeId , Port , Vec < Nic > ) > ,
165185}
166186
167187// Deserialize nodes from file
@@ -175,17 +195,34 @@ fn read_nodes(f: &Path) -> Result<ReadNodesResult> {
175195
176196 // Define the node data deserialization manually because the `Nic` type used by `Node` had
177197 // changes for v8 (the ipv6 changes). The v7 on-disk-data is of course still in the old format.
178- // We only need the num id and the port, everything else is ignored.
179198 let nodes = des. seq ( false , |des| {
180199 des. cstr ( 0 ) ?;
181200 // The v7 on-disk-data does NOT contain the total size field, so putting `false` here is
182201 // correct. It only got introduced with the ipv6 changes.
183- des. seq ( false , |des| des. skip ( 24 ) ) ?;
202+ let nics = des. seq ( false , |des| {
203+ // The v7 on-disk-data uses the old format without a protocol field, thus we deserialize
204+ // it manually here.
205+ let addr = des. u32 ( ) ?. to_le_bytes ( ) . into ( ) ;
206+ let mut name = des. bytes ( 15 ) ?;
207+ des. u8 ( ) ?;
208+ let nic_type = NicType :: try_from_bee_serde ( des. u8 ( ) ?) ?;
209+ des. skip ( 3 ) ?;
210+
211+ // Remove null bytes from name
212+ name. retain ( |b| b != & 0 ) ;
213+
214+ Ok ( Nic {
215+ addr,
216+ name,
217+ nic_type,
218+ } )
219+ } ) ?;
220+
184221 let num_id = NodeId :: deserialize ( des) ?;
185222 let port = Port :: deserialize ( des) ?;
186223 Port :: deserialize ( des) ?;
187224 des. u8 ( ) ?;
188- Ok ( ( num_id, port) )
225+ Ok ( ( num_id, port, nics ) )
189226 } ) ?;
190227 des. finish ( ) ?;
191228
0 commit comments