Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exchange-oracle crate and implement get_exchange_rate for coinGecko #442

Merged
merged 13 commits into from
Nov 17, 2021

Conversation

echevrier
Copy link
Contributor

@echevrier echevrier commented Oct 6, 2021

DO NOT MERGE THIS INTO MASTER
it's a showcase that may reside on a branch for now

@echevrier echevrier force-pushed the 292/_oracle_exchange branch 3 times, most recently from 7daacc0 to 8e87eee Compare November 8, 2021 08:25
enclave-runtime/Cargo.lock Outdated Show resolved Hide resolved
enclave-runtime/Cargo.toml Outdated Show resolved Hide resolved
enclave-runtime/src/lib.rs Outdated Show resolved Hide resolved
core-primitives/enclave-api/src/teeracle_api.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/Cargo.toml Show resolved Hide resolved
@echevrier echevrier removed their assignment Nov 9, 2021
@echevrier echevrier linked an issue Nov 9, 2021 that may be closed by this pull request
@echevrier echevrier force-pushed the 292/_oracle_exchange branch 2 times, most recently from 47bec31 to e8ca274 Compare November 9, 2021 15:15
@echevrier echevrier marked this pull request as ready for review November 9, 2021 16:24
@echevrier echevrier marked this pull request as draft November 9, 2021 16:25
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.

Good work! Some minor improvements I could identify. And I think it makes sense to wait a bit until #498 is merged and then update this PR again. Otherwise the code is already obsolete again.

app-libs/exchange-oracle/src/coingecko.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/error.rs Outdated Show resolved Hide resolved
&mut retval,
gen.as_ptr(),
gen.len() as u32,
&nonce,
Copy link
Contributor

Choose a reason for hiding this comment

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

The nonce is now handled entirely inside the enclave, so the e-call does not need to pass the nonce. Inside the enclave, we have a global accessor to the nonce. After my next PR (#498 ), we will have a proper nonce cache that can be accessed, rather than just a global static variable (which is not safe for concurrent access)

Copy link
Contributor

Choose a reason for hiding this comment

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

Genesis hash can also be retrieved from within the enclave

validator.genesis_hash(validator.num_relays()).unwrap(),

Validator can be unsealed like the following:

let mut validator = LightClientSeal::<PB>::unseal()?;

Though, I'm unsure what's the way to go for currently. @mullefel should the validator be unsealed for every ecall? Or is there already a lazy static like there is for the nonce?

Copy link
Contributor

Choose a reason for hiding this comment

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

At the moment we still unseal for every e-call. And in order to ensure data consistency, we need to acquire a lock before we unseal the light client (validator):

// we acquire lock explicitly (variable binding), since '_' will drop the lock after the statement
// see https://medium.com/codechain/rust-underscore-does-not-bind-fec6a18115a8
let _light_client_lock = EnclaveLock::write_light_client_db()?;
let mut validator = LightClientSeal::<PB>::unseal()?;

Then, after we're done using the light client, we seal it again and drop the lock (this is done either implicitly by going out of scope, or actively by calling std::mem::drop() on the lock.

// store updated state in light client in case we fail afterwards.
LightClientSeal::seal(validator)?;

If you're only using the light client to get the genesis hash, you can

  1. acquire the light client db read lock (EnclaveLock::read_light_client_db() )
  2. unseal the light client
  3. get the genesis hash
  4. seal the light client immediately afterwards
  5. drop the lock

pub unsafe extern "C" fn update_market_data_xt(
genesis_hash: *const u8,
genesis_hash_size: u32,
nonce: &mut u32,
Copy link
Contributor

Choose a reason for hiding this comment

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

handle nonce inside the enclave, not from outside (see previous comment)

service/src/main.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/Cargo.toml Outdated Show resolved Hide resolved
enclave-runtime/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@haerdib haerdib left a comment

Choose a reason for hiding this comment

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

Looks very good! Some picky comments from my side (someone has to substitute @clangenb 😋 ), but good work :)

app-libs/exchange-oracle/Cargo.toml Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/coingecko.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/error.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/error.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/coingecko.rs Outdated Show resolved Hide resolved
app-libs/exchange-oracle/src/lib.rs Outdated Show resolved Hide resolved
&mut retval,
gen.as_ptr(),
gen.len() as u32,
&nonce,
Copy link
Contributor

Choose a reason for hiding this comment

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

Genesis hash can also be retrieved from within the enclave

validator.genesis_hash(validator.num_relays()).unwrap(),

Validator can be unsealed like the following:

let mut validator = LightClientSeal::<PB>::unseal()?;

Though, I'm unsure what's the way to go for currently. @mullefel should the validator be unsealed for every ecall? Or is there already a lazy static like there is for the nonce?

core-primitives/settings/src/lib.rs Outdated Show resolved Hide resolved
core-primitives/settings/src/lib.rs Outdated Show resolved Hide resolved
service/src/main.rs Outdated Show resolved Hide resolved
echevrier added 3 commits November 15, 2021 14:40
author echevrier <[email protected]> 1633529369 +0200
committer echevrier <[email protected]> 1636648987 +0100

Exchange Rate Oracle Showcase

Add exchange-oracle crate and implement get_exchange_rate for coinGecko

Clean according review

Call update market data (Exchange rate) from enclave

Rename pallet market to teeracle

cargo fmt

Set MARKET_DATA_UPDATE_INTERVAL to 24h

[GA] bump node artifact

Changes from review

Set substrate-fixed version to 0.5.6 (before update to new substrate)

Changes from review

Rebase

Changes due to review

Rebase to get PR #498 to rework code according review
@murerfel
Copy link
Contributor

@brenzi I think we need the light-client in order to get the genesis hash (in order to create an extrinsic I think?). So there is this small usage of the light-client that we cannot get rid of. That would mean we'd have to modularize it for this use case so it only provides a subset of functionality.

@haerdib
Copy link
Contributor

haerdib commented Nov 16, 2021

As the whole TEEracle is basically one Ecall, so the genesis hash could also directly be given as a variable into the enclave. Like it's being done for the mock_register

@haerdib haerdib marked this pull request as ready for review November 17, 2021 09:35
let mut curr_slice = slice::from_raw_parts(currency, currency_size as usize);
let curr: String = Decode::decode(&mut curr_slice).unwrap();

let xts = match update_market_data_internal(genesis_hash, curr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

xt is also somewhat of a gray area - probably extrinsics would be better as well. A new developer won't know that xt = extrinsic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here again xt, xts are used for extrinsic (s) throughout the code. Even in function names. I change it here, but we should apply theses changes all over the code.

service/src/main.rs Outdated Show resolved Hide resolved
Comment on lines 487 to 488
let mut xthex = hex::encode(uxt);
xthex.insert_str(0, "0x");
Copy link
Contributor

Choose a reason for hiding this comment

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

hex_encoded_extrinsic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As above

service/src/main.rs Outdated Show resolved Hide resolved
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.

Approved. Thanks for putting my REST-client implementation to the test!

@haerdib haerdib merged commit 7d29a4c into exchange_rate_oracle Nov 17, 2021
@echevrier echevrier deleted the 292/_oracle_exchange branch November 18, 2021 14:26
Copy link
Contributor

@clangenb clangenb left a comment

Choose a reason for hiding this comment

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

Looks very nice in general. I only found the dependency issue with substrate fixed. Can you update that? @echevrier

enclave-runtime/Cargo.toml Show resolved Hide resolved
@@ -3910,7 +3926,7 @@ dependencies = [
"sp-io 4.0.0-dev (git+https://github.com/paritytech/substrate.git?branch=master)",
"sp-runtime",
"sp-std",
"substrate-fixed",
"substrate-fixed 0.5.7",
Copy link
Contributor

Choose a reason for hiding this comment

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

We seem to have two versions of substrate-fixed here.

# scs / integritee
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "master" }
my-node-runtime = { package = "integritee-node-runtime", git = "https://github.com/integritee-network/integritee-node", branch = "master" }
substrate-fixed = { package = "substrate-fixed", git = "https://github.com/encointer/substrate-fixed", tag = "v0.5.6" }
Copy link
Contributor

Choose a reason for hiding this comment

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

Also tag v0.5.7

@echevrier echevrier restored the 292/_oracle_exchange branch December 6, 2021 15:34
@echevrier echevrier deleted the 292/_oracle_exchange branch December 6, 2021 15:52
echevrier added a commit that referenced this pull request Dec 9, 2021
…ko (#442)

author echevrier <[email protected]> 1633529369 +0200
committer echevrier <[email protected]> 1636648987 +0100

Exchange Rate Oracle Showcase

Add exchange-oracle crate and implement get_exchange_rate for coinGecko

Clean according review

Call update market data (Exchange rate) from enclave

Rename pallet market to teeracle

cargo fmt

Set MARKET_DATA_UPDATE_INTERVAL to 24h

[GA] bump node artifact

Changes from review

Set substrate-fixed version to 0.5.6 (before update to new substrate)

Changes from review

Rebase

Changes due to review

Rebase to get PR #498 to rework code according review

* Rebase

* cargo clippy and catch error in execute_update_market to prevent that the thread is stopped

* settings for demo

* update CI because this branch will not be merged in master, but in exchange_rate_oracle branch

* changes from review

* changes from review

* Remove reference to light client for oracle

* Set update interval for exchange range to 24h

* Changes review

* Changes review

* Cargo clippy

* Changes from review: improve variable names

Co-authored-by: echevrier <[email protected]>
@brenzi brenzi restored the 292/_oracle_exchange branch December 13, 2021 16:38
@echevrier echevrier deleted the 292/_oracle_exchange branch January 4, 2022 17:39
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.

Exchange Rate Oracle Showcase
5 participants