Skip to content

Commit

Permalink
Remove timeout (#8)
Browse files Browse the repository at this point in the history
* Emit fee in requestMade event

* remove timeout
  • Loading branch information
jiguantong authored Oct 18, 2024
1 parent cc29279 commit 222ebcf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### On Sepolia

XAPI: `0x6984ebE378F8cb815546Cb68a98807C1fA121A81`
XAPI: `0x06e1aCbb0A1d23F57798ca36eb3e20D552AcfC62`

XAPI Consumer Example: `0xcd3627925b1396104126Fa400715A01C09703D66`

Expand Down
51 changes: 1 addition & 50 deletions aggregator/src/abstract/aggregator.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export enum RequestStatus {
FETCHING,
AGGREGATED,
PUBLISHED,
TIMEOUT,
}

export enum RequestMethod {
Expand Down Expand Up @@ -98,12 +97,6 @@ class RemoveDataSourceEvent extends Nep297Event {
}
}

class TimeoutEvent extends Nep297Event {
constructor(data: Response) {
super("Timeout", data)
}
}

class ReportEvent extends Nep297Event {
constructor(data: Report) {
super("Report", data)
Expand Down Expand Up @@ -238,17 +231,12 @@ export class Staked {
account_id: AccountId
}

// Default timeout: 5 hours
const DEFAULT_TIME_OUT = "18000000000000";

// Derivation path prefix for mpc
const DERIVATION_PATH_PREFIX = "XAPI";

export abstract class Aggregator extends ContractBase {
description: string;
latest_request_id: RequestId;
// Nanoseconds
timeout: Timestamp;

mpc_config: MpcConfig;
staking_contract: AccountId;
Expand All @@ -263,19 +251,13 @@ export abstract class Aggregator extends ContractBase {
// key: chain_id
publish_chain_config_lookup: LookupMap<PublishChainConfig>;

constructor({ description, timeout, mpc_config, reporter_required, staking_contract, contract_metadata, }: { description: string, timeout: Timestamp, mpc_config: MpcConfig, reporter_required: ReporterRequired, staking_contract: AccountId, contract_metadata: ContractSourceMetadata }) {
constructor({ description, mpc_config, reporter_required, staking_contract, contract_metadata, }: { description: string, mpc_config: MpcConfig, reporter_required: ReporterRequired, staking_contract: AccountId, contract_metadata: ContractSourceMetadata }) {
super(contract_metadata);
this.description = description;
this.mpc_config = mpc_config;
this.reporter_required = reporter_required;
this.staking_contract = staking_contract;

if (!timeout) {
this.timeout = DEFAULT_TIME_OUT;
} else {
this.timeout = timeout;
}

this.data_sources = new UnorderedMap("data_sources");
this.report_lookup = new LookupMap("report_lookup");
this.response_lookup = new LookupMap("response_lookup");
Expand Down Expand Up @@ -428,18 +410,6 @@ export abstract class Aggregator extends ContractBase {
return this.publish_chain_config_lookup.get(chain_id);
}

abstract set_timeout({ timeout }: { timeout: Timestamp }): void;
_set_timeout({ timeout }: { timeout: Timestamp }): void {
this._assert_operator();
assert(BigInt(timeout) > BigInt(0), "timeout should be greater than 0.");
this.timeout = timeout;
}

abstract get_timeout(): Timestamp;
_get_timeout(): Timestamp {
return this.timeout;
}

abstract get_latest_request_id(): string;
_get_latest_request_id(): RequestId {
return this.latest_request_id;
Expand Down Expand Up @@ -497,14 +467,6 @@ export abstract class Aggregator extends ContractBase {
_response = this.response_lookup.get(request_id);
}

// Update timeout status if necessary.
if (_response.status == RequestStatus[RequestStatus.FETCHING] && BigInt(_response.started_at) + BigInt(this.timeout) < near.blockTimestamp()) {
_response.status = RequestStatus[RequestStatus.TIMEOUT];
new TimeoutEvent(_response).emit();
this.response_lookup.set(request_id, _response);
return;
}

// Only fetching request can accept reports.
assert(
_response.status == RequestStatus[RequestStatus.FETCHING],
Expand Down Expand Up @@ -634,17 +596,6 @@ export abstract class Aggregator extends ContractBase {
assert(_response != null, `Response for ${request_id} does not exist`);
assert(_response.status == RequestStatus[RequestStatus.AGGREGATED] || _response.status == RequestStatus[RequestStatus.PUBLISHED], `Response status is ${_response.status}, can't be published`);

// Update timeout status if necessary.
if (BigInt(_response.started_at) + BigInt(this.timeout) < near.blockTimestamp()) {
if (_response.status == RequestStatus[RequestStatus.AGGREGATED]) {
_response.status = RequestStatus[RequestStatus.TIMEOUT];
new TimeoutEvent(_response).emit();
this.response_lookup.set(request_id, _response);
}
near.log(`Request ${request_id} is timeout.`);
return;
}

const _chain_config = this.publish_chain_config_lookup.get(_response.chain_id);
assert(_chain_config != null, `Chain config for ${_response.chain_id} does not exist`);

Expand Down
11 changes: 1 addition & 10 deletions aggregator/src/ormp.aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class OrmpAggregator extends Aggregator {

constructor() {
super({
description: "ORMP Aggregator", timeout: null,
description: "ORMP Aggregator",
mpc_config: new MpcConfig({ mpc_contract: "v1.signer-prod.testnet", attached_balance: BigInt(10 ** 24).toString() }),
reporter_required: new ReporterRequired(1, 1),
// todo update staking contract
Expand Down Expand Up @@ -148,11 +148,6 @@ class OrmpAggregator extends Aggregator {
super._set_publish_chain_config(publis_chain_config);
}

@call({})
set_timeout({ timeout }: { timeout: Timestamp; }): void {
super._set_timeout({ timeout })
}

@call({})
set_mpc_config(mpc_config: MpcConfig): void {
super._set_mpc_config(mpc_config);
Expand Down Expand Up @@ -191,10 +186,6 @@ class OrmpAggregator extends Aggregator {
return super._get_reports({ request_id });
}
@view({})
get_timeout(): Timestamp {
return super._get_timeout();
}
@view({})
get_publish_chain_config({ chain_id }: { chain_id: ChainId; }): PublishChainConfig {
return super._get_publish_chain_config({ chain_id })
}
Expand Down
17 changes: 11 additions & 6 deletions xapi/contracts/XAPI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ contract XAPI is IXAPI, Ownable2Step {
response: ResponseData({reporters: new address[](0), result: new bytes(0)}),
requestData: requestData
});
emit RequestMade(requestId, aggregatorConfig.aggregator, requestData, msg.sender);
emit RequestMade(
requestId,
aggregatorConfig.aggregator,
requestData,
msg.sender,
exAggregator,
aggregatorConfig.reportersFee,
aggregatorConfig.publishFee
);
return requestId;
}

Expand All @@ -53,10 +61,7 @@ contract XAPI is IXAPI, Ownable2Step {

AggregatorConfig memory aggregatorConfig = aggregatorConfigs[msg.sender];
// Avoid changing the reward configuration after the request but before the response to obtain the contract balance
require(
aggregatorConfig.publishFee + aggregatorConfig.reportersFee <= request.payment,
"Insufficient rewards"
);
require(aggregatorConfig.publishFee + aggregatorConfig.reportersFee <= request.payment, "Insufficient rewards");
for (uint256 i = 0; i < response.reporters.length; i++) {
rewards[response.reporters[i]] += aggregatorConfig.reportersFee / response.reporters.length;
}
Expand Down Expand Up @@ -110,7 +115,7 @@ contract XAPI is IXAPI, Ownable2Step {
emit AggregatorConfigSet(msg.sender, reportersFee, publishFee, aggregator, rewardAddress);
}

function suspendAggregator(address exAggregator) external onlyOwner{
function suspendAggregator(address exAggregator) external onlyOwner {
require(aggregatorConfigs[exAggregator].rewardAddress != address(0), "!Aggregator");
aggregatorConfigs[exAggregator].suspended = true;

Expand Down
16 changes: 10 additions & 6 deletions xapi/contracts/interfaces/IXAPI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ struct AggregatorConfig {
}

interface IXAPI {
event RequestMade(uint256 indexed requestId, string aggregator, string requestData, address indexed requester);
event RequestMade(
uint256 indexed requestId,
string aggregator,
string requestData,
address indexed requester,
address indexed exAggregator,
uint256 reportersFee,
uint256 publishFee
);
event Fulfilled(uint256 indexed requestId, ResponseData response, RequestStatus indexed status);
event RewardsWithdrawn(address indexed withdrawer, uint256 amount);
event AggregatorConfigSet(
address indexed exAggregator,
uint256 reportersFee,
uint256 publishFee,
string aggregator,
address rewardAddress
address indexed exAggregator, uint256 reportersFee, uint256 publishFee, string aggregator, address rewardAddress
);
event AggregatorSuspended(address indexed exAggregator, string indexed aggregator);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"XAPIModule#XAPI": "0x6984ebE378F8cb815546Cb68a98807C1fA121A81"
"XAPIModule#XAPI": "0x06e1aCbb0A1d23F57798ca36eb3e20D552AcfC62"
}
2 changes: 1 addition & 1 deletion xapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clear": "npx hardhat clean",
"deploy": "npx hardhat ignition deploy ./ignition/modules/XAPI.deploy.js --network sepolia",
"console": "npx hardhat console --network sepolia",
"verify": "npx hardhat verify --network sepolia 0x6984ebE378F8cb815546Cb68a98807C1fA121A81",
"verify": "npx hardhat verify --network sepolia 0x06e1aCbb0A1d23F57798ca36eb3e20D552AcfC62",
"test": "npx hardhat test"
},
"devDependencies": {
Expand Down

0 comments on commit 222ebcf

Please sign in to comment.