Search in sources :

Example 1 with MessageSerializer

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);
}
Also used : MessageSerializer(co.rsk.bitcoinj.core.MessageSerializer) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 2 with MessageSerializer

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));
}
Also used : MessageSerializer(co.rsk.bitcoinj.core.MessageSerializer) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Block(org.ethereum.core.Block) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

BtcBlock (co.rsk.bitcoinj.core.BtcBlock)2 MessageSerializer (co.rsk.bitcoinj.core.MessageSerializer)2 Test (org.junit.Test)2 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)1 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)1 Block (org.ethereum.core.Block)1 BlockHeader (org.ethereum.core.BlockHeader)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1