Skip to content

Commit 75bd2ac

Browse files
committed
Add light client bootstrap API test and fix existing ones.
1 parent 80ff555 commit 75bd2ac

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6469,9 +6469,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
64696469
.fork_name(&self.spec)
64706470
.map_err(Error::InconsistentFork)?;
64716471

6472-
LightClientBootstrap::from_beacon_state(&mut state)
6473-
.map(|bootstrap| Some((bootstrap, fork_name)))
6474-
.map_err(Error::LightClientError)
6472+
match fork_name {
6473+
ForkName::Altair | ForkName::Merge => {
6474+
LightClientBootstrap::from_beacon_state(&mut state)
6475+
.map(|bootstrap| Some((bootstrap, fork_name)))
6476+
.map_err(Error::LightClientError)
6477+
}
6478+
ForkName::Base | ForkName::Capella | ForkName::Deneb => Err(Error::UnsupportedFork),
6479+
}
64756480
}
64766481
}
64776482

beacon_node/beacon_chain/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub enum BeaconChainError {
222222
UnableToPublish,
223223
AvailabilityCheckError(AvailabilityCheckError),
224224
LightClientError(LightClientError),
225+
UnsupportedFork,
225226
}
226227

227228
easy_from_to!(SlotProcessingError, BeaconChainError);

beacon_node/http_api/src/test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub async fn create_api_server<T: BeaconChainTypes>(
209209
enabled: true,
210210
listen_port: port,
211211
data_dir: std::path::PathBuf::from(DEFAULT_ROOT_DIR),
212+
enable_light_client_server: true,
212213
..Config::default()
213214
},
214215
chain: Some(chain),

beacon_node/http_api/tests/tests.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,26 @@ impl ApiTester {
16441644
self
16451645
}
16461646

1647+
pub async fn test_get_beacon_light_client_bootstrap(self) -> Self {
1648+
let block_id = BlockId(CoreBlockId::Finalized);
1649+
let (block_root, _, _) = block_id.root(&self.chain).unwrap();
1650+
let (block, _, _) = block_id.full_block(&self.chain).await.unwrap();
1651+
1652+
let result = match self
1653+
.client
1654+
.get_light_client_bootstrap::<E>(block_root)
1655+
.await
1656+
{
1657+
Ok(result) => result.unwrap().data,
1658+
Err(e) => panic!("query failed incorrectly: {e:?}"),
1659+
};
1660+
1661+
let expected = block.slot();
1662+
assert_eq!(result.header.beacon.slot, expected);
1663+
1664+
self
1665+
}
1666+
16471667
pub async fn test_get_beacon_light_client_optimistic_update(self) -> Self {
16481668
// get_beacon_light_client_optimistic_update returns Ok(None) on 404 NOT FOUND
16491669
let result = match self
@@ -1652,7 +1672,7 @@ impl ApiTester {
16521672
.await
16531673
{
16541674
Ok(result) => result.map(|res| res.data),
1655-
Err(_) => panic!("query did not fail correctly"),
1675+
Err(e) => panic!("query failed incorrectly: {e:?}"),
16561676
};
16571677

16581678
let expected = self.chain.latest_seen_optimistic_update.lock().clone();
@@ -1668,7 +1688,7 @@ impl ApiTester {
16681688
.await
16691689
{
16701690
Ok(result) => result.map(|res| res.data),
1671-
Err(_) => panic!("query did not fail correctly"),
1691+
Err(e) => panic!("query failed incorrectly: {e:?}"),
16721692
};
16731693

16741694
let expected = self.chain.latest_seen_finality_update.lock().clone();
@@ -5092,17 +5112,37 @@ async fn node_get() {
50925112
.await;
50935113
}
50945114

5115+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
5116+
async fn get_light_client_bootstrap() {
5117+
let config = ApiTesterConfig {
5118+
spec: ForkName::Altair.make_genesis_spec(E::default_spec()),
5119+
..<_>::default()
5120+
};
5121+
ApiTester::new_from_config(config)
5122+
.await
5123+
.test_get_beacon_light_client_bootstrap()
5124+
.await;
5125+
}
5126+
50955127
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
50965128
async fn get_light_client_optimistic_update() {
5097-
ApiTester::new()
5129+
let config = ApiTesterConfig {
5130+
spec: ForkName::Altair.make_genesis_spec(E::default_spec()),
5131+
..<_>::default()
5132+
};
5133+
ApiTester::new_from_config(config)
50985134
.await
50995135
.test_get_beacon_light_client_optimistic_update()
51005136
.await;
51015137
}
51025138

51035139
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
51045140
async fn get_light_client_finality_update() {
5105-
ApiTester::new()
5141+
let config = ApiTesterConfig {
5142+
spec: ForkName::Altair.make_genesis_spec(E::default_spec()),
5143+
..<_>::default()
5144+
};
5145+
ApiTester::new_from_config(config)
51065146
.await
51075147
.test_get_beacon_light_client_finality_update()
51085148
.await;

consensus/types/src/light_client_bootstrap.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use serde_json::Value;
88
use ssz_derive::{Decode, Encode};
99
use std::sync::Arc;
1010
use test_random_derive::TestRandom;
11-
use tree_hash::TreeHash;
1211

1312
/// A LightClientBootstrap is the initializer we send over to lightclient nodes
1413
/// that are trying to generate their basic storage when booting up.
@@ -37,7 +36,7 @@ pub struct LightClientBootstrap<T: EthSpec> {
3736
impl<T: EthSpec> LightClientBootstrap<T> {
3837
pub fn from_beacon_state(beacon_state: &mut BeaconState<T>) -> Result<Self, Error> {
3938
let mut header = beacon_state.latest_block_header().clone();
40-
header.state_root = beacon_state.tree_hash_root();
39+
header.state_root = beacon_state.update_tree_hash_cache()?;
4140
let current_sync_committee_branch =
4241
beacon_state.compute_merkle_proof(CURRENT_SYNC_COMMITTEE_INDEX)?;
4342
Ok(LightClientBootstrap {

0 commit comments

Comments
 (0)