Skip to content

Commit

Permalink
Add skipStateFieldsUpdate flag to avoid eth-server calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed Nov 3, 2022
1 parent 6fd35f1 commit 5a46ac6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
3 changes: 3 additions & 0 deletions packages/uni-info-watcher/environments/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# Interval in number of blocks at which to clear entities cache.
clearEntitiesCacheInterval = 1000

# Boolean to skip updating entity fields required in state creation and not required in the frontend.
skipStateFieldsUpdate = false

[metrics]
host = "127.0.0.1"
port = 9004
Expand Down
88 changes: 50 additions & 38 deletions packages/uni-info-watcher/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,19 @@ export class Indexer implements IndexerInterface {
const transactionsMap = new Map();

const txHashSet = new Set();
events.forEach((event: any) => {
txHashSet.add(event.tx.hash);
});

await Promise.all(
Array.from(txHashSet).map(async (txHash: any) => {
const transaction = await getFullTransaction(this._ethClient, txHash, blockNumber);
transactionsMap.set(txHash, transaction);
})
);

if (!this._serverConfig.skipStateFieldsUpdate) {
events.forEach((event: any) => {
txHashSet.add(event.tx.hash);
});

await Promise.all(
Array.from(txHashSet).map(async (txHash: any) => {
const transaction = await getFullTransaction(this._ethClient, txHash, blockNumber);
transactionsMap.set(txHash, transaction);
})
);
}

for (let i = 0; i < events.length; i++) {
const {
Expand All @@ -638,15 +641,16 @@ export class Indexer implements IndexerInterface {
proof
} = events[i];

const { __typename: eventName, ...eventInfo } = event;

const extraInfo = { tx, eventIndex };

// Get full transaction for extra params like gasUsed and gasPrice.
const transaction = transactionsMap.get(tx.hash);

const { __typename: eventName, ...eventInfo } = event;

const extraInfo = {
tx: transaction,
eventIndex
};
if (transaction) {
extraInfo.tx = transaction;
}

dbEvents.push({
index: i,
Expand Down Expand Up @@ -685,14 +689,20 @@ export class Indexer implements IndexerInterface {
const { __typename: eventName } = event;

if (!this._fullBlock || (this._fullBlock.hash !== block.hash)) {
const { blockHash, blockNumber, timestamp, ...blockData } = await getFullBlock(this._ethClient, this._ethProvider, block.hash, block.number);

this._fullBlock = {
hash: blockHash,
number: blockNumber,
timestamp: Number(timestamp),
...blockData
};
// resultEvent.block does not contain all properties of a full block.
// It is fetched using getFullBlock below only if required for updating entity fields required in state creation.
this._fullBlock = block as Block;

if (!this._serverConfig.skipStateFieldsUpdate) {
const { blockHash, blockNumber, timestamp, ...blockData } = await getFullBlock(this._ethClient, this._ethProvider, block.hash, block.number);

this._fullBlock = {
hash: blockHash,
number: blockNumber,
timestamp: Number(timestamp),
...blockData
};
}

assert(this._fullBlock);
}
Expand Down Expand Up @@ -1014,7 +1024,7 @@ export class Indexer implements IndexerInterface {
factory.totalValueLockedETH = factory.totalValueLockedETH.plus(pool.totalValueLockedETH);
factory.totalValueLockedUSD = factory.totalValueLockedETH.times(bundle.ethPriceUSD);

const transaction = await loadTransaction(this._db, dbTx, { block, tx });
const transaction = await loadTransaction(this._db, dbTx, { block, tx }, this._serverConfig.skipStateFieldsUpdate);

const mint = new Mint();
mint.id = transaction.id + '#' + pool.txCount.toString();
Expand Down Expand Up @@ -1178,7 +1188,7 @@ export class Indexer implements IndexerInterface {
factory.totalValueLockedUSD = factory.totalValueLockedETH.times(bundle.ethPriceUSD);

// Burn entity.
const transaction = await loadTransaction(this._db, dbTx, { block, tx });
const transaction = await loadTransaction(this._db, dbTx, { block, tx }, this._serverConfig.skipStateFieldsUpdate);

const burn = new Burn();
burn.id = transaction.id + '#' + pool.txCount.toString();
Expand Down Expand Up @@ -1378,7 +1388,7 @@ export class Indexer implements IndexerInterface {
token1.totalValueLockedUSD = token1.totalValueLocked.times(token1.derivedETH).times(bundle.ethPriceUSD);

// Create Swap event
const transaction = await loadTransaction(this._db, dbTx, { block, tx });
const transaction = await loadTransaction(this._db, dbTx, { block, tx }, this._serverConfig.skipStateFieldsUpdate);

const swap = new Swap();
swap.id = transaction.id + '#' + pool.txCount.toString();
Expand Down Expand Up @@ -1730,17 +1740,19 @@ export class Indexer implements IndexerInterface {
async _updateTickFeeVarsAndSave (dbTx: QueryRunner, tick: Tick, block: Block, contractAddress: string): Promise<void> {
const poolAddress = contractAddress;

// Not all ticks are initialized so obtaining null is expected behavior.
console.time('time:indexer#_getPosition-eth_call_for_ticks');
const endTimer = eventProcessingEthCallDuration.startTimer();
const { value: tickResult } = await this._uniClient.ticks(block.hash, poolAddress, Number(tick.tickIdx));
endTimer();
console.timeEnd('time:indexer#_getPosition-eth_call_for_ticks');
if (!this._serverConfig.skipStateFieldsUpdate) {
// Not all ticks are initialized so obtaining null is expected behavior.
console.time('time:indexer#_getPosition-eth_call_for_ticks');
const endTimer = eventProcessingEthCallDuration.startTimer();
const { value: tickResult } = await this._uniClient.ticks(block.hash, poolAddress, Number(tick.tickIdx));
endTimer();
console.timeEnd('time:indexer#_getPosition-eth_call_for_ticks');

tick.feeGrowthOutside0X128 = tickResult.feeGrowthOutside0X128;
tick.feeGrowthOutside1X128 = tickResult.feeGrowthOutside1X128;
tick.feeGrowthOutside0X128 = tickResult.feeGrowthOutside0X128;
tick.feeGrowthOutside1X128 = tickResult.feeGrowthOutside1X128;

await this._db.saveTick(dbTx, tick, block);
await this._db.saveTick(dbTx, tick, block);
}

await updateTickDayData(this._db, dbTx, tick, { block });
}
Expand Down Expand Up @@ -1809,7 +1821,7 @@ export class Indexer implements IndexerInterface {
const dbTx = await this._db.createTransactionRunner();

try {
const transaction = await loadTransaction(this._db, dbTx, { block, tx });
const transaction = await loadTransaction(this._db, dbTx, { block, tx }, this._serverConfig.skipStateFieldsUpdate);
position.transaction = transaction.id;

await dbTx.commitTransaction();
Expand Down Expand Up @@ -1874,7 +1886,7 @@ export class Indexer implements IndexerInterface {
positionSnapshot.withdrawnToken1 = position.withdrawnToken1;
positionSnapshot.collectedFeesToken0 = position.collectedFeesToken0;
positionSnapshot.collectedFeesToken1 = position.collectedFeesToken1;
const transaction = await loadTransaction(this._db, dbTx, { block, tx });
const transaction = await loadTransaction(this._db, dbTx, { block, tx }, this._serverConfig.skipStateFieldsUpdate);
positionSnapshot.transaction = transaction.id;
positionSnapshot.feeGrowthInside0LastX128 = position.feeGrowthInside0LastX128;
positionSnapshot.feeGrowthInside1LastX128 = position.feeGrowthInside1LastX128;
Expand Down
27 changes: 15 additions & 12 deletions packages/uni-info-watcher/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const convertTokenToDecimal = (tokenAmount: bigint, exchangeDecimals: big
return (new GraphDecimal(tokenAmount.toString())).div(exponentToBigDecimal(exchangeDecimals));
};

export const loadTransaction = async (db: Database, dbTx: QueryRunner, event: { block: Block, tx: Transaction }): Promise<TransactionEntity> => {
export const loadTransaction = async (db: Database, dbTx: QueryRunner, event: { block: Block, tx: Transaction }, skipStateFieldsUpdate: boolean): Promise<TransactionEntity> => {
const { tx, block } = event;
// Get the txHash in lowercase.
const txHash = utils.hexlify(tx.hash);
Expand All @@ -64,20 +64,23 @@ export const loadTransaction = async (db: Database, dbTx: QueryRunner, event: {

transaction._blockNumber = BigInt(block.number);
transaction.timestamp = BigInt(block.timestamp);
transaction.gasUsed = BigInt(tx.gasLimit);

let gasPrice = tx.gasPrice;
if (!skipStateFieldsUpdate) {
transaction.gasUsed = BigInt(tx.gasLimit);

if (!gasPrice) {
// Compute gasPrice for EIP-1559 transaction
// https://ethereum.stackexchange.com/questions/122090/what-does-tx-gasprice-represent-after-eip-1559
const feeDifference = BigNumber.from(tx.maxFeePerGas).sub(BigNumber.from(block.baseFee));
const maxPriorityFeePerGas = BigNumber.from(tx.maxPriorityFeePerGas);
const priorityFeePerGas = maxPriorityFeePerGas.lt(feeDifference) ? maxPriorityFeePerGas : feeDifference;
gasPrice = BigNumber.from(block.baseFee).add(priorityFeePerGas).toString();
}
let gasPrice = tx.gasPrice;

if (!gasPrice) {
// Compute gasPrice for EIP-1559 transaction
// https://ethereum.stackexchange.com/questions/122090/what-does-tx-gasprice-represent-after-eip-1559
const feeDifference = BigNumber.from(tx.maxFeePerGas).sub(BigNumber.from(block.baseFee));
const maxPriorityFeePerGas = BigNumber.from(tx.maxPriorityFeePerGas);
const priorityFeePerGas = maxPriorityFeePerGas.lt(feeDifference) ? maxPriorityFeePerGas : feeDifference;
gasPrice = BigNumber.from(block.baseFee).add(priorityFeePerGas).toString();
}

transaction.gasPrice = BigInt(gasPrice);
transaction.gasPrice = BigInt(gasPrice);
}

return db.saveTransaction(dbTx, transaction, block);
};
Expand Down
4 changes: 2 additions & 2 deletions packages/uni-watcher/src/entity/SyncStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class SyncStatus implements SyncStatusInterface {
@Column('integer')
latestCanonicalBlockNumber!: number;

@Column('varchar', { length: 66, default: '0xf1156cb7e1a2acaa348c66fdc45b02f2547024ded63350a96a46ed12c62b4ff4' })
@Column('varchar', { length: 66 })
initialIndexedBlockHash!: string;

@Column('integer', { default: 12369621 })
@Column('integer')
initialIndexedBlockNumber!: number;
}

0 comments on commit 5a46ac6

Please sign in to comment.