Skip to content

Commit

Permalink
update pseudo-feeder to use timeout_commit in config.toml (#70)
Browse files Browse the repository at this point in the history
* update pseudo-feeder to use timeout_commit in config.toml

* feat: update core and fcd image
refactor: pseudo-feeder

Co-authored-by: Paul Kim <paul@terra.money>
  • Loading branch information
Vritra4 and hanjukim committed Mar 28, 2022
1 parent 34306d6 commit 3d19e04
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 49 deletions.
10 changes: 6 additions & 4 deletions docker-compose.yml
Expand Up @@ -2,7 +2,7 @@ version: "3"

services:
terrad:
image: terramoney/localterra-core:0.5.14
image: terramoney/localterra-core:0.5.17
pull_policy: always
volumes:
- ./config:/root/.terra/config
Expand All @@ -15,10 +15,12 @@ services:
- "9091:9091"
command: terrad start
oracle:
image: terramoney/pseudo-feeder:0.5.5
image: terramoney/pseudo-feeder:0.5.6
pull_policy: always
depends_on:
- terrad
volumes:
- ./config/config.toml:/app/config.toml
networks:
- terra
environment:
Expand All @@ -40,7 +42,7 @@ services:
# ports:
# - "6379:6379"
fcd-collector:
image: terramoney/fcd:1.0.11
image: terramoney/fcd:1.0.14
depends_on:
- terrad
- postgres
Expand All @@ -52,7 +54,7 @@ services:
command: collector
restart: unless-stopped
fcd-api:
image: terramoney/fcd:1.0.11
image: terramoney/fcd:1.0.14
depends_on:
- terrad
- postgres
Expand Down
78 changes: 49 additions & 29 deletions pseudo-feeder/index.ts
Expand Up @@ -4,8 +4,10 @@ import {
LCDClient,
MnemonicKey,
MsgAggregateExchangeRateVote,
Fee
} from "@terra-money/terra.js";
import { parse } from "toml";
import * as fs from "fs";
import * as ms from "ms"

const {
MAINNET_LCD_URL = "https://lcd.terra.dev",
Expand All @@ -15,6 +17,18 @@ const {
MNEMONIC = "satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn",
} = process.env;

const config = parse(fs.readFileSync("./config.toml").toString());

/* unused
const timeout_propose = toMs(config.consensus.timeout_propose); // 3s by default
const timeout_propose_delta = toMs(config.consensus.timeout_propose_delta); // 500ms by default
const timeout_prevote = toMs(config.consensus.timeout_prevote); // 1s by default
const timeout_prevote_delta = toMs(config.consensus.timeout_prevote_delta); // 500ms by default
const timeout_prevcommit = toMs(config.consensus.timeout_precommit); // 1s by default
const timeout_precommit_delta = toMs(config.consensus.timeout_precommit_delta); // 500ms by default
*/
const timeoutCommit = ms(config.consensus.timeout_commit); // 5s by default

function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down Expand Up @@ -52,10 +66,10 @@ async function waitForFirstBlock(client: LCDClient) {
console.info("waiting for first block");

while (!shouldTerminate) {
await delay(timeoutCommit);
shouldTerminate = await client.tendermint
.blockInfo()
.then(async (blockInfo) => {
await delay(5000);

if (blockInfo?.block) {
return +blockInfo.block?.header.height > 0;
Expand All @@ -65,7 +79,7 @@ async function waitForFirstBlock(client: LCDClient) {
})
.catch(async (err) => {
console.error(err);
await delay(1000);
await delay(timeoutCommit);
return false;
});

Expand Down Expand Up @@ -97,23 +111,33 @@ async function loop() {
let lastSuccessVotePeriod: number;
let lastSuccessVoteMsg: MsgAggregateExchangeRateVote;

// to get initial rates and params
let [rates, oracleParams] = await Promise.all([
mainnetClient.oracle.exchangeRates(),
testnetClient.oracle.parameters(),
]);

setInterval(async () => {
try {
[rates, oracleParams] = await Promise.all([
mainnetClient.oracle.exchangeRates(),
testnetClient.oracle.parameters(),
]);
} catch (e) { }
}, 10000); // 5s -> 10s: to avoid rate limit

while (true) {
const [rates, oracleParams, latestBlock] = await Promise.all([
mainnetClient.oracle.exchangeRates(),
testnetClient.oracle.parameters(),
testnetClient.tendermint.blockInfo(),
])
const latestBlock = await testnetClient.tendermint.blockInfo();

const oracleVotePeriod = oracleParams.vote_period;
const currentBlockHeight = parseInt(latestBlock.block.header.height, 10);
const currentVotePeriod = Math.floor(currentBlockHeight / oracleVotePeriod);
const indexInVotePeriod = currentBlockHeight % oracleVotePeriod;

if (
(lastSuccessVotePeriod && lastSuccessVotePeriod === currentVotePeriod) ||
indexInVotePeriod >= oracleVotePeriod - 1
(lastSuccessVotePeriod && (lastSuccessVotePeriod === currentVotePeriod)) ||
(indexInVotePeriod >= oracleVotePeriod - 1)
) {
await delay(1000);
await delay(timeoutCommit);
continue;
}

Expand All @@ -134,22 +158,18 @@ async function loop() {
);

const msgs = [lastSuccessVoteMsg, voteMsg.getPrevote()].filter(Boolean);
const tx = await wallet.createAndSignTx({ msgs, fee: new Fee(200000, '30000uusd') });

await testnetClient.tx
.broadcast(tx)
.then((result) => {
console.log(
`vote_period: ${currentVotePeriod}, txhash: ${result.txhash}`
);
lastSuccessVotePeriod = currentVotePeriod;
lastSuccessVoteMsg = voteMsg;
})
.catch((err) => {
console.error(err.message);
});

await delay(5000);
try {
const tx = await wallet.createAndSignTx({ msgs });
const result = await testnetClient.tx.broadcast(tx);
console.log(`vote_period: ${currentVotePeriod}, txhash: ${result.txhash}`);
lastSuccessVotePeriod = currentVotePeriod;
lastSuccessVoteMsg = voteMsg;
}
catch (err) {
console.log(err.message);
delay(timeoutCommit);
};
await delay(timeoutCommit * (oracleVotePeriod - 1)); // (period-1) because of broadcast
}
}

Expand All @@ -158,6 +178,6 @@ async function loop() {

while (true) {
await loop().catch(console.error);
await delay(5000);
await delay(timeoutCommit * 5);
}
})();
63 changes: 49 additions & 14 deletions pseudo-feeder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pseudo-feeder/package.json
Expand Up @@ -9,11 +9,13 @@
"author": "",
"license": "MIT",
"dependencies": {
"@terra-money/terra.js": "^3.0.3",
"@terra-money/terra.js": "^3.0.8",
"@types/node": "^17.0.8",
"bluebird": "^3.7.2",
"ms": "^2.1.3",
"node-fetch": "^3.1.1",
"ts-node": "^10.0.0",
"toml": "^3.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.1.3"
}
}

0 comments on commit 3d19e04

Please sign in to comment.