Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
save transaction per events, and process events based on transaction …
Browse files Browse the repository at this point in the history
…time

related to #51
  • Loading branch information
mohammadranjbarz committed Aug 31, 2021
1 parent d3b0f30 commit 54fba65
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ $RECYCLE.BIN/

# Others
data/
config/local.json
71 changes: 63 additions & 8 deletions feathers-src/lib/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const processNextWaitingEvent = async () => {
const query = {
status: EventStatus.WAITING,
$sort: {
// maybe we get some events from home and blockchain with different blockNumber, so the transactionTime
// should be the first important thing for decide what event should process first
timestamp: 1,
isHomeEvent: 1,
blockNumber: 1,
transactionIndex: 1,
Expand Down Expand Up @@ -114,6 +117,64 @@ async function fetchForeignEvents(foreignRange) {
return events;
}


/**
* fetches the transaction for the given hash in foreign network.
*
* first checks if the transaction has been saved in db
* if it misses, we fetch the block using web3 and save it in db
*
* if we are currently fetching a given hash, we will not fetch it twice.
* instead, we resolve the promise after we fetch the transaction for the hash.
*
* @param {string} hash the hash to fetch the transaction of
* @param {boolean} isHome get transaction of home network
*/
const getTransaction = async (hash, isHome = false) => {
if (!hash) {
throw new errors.NotFound(`Hash value cannot be undefined`);
}
const Transaction = app.get('transactionsModel');
const query = { hash, isHome };
const result = await Transaction.findOne(query);
if (result) {
return result;
}

const web3 = isHome ? getHomeWeb3() : getForeignWeb3();
const tx = await web3.eth.getTransaction(hash);
if (!tx) {
// sometimes tx is not null but the tx.blockNumber is null (maybe when transaction is not mined already)
throw new errors.NotFound(`Not tx found for ${hash}`);
}
const { from, blockNumber } = tx;

const model = {
hash,
from,
isHome: !!isHome,
};

// Transaction is mined
if (blockNumber) {
model.blockNumber = blockNumber;

const { timestamp } = await web3.eth.getBlock(blockNumber);
model.timestamp = new Date(timestamp * 1000);
const transaction = new Transaction(model);
await transaction.save();
}
return model;
};

const createEvent = async (event, isHomeEvent)=>{
const {timestamp} = await getTransaction(event.transactionHash, isHomeEvent)
await app
.service('events')
.create({ isHomeEvent, timestamp, ...event })
.catch(err => logger.error(JSON.stringify(event, null, 2), err))
}

const populate = async () => {
logger.debug('Fetch initial block numbers');
setLastForeignBlock((await getLastBlock()) + 1);
Expand Down Expand Up @@ -204,10 +265,7 @@ const populate = async () => {
try {
await Promise.all(
homeEvents.map(event =>
app
.service('events')
.create({ isHomeEvent: true, ...event })
.catch(err => logger.error(JSON.stringify(event, null, 2), err)),
createEvent(event, true)
),
);
} catch (err) {
Expand All @@ -216,10 +274,7 @@ const populate = async () => {
try {
await Promise.all(
foreignEvents.map(event =>
app
.service('events')
.create({ isHomeEvent: false, ...event })
.catch(err => logger.error(JSON.stringify(event, null, 2), err)),
createEvent(event, false)
),
);
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions feathers-src/models/events.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const createModel = app => {
processingError: { type: String },
confirmations: { type: Number, require: true },
isHomeEvent: { type: Boolean, default: false, index: true },
timestamp: { type: Date },
},
{
timestamp: true,
Expand Down

0 comments on commit 54fba65

Please sign in to comment.