Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRI-54 : Prevent front running initial account data distribution #252

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/state-manager/TransactionConsensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,23 +320,24 @@ class TransactionConsenus {
// }
// )

this.p2p.registerInternal(
'remove_timestamp_cache',
async (
payload: TimestampRemoveRequest,
respond: (result: boolean) => unknown
) => {
const { txId, cycleCounter } = payload
if (this.txTimestampCache.has(cycleCounter) && this.txTimestampCache.get(cycleCounter).has(txId)) {

/* prettier-ignore */ this.mainLogger.debug(`Removed timestamp cache for txId: ${txId}, timestamp: ${Utils.safeStringify(this.txTimestampCache.get(cycleCounter).get(txId))}`)
// remove the timestamp from the cache
this.txTimestampCache.get(cycleCounter).delete(txId)
nestedCountersInstance.countEvent('consensus', 'remove_timestamp_cache')
}
await respond(true)
}
)
// this.p2p.registerInternal(
// 'remove_timestamp_cache',
// async (
// payload: TimestampRemoveRequest,
// respond: (result: boolean) => unknown
// ) => {
// const { txId, receipt2, cycleCounter } = payload
// /* eslint-disable security/detect-object-injection */
// if (this.txTimestampCache[cycleCounter] && this.txTimestampCache[cycleCounter][txId]) {
// // remove the timestamp from the cache
// delete this.txTimestampCache[cycleCounter][txId]
// this.txTimestampCache[cycleCounter][txId] = null
// /* prettier-ignore */ this.mainLogger.debug(`Removed timestamp cache for txId: ${txId}, timestamp: ${Utils.safeStringify(this.txTimestampCache[cycleCounter][txId])}`)
// nestedCountersInstance.countEvent('consensus', 'remove_timestamp_cache')
// }
// await respond(true)
// }
// )

const getTxTimestampBinary: Route<InternalBinaryHandler<Buffer>> = {
name: InternalRouteEnum.binary_get_tx_timestamp,
Expand Down
90 changes: 45 additions & 45 deletions src/state-manager/TransactionQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,50 +341,50 @@ class TransactionQueue {
// }
// )

this.p2p.registerInternal(
'broadcast_state_complete_data',
async (payload: { txid: string; stateList: Shardus.WrappedResponse[] }) => {
profilerInstance.scopedProfileSectionStart('broadcast_state_complete_data')
try {
const queueEntry = this.getQueueEntrySafe(payload.txid) // , payload.timestamp)
if (queueEntry == null) {
nestedCountersInstance.countEvent('processing', 'broadcast_state_complete_data_noQueueEntry')
return
}
if (queueEntry.gossipedCompleteData === true) {
return
}
for (const data of payload.stateList) {
if (configContext.stateManager.collectedDataFix && configContext.stateManager.rejectSharedDataIfCovered) {
const consensusNodes = this.stateManager.transactionQueue.getConsenusGroupForAccount(data.accountId)
const coveredByUs = consensusNodes.map((node) => node.id).includes(Self.id)
if (coveredByUs) {
nestedCountersInstance.countEvent('processing', 'broadcast_state_coveredByUs')
/* prettier-ignore */ if (logFlags.verbose) console.log(`broadcast_state: coveredByUs: ${data.accountId} no need to accept this data`)
continue
} else {
this.queueEntryAddData(queueEntry, data)
}
} else {
this.queueEntryAddData(queueEntry, data)
}
}
Comms.sendGossip(
'broadcast_state_complete_data',
payload,
undefined,
undefined,
queueEntry.executionGroup,
false,
6,
queueEntry.acceptedTx.txId
)
queueEntry.gossipedCompleteData = true
} finally {
profilerInstance.scopedProfileSectionEnd('broadcast_state_complete_data')
}
}
)
// this.p2p.registerInternal(
// 'broadcast_state_complete_data',
// async (payload: { txid: string; stateList: Shardus.WrappedResponse[] }) => {
// profilerInstance.scopedProfileSectionStart('broadcast_state_complete_data')
// try {
// const queueEntry = this.getQueueEntrySafe(payload.txid) // , payload.timestamp)
// if (queueEntry == null) {
// nestedCountersInstance.countEvent('processing', 'broadcast_state_complete_data_noQueueEntry')
// return
// }
// if (queueEntry.gossipedCompleteData === true) {
// return
// }
// for (const data of payload.stateList) {
// if (configContext.stateManager.collectedDataFix && configContext.stateManager.rejectSharedDataIfCovered) {
// const consensusNodes = this.stateManager.transactionQueue.getConsenusGroupForAccount(data.accountId)
// const coveredByUs = consensusNodes.map((node) => node.id).includes(Self.id)
// if (coveredByUs) {
// nestedCountersInstance.countEvent('processing', 'broadcast_state_coveredByUs')
// /* prettier-ignore */ if (logFlags.verbose) console.log(`broadcast_state: coveredByUs: ${data.accountId} no need to accept this data`)
// continue
// } else {
// this.queueEntryAddData(queueEntry, data)
// }
// } else {
// this.queueEntryAddData(queueEntry, data)
// }
// }
// Comms.sendGossip(
// 'broadcast_state_complete_data',
// payload,
// undefined,
// undefined,
// queueEntry.executionGroup,
// false,
// 6,
// queueEntry.acceptedTx.txId
// )
// queueEntry.gossipedCompleteData = true
// } finally {
// profilerInstance.scopedProfileSectionEnd('broadcast_state_complete_data')
// }
// }
// )

const broadcastStateRoute: P2PTypes.P2PTypes.Route<InternalBinaryHandler<Buffer>> = {
name: InternalRouteEnum.binary_broadcast_state,
Expand Down Expand Up @@ -2822,7 +2822,7 @@ class TransactionQueue {
const payload = {txid: queueEntry.acceptedTx.txId, stateList}
if (stateList.length > 0) {
Comms.sendGossip(
'broadcast_state_complete_data',
'broadcast_state_complete_data', // deprecated
payload,
'',
Self.id,
Expand Down
58 changes: 29 additions & 29 deletions src/state-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1866,34 +1866,34 @@ class StateManager {

// TODO STATESHARDING4 ENDPOINTS ok, I changed this to tell, but we still need to check sender!
//this.p2p.registerGossipHandler('spread_appliedVote', async (payload, sender, tracker) => {
this.p2p.registerInternal(
'spread_appliedVote',
async (
payload: AppliedVote,
_respond: unknown,
_sender: unknown,
_tracker: string,
msgSize: number
) => {
profilerInstance.scopedProfileSectionStart('spread_appliedVote', false, msgSize)
try {
const queueEntry = this.transactionQueue.getQueueEntrySafe(payload.txid) // , payload.timestamp)
if (queueEntry == null) {
/* prettier-ignore */ nestedCountersInstance.countEvent('stateManager', 'spread_appliedVote_no_queue_entry')
return
}
const newVote = payload as AppliedVote
// TODO STATESHARDING4 ENDPOINTS check payload format
// TODO STATESHARDING4 ENDPOINTS that this message is from a valid sender (may need to check docs)
// this.p2p.registerInternal(
// 'spread_appliedVote',
// async (
// payload: AppliedVote,
// _respond: unknown,
// _sender: unknown,
// _tracker: string,
// msgSize: number
// ) => {
// profilerInstance.scopedProfileSectionStart('spread_appliedVote', false, msgSize)
// try {
// const queueEntry = this.transactionQueue.getQueueEntrySafe(payload.txid) // , payload.timestamp)
// if (queueEntry == null) {
// /* prettier-ignore */ nestedCountersInstance.countEvent('stateManager', 'spread_appliedVote_no_queue_entry')
// return
// }
// const newVote = payload as AppliedVote
// // TODO STATESHARDING4 ENDPOINTS check payload format
// // TODO STATESHARDING4 ENDPOINTS that this message is from a valid sender (may need to check docs)

if (this.transactionConsensus.tryAppendVote(queueEntry, newVote)) {
// Note this was sending out gossip, but since this needs to be converted to a tell function i deleted the gossip send
}
} finally {
profilerInstance.scopedProfileSectionEnd('spread_appliedVote')
}
}
)
// if (this.transactionConsensus.tryAppendVote(queueEntry, newVote)) {
// // Note this was sending out gossip, but since this needs to be converted to a tell function i deleted the gossip send
// }
// } finally {
// profilerInstance.scopedProfileSectionEnd('spread_appliedVote')
// }
// }
// )

// this.p2p.registerInternal(
// 'spread_appliedVoteHash',
Expand Down Expand Up @@ -2338,7 +2338,7 @@ class StateManager {
this.p2p.unregisterGossipHandler('spread_tx_to_group')
// this.p2p.unregisterInternal('get_account_data_with_queue_hints')
// this.p2p.unregisterInternal('get_globalaccountreport')
this.p2p.unregisterInternal('spread_appliedVote')
// this.p2p.unregisterInternal('spread_appliedVote')
this.p2p.unregisterGossipHandler('spread_appliedReceipt')

// this.p2p.unregisterInternal('get_trie_hashes')
Expand Down Expand Up @@ -4390,7 +4390,7 @@ class StateManager {
// attach challenge receipt to payload
const payload: TimestampRemoveRequest = {txId: acceptedTx.txId, signedReceipt, cycleCounter}
try {
await this.p2p.tell([homeNode.node], 'remove_timestamp_cache', payload)
await this.p2p.tell([homeNode.node], 'remove_timestamp_cache', payload) //deprecated
} catch (e) {
nestedCountersInstance.countEvent('askToRemoveTimestampCache', `error: ${e.message}`)
this.mainLogger.error(`askToRemoveTimestampCache error: ${e.message}`)
Expand Down
Loading