use of co.rsk.peg.simples.SimpleBlockChain in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransactionTxNotLockNorReleaseTx.
@Test
public void registerBtcTransactionTxNotLockNorReleaseTx() throws BlockStoreException, AddressFormatException, IOException {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
BtcTransaction tx = new BtcTransaction(this.btcParams);
Address address = ScriptBuilder.createP2SHOutputScript(2, Lists.newArrayList(new BtcECKey(), new BtcECKey(), new BtcECKey())).getToAddress(bridgeConstants.getBtcParams());
tx.addOutput(Coin.COIN, address);
tx.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, new BtcECKey()));
Context btcContext = new Context(bridgeConstants.getBtcParams());
BtcBlockStore btcBlockStore = new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
BtcBlockChain btcBlockChain = new SimpleBlockChain(btcContext, btcBlockStore);
BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
BridgeSupport bridgeSupport = new BridgeSupport(config, track, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, btcBlockStore, btcBlockChain);
byte[] bits = new byte[1];
bits[0] = 0x01;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock block = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
btcBlockChain.add(block);
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx, 1, pmt);
bridgeSupport.save();
track.commit();
BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(0, provider2.getNewFederationBtcUTXOs().size());
Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider2.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider2.getBtcTxHashesAlreadyProcessed().isEmpty());
}
use of co.rsk.peg.simples.SimpleBlockChain in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransactionMigrationTx.
@Test
public void registerBtcTransactionMigrationTx() throws BlockStoreException, AddressFormatException, IOException {
BridgeConstants bridgeConstants = BridgeRegTestConstants.getInstance();
NetworkParameters parameters = bridgeConstants.getBtcParams();
List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation activeFederation = new Federation(activeFederationKeys, Instant.ofEpochMilli(2000L), 2L, parameters);
List<BtcECKey> retiringFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation retiringFederation = new Federation(retiringFederationKeys, Instant.ofEpochMilli(1000L), 1L, parameters);
Repository repository = new RepositoryImpl(config);
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
Block executionBlock = Mockito.mock(Block.class);
Mockito.when(executionBlock.getNumber()).thenReturn(15L);
Repository track = repository.startTracking();
BtcTransaction tx = new BtcTransaction(parameters);
Address activeFederationAddress = activeFederation.getAddress();
tx.addOutput(Coin.COIN, activeFederationAddress);
// Create previous tx
BtcTransaction prevTx = new BtcTransaction(btcParams);
TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, retiringFederation.getAddress());
prevTx.addOutput(prevOut);
// Create tx input
tx.addInput(prevOut);
// Create tx input base script sig
Script scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(retiringFederation);
// Create sighash
Script redeemScript = ScriptBuilder.createRedeemScript(retiringFederation.getNumberOfSignaturesRequired(), retiringFederation.getPublicKeys());
Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
// Sign by federator 0
BtcECKey.ECDSASignature sig0 = retiringFederationKeys.get(0).sign(sighash);
TransactionSignature txSig0 = new TransactionSignature(sig0, BtcTransaction.SigHash.ALL, false);
int sigIndex0 = scriptSig.getSigInsertionIndex(sighash, retiringFederation.getPublicKeys().get(0));
scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig0.encodeToBitcoin(), sigIndex0, 1, 1);
// Sign by federator 1
BtcECKey.ECDSASignature sig1 = retiringFederationKeys.get(1).sign(sighash);
TransactionSignature txSig1 = new TransactionSignature(sig1, BtcTransaction.SigHash.ALL, false);
int sigIndex1 = scriptSig.getSigInsertionIndex(sighash, retiringFederation.getPublicKeys().get(1));
scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig1.encodeToBitcoin(), sigIndex1, 1, 1);
// Set scipt sign to tx input
tx.getInput(0).setScriptSig(scriptSig);
Context btcContext = new Context(bridgeConstants.getBtcParams());
BtcBlockStore btcBlockStore = new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
BtcBlockChain btcBlockChain = new SimpleBlockChain(btcContext, btcBlockStore);
BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider.setNewFederation(activeFederation);
provider.setOldFederation(retiringFederation);
BridgeSupport bridgeSupport = new BridgeSupport(config, track, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, btcBlockStore, btcBlockChain);
Whitebox.setInternalState(bridgeSupport, "rskExecutionBlock", executionBlock);
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock block = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
btcBlockChain.add(block);
((SimpleBlockChain) btcBlockChain).useHighBlock();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx, 1, pmt);
bridgeSupport.save();
((SimpleBlockChain) btcBlockChain).useBlock();
track.commit();
List<UTXO> activeFederationBtcUTXOs = provider.getNewFederationBtcUTXOs();
List<Coin> activeFederationBtcCoins = activeFederationBtcUTXOs.stream().map(UTXO::getValue).collect(Collectors.toList());
assertThat(activeFederationBtcUTXOs, hasSize(1));
assertThat(activeFederationBtcCoins, hasItem(Coin.COIN));
}
use of co.rsk.peg.simples.SimpleBlockChain in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransactionLockTxNotWhitelisted.
@Test
public void registerBtcTransactionLockTxNotWhitelisted() throws BlockStoreException, AddressFormatException, IOException {
BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
NetworkParameters parameters = bridgeConstants.getBtcParams();
List<BtcECKey> federation1Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")) });
federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation federation1 = new Federation(federation1Keys, Instant.ofEpochMilli(1000L), 0L, parameters);
List<BtcECKey> federation2Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")) });
federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation federation2 = new Federation(federation2Keys, Instant.ofEpochMilli(2000L), 0L, parameters);
Repository repository = new RepositoryImpl(config);
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
Block executionBlock = Mockito.mock(Block.class);
Mockito.when(executionBlock.getNumber()).thenReturn(10L);
Repository track = repository.startTracking();
// First transaction goes only to the first federation
BtcTransaction tx1 = new BtcTransaction(this.btcParams);
tx1.addOutput(Coin.COIN.multiply(5), federation1.getAddress());
BtcECKey srcKey1 = new BtcECKey();
tx1.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey1));
// Second transaction goes only to the second federation
BtcTransaction tx2 = new BtcTransaction(this.btcParams);
tx2.addOutput(Coin.COIN.multiply(10), federation2.getAddress());
BtcECKey srcKey2 = new BtcECKey();
tx2.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey2));
// Third transaction has one output to each federation
// Lock is expected to be done accordingly and utxos assigned accordingly as well
BtcTransaction tx3 = new BtcTransaction(this.btcParams);
tx3.addOutput(Coin.COIN.multiply(3), federation1.getAddress());
tx3.addOutput(Coin.COIN.multiply(4), federation2.getAddress());
BtcECKey srcKey3 = new BtcECKey();
tx3.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey3));
Context btcContext = new Context(bridgeConstants.getBtcParams());
BtcBlockStore btcBlockStore = new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
BtcBlockChain btcBlockChain = new SimpleBlockChain(btcContext, btcBlockStore);
BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider.setNewFederation(federation1);
provider.setOldFederation(federation2);
BridgeSupport bridgeSupport = new BridgeSupport(config, track, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, btcBlockStore, btcBlockChain);
Whitebox.setInternalState(bridgeSupport, "rskExecutionBlock", executionBlock);
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx1.getHash());
hashes.add(tx2.getHash());
hashes.add(tx3.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 3);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock block = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
btcBlockChain.add(block);
Transaction rskTx1 = getMockedRskTxWithHash("aa");
Transaction rskTx2 = getMockedRskTxWithHash("bb");
Transaction rskTx3 = getMockedRskTxWithHash("cc");
((SimpleBlockChain) btcBlockChain).useHighBlock();
bridgeSupport.registerBtcTransaction(rskTx1, tx1, 1, pmt);
bridgeSupport.registerBtcTransaction(rskTx2, tx2, 1, pmt);
bridgeSupport.registerBtcTransaction(rskTx3, tx3, 1, pmt);
bridgeSupport.save();
((SimpleBlockChain) btcBlockChain).useBlock();
track.commit();
RskAddress srcKey1RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey1.getPrivKey()).getAddress());
RskAddress srcKey2RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey2.getPrivKey()).getAddress());
RskAddress srcKey3RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey3.getPrivKey()).getAddress());
Assert.assertEquals(0, repository.getBalance(srcKey1RskAddress).asBigInteger().intValue());
Assert.assertEquals(0, repository.getBalance(srcKey2RskAddress).asBigInteger().intValue());
Assert.assertEquals(0, repository.getBalance(srcKey3RskAddress).asBigInteger().intValue());
Assert.assertEquals(LIMIT_MONETARY_BASE, repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(0, provider2.getNewFederationBtcUTXOs().size());
Assert.assertEquals(0, provider2.getOldFederationBtcUTXOs().size());
Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(3, provider2.getReleaseTransactionSet().getEntries().size());
List<BtcTransaction> releaseTxs = provider2.getReleaseTransactionSet().getEntries().stream().map(e -> e.getTransaction()).sorted(Comparator.comparing(BtcTransaction::getOutputSum)).collect(Collectors.toList());
// First release tx should correspond to the 5 BTC lock tx
BtcTransaction releaseTx = releaseTxs.get(0);
Assert.assertEquals(1, releaseTx.getOutputs().size());
Assert.assertThat(Coin.COIN.multiply(5).subtract(releaseTx.getOutput(0).getValue()), is(lessThanOrEqualTo(Coin.MILLICOIN)));
Assert.assertEquals(srcKey1.toAddress(parameters), releaseTx.getOutput(0).getAddressFromP2PKHScript(parameters));
Assert.assertEquals(1, releaseTx.getInputs().size());
Assert.assertEquals(tx1.getHash(), releaseTx.getInput(0).getOutpoint().getHash());
Assert.assertEquals(0, releaseTx.getInput(0).getOutpoint().getIndex());
// Second release tx should correspond to the 7 (3+4) BTC lock tx
releaseTx = releaseTxs.get(1);
Assert.assertEquals(1, releaseTx.getOutputs().size());
Assert.assertThat(Coin.COIN.multiply(7).subtract(releaseTx.getOutput(0).getValue()), is(lessThanOrEqualTo(Coin.MILLICOIN)));
Assert.assertEquals(srcKey3.toAddress(parameters), releaseTx.getOutput(0).getAddressFromP2PKHScript(parameters));
Assert.assertEquals(2, releaseTx.getInputs().size());
List<TransactionOutPoint> releaseOutpoints = releaseTx.getInputs().stream().map(i -> i.getOutpoint()).sorted(Comparator.comparing(TransactionOutPoint::getIndex)).collect(Collectors.toList());
Assert.assertEquals(tx3.getHash(), releaseOutpoints.get(0).getHash());
Assert.assertEquals(tx3.getHash(), releaseOutpoints.get(1).getHash());
Assert.assertEquals(0, releaseOutpoints.get(0).getIndex());
Assert.assertEquals(1, releaseOutpoints.get(1).getIndex());
// Third release tx should correspond to the 10 BTC lock tx
releaseTx = releaseTxs.get(2);
Assert.assertEquals(1, releaseTx.getOutputs().size());
Assert.assertThat(Coin.COIN.multiply(10).subtract(releaseTx.getOutput(0).getValue()), is(lessThanOrEqualTo(Coin.MILLICOIN)));
Assert.assertEquals(srcKey2.toAddress(parameters), releaseTx.getOutput(0).getAddressFromP2PKHScript(parameters));
Assert.assertEquals(1, releaseTx.getInputs().size());
Assert.assertEquals(tx2.getHash(), releaseTx.getInput(0).getOutpoint().getHash());
Assert.assertEquals(0, releaseTx.getInput(0).getOutpoint().getIndex());
Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertEquals(3, provider2.getBtcTxHashesAlreadyProcessed().size());
}
Aggregations