Search in sources :

Example 51 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class RemascTestRunner method start.

public void start() {
    this.blockchain = this.builder.build();
    ((BlockChainImpl) this.blockchain).setNoValidation(true);
    this.addedSiblings = new ArrayList<>();
    List<Block> mainChainBlocks = new ArrayList<>();
    this.blockchain.tryToConnect(this.genesis);
    BlockExecutor blockExecutor = new BlockExecutor(new RskSystemProperties(), blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (int i = 0; i <= this.initialHeight; i++) {
        int finalI = i;
        List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
        List<BlockHeader> blockSiblings = new ArrayList<>();
        // Going to add siblings
        BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
        if (siblingsForCurrentHeight.size() > 0) {
            cummDifficulty = blockchain.getTotalDifficulty();
        }
        for (SiblingElement sibling : siblingsForCurrentHeight) {
            RskAddress siblingCoinbase = TestUtils.randomAddress();
            Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
            Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, null, minerFee, Long.valueOf(i), this.txValue, this.txSigningKey, null);
            blockSiblings.add(siblingBlock.getHeader());
            blockchain.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
            this.addedSiblings.add(siblingBlock);
        }
        long txNonce = i;
        RskAddress coinbase = TestUtils.randomAddress();
        Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, txNonce, this.txValue, this.txSigningKey, null);
        mainChainBlocks.add(block);
        blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock());
        block.seal();
        ImportResult result = this.blockchain.tryToConnect(block);
        System.out.println(result);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestUtils(org.ethereum.TestUtils) HashUtil(org.ethereum.crypto.HashUtil) RLP(org.ethereum.util.RLP) RskAddress(co.rsk.core.RskAddress) BlockExecutor(co.rsk.core.bc.BlockExecutor) Coin(co.rsk.core.Coin) PegTestUtils(co.rsk.peg.PegTestUtils) Keccak256(co.rsk.crypto.Keccak256) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) BigInteger(java.math.BigInteger) RskSystemProperties(co.rsk.config.RskSystemProperties) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) BlockDifficulty(co.rsk.core.BlockDifficulty) RskAddress(co.rsk.core.RskAddress) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 52 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class AddressBasedAuthorizerTest method isAuthorized.

@Test
public void isAuthorized() {
    AddressBasedAuthorizer auth = new AddressBasedAuthorizer(Arrays.asList(ECKey.fromPrivate(BigInteger.valueOf(100L)), ECKey.fromPrivate(BigInteger.valueOf(101L)), ECKey.fromPrivate(BigInteger.valueOf(102L))), AddressBasedAuthorizer.MinimumRequiredCalculation.MAJORITY);
    for (long n = 100L; n <= 102L; n++) {
        Transaction mockedTx = mock(Transaction.class);
        when(mockedTx.getSender()).thenReturn(new RskAddress(ECKey.fromPrivate(BigInteger.valueOf(n)).getAddress()));
        Assert.assertTrue(auth.isAuthorized(new RskAddress(ECKey.fromPrivate(BigInteger.valueOf(n)).getAddress())));
        Assert.assertTrue(auth.isAuthorized(mockedTx));
    }
    Assert.assertFalse(auth.isAuthorized(new RskAddress(ECKey.fromPrivate(BigInteger.valueOf(50L)).getAddress())));
    Transaction mockedTx = mock(Transaction.class);
    when(mockedTx.getSender()).thenReturn(new RskAddress(ECKey.fromPrivate(BigInteger.valueOf(50L)).getAddress()));
    Assert.assertFalse(auth.isAuthorized(mockedTx));
}
Also used : Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test)

Example 53 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class VMComplexTest method test8.

// sha3_memSizeQuadraticCost31
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test8() {
    int expectedGas = 354;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "60016103c020600055";
    RskAddress contractAddrB = new RskAddress(contractAddr);
    RskAddress callerAddrB = new RskAddress(callerAddr);
    byte[] codeB = Hex.decode(code);
    byte[] codeKey = HashUtil.keccak256(codeB);
    AccountState accountState = new AccountState();
    accountState.setCodeHash(codeKey);
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();
    repository.createAccount(callerAddrB);
    final BigInteger value = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    repository.addBalance(callerAddrB, new Coin(value));
    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);
    // Play the program
    VM vm = getSubject();
    Program program = getProgram(codeB, pi);
    try {
        while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
        program.setRuntimeFailure(e);
    }
    System.out.println();
    System.out.println("============ Results ============");
    Coin balance = repository.getBalance(callerAddrB);
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    System.out.println("*** Contract Balance: " + balance);
    // todo: assert caller balance after contract exec
    repository.close();
    assertEquals(expectedGas, program.getResult().getGasUsed());
}
Also used : Program(org.ethereum.vm.program.Program) AccountState(org.ethereum.core.AccountState) Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 54 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class VMComplexTest method test9.

// sha3_memSizeQuadraticCost32
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test9() {
    int expectedGas = 356;
    DataWord key1 = new DataWord(9999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "60016103e020600055";
    RskAddress contractAddrB = new RskAddress(contractAddr);
    RskAddress callerAddrB = new RskAddress(callerAddr);
    byte[] codeB = Hex.decode(code);
    byte[] codeKey = HashUtil.keccak256(codeB);
    AccountState accountState = new AccountState();
    accountState.setCodeHash(codeKey);
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();
    repository.createAccount(callerAddrB);
    final BigInteger value = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    repository.addBalance(callerAddrB, new Coin(value));
    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);
    // Play the program
    VM vm = getSubject();
    Program program = getProgram(codeB, pi);
    try {
        while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
        program.setRuntimeFailure(e);
    }
    System.out.println();
    System.out.println("============ Results ============");
    Coin balance = repository.getBalance(callerAddrB);
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    System.out.println("*** Contract Balance: " + balance);
    // todo: assert caller balance after contract exec
    repository.close();
    assertEquals(expectedGas, program.getResult().getGasUsed());
}
Also used : Program(org.ethereum.vm.program.Program) AccountState(org.ethereum.core.AccountState) Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 55 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class VMComplexTest method test7.

// sha3_memSizeQuadraticCost33
// TODO #POC9
@Ignore
// contract call quadratic memory use
@Test
public void test7() {
    int expectedGas = 357;
    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);
    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "600161040020600055";
    RskAddress contractAddrB = new RskAddress(contractAddr);
    RskAddress callerAddrB = new RskAddress(callerAddr);
    byte[] codeB = Hex.decode(code);
    byte[] codeKey = HashUtil.keccak256(codeB);
    AccountState accountState = new AccountState();
    accountState.setCodeHash(codeKey);
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();
    repository.createAccount(callerAddrB);
    final BigInteger value = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639935");
    repository.addBalance(callerAddrB, new Coin(value));
    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);
    // Play the program
    VM vm = getSubject();
    Program program = getProgram(codeB, pi);
    try {
        while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
        program.setRuntimeFailure(e);
    }
    System.out.println();
    System.out.println("============ Results ============");
    Coin balance = repository.getBalance(callerAddrB);
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    System.out.println("*** Contract Balance: " + balance);
    // todo: assert caller balance after contract exec
    repository.close();
    assertEquals(expectedGas, program.getResult().getGasUsed());
}
Also used : Program(org.ethereum.vm.program.Program) AccountState(org.ethereum.core.AccountState) Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8