Skip to content

Commit f480624

Browse files
author
Matthieu Vachon
authored
chain/near,chain/ethereum: improved revert code and fixed NEAR on revert to pass parent_ptr (#3122)
* chain/near,chain/ethereum: improved revert code and fixed NEAR on revert to pass parent_ptr
1 parent 3ad1aa7 commit f480624

File tree

3 files changed

+29
-52
lines changed

3 files changed

+29
-52
lines changed

chain/ethereum/src/chain.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -602,17 +602,17 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
602602
))
603603
}
604604

605-
StepUndo => Ok(BlockStreamEvent::Revert(
606-
BlockPtr {
607-
hash: BlockHash::from(block.hash),
608-
number: block.number as i32,
609-
},
610-
FirehoseCursor::Some(response.cursor.clone()),
611-
Some(BlockPtr {
612-
hash: BlockHash::from(block.header.unwrap().parent_hash),
613-
number: (block.number.checked_sub(1).unwrap() as i32), // Will never receive undo on blocknum 0
614-
}),
615-
)),
605+
StepUndo => {
606+
let parent_ptr = block
607+
.parent_ptr()
608+
.expect("Genesis block should never be reverted");
609+
610+
Ok(BlockStreamEvent::Revert(
611+
block.ptr(),
612+
FirehoseCursor::Some(response.cursor.clone()),
613+
Some(parent_ptr),
614+
))
615+
}
616616

617617
StepIrreversible => {
618618
unreachable!("irreversible step is not handled and should not be requested in the Firehose request")

chain/near/src/chain.rs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use graph::{
1515
},
1616
components::store::DeploymentLocator,
1717
firehose::{self as firehose, ForkStep},
18-
log::factory::{ComponentLoggerConfig, ElasticComponentLoggerConfig},
1918
prelude::{async_trait, o, BlockNumber, ChainStore, Error, Logger, LoggerFactory},
2019
};
2120
use prost::Message;
@@ -154,20 +153,7 @@ impl Blockchain for Chain {
154153
}
155154

156155
fn ingestor_adapter(&self) -> Arc<Self::IngestorAdapter> {
157-
let logger = self
158-
.logger_factory
159-
.component_logger(
160-
"BlockIngestor",
161-
Some(ComponentLoggerConfig {
162-
elastic: Some(ElasticComponentLoggerConfig {
163-
index: String::from("block-ingestor-logs"),
164-
}),
165-
}),
166-
)
167-
.new(o!());
168-
169-
let adapter = IngestorAdapter { logger };
170-
Arc::new(adapter)
156+
Arc::new(IngestorAdapter {})
171157
}
172158

173159
fn chain_store(&self) -> Arc<dyn ChainStore> {
@@ -298,6 +284,7 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
298284
response.step
299285
)
300286
});
287+
301288
let any_block = response
302289
.block
303290
.as_ref()
@@ -321,14 +308,14 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
321308

322309
StepUndo => {
323310
let header = block.header();
311+
let parent_ptr = header
312+
.parent_ptr()
313+
.expect("Genesis block should never be reverted");
324314

325315
Ok(BlockStreamEvent::Revert(
326-
BlockPtr {
327-
hash: BlockHash::from(header.hash.as_ref().unwrap().bytes.clone()),
328-
number: header.height as i32,
329-
},
316+
block.ptr(),
330317
Some(response.cursor.clone()),
331-
None, // FIXME: we should get the parent block pointer when we have access to parent block height
318+
Some(parent_ptr),
332319
))
333320
}
334321

@@ -343,42 +330,34 @@ impl FirehoseMapperTrait<Chain> for FirehoseMapper {
343330
}
344331
}
345332

346-
pub struct IngestorAdapter {
347-
logger: Logger,
348-
}
333+
pub struct IngestorAdapter {}
349334

350335
#[async_trait]
351336
impl IngestorAdapterTrait<Chain> for IngestorAdapter {
352337
fn logger(&self) -> &Logger {
353-
&self.logger
338+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
354339
}
355340

356341
fn ancestor_count(&self) -> BlockNumber {
357-
0
342+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
358343
}
359344

360345
async fn latest_block(&self) -> Result<BlockPtr, IngestorError> {
361-
Ok(BlockPtr {
362-
hash: BlockHash::from(vec![0xff; 32]),
363-
number: 0,
364-
})
346+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
365347
}
366348

367349
async fn ingest_block(
368350
&self,
369351
_block_hash: &BlockHash,
370352
) -> Result<Option<BlockHash>, IngestorError> {
371-
// FIXME (NEAR): Might not be necessary for NEAR support for now
372-
Ok(None)
353+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
373354
}
374355

375356
fn chain_head_ptr(&self) -> Result<Option<BlockPtr>, Error> {
376-
// FIXME (NEAR): Might not be necessary for NEAR support for now
377-
Ok(None)
357+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
378358
}
379359

380360
fn cleanup_cached_blocks(&self) -> Result<Option<(i32, usize)>, Error> {
381-
// FIXME (NEAR): Might not be necessary for NEAR support for now
382-
Ok(None)
361+
panic!("Should never be called, FirehoseBlockIngestor must be used instead")
383362
}
384363
}

chain/near/src/codec.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ impl Block {
4343
self.header.as_ref().unwrap()
4444
}
4545

46-
pub fn parent_ptr(&self) -> Option<BlockPtr> {
47-
self.header().parent_ptr()
46+
pub fn ptr(&self) -> BlockPtr {
47+
BlockPtr::from(self.header())
4848
}
49-
}
5049

51-
impl From<Block> for BlockPtr {
52-
fn from(b: Block) -> BlockPtr {
53-
(&b).into()
50+
pub fn parent_ptr(&self) -> Option<BlockPtr> {
51+
self.header().parent_ptr()
5452
}
5553
}
5654

0 commit comments

Comments
 (0)