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);
}
}
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);
}
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);
}
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;
}
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));
}
Aggregations