Skip to content

Sync state from fellow validateer#615

Merged
haerdib merged 34 commits intomasterfrom
bh-sync-state-from-fellow-validateer
Jan 24, 2022
Merged

Sync state from fellow validateer#615
haerdib merged 34 commits intomasterfrom
bh-sync-state-from-fellow-validateer

Conversation

@haerdib
Copy link
Contributor

@haerdib haerdib commented Jan 11, 2022

  • Divides tls_ra.rs into server and client
  • Adds header with opcode and payload length indicator, such that the ordering and length of the payload may vary.
  • add state syncing
  • add some unit tests

TODO:

  • sync state: add state handler
  • add unit tests

Tested manually with following steps (wasn't sure how to do an integration test here..)

  1. start integritee node
  2. start worker 1 with ./integritee-service -P 2094 -p 9994 -r 3494 run --dev
  3. run demo_direct_call: ./demo_direct_call.sh -P 2094 -p 9994:
    grafik
  4. request-state with worker2 (with empty shard directory): ./integritee-service -p 9994 request-state
    grafik
  5. Stop worker 1 (such that no sidechain blocks will reach worker 2)
  6. Start worker 2 ./integritee-service -P 2095 -p 9994 -r 3494 run --dev
  7. Query balance of //AliceIncognito of second worker:
./integritee-cli -p 9994 -P 2095 trusted balance //AliceIncognito --mrenclave DBQbPN2WKh54fucAmpU7Z39ip5FYRBzzmnkJPmuQ1GNH
10000000000
./integritee-cli -p 9994 -P 2095 trusted balance //BobIncognito --mrenclave DBQbPN2WKh54fucAmpU7Z39ip5FYRBzzmnkJPmuQ1GNH
40000000000

closes #614

@haerdib haerdib self-assigned this Jan 14, 2022
@haerdib haerdib force-pushed the bh-sync-state-from-fellow-validateer branch from 8f7b908 to 3574be9 Compare January 17, 2022 16:18
@haerdib haerdib requested a review from echevrier January 19, 2022 08:26
@haerdib haerdib marked this pull request as ready for review January 19, 2022 08:27
@haerdib haerdib requested a review from clangenb January 19, 2022 08:35
@haerdib haerdib force-pushed the bh-sync-state-from-fellow-validateer branch from 9eebd29 to 277b49d Compare January 19, 2022 08:53

*/

//! Remote attestation certificate authentication of server and client
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not change this part, simply copy pasted from the old tls_ra.rs file. So I'm unsure why there are so many unused variables in the function definition. Hence, I left as it is for now.

@haerdib haerdib requested review from murerfel and removed request for clangenb January 19, 2022 09:08
}

#[no_mangle]
pub unsafe extern "C" fn request_state_provisioning(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code from here on (in this file) left as is (copy pasted from tls_ra.rs), except the inclusion of the request_state_provisioning_internal for easy testing & usage of ?

}

#[no_mangle]
pub unsafe extern "C" fn run_state_provisioning_server(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code from here on (in this file) left as is (copy pasted from tls_ra.rs), except the inclusion of the run_state_provisioning_server_internal for easy testing & usage of ?

Copy link
Contributor

@murerfel murerfel left a comment

Choose a reason for hiding this comment

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

All in all it's looking good - there are some issues I would like to have addressed ☺️

Comment on lines +44 to +47
fn seal_shielding_key(&self, bytes: &[u8]) -> EnclaveResult<()> {
*SHIELDING_KEY.write().unwrap() = bytes.to_vec();
Ok(())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need these global variables in this mock? Is it not enough to use the members?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem lies with the mutability of the variables. We don't pass a mutable self here because it's not needed by the original function.

And if we have a mutable self, the whole request_state_provisioning_internal needs a mut reference input for the client_seal_handler, otherwise my server - client connection test would not work.

I didn't think adapting the code to use mutable references, which are not actually needed, are the way to go. Or what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

What you need then, is RwLock on your members (that's something you'll see in many of my mock implementations). But they'll still be members and scoped inside the struct, not globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better now? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes indeed, that's what I imagined ☺️

Comment on lines +63 to +64
thread::spawn(move || {
run_state_provisioning_server(server_seal_handler);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

You spawn a thread here, but you never join - so this thread keeps on running until the entire program is terminated? However, the thread should probably terminate when the test does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ops, that is correct. But joining simply waits for the thread to finish, it does not terminate anything. So in the worst case, the test will never end. I removed the (now obsolete) loop, so the thread finishes as soon it's done writing.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes simple joining wouldn't have solved problem if the thread is in an endless loop. You would've needed a sender/receiver to terminate the thread. But in case the loop is not required anymore, you can do the simple join.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a join 👍

Copy link
Contributor

@echevrier echevrier left a comment

Choose a reason for hiding this comment

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

I am not a specialist in this part of the code, so my opinion is limited. It looks good to me. I really like the clean code and the tests

@haerdib
Copy link
Contributor Author

haerdib commented Jan 19, 2022

I am not a specialist in this part of the code, so my opinion is limited.

And that's the reason why you're a good reviewer. If you don't understand something, it means it's not good code or not enough documented. Thanks for your comment ! 👍

@haerdib haerdib force-pushed the bh-sync-state-from-fellow-validateer branch from 1041451 to 5536dd1 Compare January 20, 2022 16:47
@haerdib haerdib requested a review from murerfel January 21, 2022 08:24
Comment on lines +36 to +37
#[cfg(all(feature = "mocks", feature = "sgx"))]
pub mod mocks;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +28 to +30
pub shielding_key: Arc<RwLock<Vec<u8>>>,
pub signing_key: Arc<RwLock<Vec<u8>>>,
pub state: Arc<RwLock<Vec<u8>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing you're using Arcs here for the #[derive(Clone)], because RwLock is not clonable? For the mock it's probably okay, but in a real implementation, this would most likely not be what we want. We'd have to implement clone ourselves and clone the inner value of RwLock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, but for the mock that was the easiest way to implement a test. At least for what I could see.

Comment on lines +89 to +90
// Ensure server thread has finished.
server_thread_handle.join().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +44 to +47
fn seal_shielding_key(&self, bytes: &[u8]) -> EnclaveResult<()> {
*SHIELDING_KEY.write().unwrap() = bytes.to_vec();
Ok(())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes indeed, that's what I imagined ☺️

Copy link
Contributor

@murerfel murerfel left a comment

Choose a reason for hiding this comment

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

It's looking good to me now 👍 good job

@haerdib haerdib merged commit 9a14d49 into master Jan 24, 2022
@haerdib haerdib deleted the bh-sync-state-from-fellow-validateer branch January 24, 2022 07:10
haerdib added a commit to ajuna-network/worker that referenced this pull request Feb 10, 2022
* Make mu-ra and untrusted worker url queriable (integritee-network#595)

* extract request_keys() to separate file

* remove providr input, add dummy getter function

* add node_api worker_for_shard call

* fix error message

* add primitives cache and rpc call

* fix tests

* add primitives-cache to workspace

* fix unit tests

* remove obsolete .yml provider from request-keys cmd

* remove provider_addr from CI py scripts

* fix reported worker address

* improve usability of rpc-client

* make it work

* fix rebase error

* add some delay

* update local setup script

* remove ugly async worker url, replace with enclave getter function

* some steps towards a working exmaple..

* add peer_updater

* fix unit test

* fix some test clippy warnings

* fix function name

* fix client mu ra url

* fix comment

* fix comment

* rename state_sync to appropriate request keys

* fix comments and add missing _size to untrusted_worker_addr

* update cargo.lock after rebase

* fix typos

* rename store_peers to set_peers

* fix comment

* move set_primitives to primitves cache repository

* return read guard instead of primittves clone

* rename config worker_rpc_port to trusted_worker_port

* remove obsolete Error enum from request_keys.rs

* fix unit tests

* move thread spawning back into watch fn

* rename worker-rpc-port to trusted-worker-port

* readd external worker address

* fix unit tests

* fix unit test

* add external addresses, optional port input and unit tests

* update test names

* [cli.yml] update shorts

* fix local setup configs

* change untrusted worker port to w

* [sidechain] detect out of sync error (integritee-network#606)

* inital commit

* remove unwrap_err from assert_matches

* Update substrate sp-core to version 4.1.0-dev (integritee-network#612)

Co-authored-by: Gaudenz Kessler <gaudenz.kessler@scs.ch>

* Renaming of unspecific SB and PB variable names (integritee-network#605)

* some clean up & add hanlde import error

* remove logic changes

* fix unit tests

* [aura block importer] rename SidechainBlock to SignedSidechainBlock

* fix rebase errors

* [aura mock] rename xxT import to xxTrait

* [aura verifier] rename SidechainBlock to SignedSidechainblock where appropriate

* fix rebase errors

* [aura] rename Sidechainblock to SignedSidechainBlock

* rename SB & PB to full written version and adapt to SignedSidechainBlock Where necessary

* [sidechain block imported] remove extra generic from SignedParentchainBlock

* some further SB & PB clean up

* rename B & SB to SidechainBlock & SignedSidechainBlock

* some further renaming

* completely remove SB

* rename all left over PBs

* remove rebase error & rename to SignedSidechainBlock

* rename to SignedSidechainBlock

* Sidechain peer fetch blocks - RPC client/server (integritee-network#580)

* WIP: RPC call to fetch sidechain blocks

* WIP: sidechain peer fetch crate with RPC server and client impl

* WIP: test for RPC peer sidechain block fetching

* fix unit test

* remove obsolete comment

* fix rebase error

* cargo fmt

* fix tests

* fix Fixme + add som Send+Sync to errors

* update add_block_to_batch to return error. Otherwise silent fail

* small comment fixes

* make some comments better understandable

* remove FIXME comment

* remove new lines

* fix trailing comments

* [peer-fetch] change order or crates in .toml

* [sidechain storage] fix error message of HeaderAncestryMismatch

* [sidechain storage] exchange match statement with ok_or

* [sidechain storage] use temp-dir in tests

* [sidechain storage] remove extra genesis block check

* fix rebase errors

* remove untrstued url, replace with untrusted peer fetcher

* [sidechain storage] fix comment

* update delete_last_block description comment

* [sidechain storage] fix comment grammer

* move FetchUntrustedPeers trait to the top

* [FetchBlocksFromPeer] extend description comment

* update cargo.lock

* rename get_blocks_following to get_blocks_after

* rename get_blocks_following to get_blocks_after

* rename all leftover "blocks_following" to "block_after"

Co-authored-by: Bigna Härdi <bhaerdi@devsgx02.scs-ad.scs.ch>

* [Sidechain] Peer block fetching o-call implementation (integritee-network#619)

* introduce o-call for fetching sidechain blocks from peer
* re-name api-client-extensions to node-api-extensions

Sub-task of integritee-network#567

* add direct call rpc doc (integritee-network#620)

* add some doc

* add some structure to the links

* restructure rpc interface

* Update docs/README.md

Co-authored-by: gaudenzkessler <92718752+gaudenzkessler@users.noreply.github.com>

* adapt readmes according to review comments

Co-authored-by: gaudenzkessler <92718752+gaudenzkessler@users.noreply.github.com>

* [Sidechain] Peer sync and block production suspension (integritee-network#618)

Peer syncer implementation (not in use yet) and block production suspender (also not in use yet)

* update to most recent teaclave commit (integritee-network#624)

* Sync state from fellow validateer (integritee-network#615)

* rename request_keys to sync_state

* rename request_key_prov to request_state_prov

* rename request_keys.rs to sync_state.rs

* restructure key and state provisioning server

* some refactoring

* add TlsServer struct

* add test file

* rename key_provision_server to state_provisioning_server

* add unit test

* update unit test

* introduce mockable key handler struct

* shielding key success

* remove clippy warnings

* fix test

* add unit tests for KeyHandler

* rename to prepare for state inclusion

* rename seal_handler

* add shard as argument to sync state

* some more renaming

* add shard read & write process

* [SealHandler] add unit tests & fix state

* update networking test to include state

* add default shard

* add some documentation

* remove ugly for loop

* move authentications to separate file

* update comment

* remove obsolete, never ending loop

* add error logs

* remove extra phantom field

* add sgx feature flag

* remove global variables from test

* add join handle to test

* add some more logging info

* Change tokio runtime to use 2 worker threads. Gossiping spawns new tokio tasks. (integritee-network#626)

* Add state update sequence (integritee-network#632)

* add bock_import_sequence.svg

* move block_import.svg to docs/diagramms

* update diagramm

* add block import sequence

* RPC call to get metadata from sgx-runtime (integritee-network#642)

* RPC call to get metadata from sgx-runtime
- rcp call
- print sgx metadata cli

* Change from review: Metadata is already encoded

* Change from review

Co-authored-by: echevrier <edith.chevrier@scs.ch>

* bump substrate to commit 59649dd (integritee-network#645)

* update .tomls to new substrate versions

* cargo update

* RawEvent -> Event

* remove default from Accountid

* RawEvent -> Event

* cargo update enclave-runtime

* fix bump errors

* remove unused patch

* finaly compiling

* update sgx-runtime and substrate-api-client to github

* remove integritee-node-runtime patch

* cargo update -p std-std --precise 59649dd

* update Github Actions integritee node

* remove bh-config

* fix clippy

* fix cargo test

* update spec version

* update substrate-api-client

* update sgx-runtime source

* update substrate

* adjust node version values

* detect new game

* solve merge conflicts

* update sgx-runtime

* fix some things

* cargo format

Co-authored-by: gaudenzkessler <92718752+gaudenzkessler@users.noreply.github.com>
Co-authored-by: Gaudenz Kessler <gaudenz.kessler@scs.ch>
Co-authored-by: Felix Müller <mullefel@users.noreply.github.com>
Co-authored-by: echevrier <84318241+echevrier@users.noreply.github.com>
Co-authored-by: echevrier <edith.chevrier@scs.ch>
haerdib added a commit to ajuna-network/worker that referenced this pull request Feb 10, 2022
* Make mu-ra and untrusted worker url queriable (integritee-network#595)

* extract request_keys() to separate file

* remove providr input, add dummy getter function

* add node_api worker_for_shard call

* fix error message

* add primitives cache and rpc call

* fix tests

* add primitives-cache to workspace

* fix unit tests

* remove obsolete .yml provider from request-keys cmd

* remove provider_addr from CI py scripts

* fix reported worker address

* improve usability of rpc-client

* make it work

* fix rebase error

* add some delay

* update local setup script

* remove ugly async worker url, replace with enclave getter function

* some steps towards a working exmaple..

* add peer_updater

* fix unit test

* fix some test clippy warnings

* fix function name

* fix client mu ra url

* fix comment

* fix comment

* rename state_sync to appropriate request keys

* fix comments and add missing _size to untrusted_worker_addr

* update cargo.lock after rebase

* fix typos

* rename store_peers to set_peers

* fix comment

* move set_primitives to primitves cache repository

* return read guard instead of primittves clone

* rename config worker_rpc_port to trusted_worker_port

* remove obsolete Error enum from request_keys.rs

* fix unit tests

* move thread spawning back into watch fn

* rename worker-rpc-port to trusted-worker-port

* readd external worker address

* fix unit tests

* fix unit test

* add external addresses, optional port input and unit tests

* update test names

* [cli.yml] update shorts

* fix local setup configs

* change untrusted worker port to w

* [sidechain] detect out of sync error (integritee-network#606)

* inital commit

* remove unwrap_err from assert_matches

* Update substrate sp-core to version 4.1.0-dev (integritee-network#612)

Co-authored-by: Gaudenz Kessler <gaudenz.kessler@scs.ch>

* Renaming of unspecific SB and PB variable names (integritee-network#605)

* some clean up & add hanlde import error

* remove logic changes

* fix unit tests

* [aura block importer] rename SidechainBlock to SignedSidechainBlock

* fix rebase errors

* [aura mock] rename xxT import to xxTrait

* [aura verifier] rename SidechainBlock to SignedSidechainblock where appropriate

* fix rebase errors

* [aura] rename Sidechainblock to SignedSidechainBlock

* rename SB & PB to full written version and adapt to SignedSidechainBlock Where necessary

* [sidechain block imported] remove extra generic from SignedParentchainBlock

* some further SB & PB clean up

* rename B & SB to SidechainBlock & SignedSidechainBlock

* some further renaming

* completely remove SB

* rename all left over PBs

* remove rebase error & rename to SignedSidechainBlock

* rename to SignedSidechainBlock

* Sidechain peer fetch blocks - RPC client/server (integritee-network#580)

* WIP: RPC call to fetch sidechain blocks

* WIP: sidechain peer fetch crate with RPC server and client impl

* WIP: test for RPC peer sidechain block fetching

* fix unit test

* remove obsolete comment

* fix rebase error

* cargo fmt

* fix tests

* fix Fixme + add som Send+Sync to errors

* update add_block_to_batch to return error. Otherwise silent fail

* small comment fixes

* make some comments better understandable

* remove FIXME comment

* remove new lines

* fix trailing comments

* [peer-fetch] change order or crates in .toml

* [sidechain storage] fix error message of HeaderAncestryMismatch

* [sidechain storage] exchange match statement with ok_or

* [sidechain storage] use temp-dir in tests

* [sidechain storage] remove extra genesis block check

* fix rebase errors

* remove untrstued url, replace with untrusted peer fetcher

* [sidechain storage] fix comment

* update delete_last_block description comment

* [sidechain storage] fix comment grammer

* move FetchUntrustedPeers trait to the top

* [FetchBlocksFromPeer] extend description comment

* update cargo.lock

* rename get_blocks_following to get_blocks_after

* rename get_blocks_following to get_blocks_after

* rename all leftover "blocks_following" to "block_after"

Co-authored-by: Bigna Härdi <bhaerdi@devsgx02.scs-ad.scs.ch>

* [Sidechain] Peer block fetching o-call implementation (integritee-network#619)

* introduce o-call for fetching sidechain blocks from peer
* re-name api-client-extensions to node-api-extensions

Sub-task of integritee-network#567

* add direct call rpc doc (integritee-network#620)

* add some doc

* add some structure to the links

* restructure rpc interface

* Update docs/README.md

Co-authored-by: gaudenzkessler <92718752+gaudenzkessler@users.noreply.github.com>

* adapt readmes according to review comments

Co-authored-by: gaudenzkessler <92718752+gaudenzkessler@users.noreply.github.com>

* [Sidechain] Peer sync and block production suspension (integritee-network#618)

Peer syncer implementation (not in use yet) and block production suspender (also not in use yet)

* update to most recent teaclave commit (integritee-network#624)

* Sync state from fellow validateer (integritee-network#615)

* rename request_keys to sync_state

* rename request_key_prov to request_state_prov

* rename request_keys.rs to sync_state.rs

* restructure key and state provisioning server

* some refactoring

* add TlsServer struct

* add test file

* rename key_provision_server to state_provisioning_server

* add unit test

* update unit test

* introduce mockable key handler struct

* shielding key success

* remove clippy warnings

* fix test

* add unit tests for KeyHandler

* rename to prepare for state inclusion

* rename seal_handler

* add shard as argument to sync state

* some more renaming

* add shard read & write process

* [SealHandler] add unit tests & fix state

* update networking test to include state

* add default shard

* add some documentation

* remove ugly for loop

* move authentications to separate file

* update comment

* remove obsolete, never ending loop

* add error logs

* remove extra phantom field

* add sgx feature flag

* remove global variables from test

* add join handle to test

* add some more logging info

* Change tokio runtime to use 2 worker threads. Gossiping spawns new tokio tasks. (integritee-network#626)

* Add state update sequence (integritee-network#632)

* add bock_import_sequence.svg

* move block_import.svg to docs/diagramms

* update diagramm

* add block import sequence

* RPC call to get metadata from sgx-runtime (integritee-network#642)

* RPC call to get metadata from sgx-runtime
- rcp call
- print sgx metadata cli

* Change from review: Metadata is already encoded

* Change from review

Co-authored-by: echevrier <edith.chevrier@scs.ch>

* bump substrate to commit 59649dd (integritee-network#645)

* update .tomls to new substrate versions

* cargo update

* RawEvent -> Event

* remove default from Accountid

* RawEvent -> Event

* cargo update enclave-runtime

* fix bump errors

* remove unused patch

* finaly compiling

* update sgx-runtime and substrate-api-client to github

* remove integritee-node-runtime patch

* cargo update -p std-std --precise 59649dd

* update Github Actions integritee node

* remove bh-config

* fix clippy

* fix cargo test

* update spec version

* update substrate-api-client

* update sgx-runtime source

* update substrate

* adjust node version values

* detect new game

* solve merge conflicts

* update sgx-runtime

* fix some things

* cargo format

Co-authored-by: haerdib <73821294+haerdib@users.noreply.github.com>
Co-authored-by: Gaudenz Kessler <gaudenz.kessler@scs.ch>
Co-authored-by: Felix Müller <mullefel@users.noreply.github.com>
Co-authored-by: Bigna Härdi <bhaerdi@devsgx02.scs-ad.scs.ch>
Co-authored-by: echevrier <84318241+echevrier@users.noreply.github.com>
Co-authored-by: echevrier <edith.chevrier@scs.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync state by retrieving a full state from a different, given worker

3 participants