Search in sources :

Example 11 with BridgeConstants

use of co.rsk.config.BridgeConstants in project rskj by rsksmart.

the class BridgeSupportTest method voteFeePerKbChange_successfulVoteWithFeeChange.

@Test
public void voteFeePerKbChange_successfulVoteWithFeeChange() {
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    Transaction tx = mock(Transaction.class);
    BridgeConstants constants = mock(BridgeConstants.class);
    AddressBasedAuthorizer authorizer = mock(AddressBasedAuthorizer.class);
    byte[] senderBytes = ByteUtil.leftPadBytes(new byte[] { 0x43 }, 20);
    when(provider.getFeePerKbElection(any())).thenReturn(new ABICallElection(authorizer));
    when(tx.getSender()).thenReturn(new RskAddress(senderBytes));
    when(constants.getFeePerKbChangeAuthorizer()).thenReturn(authorizer);
    when(authorizer.isAuthorized(tx)).thenReturn(true);
    when(authorizer.isAuthorized(tx.getSender())).thenReturn(true);
    when(authorizer.getRequiredAuthorizedKeys()).thenReturn(1);
    BridgeSupport bridgeSupport = new BridgeSupport(config, repositoryMock, null, constants, provider, null, null);
    assertThat(bridgeSupport.voteFeePerKbChange(tx, Coin.CENT), is(1));
    verify(provider).setFeePerKb(Coin.CENT);
}
Also used : SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with BridgeConstants

use of co.rsk.config.BridgeConstants 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));
}
Also used : RskAddress(co.rsk.core.RskAddress) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) SimpleBlockChain(co.rsk.peg.simples.SimpleBlockChain) BridgeConstants(co.rsk.config.BridgeConstants) Script(co.rsk.bitcoinj.script.Script) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with BridgeConstants

use of co.rsk.config.BridgeConstants in project rskj by rsksmart.

the class BridgeSupportTest method voteFeePerKbChange_nullFeeThrows.

@Test(expected = NullPointerException.class)
public void voteFeePerKbChange_nullFeeThrows() {
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    Transaction tx = mock(Transaction.class);
    BridgeConstants constants = mock(BridgeConstants.class);
    AddressBasedAuthorizer authorizer = mock(AddressBasedAuthorizer.class);
    when(provider.getFeePerKbElection(any())).thenReturn(new ABICallElection(null));
    when(tx.getSender()).thenReturn(new RskAddress(ByteUtil.leftPadBytes(new byte[] { 0x43 }, 20)));
    when(constants.getFeePerKbChangeAuthorizer()).thenReturn(authorizer);
    when(authorizer.isAuthorized(tx)).thenReturn(true);
    BridgeSupport bridgeSupport = new BridgeSupport(config, repositoryMock, null, constants, provider, null, null);
    bridgeSupport.voteFeePerKbChange(tx, null);
    verify(provider, never()).setFeePerKb(any());
}
Also used : SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with BridgeConstants

use of co.rsk.config.BridgeConstants in project rskj by rsksmart.

the class BridgeSupportTest method voteFeePerKbChange_unsuccessfulVote.

@Test
public void voteFeePerKbChange_unsuccessfulVote() {
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    Transaction tx = mock(Transaction.class);
    BridgeConstants constants = mock(BridgeConstants.class);
    AddressBasedAuthorizer authorizer = mock(AddressBasedAuthorizer.class);
    byte[] senderBytes = ByteUtil.leftPadBytes(new byte[] { 0x43 }, 20);
    when(provider.getFeePerKbElection(any())).thenReturn(new ABICallElection(authorizer));
    when(tx.getSender()).thenReturn(new RskAddress(senderBytes));
    when(constants.getFeePerKbChangeAuthorizer()).thenReturn(authorizer);
    when(authorizer.isAuthorized(tx)).thenReturn(false);
    BridgeSupport bridgeSupport = new BridgeSupport(config, repositoryMock, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, null, null);
    assertThat(bridgeSupport.voteFeePerKbChange(tx, Coin.CENT), is(-10));
    verify(provider, never()).setFeePerKb(any());
}
Also used : SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with BridgeConstants

use of co.rsk.config.BridgeConstants in project rskj by rsksmart.

the class BridgeSupportTest method voteFeePerKbChange_successfulVote.

@Test
public void voteFeePerKbChange_successfulVote() {
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    Transaction tx = mock(Transaction.class);
    BridgeConstants constants = mock(BridgeConstants.class);
    AddressBasedAuthorizer authorizer = mock(AddressBasedAuthorizer.class);
    byte[] senderBytes = ByteUtil.leftPadBytes(new byte[] { 0x43 }, 20);
    when(provider.getFeePerKbElection(any())).thenReturn(new ABICallElection(authorizer));
    when(tx.getSender()).thenReturn(new RskAddress(senderBytes));
    when(constants.getFeePerKbChangeAuthorizer()).thenReturn(authorizer);
    when(authorizer.isAuthorized(tx)).thenReturn(true);
    when(authorizer.isAuthorized(tx.getSender())).thenReturn(true);
    when(authorizer.getRequiredAuthorizedKeys()).thenReturn(2);
    BridgeSupport bridgeSupport = new BridgeSupport(config, repositoryMock, null, constants, provider, null, null);
    assertThat(bridgeSupport.voteFeePerKbChange(tx, Coin.CENT), is(1));
    verify(provider, never()).setFeePerKb(any());
}
Also used : SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

BridgeConstants (co.rsk.config.BridgeConstants)19 Test (org.junit.Test)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 RskAddress (co.rsk.core.RskAddress)10 RepositoryImpl (co.rsk.db.RepositoryImpl)10 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)10 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)9 org.ethereum.core (org.ethereum.core)8 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)7 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)5 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)3 Repository (org.ethereum.core.Repository)3 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)2 Script (co.rsk.bitcoinj.script.Script)2 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 RskSystemProperties (co.rsk.config.RskSystemProperties)2 Block (org.ethereum.core.Block)2 RLPList (org.ethereum.util.RLPList)2 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)1 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)1