Use publish time instead of attest time (#194)

* Use publish time instead of attest time

This is for the logic of price freshness.
This commit is contained in:
Ali Behjati 2022-05-02 12:42:08 +04:30 committed by GitHub
parent 60a69d3e83
commit 8635668417
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 8 deletions

View File

@ -210,7 +210,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query_price_info(deps: Deps, env: Env, address: &[u8]) -> StdResult<PriceFeedResponse> { pub fn query_price_info(deps: Deps, env: Env, address: &[u8]) -> StdResult<PriceFeedResponse> {
match price_info_read(deps.storage).load(address) { match price_info_read(deps.storage).load(address) {
Ok(mut terra_price_info) => { Ok(mut terra_price_info) => {
// Attestation time is very close to the actual price time (maybe a few seconds older). let env_time_sec = env.block.time.seconds();
let price_pub_time_sec = terra_price_info.price_feed.publish_time as u64;
// Cases that it will cover: // Cases that it will cover:
// - This will ensure to set status unknown if the price has become very old and hasn't updated yet. // - This will ensure to set status unknown if the price has become very old and hasn't updated yet.
// - If a price has arrived very late to terra it will set the status to unknown. // - If a price has arrived very late to terra it will set the status to unknown.
@ -219,10 +221,10 @@ pub fn query_price_info(deps: Deps, env: Env, address: &[u8]) -> StdResult<Price
// problem in a either Terra or Solana blockchain and if it is Solana we don't want to propagate // problem in a either Terra or Solana blockchain and if it is Solana we don't want to propagate
// Solana internal problems to Terra // Solana internal problems to Terra
let time_abs_diff = let time_abs_diff =
if env.block.time.seconds() > terra_price_info.attestation_time.seconds() { if env_time_sec > price_pub_time_sec {
env.block.time.seconds() - terra_price_info.attestation_time.seconds() env_time_sec - price_pub_time_sec
} else { } else {
terra_price_info.attestation_time.seconds() - env.block.time.seconds() price_pub_time_sec - env_time_sec
}; };
if time_abs_diff > VALID_TIME_PERIOD.as_secs() { if time_abs_diff > VALID_TIME_PERIOD.as_secs() {
@ -427,14 +429,14 @@ mod test {
let address = b"123".as_ref(); let address = b"123".as_ref();
let mut dummy_price_info = PriceInfo::default(); let mut dummy_price_info = PriceInfo::default();
dummy_price_info.attestation_time = Timestamp::from_seconds(80); dummy_price_info.price_feed.publish_time = 80;
dummy_price_info.price_feed.status = PriceStatus::Trading; dummy_price_info.price_feed.status = PriceStatus::Trading;
price_info(&mut deps.storage) price_info(&mut deps.storage)
.save(address, &dummy_price_info) .save(address, &dummy_price_info)
.unwrap(); .unwrap();
env.block.time = Timestamp::from_seconds(100); env.block.time = Timestamp::from_seconds(80 + VALID_TIME_PERIOD.as_secs());
let price_feed = query_price_info(deps.as_ref(), env, address) let price_feed = query_price_info(deps.as_ref(), env, address)
.unwrap() .unwrap()
@ -449,7 +451,7 @@ mod test {
let address = b"123".as_ref(); let address = b"123".as_ref();
let mut dummy_price_info = PriceInfo::default(); let mut dummy_price_info = PriceInfo::default();
dummy_price_info.attestation_time = Timestamp::from_seconds(500); dummy_price_info.price_feed.publish_time = 500;
dummy_price_info.price_feed.status = PriceStatus::Trading; dummy_price_info.price_feed.status = PriceStatus::Trading;
price_info(&mut deps.storage) price_info(&mut deps.storage)
@ -465,6 +467,29 @@ mod test {
assert_eq!(price_feed.status, PriceStatus::Unknown); assert_eq!(price_feed.status, PriceStatus::Unknown);
} }
#[test]
fn test_query_price_info_ok_trading_future() {
let (mut deps, mut env) = setup_test();
let address = b"123".as_ref();
let mut dummy_price_info = PriceInfo::default();
dummy_price_info.price_feed.publish_time = 500;
dummy_price_info.price_feed.status = PriceStatus::Trading;
price_info(&mut deps.storage)
.save(address, &dummy_price_info)
.unwrap();
env.block.time = Timestamp::from_seconds(500 - VALID_TIME_PERIOD.as_secs());
let price_feed = query_price_info(deps.as_ref(), env, address)
.unwrap()
.price_feed;
assert_eq!(price_feed.status, PriceStatus::Trading);
}
#[test] #[test]
fn test_query_price_info_ok_stale_future() { fn test_query_price_info_ok_stale_future() {
let (mut deps, mut env) = setup_test(); let (mut deps, mut env) = setup_test();
@ -472,7 +497,7 @@ mod test {
let address = b"123".as_ref(); let address = b"123".as_ref();
let mut dummy_price_info = PriceInfo::default(); let mut dummy_price_info = PriceInfo::default();
dummy_price_info.attestation_time = Timestamp::from_seconds(500); dummy_price_info.price_feed.publish_time = 500;
dummy_price_info.price_feed.status = PriceStatus::Trading; dummy_price_info.price_feed.status = PriceStatus::Trading;
price_info(&mut deps.storage) price_info(&mut deps.storage)