use of co.rsk.bitcoinj.core.MessageSerializer in project rskj by rsksmart.
the class BlockValidatorTest method blockInTheFuture.
@Test
public void blockInTheFuture() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
byte[] bitcoinMergedMiningHeader = new byte[0];
int validPeriod = 6000;
// some random timestamp (taken from Sys.currentTimeMills())
long baseTimeStamp = 1627932722L;
BlockHeader header = mock(BlockHeader.class);
when(header.getBitcoinMergedMiningHeader()).thenReturn(bitcoinMergedMiningHeader);
when(header.getTimestamp()).thenReturn(baseTimeStamp + 2 * validPeriod);
when(header.getParentHash()).thenReturn(genesis.getHash());
Block block = mock(Block.class);
when(block.getHeader()).thenReturn(header);
BtcBlock btcBlock = mock(BtcBlock.class);
// a close enough block
when(btcBlock.getTimeSeconds()).thenReturn(baseTimeStamp + validPeriod - 100);
MessageSerializer messageSerializer = mock(BitcoinSerializer.class);
when(messageSerializer.makeBlock(bitcoinMergedMiningHeader)).thenReturn(btcBlock);
NetworkParameters bitcoinNetworkParameters = mock(NetworkParameters.class);
when(bitcoinNetworkParameters.getDefaultSerializer()).thenReturn(messageSerializer);
// Before Iris
blockTimeStampValidation(validPeriod, baseTimeStamp, header, block, bitcoinNetworkParameters, false);
// After Iris
blockTimeStampValidation(validPeriod, baseTimeStamp, header, block, bitcoinNetworkParameters, true);
}
use of co.rsk.bitcoinj.core.MessageSerializer in project rskj by rsksmart.
the class BlockTimeStampValidationRuleTest method timestampsAreCloseEnough.
@Test
public void timestampsAreCloseEnough() {
int validPeriod = 540;
BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod, postRskip179Config, timeProvider, bitcoinNetworkParameters);
byte[] bitcoinMergedMiningHeader = new byte[0];
when(timeProvider.currentTimeMillis()).thenReturn(10_000_000L);
BlockHeader header = mock(BlockHeader.class);
Block block = mock(Block.class);
when(block.getHeader()).thenReturn(header);
when(header.getBitcoinMergedMiningHeader()).thenReturn(bitcoinMergedMiningHeader);
BtcBlock btcBlock = mock(BtcBlock.class);
MessageSerializer messageSerializer = mock(MessageSerializer.class);
when(messageSerializer.makeBlock(eq(bitcoinMergedMiningHeader))).thenReturn(btcBlock);
when(bitcoinNetworkParameters.getDefaultSerializer()).thenReturn(messageSerializer);
when(btcBlock.getTimeSeconds()).thenReturn(1_000L);
when(header.getTimestamp()).thenReturn(1_000L);
assertTrue(validationRule.isValid(header));
when(btcBlock.getTimeSeconds()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
when(header.getTimestamp()).thenReturn(1_000L);
assertFalse(validationRule.isValid(header));
when(btcBlock.getTimeSeconds()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
when(header.getTimestamp()).thenReturn(1_001L);
assertTrue(validationRule.isValid(header));
when(btcBlock.getTimeSeconds()).thenReturn(1_001L);
when(header.getTimestamp()).thenReturn(1_000L + MAX_TIMESTAMPS_DIFF_IN_SECS);
assertTrue(validationRule.isValid(header));
}
Aggregations