Search in sources :

Example 21 with BtcBlock

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

the class MnrModuleImpl method submitBitcoinBlockPartialMerkle.

@Override
public SubmittedBlockInfo submitBitcoinBlockPartialMerkle(String blockHashHex, String blockHeaderHex, String coinbaseHex, String merkleHashesHex, String blockTxnCountHex) {
    logger.debug("submitBitcoinBlockPartialMerkle(): {}, {}, {}, {}, {}", blockHashHex, blockHeaderHex, coinbaseHex, merkleHashesHex, blockTxnCountHex);
    if (merkleHashesHex.isEmpty()) {
        throw new JsonRpcSubmitBlockException("The list of merkle hashes can't be empty");
    }
    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> merkleHashes = parseHashes(merkleHashesHex);
    int txnCount = Integer.parseInt(blockTxnCountHex, 16);
    SubmitBlockResult result = minerServer.submitBitcoinBlockPartialMerkle(blockHashForMergedMining, bitcoinBlockWithHeaderOnly, coinbase, merkleHashes, txnCount);
    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) JsonRpcSubmitBlockException(co.rsk.rpc.exception.JsonRpcSubmitBlockException)

Example 22 with BtcBlock

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

the class MnrModuleImpl method submitBitcoinBlock.

@Override
public SubmittedBlockInfo submitBitcoinBlock(String bitcoinBlockHex) {
    logger.debug("submitBitcoinBlock(): {}", bitcoinBlockHex.length());
    NetworkParameters params = RegTestParams.get();
    new Context(params);
    BtcBlock bitcoinBlock = getBtcBlock(bitcoinBlockHex, params);
    BtcTransaction coinbase = bitcoinBlock.getTransactions().get(0);
    String blockHashForMergedMining = extractBlockHashForMergedMining(coinbase);
    SubmitBlockResult result = minerServer.submitBitcoinBlock(blockHashForMergedMining, bitcoinBlock);
    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 23 with BtcBlock

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

the class RepositoryBtcBlockStoreWithCacheTest method ifCacheNullAlwaysGoToDisk.

@Test
public void ifCacheNullAlwaysGoToDisk() throws BlockStoreException {
    Repository repository = createRepository();
    BtcBlockStoreWithCache btcBlockStore = new RepositoryBtcBlockStoreWithCache(networkParameters, repository.startTracking(), null, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
    BtcBlock genesis = networkParameters.getGenesisBlock();
    StoredBlock firstStoredBlock = createStoredBlock(genesis, 1, 0);
    Sha256Hash firstBlockHash = firstStoredBlock.getHeader().getHash();
    btcBlockStore.put(firstStoredBlock);
    btcBlockStore.setChainHead(firstStoredBlock);
    assertEquals(firstStoredBlock, btcBlockStore.get(firstBlockHash));
    assertNull(btcBlockStore.getFromCache(firstBlockHash));
    assertEquals(firstStoredBlock, btcBlockStore.getChainHead());
    assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainDepth(1).getHeader());
    assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainHeight(0).getHeader());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 24 with BtcBlock

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

the class RepositoryBtcBlockStoreWithCacheTest method put_oldBlockShouldNotGoToCache.

@Test
public void put_oldBlockShouldNotGoToCache() throws BlockStoreException {
    BtcBlockStoreWithCache btcBlockStore = createBlockStore();
    BtcBlock genesis = networkParameters.getGenesisBlock();
    // Set chain head at height 6000
    StoredBlock storedBlock1 = createStoredBlock(genesis, 6000, 0);
    Sha256Hash firstBlockHash = storedBlock1.getHeader().getHash();
    btcBlockStore.put(storedBlock1);
    btcBlockStore.setChainHead(storedBlock1);
    // Store a block of height 1 which is lesser than chainHead - 5000
    StoredBlock storedBlock2 = createStoredBlock(genesis, 1, 1);
    Sha256Hash secondBlockHash = storedBlock2.getHeader().getHash();
    btcBlockStore.put(storedBlock2);
    assertEquals(storedBlock1, btcBlockStore.getFromCache(firstBlockHash));
    assertNull(btcBlockStore.getFromCache(secondBlockHash));
    assertEquals(storedBlock1, btcBlockStore.get(firstBlockHash));
    assertEquals(storedBlock2, btcBlockStore.get(secondBlockHash));
    assertEquals(storedBlock1, btcBlockStore.getChainHead());
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

Example 25 with BtcBlock

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

the class RepositoryBtcBlockStoreWithCacheTest method getStoredBlockAtMainChainDepth.

@Test
public void getStoredBlockAtMainChainDepth() throws BlockStoreException {
    BtcBlockStoreWithCache btcBlockStore = createBlockStore();
    BtcBlock genesis = networkParameters.getGenesisBlock();
    StoredBlock storedBlock1 = createStoredBlock(genesis, 1, 0);
    btcBlockStore.put(storedBlock1);
    StoredBlock storedBlock2 = createStoredBlock(storedBlock1.getHeader(), 2, 0);
    btcBlockStore.put(storedBlock2);
    StoredBlock storedBlock3 = createStoredBlock(storedBlock2.getHeader(), 3, 0);
    btcBlockStore.put(storedBlock3);
    StoredBlock storedBlock4 = createStoredBlock(storedBlock3.getHeader(), 4, 0);
    btcBlockStore.put(storedBlock4);
    btcBlockStore.setChainHead(storedBlock4);
    assertEquals(storedBlock4, btcBlockStore.getChainHead());
    int maxHeight = storedBlock4.getHeight();
    // Check getStoredBlockAtMainChainDepth
    assertEquals(storedBlock3, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock3.getHeight()));
    assertEquals(storedBlock2, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock2.getHeight()));
    assertEquals(storedBlock1, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight - storedBlock1.getHeight()));
    assertEquals(genesis, btcBlockStore.getStoredBlockAtMainChainDepth(maxHeight).getHeader());
}
Also used : StoredBlock(co.rsk.bitcoinj.core.StoredBlock) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) Test(org.junit.Test)

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