Search in sources :

Example 56 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class UDPServerTest method run3NodesFullTest.

@Test
public void run3NodesFullTest() throws InterruptedException {
    ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
    ECKey key2 = ECKey.fromPrivate(Hex.decode(KEY_2)).decompress();
    ECKey key3 = ECKey.fromPrivate(Hex.decode(KEY_3)).decompress();
    List<String> node1BootNode = new ArrayList<>();
    node1BootNode.add(HOST + ":5555");
    node1BootNode.add(HOST + ":" + PORT_2);
    List<String> node2BootNode = new ArrayList<>();
    node2BootNode.add(HOST + ":" + PORT_3);
    List<String> node3BootNode = new ArrayList<>();
    Node node1 = new Node(key1.getNodeId(), HOST, PORT_1);
    Node node2 = new Node(key2.getNodeId(), HOST, PORT_2);
    Node node3 = new Node(key3.getNodeId(), HOST, PORT_3);
    NodeDistanceTable distanceTable1 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node1);
    NodeDistanceTable distanceTable2 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node2);
    NodeDistanceTable distanceTable3 = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node3);
    PeerExplorer peerExplorer1 = new PeerExplorer(node1BootNode, node1, distanceTable1, key1, TIMEOUT, REFRESH);
    PeerExplorer peerExplorer2 = new PeerExplorer(node2BootNode, node2, distanceTable2, key2, TIMEOUT, REFRESH);
    PeerExplorer peerExplorer3 = new PeerExplorer(node3BootNode, node3, distanceTable3, key3, TIMEOUT, REFRESH);
    Assert.assertEquals(0, peerExplorer1.getNodes().size());
    Assert.assertEquals(0, peerExplorer2.getNodes().size());
    Assert.assertEquals(0, peerExplorer3.getNodes().size());
    UDPServer udpServer1 = new UDPServer(HOST, PORT_1, peerExplorer1);
    UDPServer udpServer2 = new UDPServer(HOST, PORT_2, peerExplorer2);
    UDPServer udpServer3 = new UDPServer(HOST, PORT_3, peerExplorer3);
    udpServer3.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer3.cleanAndUpdate();
    udpServer2.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer2.cleanAndUpdate();
    peerExplorer3.cleanAndUpdate();
    udpServer1.start();
    TimeUnit.SECONDS.sleep(2);
    peerExplorer1.cleanAndUpdate();
    peerExplorer2.cleanAndUpdate();
    peerExplorer3.cleanAndUpdate();
    TimeUnit.SECONDS.sleep(2);
    List<Node> foundNodes1 = peerExplorer1.getNodes();
    List<Node> foundNodes2 = peerExplorer2.getNodes();
    List<Node> foundNodes3 = peerExplorer3.getNodes();
    Assert.assertEquals(2, foundNodes1.size());
    Assert.assertEquals(2, foundNodes2.size());
    Assert.assertEquals(2, foundNodes3.size());
    udpServer1.stop();
    udpServer2.stop();
    udpServer3.stop();
    TimeUnit.SECONDS.sleep(5);
    Assert.assertTrue(checkNodeIds(foundNodes1, NODE_ID_2, NODE_ID_3));
    Assert.assertTrue(checkNodeIds(foundNodes2, NODE_ID_1, NODE_ID_3));
    Assert.assertTrue(checkNodeIds(foundNodes3, NODE_ID_2, NODE_ID_1));
}
Also used : NodeDistanceTable(co.rsk.net.discovery.table.NodeDistanceTable) Node(org.ethereum.net.rlpx.Node) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 57 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class TxValidatorAccountBalanceValidatorTest method balanceIsNotValidatedIfFreeTx.

@Test
public void balanceIsNotValidatedIfFreeTx() {
    Transaction tx = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21071).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), new RskSystemProperties().getBlockchainConfig().getCommonConstants().getChainId());
    tx.sign(new ECKey().getPrivKeyBytes());
    TxValidatorAccountBalanceValidator tv = new TxValidatorAccountBalanceValidator();
    Assert.assertTrue(tv.validate(tx, new AccountState(), BigInteger.ONE, Coin.valueOf(1L), Long.MAX_VALUE, true));
}
Also used : Transaction(org.ethereum.core.Transaction) ECKey(org.ethereum.crypto.ECKey) AccountState(org.ethereum.core.AccountState) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 58 with ECKey

use of org.ethereum.crypto.ECKey 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));
}
Also used : BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) Transaction(org.ethereum.core.Transaction) ECKey(org.ethereum.crypto.ECKey) AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 59 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class ABICallElectionTest method createMockKeyForAddress.

private ECKey createMockKeyForAddress(String hex) {
    ECKey mockedKey = mock(ECKey.class);
    when(mockedKey.getAddress()).thenReturn(Hex.decode(TestUtils.padZeroesLeft(hex, 40)));
    return mockedKey;
}
Also used : ECKey(org.ethereum.crypto.ECKey)

Example 60 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class ECKeyTest method testSignedMessageToKeyThrowsSignatureException.

@Test(expected = SignatureException.class)
public void testSignedMessageToKeyThrowsSignatureException() throws SignatureException {
    byte[] messageHash = HashUtil.keccak256(exampleMessage.getBytes());
    String signature = Base64.toBase64String(new byte[128]);
    ECKey key = ECKey.signatureToKey(messageHash, signature);
    assertNull(key);
}
Also used : ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Aggregations

ECKey (org.ethereum.crypto.ECKey)76 Test (org.junit.Test)43 BigInteger (java.math.BigInteger)17 NodeDistanceTable (co.rsk.net.discovery.table.NodeDistanceTable)10 ArrayList (java.util.ArrayList)10 Node (org.ethereum.net.rlpx.Node)10 InetSocketAddress (java.net.InetSocketAddress)9 Transaction (org.ethereum.core.Transaction)8 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)7 RepositoryImpl (co.rsk.db.RepositoryImpl)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Channel (io.netty.channel.Channel)7 org.ethereum.core (org.ethereum.core)7 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)6 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 Account (org.ethereum.core.Account)4 RskAddress (co.rsk.core.RskAddress)3