Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Problem (Fix #1313): light client doesn't verify the fetched staking state #1511

Merged
merged 1 commit into from
May 5, 2020

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Apr 29, 2020

Solution:

  • Add staking_root in sync state
  • Remove the "account" path in favor of the new "staking" path
  • Client staking state command add wallet name parameter,
    and verify staking state and proof against the trusted staking_root in sync state
  • Need to sync wallet before fetching the new staking state now. (you can still get any staking state without verification by request abci_query API directly)

Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

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

Tx construction in network-ops doesn't verify staking state for now, because the unit tests are hard to mock the env.
Need to sync wallet before fetching the new staking state now.

maybe the verification option could be added as extra argument -- in local setting, one could disable it if one knows it's querying against the user's own full node; in mocked unit tests it could also be disabled?

address: &StakedStateAddress,
verify: bool,
) -> Result<StakedState> {
let mstaking = if verify {
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of mstaking,
how about full word variable name ?

Copy link
Collaborator Author

@yihuang yihuang Apr 29, 2020

Choose a reason for hiding this comment

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

I believe the name style of mxxx is adopted from Haskell, because I've seen this style in our code base several times before, so I keep on using it. Basically in these cases mxxx means Option<xxx> or Result<xxx>.

) -> Result<StakedState> {
let mstaking = if verify {
let sync_state = self.wallet_client.get_sync_state(name)?;
let rsp = self.client.query(
Copy link
Collaborator

Choose a reason for hiding this comment

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

full name with "response"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Personally, I think well-known shortcuts should be ok to keep the code short, actually easier to read.

format!("Cannot deserialize staked state for address: {}", address)
})?;

let mut proof_bytes = rsp.proof.as_ref().unwrap().ops[0].data.as_slice();
Copy link
Collaborator

Choose a reason for hiding this comment

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

expect and
assert!(ops.len()>0)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to return proper error for invalid response.

Copy link
Collaborator

@leejw51crypto leejw51crypto left a comment

Choose a reason for hiding this comment

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

lgtm

@yihuang
Copy link
Collaborator Author

yihuang commented Apr 29, 2020

Tx construction in network-ops doesn't verify staking state for now, because the unit tests are hard to mock the env.
Need to sync wallet before fetching the new staking state now.

maybe the verification option could be added as extra argument -- in local setting, one could disable it if one knows it's querying against the user's own full node; in mocked unit tests it could also be disabled?

Added a verify_staking parameter, unit tests pass false, client-cli and client-rpc passes true.
Hope in the future we'll have more sophisticated unit test mocking ability, then we can remove this parameter.

@yihuang yihuang force-pushed the issue1313 branch 2 times, most recently from 69ec821 to a038294 Compare April 29, 2020 16:37
@yihuang yihuang requested a review from tomtau April 29, 2020 16:38
@yihuang yihuang force-pushed the issue1313 branch 3 times, most recently from 3810b92 to c8eb7e8 Compare April 30, 2020 02:03
@codecov
Copy link

codecov bot commented Apr 30, 2020

Codecov Report

Merging #1511 into master will decrease coverage by 0.11%.
The diff coverage is 41.32%.

@@            Coverage Diff             @@
##           master    #1511      +/-   ##
==========================================
- Coverage   65.78%   65.66%   -0.12%     
==========================================
  Files         161      161              
  Lines       21818    21892      +74     
==========================================
+ Hits        14353    14376      +23     
- Misses       7465     7516      +51     
Impacted Files Coverage Δ
client-cli/src/command.rs 0.00% <0.00%> (ø)
client-cli/src/command/transaction_command.rs 0.00% <0.00%> (ø)
client-core/src/wallet.rs 100.00% <ø> (ø)
client-core/src/wallet/default_wallet_client.rs 49.85% <0.00%> (-0.59%) ⬇️
client-rpc/src/rpc/staking_rpc.rs 0.00% <0.00%> (ø)
cro-clib/src/transaction.rs 0.00% <0.00%> (ø)
...work/src/network_ops/default_network_ops_client.rs 74.40% <38.77%> (-3.64%) ⬇️
client-core/src/wallet/syncer.rs 71.52% <72.00%> (-0.47%) ⬇️
client-core/src/service/sync_state_service.rs 81.25% <83.33%> (-1.17%) ⬇️
chain-core/src/init/config.rs 80.00% <100.00%> (ø)
... and 5 more

@tomtau
Copy link
Contributor

tomtau commented May 1, 2020

bors r+

bors bot added a commit that referenced this pull request May 1, 2020
1511: Problem (Fix #1313): light client doesn't verify the fetched staking state r=tomtau a=yihuang

Solution:
- Add staking_root in sync state
- Remove the "account" path in favor of the new "staking" path
- Client staking state command add wallet name parameter,
  and verify staking state and proof against the trusted staking_root in sync state
- Need to sync wallet before fetching the new staking state now. (you can still get any staking state without verification by request abci_query API directly)

1516: Problem (Fix #1515): unbond tx don't subtract fee from bonded r=tomtau a=yihuang

Solution:
- Fix the bug and add unit test


Co-authored-by: yihuang <huang@crypto.com>
@bors
Copy link
Contributor

bors bot commented May 1, 2020

Build failed (retrying...):

bors bot added a commit that referenced this pull request May 1, 2020
1511: Problem (Fix #1313): light client doesn't verify the fetched staking state r=tomtau a=yihuang

Solution:
- Add staking_root in sync state
- Remove the "account" path in favor of the new "staking" path
- Client staking state command add wallet name parameter,
  and verify staking state and proof against the trusted staking_root in sync state
- Need to sync wallet before fetching the new staking state now. (you can still get any staking state without verification by request abci_query API directly)

Co-authored-by: yihuang <huang@crypto.com>
@tomtau
Copy link
Contributor

tomtau commented May 1, 2020

bors r-

@bors
Copy link
Contributor

bors bot commented May 1, 2020

Canceled.

@tomtau
Copy link
Contributor

tomtau commented May 1, 2020

multi-node integration test fails with

jsonrpcclient.exceptions.ReceivedErrorResponseError: Deserialization error: Unable to deserialize global state for wallet with name target

@yihuang
Copy link
Collaborator Author

yihuang commented May 1, 2020

multi-node integration test fails with

jsonrpcclient.exceptions.ReceivedErrorResponseError: Deserialization error: Unable to deserialize global state for wallet with name target

This is actually a separate issue, solved in #1524.

@yihuang yihuang force-pushed the issue1313 branch 2 times, most recently from a70190c to 6322057 Compare May 3, 2020 01:20
Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

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

looks all right, but perhaps no need to make this a breaking change immediately?

Comment on lines 173 to 192
"account" => {
let account_address = StakedStateAddress::try_from(_req.data.as_slice());
if let (Some(state), Ok(address)) = (&self.last_state, account_address) {
let (account, _proof) =
get_with_proof(&self.storage, state.staking_version, &address);
match account {
Some(a) => {
resp.value = a.encode();
// TODO: inclusion proof
}
None => {
resp.log += "account lookup failed: account not exists";
resp.code = 1;
}
}
} else {
resp.log += "account lookup failed (either invalid address or node not correctly restored / initialized)";
resp.code = 3;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a need to do a breaking change? the original default behaviour should identical to "staking" path without any parameters? it could possibly be a non-breaking change with "staking" | "account" => {... pattern match... it could log some deprecation warning for "account" path that it'd be removed in the future

@yihuang
Copy link
Collaborator Author

yihuang commented May 4, 2020

looks all right, but perhaps no need to make this a breaking change immediately?

Do you mean keep the account path and staking path at the same time?

@tomtau
Copy link
Contributor

tomtau commented May 4, 2020

looks all right, but perhaps no need to make this a breaking change immediately?

Do you mean keep the account path and staking path at the same time?

yes, for the time being

…d staking state

Solution:
- Add staking_root in sync state
- Remove the "account" path in favor of the new "staking" path
- Client staking state command add wallet name parameter,
  and verify staking state and proof against the trusted staking_root in sync state
- Need to sync wallet before fetching the new staking state now.
@yihuang
Copy link
Collaborator Author

yihuang commented May 4, 2020

looks all right, but perhaps no need to make this a breaking change immediately?

Do you mean keep the account path and staking path at the same time?

yes, for the time being

Ok, done.

@yihuang yihuang requested a review from tomtau May 4, 2020 09:04
@tomtau
Copy link
Contributor

tomtau commented May 5, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented May 5, 2020

Build succeeded:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants