use of co.rsk.config.BridgeRegTestConstants in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransactionReleaseTx.
@Test
public void registerBtcTransactionReleaseTx() throws BlockStoreException, AddressFormatException, IOException {
// Federation is the genesis federation ATM
Federation federation = bridgeConstants.getGenesisFederation();
Repository repository = new RepositoryImpl(config);
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
Repository track = repository.startTracking();
Block executionBlock = Mockito.mock(Block.class);
Mockito.when(executionBlock.getNumber()).thenReturn(10L);
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
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);
Address address2 = federation.getAddress();
tx.addOutput(Coin.COIN, address2);
// Create previous tx
BtcTransaction prevTx = new BtcTransaction(btcParams);
TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, federation.getAddress());
prevTx.addOutput(prevOut);
// Create tx input
tx.addInput(prevOut);
// Create tx input base script sig
Script scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
// Create sighash
Script redeemScript = ScriptBuilder.createRedeemScript(federation.getNumberOfSignaturesRequired(), federation.getPublicKeys());
Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
// Sign by federator 0
BtcECKey.ECDSASignature sig0 = bridgeConstants.getFederatorPrivateKeys().get(0).sign(sighash);
TransactionSignature txSig0 = new TransactionSignature(sig0, BtcTransaction.SigHash.ALL, false);
int sigIndex0 = scriptSig.getSigInsertionIndex(sighash, federation.getPublicKeys().get(0));
scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig0.encodeToBitcoin(), sigIndex0, 1, 1);
// Sign by federator 1
BtcECKey.ECDSASignature sig1 = bridgeConstants.getFederatorPrivateKeys().get(1).sign(sighash);
TransactionSignature txSig1 = new TransactionSignature(sig1, BtcTransaction.SigHash.ALL, false);
int sigIndex1 = scriptSig.getSigInsertionIndex(sighash, federation.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());
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] = 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);
((SimpleBlockChain) btcBlockChain).useHighBlock();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx, 1, pmt);
bridgeSupport.save();
((SimpleBlockChain) btcBlockChain).useBlock();
track.commit();
Assert.assertEquals(LIMIT_MONETARY_BASE, repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(1, provider2.getNewFederationBtcUTXOs().size());
Assert.assertEquals(Coin.COIN, provider2.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider2.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertEquals(1, provider2.getBtcTxHashesAlreadyProcessed().size());
}
use of co.rsk.config.BridgeRegTestConstants in project rskj by rsksmart.
the class BridgeUtilsTest method testIsLockForTwoFederations.
@Test
public void testIsLockForTwoFederations() throws Exception {
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
NetworkParameters parameters = bridgeConstants.getBtcParams();
Context btcContext = new Context(parameters);
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);
Address address1 = federation1.getAddress();
Address address2 = federation2.getAddress();
List<Federation> federations = Arrays.asList(federation1, federation2);
List<Address> addresses = Arrays.asList(address1, address2);
// Tx sending less than 1 btc to the first federation, not a lock tx
BtcTransaction tx = new BtcTransaction(parameters);
tx.addOutput(Coin.CENT, address1);
tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
// Tx sending less than 1 btc to the second federation, not a lock tx
tx = new BtcTransaction(parameters);
tx.addOutput(Coin.CENT, address2);
tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
// Tx sending less than 1 btc to both federations, not a lock tx
tx = new BtcTransaction(parameters);
tx.addOutput(Coin.CENT, address1);
tx.addOutput(Coin.CENT, address2);
tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to the first federation, but also spending from the first federation address, the typical release tx, not a lock tx.
BtcTransaction tx2 = new BtcTransaction(parameters);
tx2.addOutput(Coin.COIN, address1);
TransactionInput txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to the second federation, but also spending from the second federation address, the typical release tx, not a lock tx.
tx2 = new BtcTransaction(parameters);
tx2.addOutput(Coin.COIN, address2);
txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to both federations, but also spending from the first federation address, the typical release tx, not a lock tx.
tx2 = new BtcTransaction(parameters);
tx2.addOutput(Coin.COIN, address1);
tx2.addOutput(Coin.COIN, address2);
txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to both federations, but also spending from the second federation address, the typical release tx, not a lock tx.
tx2 = new BtcTransaction(parameters);
tx2.addOutput(Coin.COIN, address1);
tx2.addOutput(Coin.COIN, address2);
txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to the first federation, is a lock tx
BtcTransaction tx3 = new BtcTransaction(parameters);
tx3.addOutput(Coin.COIN, address1);
tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to the second federation, is a lock tx
tx3 = new BtcTransaction(parameters);
tx3.addOutput(Coin.COIN, address2);
tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
// Tx sending 1 btc to the both federations, is a lock tx
tx3 = new BtcTransaction(parameters);
tx3.addOutput(Coin.COIN, address1);
tx3.addOutput(Coin.COIN, address2);
tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
// Tx sending 50 btc to the first federation, is a lock tx
BtcTransaction tx4 = new BtcTransaction(parameters);
tx4.addOutput(Coin.FIFTY_COINS, address1);
tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
// Tx sending 50 btc to the second federation, is a lock tx
tx4 = new BtcTransaction(parameters);
tx4.addOutput(Coin.FIFTY_COINS, address2);
tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
// Tx sending 50 btc to the both federations, is a lock tx
tx4 = new BtcTransaction(parameters);
tx4.addOutput(Coin.FIFTY_COINS, address1);
tx4.addOutput(Coin.FIFTY_COINS, address2);
tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
}
use of co.rsk.config.BridgeRegTestConstants in project rskj by rsksmart.
the class BridgeUtilsTest method testIsMigrationTx.
@Test
public void testIsMigrationTx() {
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
NetworkParameters parameters = bridgeConstants.getBtcParams();
Context btcContext = new Context(parameters);
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")), BtcECKey.fromPrivate(Hex.decode("fb03"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation retiringFederation = new Federation(retiringFederationKeys, Instant.ofEpochMilli(1000L), 1L, parameters);
Address activeFederationAddress = activeFederation.getAddress();
BtcTransaction migrationTx = new BtcTransaction(parameters);
migrationTx.addOutput(Coin.COIN, activeFederationAddress);
TransactionInput migrationTxInput = new TransactionInput(parameters, migrationTx, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
migrationTx.addInput(migrationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, migrationTxInput, migrationTx, bridgeConstants);
assertThat(BridgeUtils.isMigrationTx(migrationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(true));
BtcTransaction toActiveFederationTx = new BtcTransaction(parameters);
toActiveFederationTx.addOutput(Coin.COIN, activeFederationAddress);
toActiveFederationTx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertThat(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(false));
Address randomAddress = Address.fromBase58(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
BtcTransaction fromRetiringFederationTx = new BtcTransaction(parameters);
fromRetiringFederationTx.addOutput(Coin.COIN, randomAddress);
TransactionInput fromRetiringFederationTxInput = new TransactionInput(parameters, fromRetiringFederationTx, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
fromRetiringFederationTx.addInput(fromRetiringFederationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, fromRetiringFederationTxInput, fromRetiringFederationTx, bridgeConstants);
assertThat(BridgeUtils.isMigrationTx(fromRetiringFederationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(false));
assertThat(BridgeUtils.isMigrationTx(migrationTx, activeFederation, null, btcContext, bridgeConstants), is(false));
}
use of co.rsk.config.BridgeRegTestConstants in project rskj by rsksmart.
the class BridgeUtilsTest method testIsLock.
@Test
public void testIsLock() throws Exception {
// Lock is for the genesis federation ATM
NetworkParameters params = RegTestParams.get();
Context btcContext = new Context(params);
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
Federation federation = bridgeConstants.getGenesisFederation();
Wallet wallet = new BridgeBtcWallet(btcContext, Arrays.asList(federation));
wallet.addWatchedAddress(federation.getAddress(), federation.getCreationTime().toEpochMilli());
Address address = federation.getAddress();
// Tx sending less than 1 btc to the federation, not a lock tx
BtcTransaction tx = new BtcTransaction(params);
tx.addOutput(Coin.CENT, address);
tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isLockTx(tx, federation, btcContext, bridgeConstants));
// Tx sending 1 btc to the federation, but also spending from the federation addres, the typical release tx, not a lock tx.
BtcTransaction tx2 = new BtcTransaction(params);
tx2.addOutput(Coin.COIN, address);
TransactionInput txIn = new TransactionInput(params, tx2, new byte[] {}, new TransactionOutPoint(params, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(bridgeConstants.getGenesisFederation(), bridgeConstants.getFederatorPrivateKeys(), txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federation, btcContext, bridgeConstants));
// Tx sending 1 btc to the federation, is a lock tx
BtcTransaction tx3 = new BtcTransaction(params);
tx3.addOutput(Coin.COIN, address);
tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx3, federation, btcContext, bridgeConstants));
// Tx sending 50 btc to the federation, is a lock tx
BtcTransaction tx4 = new BtcTransaction(params);
tx4.addOutput(Coin.FIFTY_COINS, address);
tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx4, federation, btcContext, bridgeConstants));
}
use of co.rsk.config.BridgeRegTestConstants in project rskj by rsksmart.
the class TxValidatorIntrinsicGasLimitValidatorTest method validIntrinsicGasPrice.
@Test
public void validIntrinsicGasPrice() {
Transaction tx1 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.valueOf(21000).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
tx1.sign(new ECKey().getPrivKeyBytes());
Transaction tx2 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.valueOf(30000).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), config.getBlockchainConfig().getCommonConstants().getChainId());
tx2.sign(new ECKey().getPrivKeyBytes());
Transaction tx3 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.valueOf(21072).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), config.getBlockchainConfig().getCommonConstants().getChainId());
tx3.sign(new ECKey().getPrivKeyBytes());
Transaction tx4 = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), BigInteger.ZERO.toByteArray(), PrecompiledContracts.BRIDGE_ADDR.getBytes(), BigInteger.ZERO.toByteArray(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
BridgeRegTestConstants bridgeRegTestConstants = BridgeRegTestConstants.getInstance();
tx4.sign(bridgeRegTestConstants.getFederatorPrivateKeys().get(0).getPrivKeyBytes());
TxValidatorIntrinsicGasLimitValidator tvigpv = new TxValidatorIntrinsicGasLimitValidator(config);
Assert.assertTrue(tvigpv.validate(tx1, new AccountState(), null, null, Long.MAX_VALUE, false));
Assert.assertTrue(tvigpv.validate(tx2, new AccountState(), null, null, Long.MAX_VALUE, false));
Assert.assertTrue(tvigpv.validate(tx3, new AccountState(), null, null, Long.MAX_VALUE, false));
Assert.assertTrue(tvigpv.validate(tx4, new AccountState(), null, null, Long.MAX_VALUE, false));
}
Aggregations