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

[stdlib] Implement oracle data protocol #2727

Closed
wants to merge 2 commits into from
Closed

[stdlib] Implement oracle data protocol #2727

wants to merge 2 commits into from

Conversation

jolestar
Copy link
Member

@jolestar jolestar commented Jul 20, 2021

  1. 只考虑了数据格式的定义和更新
  2. 如果要实现去中心化 Oracle 协议,可以把 UpdatePriceCapability 托管到另外的Module 中,然后设计更新协议。
  3. 只实现了简单的一种聚合方法,未考虑历史数据的获取和更新。

public fun register_price_oracle<DataT: copy+store+drop>(signer: &signer, precision: u8, description: vector<u8>){
//TODO implement a global register by contact account.
CoreAddresses::assert_genesis_address(signer);
move_to(signer, PriceOracleInfo<DataT> {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we define a constant to limit the range of precision? like MAX_DECIMALS

Copy link
Member Author

Choose a reason for hiding this comment

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

不好确定 MAX_DECIMALS 的值,理论上 Math::pow(10, precision) < u128::max_value() 就可以吧

@codecov
Copy link

codecov bot commented Jul 20, 2021

Codecov Report

Merging #2727 (018fc09) into master (c789f7f) will decrease coverage by 0.09%.
The diff coverage is n/a.

❗ Current head 018fc09 differs from pull request most recent head 7a8fc21. Consider uploading reports for the commit 7a8fc21 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2727      +/-   ##
==========================================
- Coverage   31.78%   31.70%   -0.08%     
==========================================
  Files         508      508              
  Lines       44755    44769      +14     
  Branches    19901    19911      +10     
==========================================
- Hits        14222    14190      -32     
- Misses      16617    16752     +135     
+ Partials    13916    13827      -89     
Flag Coverage Δ
unittests 31.70% <ø> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
executor/src/script_function_test.rs 8.14% <0.00%> (-10.46%) ⬇️
sync/src/block_connector/test_write_block_chain.rs 15.89% <0.00%> (-9.34%) ⬇️
network-rpc/core/src/lib.rs 23.53% <0.00%> (-5.88%) ⬇️
network-rpc/api/src/lib.rs 18.10% <0.00%> (-2.85%) ⬇️
network-rpc/src/rpc.rs 16.03% <0.00%> (-1.92%) ⬇️
chain/src/verifier/mod.rs 29.32% <0.00%> (-1.72%) ⬇️
chain/src/chain.rs 30.66% <0.00%> (-1.71%) ⬇️
network-p2p/src/peer_info.rs 42.78% <0.00%> (-1.66%) ⬇️
sync/src/tasks/mock.rs 44.76% <0.00%> (-1.65%) ⬇️
types/src/peer_info.rs 51.56% <0.00%> (-1.24%) ⬇️
... and 19 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update deabdbc...7a8fc21. Read the comment docs.

@github-actions
Copy link

Benchmark for bc63bf0

Click to view benchmark
Test PR Benchmark Master Benchmark %
accumulator_append 3.0±0.15ms 3.0±0.18ms 0.00%
block_apply/block_apply_10 533.6±15.65ms 560.4±24.43ms -4.78%
block_apply/block_apply_1000 54.6±0.70s 54.1±0.59s +0.92%
get_with_proof/db_store 38.5±0.24µs 38.0±0.40µs +1.32%
get_with_proof/mem_store 31.7±0.32µs 31.5±0.37µs +0.63%
put_and_commit/db_store/1 2.1±0.14ms 2.1±1.11ms 0.00%
put_and_commit/db_store/10 3.2±0.77ms 3.0±0.63ms +6.67%
put_and_commit/db_store/100 10.1±1.82ms 10.1±1.73ms 0.00%
put_and_commit/db_store/5 2.6±0.28ms 2.5±0.22ms +4.00%
put_and_commit/db_store/50 6.4±1.23ms 6.3±1.21ms +1.59%
put_and_commit/mem_store/1 62.6±5.59µs 63.0±5.55µs -0.63%
put_and_commit/mem_store/10 585.4±46.67µs 587.9±48.10µs -0.43%
put_and_commit/mem_store/100 5.7±0.81ms 5.7±0.80ms 0.00%
put_and_commit/mem_store/5 298.5±26.36µs 296.4±23.86µs +0.71%
put_and_commit/mem_store/50 2.9±0.19ms 2.9±0.17ms 0.00%
query_block/query_block_in(10)_times(100) 3.0±0.16ms 3.0±0.07ms 0.00%
query_block/query_block_in(10)_times(1000) 30.5±1.07ms 29.4±0.76ms +3.74%
query_block/query_block_in(10)_times(10000) 305.4±5.99ms 293.4±5.93ms +4.09%
query_block/query_block_in(1000)_times(100) 900.7±3.71µs 928.7±4.43µs -3.01%
query_block/query_block_in(1000)_times(1000) 9.0±0.06ms 9.4±0.05ms -4.26%
query_block/query_block_in(1000)_times(10000) 90.1±0.29ms 92.7±0.74ms -2.80%
storage_transaction 73.1±4.25ms 74.0±3.55ms -1.22%
vm/transaction_execution/1 329.4±1.01ms 327.0±2.18ms +0.73%
vm/transaction_execution/10 108.9±1.94ms 106.9±1.08ms +1.87%
vm/transaction_execution/20 101.6±0.92ms 99.7±0.41ms +1.91%
vm/transaction_execution/5 130.6±0.46ms 129.3±0.38ms +1.01%
vm/transaction_execution/50 116.3±0.97ms 114.4±0.72ms +1.66%


public fun read_price_data<DataT:copy+store+drop>(addr: address): PriceData acquires PriceFeed{
let price_feed = borrow_global<PriceFeed<DataT>>(addr);
*&price_feed.value
Copy link
Contributor

@wh-antares wh-antares Jul 20, 2021

Choose a reason for hiding this comment

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

Maybe this return value should be a struct containing the precision and the number after pow.
Like struct Num { value: u64, dec: u64 } . .

Copy link
Member Author

Choose a reason for hiding this comment

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

PriceData.price 保存的是原始数据,precision 是用来展示分数的时候才需要的

@github-actions
Copy link

Benchmark for 695fd2d

Click to view benchmark
Test PR Benchmark Master Benchmark %
accumulator_append 2.1±0.13ms 2.1±0.10ms 0.00%
block_apply/block_apply_10 411.2±4.25ms 408.8±6.24ms +0.59%
block_apply/block_apply_1000 41.7±0.19s 41.7±0.14s 0.00%
get_with_proof/db_store 38.4±0.38µs 38.1±0.35µs +0.79%
get_with_proof/mem_store 31.6±0.23µs 31.5±0.25µs +0.32%
put_and_commit/db_store/1 1205.7±46.00µs 1172.2±43.64µs +2.86%
put_and_commit/db_store/10 2.2±0.30ms 2.2±0.32ms 0.00%
put_and_commit/db_store/100 9.3±0.61ms 9.2±0.64ms +1.09%
put_and_commit/db_store/5 1841.6±132.86µs 1852.3±152.03µs -0.58%
put_and_commit/db_store/50 5.4±0.48ms 5.5±0.62ms -1.82%
put_and_commit/mem_store/1 63.3±5.63µs 62.8±5.59µs +0.80%
put_and_commit/mem_store/10 586.0±47.07µs 588.1±46.21µs -0.36%
put_and_commit/mem_store/100 5.7±0.82ms 5.7±0.83ms 0.00%
put_and_commit/mem_store/5 296.3±24.23µs 296.2±24.15µs +0.03%
put_and_commit/mem_store/50 2.9±0.18ms 2.9±0.20ms 0.00%
query_block/query_block_in(10)_times(100) 3.1±0.06ms 3.0±0.08ms +3.33%
query_block/query_block_in(10)_times(1000) 30.7±0.64ms 29.8±0.54ms +3.02%
query_block/query_block_in(10)_times(10000) 305.5±6.79ms 289.8±6.28ms +5.42%
query_block/query_block_in(1000)_times(100) 928.6±3.89µs 911.7±4.94µs +1.85%
query_block/query_block_in(1000)_times(1000) 9.2±0.04ms 9.4±0.05ms -2.13%
query_block/query_block_in(1000)_times(10000) 92.7±0.32ms 93.3±0.54ms -0.64%
storage_transaction 43.2±2.87ms 42.5±2.60ms +1.65%
vm/transaction_execution/1 329.8±1.47ms 327.9±0.73ms +0.58%
vm/transaction_execution/10 108.9±1.12ms 107.6±0.92ms +1.21%
vm/transaction_execution/20 101.6±0.25ms 100.4±0.55ms +1.20%
vm/transaction_execution/5 130.8±0.37ms 129.8±0.91ms +0.77%
vm/transaction_execution/50 116.6±1.34ms 115.0±0.56ms +1.39%

@nanne007
Copy link
Member

nanne007 commented Jul 21, 2021

Can it be generalized to store any data, instead of only num value.
I imagine it to be a generalized data oracle, which can also store other values like hash root, structured data.

Then on top of it, we can do another abstraction, like price oracle to do price related oracle, merkle root oracle to do block changing oracle。

@jolestar
Copy link
Member Author

Can it be generalized to store any data, instead of only num value.
I imagine it to be a generalized data oracle, which can also store other values like hash root, structured data.

Then on top of it, we can do another abstraction, like price oracle to do price related oracle, merkle root oracle to do block changing oracle。

我开始也是这样想的,但那样设计通用的方法比较困难。我尝试另外一个思路。

@github-actions
Copy link

Benchmark for 7a5e897

Click to view benchmark
Test PR Benchmark Master Benchmark %
accumulator_append 2.5±0.11ms 2.6±0.13ms -3.85%
block_apply/block_apply_10 463.4±5.56ms 465.3±5.80ms -0.41%
block_apply/block_apply_1000 47.6±0.32s 47.2±0.38s +0.85%
get_with_proof/db_store 38.5±0.32µs 39.0±0.57µs -1.28%
get_with_proof/mem_store 31.7±0.73µs 32.0±0.23µs -0.94%
put_and_commit/db_store/1 1499.9±65.10µs 1487.9±65.87µs +0.81%
put_and_commit/db_store/10 2.7±0.34ms 2.6±0.41ms +3.85%
put_and_commit/db_store/100 9.6±1.03ms 9.6±0.97ms 0.00%
put_and_commit/db_store/5 2.1±0.22ms 2.1±0.18ms 0.00%
put_and_commit/db_store/50 5.9±0.67ms 5.8±0.70ms +1.72%
put_and_commit/mem_store/1 62.5±5.57µs 62.7±5.55µs -0.32%
put_and_commit/mem_store/10 584.1±48.41µs 587.6±48.57µs -0.60%
put_and_commit/mem_store/100 5.7±0.81ms 5.7±0.82ms 0.00%
put_and_commit/mem_store/5 294.2±24.00µs 297.3±25.46µs -1.04%
put_and_commit/mem_store/50 2.9±0.18ms 2.9±0.18ms 0.00%
query_block/query_block_in(10)_times(100) 3.0±0.07ms 2.9±0.09ms +3.45%
query_block/query_block_in(10)_times(1000) 30.0±0.52ms 29.8±0.73ms +0.67%
query_block/query_block_in(10)_times(10000) 304.6±2.80ms 292.5±2.98ms +4.14%
query_block/query_block_in(1000)_times(100) 933.5±5.74µs 917.1±8.29µs +1.79%
query_block/query_block_in(1000)_times(1000) 9.4±0.03ms 9.2±0.04ms +2.17%
query_block/query_block_in(1000)_times(10000) 95.8±0.45ms 91.3±0.53ms +4.93%
storage_transaction 57.1±4.02ms 61.0±3.98ms -6.39%
vm/transaction_execution/1 329.4±1.17ms 328.8±2.10ms +0.18%
vm/transaction_execution/10 109.4±1.80ms 107.7±1.98ms +1.58%
vm/transaction_execution/20 101.9±1.17ms 100.2±0.55ms +1.70%
vm/transaction_execution/5 130.7±0.42ms 129.8±1.52ms +0.69%
vm/transaction_execution/50 115.9±0.29ms 116.1±4.94ms -0.17%

@jolestar
Copy link
Member Author

Can it be generalized to store any data, instead of only num value.
I imagine it to be a generalized data oracle, which can also store other values like hash root, structured data.

Then on top of it, we can do another abstraction, like price oracle to do price related oracle, merkle root oracle to do block changing oracle。

#2732 中提供了另外一种实现

@jolestar jolestar closed this Jul 27, 2021
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.

3 participants