use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class LockWhitelistTest method buildInitializer.
private BridgeStorageProviderInitializer buildInitializer() {
final int minSize = 10;
final int maxSize = 100;
final int minBtcBlocks = 500;
final int maxBtcBlocks = 1000;
return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
BtcBlockStore btcBlockStore = new RepositoryBlockStore(new RskSystemProperties(), repository, PrecompiledContracts.BRIDGE_ADDR);
Context btcContext = new Context(networkParameters);
BtcBlockChain btcBlockChain;
try {
btcBlockChain = new BtcBlockChain(btcContext, btcBlockStore);
} catch (BlockStoreException e) {
throw new RuntimeException("Error initializing btc blockchain for tests");
}
int blocksToGenerate = Helper.randomInRange(minBtcBlocks, maxBtcBlocks);
Helper.generateAndAddBlocks(btcBlockChain, blocksToGenerate);
lockWhitelist = provider.getLockWhitelist();
int size = Helper.randomInRange(minSize, maxSize);
for (int i = 0; i < size; i++) {
Address address = new BtcECKey().toAddress(networkParameters);
Coin value = Helper.randomCoin(Coin.COIN, 1, 30);
lockWhitelist.put(address, value);
}
};
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class ReceiveHeadersTest method doReceiveHeaders.
private ExecutionStats doReceiveHeaders(String caseName, int times, int numHeaders, int forkDepth) throws VMException {
String name = String.format("%s-forkdepth-%d-headers-%d", caseName, forkDepth, numHeaders);
ExecutionStats stats = new ExecutionStats(name);
int totalHeaders = numHeaders + forkDepth;
return executeAndAverage(name, times, generateABIEncoder(totalHeaders, totalHeaders, forkDepth), buildInitializer(1000, 2000), Helper.getZeroValueTxBuilder(Helper.getRandomFederatorECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider((Repository) environment.getBenchmarkedRepository(), PrecompiledContracts.BRIDGE_ADDR, constants.getBridgeConstants(), activationConfig.forBlock(0));
btcBlockStore = new RepositoryBtcBlockStoreWithCache(BridgeRegTestConstants.getInstance().getBtcParams(), (Repository) environment.getBenchmarkedRepository(), new HashMap<>(), PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, bridgeStorageProvider, activationConfig.forBlock(0));
Sha256Hash bestBlockHash = null;
try {
bestBlockHash = btcBlockStore.getChainHead().getHeader().getHash();
} catch (BlockStoreException e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals(expectedBestBlock.getHash(), bestBlockHash);
});
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCache method getStoredBlockAtMainChainHeight.
@Override
public StoredBlock getStoredBlockAtMainChainHeight(int height) throws BlockStoreException {
StoredBlock chainHead = getChainHead();
int depth = chainHead.getHeight() - height;
logger.trace("Getting btc block at depth: {}", depth);
if (depth < 0) {
String message = String.format("Height provided is higher than chain head. provided: %n. chain head: %n", height, chainHead.getHeight());
logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
throw new BlockStoreException(message);
}
if (activations.isActive(ConsensusRule.RSKIP199)) {
int btcHeightWhenBlockIndexActivates = this.bridgeConstants.getBtcHeightWhenBlockIndexActivates();
int maxDepthToSearch = this.bridgeConstants.getMaxDepthToSearchBlocksBelowIndexActivation();
int limit;
if (chainHead.getHeight() - btcHeightWhenBlockIndexActivates > maxDepthToSearch) {
limit = btcHeightWhenBlockIndexActivates;
} else {
limit = chainHead.getHeight() - maxDepthToSearch;
}
logger.trace("[getStoredBlockAtMainChainHeight] Chain head height is {} and the depth limit {}", chainHead.getHeight(), limit);
if (height < limit) {
String message = String.format("Height provided is lower than the depth limit defined to search for blocks. Provided: %n, limit: %n", height, limit);
logger.trace("[getStoredBlockAtMainChainHeight] {}", message);
throw new BlockStoreException(message);
}
}
StoredBlock block;
Optional<StoredBlock> blockOptional = getInMainchain(height);
if (blockOptional.isPresent()) {
block = blockOptional.get();
} else {
block = getStoredBlockAtMainChainDepth(depth);
}
return block;
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class Bridge method registerBtcTransaction.
public void registerBtcTransaction(Object[] args) throws VMException {
logger.trace("registerBtcTransaction");
byte[] btcTxSerialized = (byte[]) args[0];
int height = ((BigInteger) args[1]).intValue();
byte[] pmtSerialized = (byte[]) args[2];
try {
bridgeSupport.registerBtcTransaction(rskTx, btcTxSerialized, height, pmtSerialized);
} catch (IOException | BlockStoreException e) {
logger.warn("Exception in registerBtcTransaction", e);
throw new VMException("Exception in registerBtcTransaction", e);
}
}
use of co.rsk.bitcoinj.store.BlockStoreException in project rskj by rsksmart.
the class BridgeSupport method getBtcTransactionConfirmationsGetCost.
public Long getBtcTransactionConfirmationsGetCost(Object[] args) {
final long BASIC_COST = 27_000;
final long STEP_COST = 315;
// 72 * 2. 72 is the cost of the hash operation
final long DOUBLE_HASH_COST = 144;
Sha256Hash btcBlockHash;
int branchHashesSize;
try {
btcBlockHash = Sha256Hash.wrap((byte[]) args[1]);
Object[] merkleBranchHashesArray = (Object[]) args[3];
branchHashesSize = merkleBranchHashesArray.length;
} catch (NullPointerException | IllegalArgumentException e) {
return BASIC_COST;
}
// Dynamic cost based on the depth of the block that contains
// the transaction. Find such depth first, then calculate
// the cost.
Context.propagate(btcContext);
try {
this.ensureBtcBlockStore();
final StoredBlock block = btcBlockStore.getFromCache(btcBlockHash);
// Block not found, default to basic cost
if (block == null) {
return BASIC_COST;
}
final int bestChainHeight = getBtcBlockchainBestChainHeight();
// Make sure calculated depth is >= 0
final int blockDepth = Math.max(0, bestChainHeight - block.getHeight());
// Block too deep, default to basic cost
if (blockDepth > BTC_TRANSACTION_CONFIRMATION_MAX_DEPTH) {
return BASIC_COST;
}
return BASIC_COST + blockDepth * STEP_COST + branchHashesSize * DOUBLE_HASH_COST;
} catch (IOException | BlockStoreException e) {
logger.warn("getBtcTransactionConfirmationsGetCost btcBlockHash:{} there was a problem " + "gathering the block depth while calculating the gas cost. " + "Defaulting to basic cost.", btcBlockHash, e);
return BASIC_COST;
}
}
Aggregations