Search in sources :

Example 11 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeSupportTest method testInitialChainHeadWithBtcCheckpoints.

@Test
public void testInitialChainHeadWithBtcCheckpoints() throws Exception {
    config = new RskSystemProperties();
    config.setBlockchainConfig(new TestNetConfig());
    bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
    btcParams = bridgeConstants.getBtcParams();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, mock(BridgeEventLogger.class), provider, null);
    Assert.assertEquals(1229760, bridgeSupport.getBtcBlockStore().getChainHead().getHeight());
}
Also used : TestNetConfig(org.ethereum.config.net.TestNetConfig) RepositoryImpl(co.rsk.db.RepositoryImpl) RskSystemProperties(co.rsk.config.RskSystemProperties) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeSupportTest method addSignatureCreateEventLog.

@Test
public void addSignatureCreateEventLog() throws Exception {
    // Setup
    Federation federation = bridgeConstants.getGenesisFederation();
    Repository track = new RepositoryImpl(config).startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    // Build prev btc tx
    BtcTransaction prevTx = new BtcTransaction(btcParams);
    TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, federation.getAddress());
    prevTx.addOutput(prevOut);
    // Build btc tx to be signed
    BtcTransaction btcTx = new BtcTransaction(btcParams);
    btcTx.addInput(prevOut).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
    TransactionOutput output = new TransactionOutput(btcParams, btcTx, Coin.COIN, new BtcECKey().toAddress(btcParams));
    btcTx.addOutput(output);
    // Save btc tx to be signed
    final Keccak256 rskTxHash = PegTestUtils.createHash3();
    provider.getRskTxsWaitingForSignatures().put(rskTxHash, btcTx);
    provider.save();
    track.commit();
    // Setup BridgeSupport
    List<LogInfo> eventLogs = new ArrayList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(bridgeConstants, eventLogs);
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, eventLogger, contractAddress, null);
    // Create signed hash of Btc tx
    Script inputScript = btcTx.getInputs().get(0).getScriptSig();
    List<ScriptChunk> chunks = inputScript.getChunks();
    byte[] program = chunks.get(chunks.size() - 1).data;
    Script redeemScript = new Script(program);
    Sha256Hash sigHash = btcTx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    BtcECKey privateKeyToSignWith = ((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(0);
    BtcECKey.ECDSASignature sig = privateKeyToSignWith.sign(sigHash);
    List derEncodedSigs = Collections.singletonList(sig.encodeToDER());
    BtcECKey federatorPubKey = findPublicKeySignedBy(federation.getPublicKeys(), privateKeyToSignWith);
    bridgeSupport.addSignature(1, federatorPubKey, derEncodedSigs, rskTxHash.getBytes());
    Assert.assertEquals(1, eventLogs.size());
    // Assert address that made the log
    LogInfo result = eventLogs.get(0);
    Assert.assertArrayEquals(PrecompiledContracts.BRIDGE_ADDR.getBytes(), result.getAddress());
    // Assert log topics
    Assert.assertEquals(1, result.getTopics().size());
    Assert.assertEquals(Bridge.ADD_SIGNATURE_TOPIC, result.getTopics().get(0));
    // Assert log data
    Assert.assertNotNull(result.getData());
    List<RLPElement> rlpData = RLP.decode2(result.getData());
    Assert.assertEquals(1, rlpData.size());
    RLPList dataList = (RLPList) rlpData.get(0);
    Assert.assertEquals(3, dataList.size());
    Assert.assertArrayEquals(btcTx.getHashAsString().getBytes(), dataList.get(0).getRLPData());
    Assert.assertArrayEquals(federatorPubKey.getPubKeyHash(), dataList.get(1).getRLPData());
    Assert.assertArrayEquals(rskTxHash.getBytes(), dataList.get(2).getRLPData());
}
Also used : BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) Script(co.rsk.bitcoinj.script.Script) LogInfo(org.ethereum.vm.LogInfo) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) RLPList(org.ethereum.util.RLPList) RLPElement(org.ethereum.util.RLPElement) RepositoryImpl(co.rsk.db.RepositoryImpl) RLPList(org.ethereum.util.RLPList) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeTest method registerBtcTransactionWithNonParseableMerkleeProof2.

@Test
public void registerBtcTransactionWithNonParseableMerkleeProof2() throws Exception {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    bridge.init(null, null, track, null, null, null);
    NetworkParameters btcParams = RegTestParams.get();
    BtcTransaction tx = new BtcTransaction(btcParams);
    tx.addOutput(Coin.COIN, new BtcECKey().toAddress(btcParams));
    tx.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, new BtcECKey()));
    byte[] data = Bridge.REGISTER_BTC_TRANSACTION.encode(tx.bitcoinSerialize(), 1, new byte[30]);
    Assert.assertNull(bridge.execute(data));
}
Also used : RepositoryImpl(co.rsk.db.RepositoryImpl) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) Test(org.junit.Test)

Example 14 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeTest method registerBtcTransactionWithNonParseableMerkleeProof1.

@Test
public void registerBtcTransactionWithNonParseableMerkleeProof1() throws Exception {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    bridge.init(null, null, track, null, null, null);
    NetworkParameters btcParams = RegTestParams.get();
    BtcTransaction tx = new BtcTransaction(btcParams);
    tx.addOutput(Coin.COIN, new BtcECKey().toAddress(btcParams));
    tx.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, new BtcECKey()));
    byte[] data = Bridge.REGISTER_BTC_TRANSACTION.encode(tx.bitcoinSerialize(), 1, new byte[3]);
    Assert.assertNull(bridge.execute(data));
}
Also used : RepositoryImpl(co.rsk.db.RepositoryImpl) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) Test(org.junit.Test)

Example 15 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BridgeTest method callUpdateCollectionsWithTransactionsWaitingForConfirmation.

@Test
public void callUpdateCollectionsWithTransactionsWaitingForConfirmation() throws IOException {
    BtcTransaction tx1 = createTransaction();
    BtcTransaction tx2 = createTransaction();
    BtcTransaction tx3 = createTransaction();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    provider0.getReleaseTransactionSet().add(tx1, 1L);
    provider0.getReleaseTransactionSet().add(tx2, 2L);
    provider0.getReleaseTransactionSet().add(tx3, 3L);
    provider0.save();
    track.commit();
    track = repository.startTracking();
    Transaction rskTx = Transaction.create(config, PrecompiledContracts.BRIDGE_ADDR_STR, AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
    rskTx.sign(new ECKey().getPrivKeyBytes());
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    World world = new World();
    bridge.init(rskTx, world.getBlockChain().getBestBlock(), track, world.getBlockChain().getBlockStore(), null, new LinkedList<>());
    bridge.execute(Bridge.UPDATE_COLLECTIONS.encode());
    track.commit();
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Assert.assertEquals(3, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertEquals(0, provider.getRskTxsWaitingForSignatures().size());
}
Also used : SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) ECKey(org.ethereum.crypto.ECKey) World(co.rsk.test.World) Test(org.junit.Test)

Aggregations

RepositoryImpl (co.rsk.db.RepositoryImpl)62 Test (org.junit.Test)54 Repository (org.ethereum.core.Repository)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RskAddress (co.rsk.core.RskAddress)19 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)16 HashMapDB (org.ethereum.datasource.HashMapDB)15 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)13 org.ethereum.core (org.ethereum.core)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 LogInfo (org.ethereum.vm.LogInfo)12 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)11 BridgeConstants (co.rsk.config.BridgeConstants)10 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)10 DataWord (org.ethereum.vm.DataWord)9 ArrayList (java.util.ArrayList)8 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)7 ECKey (org.ethereum.crypto.ECKey)7 Keccak256 (co.rsk.crypto.Keccak256)6 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)6