Skip to content

Commit 370c737

Browse files
committed
refactor(storage): move all LightClientStorage methods to default implementations
Move all 30+ methods from LightClientStorage trait to have default implementations in storage_trait.rs. The methods now use StorageBackend trait methods (get, put, delete, batch, collect_iterator). This dramatically simplifies the backend implementations: - native.rs: Reduced from ~1100 to ~180 lines - browser.rs: Reduced from ~1400 to ~400 lines Backends now only need to implement StorageBackend (5 methods), then get all LightClientStorage methods for free via default implementations. Also adds LightClientStorage trait imports to all files that call trait methods on Storage, and fixes platform-specific HeaderProvider disambiguation (native uses HeaderProvider::get_header, wasm uses LightClientStorage::get_header).
1 parent 1b0c516 commit 370c737

File tree

22 files changed

+943
-2322
lines changed

22 files changed

+943
-2322
lines changed

light-client-bin/src/subcmds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ckb_light_client_lib::{
1616
FilterProtocol, LightClientProtocol, Peers, PendingTxs, RelayProtocol, SyncProtocol,
1717
BAD_MESSAGE_ALLOWED_EACH_HOUR, CHECK_POINT_INTERVAL,
1818
},
19-
storage::Storage,
19+
storage::{LightClientStorage, Storage},
2020
utils,
2121
};
2222

light-client-bin/src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ckb_chain_spec::{consensus::Consensus, ChainSpec};
44
use ckb_light_client_lib::{
55
protocols::{Peers, CHECK_POINT_INTERVAL},
66
service::{LightClientChainService, LightClientService},
7-
storage::{Storage, StorageWithChainData},
7+
storage::{LightClientStorage, Storage, StorageWithChainData},
88
};
99
use ckb_resource::Resource;
1010

light-client-lib/src/protocols/filter/block_filter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{components, BAD_MESSAGE_BAN_TIME};
22
use crate::protocols::{Peers, Status, StatusCode};
3-
use crate::storage::Storage;
3+
use crate::storage::{LightClientStorage, Storage};
44
use crate::types::{Duration, Instant, RwLock};
55
use crate::utils::network::prove_or_download_matched_blocks;
66
use crate::{read_lock, write_lock};

light-client-lib/src/protocols/filter/components/block_filter_hashes_process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use log::{debug, trace};
44
use rand::seq::SliceRandom as _;
55

66
use crate::protocols::{FilterProtocol, Status, StatusCode};
7+
use crate::storage::LightClientStorage;
78

89
pub struct BlockFilterHashesProcess<'a> {
910
message: packed::BlockFilterHashesReader<'a>,

light-client-lib/src/protocols/filter/components/block_filters_process.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::protocols::FilterProtocol;
22
use crate::protocols::{Status, StatusCode};
3+
use crate::storage::LightClientStorage;
34
use crate::utils::network::prove_or_download_matched_blocks;
45
use ckb_constant::sync::INIT_BLOCKS_IN_TRANSIT_PER_PEER;
56
use ckb_network::{BoxedCKBProtocolContext, PeerIndex};

light-client-lib/src/protocols/light_client/components/send_blocks_proof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ckb_types::{
88
use log::{debug, error, info};
99
use rand::seq::SliceRandom;
1010

11-
use crate::storage::HeaderWithExtension;
11+
use crate::storage::{HeaderWithExtension, LightClientStorage};
1212

1313
use super::{
1414
super::{LightClientProtocol, Status, StatusCode},

light-client-lib/src/protocols/light_client/components/send_transactions_proof.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use ckb_types::{
66
};
77
use log::{debug, error};
88

9-
use crate::{protocols::light_client::components::verify_extra_hash, storage::HeaderWithExtension};
9+
use crate::{
10+
protocols::light_client::components::verify_extra_hash,
11+
storage::{HeaderWithExtension, LightClientStorage},
12+
};
1013

1114
use super::{
1215
super::{LightClientProtocol, Status, StatusCode},

light-client-lib/src/protocols/light_client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use super::{
4646
};
4747

4848
use crate::protocols::{GET_BLOCKS_PROOF_LIMIT, GET_TRANSACTIONS_PROOF_LIMIT, LAST_N_BLOCKS};
49-
use crate::storage::Storage;
49+
use crate::storage::{LightClientStorage, Storage};
5050
use crate::utils::network::prove_or_download_matched_blocks;
5151

5252
pub struct LightClientProtocol {

light-client-lib/src/protocols/relayer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet};
1010
use std::sync::Arc;
1111

1212
use crate::protocols::{Peers, BAD_MESSAGE_BAN_TIME};
13-
use crate::storage::Storage;
13+
use crate::storage::{LightClientStorage, Storage};
1414
use crate::types::{Duration, Instant, RwLock};
1515
use crate::{read_lock, write_lock};
1616

0 commit comments

Comments
 (0)