Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Light client RPCs #7603

Merged
merged 7 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethcore/res/wasm-tests
42 changes: 39 additions & 3 deletions js-old/src/redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ export default function (api, browserHistory, forEmbed = false) {
.then(() => console.log('v1: started Status Provider'))

.then(() => console.log('v1: starting Personal Provider...'))
.then(() => PersonalProvider.start())
.then(() => withTimeoutForLight('personal', PersonalProvider.start(), store))
.then(() => console.log('v1: started Personal Provider'))

.then(() => console.log('v1: starting Balances Provider...'))
.then(() => BalancesProvider.start())
.then(() => withTimeoutForLight('balances', BalancesProvider.start(), store))
.then(() => console.log('v1: started Balances Provider'))

.then(() => console.log('v1: starting Tokens Provider...'))
.then(() => TokensProvider.start())
.then(() => withTimeoutForLight('tokens', TokensProvider.start(), store))
.then(() => console.log('v1: started Tokens Provider'));
};

Expand All @@ -97,3 +97,39 @@ export default function (api, browserHistory, forEmbed = false) {

return store;
}

function withTimeoutForLight(id, promise, store) {
const { nodeKind } = store.getState().nodeStatus;
const isLightNode = nodeKind.capability !== 'full';

if (!isLightNode) {
// make sure that no values are passed
return promise.then(() => {});
}

return new Promise((resolve, reject) => {
let isResolved = false;
const doResolve = () => {
if (!isResolved) {
isResolved = true;
resolve();
}
}
const timeout = setTimeout(() => {
console.warn(`Resolving ${id} by timeout.`);
doResolve()
}, 1000);

promise
.then(() => {
clearTimeout(timeout);
doResolve();
})
.catch(err => {
clearTimeout(timeout);
if (!isResolved) {
reject(err);
}
})
});
}
22 changes: 11 additions & 11 deletions js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 52 additions & 54 deletions rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,30 @@ impl LightFetch {
Err(e) => return Box::new(future::err(e)),
};

let maybe_future = self.sync.with_context(move |ctx| {
Box::new(self.on_demand.request_raw(ctx, reqs)
.expect("all back-references known to be valid; qed")
.map(|res| extract_header(&res, header_ref)
.expect("these responses correspond to requests that header_ref belongs to. \
therefore it will not fail; qed"))
.map_err(errors::on_demand_cancel))
});

match maybe_future {
Some(recv) => recv,
None => Box::new(future::err(errors::network_disabled()))
}
self.send_requests(reqs, |res|
extract_header(&res, header_ref)
.expect("these responses correspond to requests that header_ref belongs to \
therefore it will not fail; qed")
)
}

/// Helper for getting contract code at a given block.
pub fn code(&self, address: Address, id: BlockId) -> BoxFuture<Vec<u8>> {
let mut reqs = Vec::new();
let header_ref = match self.make_header_requests(id, &mut reqs) {
Ok(r) => r,
Err(e) => return Box::new(future::err(e)),
};

reqs.push(request::Account { header: header_ref.clone(), address: address }.into());
let account_idx = reqs.len() - 1;
reqs.push(request::Code { header: header_ref, code_hash: Field::back_ref(account_idx, 0) }.into());

self.send_requests(reqs, |mut res| match res.pop() {
Some(OnDemandResponse::Code(code)) => code,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
}

/// Helper for getting account info at a given block.
Expand All @@ -172,20 +183,10 @@ impl LightFetch {

reqs.push(request::Account { header: header_ref, address: address }.into());

let maybe_future = self.sync.with_context(move |ctx| {
Box::new(self.on_demand.request_raw(ctx, reqs)
.expect("all back-references known to be valid; qed")
.map(|mut res| match res.pop() {
Some(OnDemandResponse::Account(acc)) => acc,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
.map_err(errors::on_demand_cancel))
});

match maybe_future {
Some(recv) => recv,
None => Box::new(future::err(errors::network_disabled()))
}
self.send_requests(reqs, |mut res|match res.pop() {
Some(OnDemandResponse::Account(acc)) => acc,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
}

/// Helper for getting proved execution.
Expand Down Expand Up @@ -276,20 +277,10 @@ impl LightFetch {

reqs.push(request::Body(header_ref).into());

let maybe_future = self.sync.with_context(move |ctx| {
Box::new(self.on_demand.request_raw(ctx, reqs)
.expect(NO_INVALID_BACK_REFS)
.map(|mut res| match res.pop() {
Some(OnDemandResponse::Body(b)) => b,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
.map_err(errors::on_demand_cancel))
});

match maybe_future {
Some(recv) => recv,
None => Box::new(future::err(errors::network_disabled()))
}
self.send_requests(reqs, |mut res| match res.pop() {
Some(OnDemandResponse::Body(b)) => b,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
}

/// Get the block receipts. Fails on unknown block ID.
Expand All @@ -302,20 +293,10 @@ impl LightFetch {

reqs.push(request::BlockReceipts(header_ref).into());

let maybe_future = self.sync.with_context(move |ctx| {
Box::new(self.on_demand.request_raw(ctx, reqs)
.expect(NO_INVALID_BACK_REFS)
.map(|mut res| match res.pop() {
Some(OnDemandResponse::Receipts(b)) => b,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
.map_err(errors::on_demand_cancel))
});

match maybe_future {
Some(recv) => recv,
None => Box::new(future::err(errors::network_disabled()))
}
self.send_requests(reqs, |mut res| match res.pop() {
Some(OnDemandResponse::Receipts(b)) => b,
_ => panic!("responses correspond directly with requests in amount and type; qed"),
})
}

/// Get transaction logs
Expand Down Expand Up @@ -432,6 +413,23 @@ impl LightFetch {
Either::B(extract_transaction)
}))
}

fn send_requests<T, F>(&self, reqs: Vec<OnDemandRequest>, parse_response: F) -> BoxFuture<T> where
Copy link
Contributor

@rphmeier rphmeier Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might be able to make this more generic by accepting a R: on_demand::RequestAdapter and F: FnOnce(R::Out) -> T.

Note that this trait is implemented for Vec<OnDemandRequest>
A high level API would help you construct tuples of specific requests with consistent back-references and we could skip the annoying panics currently used

F: FnOnce(Vec<OnDemandResponse>) -> T + Send + 'static,
T: Send + 'static,
{
let maybe_future = self.sync.with_context(move |ctx| {
Box::new(self.on_demand.request_raw(ctx, reqs)
.expect(NO_INVALID_BACK_REFS)
.map(parse_response)
.map_err(errors::on_demand_cancel))
});

match maybe_future {
Some(recv) => recv,
None => Box::new(future::err(errors::network_disabled()))
}
}
}

#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/impls/light/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ impl<T: LightChainClient + 'static> Eth for EthClient<T> {
}))
}

fn code_at(&self, _address: RpcH160, _num: Trailing<BlockNumber>) -> BoxFuture<Bytes> {
Box::new(future::err(errors::unimplemented(None)))
fn code_at(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<Bytes> {
Box::new(self.fetcher().code(address.into(), num.unwrap_or_default().into()).map(Into::into))
}

fn send_raw_transaction(&self, raw: Bytes) -> Result<RpcH256> {
Expand Down
7 changes: 6 additions & 1 deletion rpc/src/v1/impls/light/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ impl Parity for ParityClient {
}

fn registry_address(&self) -> Result<Option<H160>> {
Err(errors::light_unimplemented(None))
let reg = self.light_dispatch.client.engine().params().registrar;
if reg == Default::default() {
Ok(None)
} else {
Ok(Some(reg.into()))
}
}

fn rpc_settings(&self) -> Result<RpcSettings> {
Expand Down