Search in sources :

Example 16 with BtcBlock

use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.

the class RepositoryBtcBlockStoreWithCache method checkIfInitialized.

private void checkIfInitialized() {
    if (getChainHead() == null) {
        BtcBlock genesisHeader = this.btcNetworkParams.getGenesisBlock().cloneAsHeader();
        StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0);
        put(storedGenesis);
        setChainHead(storedGenesis);
    }
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 17 with BtcBlock

use of co.rsk.bitcoinj.core.BtcBlock 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 18 with BtcBlock

use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.

the class MnrModuleImpl method submitBitcoinBlockTransactions.

@Override
public SubmittedBlockInfo submitBitcoinBlockTransactions(String blockHashHex, String blockHeaderHex, String coinbaseHex, String txnHashesHex) {
    logger.debug("submitBitcoinBlockTransactions(): {}, {}, {}, {}", blockHashHex, blockHeaderHex, coinbaseHex, txnHashesHex);
    NetworkParameters params = RegTestParams.get();
    new Context(params);
    BtcBlock bitcoinBlockWithHeaderOnly = getBtcBlock(blockHeaderHex, params);
    BtcTransaction coinbase = new BtcTransaction(params, Hex.decode(coinbaseHex));
    String blockHashForMergedMining = extractBlockHashForMergedMining(coinbase);
    List<String> txnHashes = parseHashes(txnHashesHex);
    SubmitBlockResult result = minerServer.submitBitcoinBlockTransactions(blockHashForMergedMining, bitcoinBlockWithHeaderOnly, coinbase, txnHashes);
    return parseResultAndReturn(result);
}
Also used : Context(co.rsk.bitcoinj.core.Context) SubmitBlockResult(co.rsk.mine.SubmitBlockResult) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 19 with BtcBlock

use of co.rsk.bitcoinj.core.BtcBlock in project rskj by rsksmart.

the class BlockTimeStampValidationRule method isBitcoinTimestampValid.

private boolean isBitcoinTimestampValid(BlockHeader header) {
    if (!activationConfig.isActive(ConsensusRule.RSKIP179, header.getNumber())) {
        return true;
    }
    byte[] bitcoinMergedMiningHeader = header.getBitcoinMergedMiningHeader();
    if (bitcoinMergedMiningHeader == null) {
        return false;
    }
    BtcBlock btcBlock = makeBlock(bitcoinMergedMiningHeader);
    if (btcBlock == null) {
        return false;
    }
    long bitcoinTimestampInSecs = btcBlock.getTimeSeconds();
    long rskTimestampInSecs = header.getTimestamp();
    boolean valid = Math.abs(bitcoinTimestampInSecs - rskTimestampInSecs) < MAX_TIMESTAMPS_DIFF_IN_SECS;
    if (!valid) {
        logger.warn("Error validating block. RSK block timestamp {} and BTC block timestamp {} differ by more than {} secs.", rskTimestampInSecs, bitcoinTimestampInSecs, MAX_TIMESTAMPS_DIFF_IN_SECS);
    }
    return valid;
}
Also used : BtcBlock(co.rsk.bitcoinj.core.BtcBlock)

Example 20 with BtcBlock

use of co.rsk.bitcoinj.core.BtcBlock 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)37 Test (org.junit.Test)17 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)16 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)11 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)10 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)9 Repository (org.ethereum.core.Repository)8 Context (co.rsk.bitcoinj.core.Context)7 BigInteger (java.math.BigInteger)7 MutableRepository (org.ethereum.db.MutableRepository)6 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 SubmitBlockResult (co.rsk.mine.SubmitBlockResult)3 InputStream (java.io.InputStream)3 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)3 MessageSerializer (co.rsk.bitcoinj.core.MessageSerializer)2 VerificationException (co.rsk.bitcoinj.core.VerificationException)2 BlockMiner (co.rsk.blockchain.utils.BlockMiner)2 RskSystemProperties (co.rsk.config.RskSystemProperties)2 Keccak256 (co.rsk.crypto.Keccak256)2