use of org.ethereum.crypto.ECKey in project rskj by rsksmart.
the class TransactionTest method multiSuicideTest.
@Test
public void multiSuicideTest() throws IOException, InterruptedException {
/*
Original contract
pragma solidity ^0.4.3;
contract PsychoKiller {
function () payable {}
function homicide() {
suicide(msg.sender);
}
function multipleHomicide() {
PsychoKiller k = this;
k.homicide();
k.homicide();
k.homicide();
k.homicide();
}
}
*/
BigInteger nonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
Blockchain blockchain = ImportLightTest.createBlockchain(GenesisLoader.loadGenesis(config, nonce, getClass().getResourceAsStream("/genesis/genesis-light.json"), false));
ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
System.out.println("address: " + Hex.toHexString(sender.getAddress()));
String code = "6060604052341561000c57fe5b5b6102938061001c6000396000f3006060604052361561004a576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806309e587a514610053578063de990da914610065575b6100515b5b565b005b341561005b57fe5b610063610077565b005b341561006d57fe5b610075610092565b005b3373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60003090508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156100fa57fe5b60325a03f1151561010757fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561016d57fe5b60325a03f1151561017a57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156101e057fe5b60325a03f115156101ed57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561025357fe5b60325a03f1151561026057fe5b5050505b505600a165627a7a72305820084e74021c556522723b6725354378df2fb4b6732f82dd33f5daa29e2820b37c0029";
String abi = "[{\"constant\":false,\"inputs\":[],\"name\":\"homicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"multipleHomicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"payable\":true,\"type\":\"fallback\"}]";
Transaction tx = createTx(blockchain, sender, new byte[0], Hex.decode(code));
executeTransaction(blockchain, tx);
byte[] contractAddress = tx.getContractAddress().getBytes();
CallTransaction.Contract contract1 = new CallTransaction.Contract(abi);
byte[] callData = contract1.getByName("multipleHomicide").encode();
Assert.assertNull(contract1.getConstructor());
Assert.assertNotNull(contract1.parseInvocation(callData));
Assert.assertNotNull(contract1.parseInvocation(callData).toString());
try {
contract1.parseInvocation(new byte[32]);
Assert.fail();
} catch (RuntimeException ex) {
}
try {
contract1.parseInvocation(new byte[2]);
Assert.fail();
} catch (RuntimeException ex) {
}
Transaction tx1 = createTx(blockchain, sender, contractAddress, callData, 0);
ProgramResult programResult = executeTransaction(blockchain, tx1).getResult();
// suicide of a single account should be counted only once
Assert.assertEquals(24000, programResult.getFutureRefund());
}
use of org.ethereum.crypto.ECKey in project rskj by rsksmart.
the class TransactionTest method test3.
@Ignore
@Test
public /* achieve public key of the sender nonce: 01 */
void test3() throws Exception {
// cat --> 79b08ad8787060333663d19704909ee7b1903e58
// cow --> cd2a3d9f938e13cd947ec05abc7fe734df8dd826
ECKey ecKey = ECKey.fromPrivate(HashUtil.keccak256("cat".getBytes()));
byte[] senderPrivKey = HashUtil.keccak256("cow".getBytes());
byte[] nonce = { 0x01 };
byte[] gasPrice = Hex.decode("09184e72a000");
byte[] gasLimit = Hex.decode("4255");
BigInteger value = new BigInteger("1000000000000000000000000");
Transaction tx = new Transaction(nonce, gasPrice, gasLimit, ecKey.getAddress(), value.toByteArray(), null);
tx.sign(senderPrivKey);
System.out.println("v\t\t\t: " + Hex.toHexString(new byte[] { tx.getSignature().v }));
System.out.println("r\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().r)));
System.out.println("s\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().s)));
System.out.println("RLP encoded tx\t\t: " + Hex.toHexString(tx.getEncoded()));
// retrieve the signer/sender of the transaction
ECKey key = ECKey.signatureToKey(tx.getHash().getBytes(), tx.getSignature().toBase64());
System.out.println("Tx unsigned RLP\t\t: " + Hex.toHexString(tx.getEncodedRaw()));
System.out.println("Tx signed RLP\t\t: " + Hex.toHexString(tx.getEncoded()));
System.out.println("Signature public key\t: " + Hex.toHexString(key.getPubKey()));
System.out.println("Sender is\t\t: " + Hex.toHexString(key.getAddress()));
assertEquals("cd2a3d9f938e13cd947ec05abc7fe734df8dd826", Hex.toHexString(key.getAddress()));
}
use of org.ethereum.crypto.ECKey in project rskj by rsksmart.
the class ECKeyTest method fromComponentsWithRecoveryCalculation.
@Test
public void fromComponentsWithRecoveryCalculation() {
ECKey key = new ECKey();
byte[] hash = HashUtil.randomHash();
ECKey.ECDSASignature signature = key.sign(hash);
// With uncompressed public key
ECKey.ECDSASignature signatureWithCalculatedV = ECKey.ECDSASignature.fromComponentsWithRecoveryCalculation(signature.r.toByteArray(), signature.s.toByteArray(), hash, key.getPubKey(false));
Assert.assertEquals(signature.r, signatureWithCalculatedV.r);
Assert.assertEquals(signature.s, signatureWithCalculatedV.s);
Assert.assertEquals(signature.v, signatureWithCalculatedV.v);
// With compressed public key
signatureWithCalculatedV = ECKey.ECDSASignature.fromComponentsWithRecoveryCalculation(signature.r.toByteArray(), signature.s.toByteArray(), hash, key.getPubKey(true));
Assert.assertEquals(signature.r, signatureWithCalculatedV.r);
Assert.assertEquals(signature.s, signatureWithCalculatedV.s);
Assert.assertEquals(signature.v, signatureWithCalculatedV.v);
}
use of org.ethereum.crypto.ECKey in project rskj by rsksmart.
the class MainNetMinerTest method generateFallbackMinedBlock.
@Test
public void generateFallbackMinedBlock() throws InterruptedException, IOException {
// generate private keys for testing now.
ECKey privateMiningKey0 = ECKey.fromPrivate(BigInteger.TEN);
ECKey privateMiningKey1 = ECKey.fromPrivate(BigInteger.TEN.add(BigInteger.ONE));
byte[] privKey0 = privateMiningKey0.getPrivKeyBytes();
saveToFile(privKey0, new File(folder.getRoot().getCanonicalPath(), "privkey0.bin"));
byte[] privKey1 = privateMiningKey1.getPrivKeyBytes();
saveToFile(privKey1, new File(folder.getRoot().getCanonicalPath(), "privkey1.bin"));
RskSystemProperties tempConfig = new RskSystemProperties() {
BlockchainNetConfig blockchainNetConfig = config.getBlockchainConfig();
@Override
public String fallbackMiningKeysDir() {
try {
return folder.getRoot().getCanonicalPath();
} catch (Exception e) {
}
return null;
}
@Override
public BlockchainNetConfig getBlockchainConfig() {
return new BlockchainNetConfig() {
@Override
public BlockchainConfig getConfigForBlock(long blockNumber) {
return blockchainNetConfig.getConfigForBlock(blockNumber);
}
@Override
public Constants getCommonConstants() {
return new Constants() {
@Override
public byte[] getFallbackMiningPubKey0() {
return privateMiningKey0.getPubKey();
}
@Override
public byte[] getFallbackMiningPubKey1() {
return privateMiningKey1.getPubKey();
}
};
}
};
}
};
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
MinerServer minerServer = new MinerServerImpl(tempConfig, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(tempConfig).setFallbackMiningEnabled(true), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.setFallbackMining(true);
// Accelerate mining
((MinerServerImpl) minerServer).setSecsBetweenFallbackMinedBlocks(1);
minerServer.start();
// Blocks are generated auomatically
// but we can call minerServer.generateFallbackBlock() to generate it manually
// boolean result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
long start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 0) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
// mine another
// NOTE that is NOT using the next block (parity change) because of the blockchain mockito
// to mine a subsequent block, use a real blockchain, not the mockito.
minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
// result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 1) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of org.ethereum.crypto.ECKey in project rskj by rsksmart.
the class NodeChallengeManagerTest method startChallenge.
@Test
public void startChallenge() {
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();
Node node1 = new Node(key1.getNodeId(), HOST_1, PORT_1);
Node node2 = new Node(key2.getNodeId(), HOST_2, PORT_2);
Node node3 = new Node(key3.getNodeId(), HOST_3, PORT_3);
NodeDistanceTable distanceTable = new NodeDistanceTable(KademliaOptions.BINS, KademliaOptions.BUCKET_SIZE, node1);
PeerExplorer peerExplorer = new PeerExplorer(new ArrayList<>(), node1, distanceTable, new ECKey(), TIMEOUT, REFRESH);
peerExplorer.setUDPChannel(Mockito.mock(UDPChannel.class));
NodeChallengeManager manager = new NodeChallengeManager();
NodeChallenge challenge = manager.startChallenge(node2, node3, peerExplorer);
Assert.assertNotNull(challenge);
Assert.assertEquals(challenge.getChallengedNode(), node2);
Assert.assertEquals(challenge.getChallenger(), node3);
NodeChallenge anotherChallenge = manager.removeChallenge(UUID.randomUUID().toString());
Assert.assertNull(anotherChallenge);
anotherChallenge = manager.removeChallenge(challenge.getChallengeId());
Assert.assertEquals(challenge, anotherChallenge);
}
Aggregations