Search in sources :

Example 61 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class VMComplexTest method test3.

@Ignore
// contractB call contractA with return expectation
@Test
public void test3() {
    /**
     *       #The code will run
     *       ------------------
     *
     *         contract A: 77045e71a7a2c50903d88e564cd72fab11e82051
     *         ---------------
     *
     *           a = 11
     *           b = 22
     *           c = 33
     *           d = 44
     *           e = 55
     *           f = 66
     *
     *           [asm  192 0 RETURN asm]
     *
     *         contract B: 83c5541a6c8d2dbad642f385d8d06ca9b6c731ee
     *         -----------
     *             a = msg((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0, [11, 22, 33], 3, 6)
     */
    long expectedVal_1 = 11;
    long expectedVal_2 = 22;
    long expectedVal_3 = 33;
    long expectedVal_4 = 44;
    long expectedVal_5 = 55;
    long expectedVal_6 = 66;
    // Set contract into Database
    RskAddress caller_addr = new RskAddress("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
    RskAddress contractA_addr = new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051");
    RskAddress contractB_addr = new RskAddress("83c5541a6c8d2dbad642f385d8d06ca9b6c731ee");
    byte[] codeA = Hex.decode("600b60005260166020526021604052602c6060526037608052604260a05260c06000f2");
    byte[] codeB = Hex.decode("6000601f5360e05960e05952600060c05901536060596020015980602001600b9052806040016016905280606001602190526080905260007377045e71a7a2c50903d88e564cd72fab11e820516103e8f1602060000260a00160200151600052");
    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractB_addr);
    Repository repository = pi.getRepository();
    repository.createAccount(contractA_addr);
    repository.saveCode(contractA_addr, codeA);
    repository.createAccount(contractB_addr);
    repository.saveCode(contractB_addr, codeB);
    repository.createAccount(caller_addr);
    final BigInteger value = new BigInteger("100000000000000000000");
    repository.addBalance(caller_addr, new Coin(value));
    // ****************** //
    // 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 ============");
    System.out.println("*** Used gas: " + program.getResult().getGasUsed());
    DataWord value1 = program.memoryLoad(new DataWord(32));
    DataWord value2 = program.memoryLoad(new DataWord(64));
    DataWord value3 = program.memoryLoad(new DataWord(96));
    DataWord value4 = program.memoryLoad(new DataWord(128));
    DataWord value5 = program.memoryLoad(new DataWord(160));
    DataWord value6 = program.memoryLoad(new DataWord(192));
    repository.close();
    assertEquals(expectedVal_1, value1.longValue());
    assertEquals(expectedVal_2, value2.longValue());
    assertEquals(expectedVal_3, value3.longValue());
    assertEquals(expectedVal_4, value4.longValue());
    assertEquals(expectedVal_5, value5.longValue());
    assertEquals(expectedVal_6, value6.longValue());
// TODO: check that the value pushed after exec is 1
}
Also used : Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 62 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class VMTest method testSSTORE_2.

// SSTORE OP
@Test
public void testSSTORE_2() {
    VM vm = getSubject();
    program = getProgram("602260AA55602260BB55");
    String s_expected_key = "00000000000000000000000000000000000000000000000000000000000000BB";
    String s_expected_val = "0000000000000000000000000000000000000000000000000000000000000022";
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    vm.step(program);
    Repository repository = program.getStorage();
    DataWord key = new DataWord(Hex.decode(s_expected_key));
    DataWord val = repository.getStorageValue(new RskAddress(invoke.getOwnerAddress()), key);
    assertEquals(s_expected_val, Hex.toHexString(val.getData()).toUpperCase());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) ByteUtil.oneByteToHexString(org.ethereum.util.ByteUtil.oneByteToHexString)

Example 63 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class Remasc method processMinersFees.

/**
 * Implements the actual Remasc distribution logic
 */
void processMinersFees() throws IOException, BlockStoreException {
    if (!(executionTx instanceof RemascTransaction)) {
        // 2) invocation to remasc from another contract (ie call opcode)
        throw new RemascInvalidInvocationException("Invoked Remasc outside last tx of the block");
    }
    this.addNewSiblings();
    long blockNbr = executionBlock.getNumber();
    long processingBlockNumber = blockNbr - remascConstants.getMaturity();
    if (processingBlockNumber < 1) {
        logger.debug("First block has not reached maturity yet, current block is {}", blockNbr);
        return;
    }
    Block processingBlock = blockStore.getBlockByHashAndDepth(executionBlock.getParentHash().getBytes(), remascConstants.getMaturity() - 1);
    BlockHeader processingBlockHeader = processingBlock.getHeader();
    // Adds current block fees to accumulated rewardBalance
    Coin processingBlockReward = processingBlockHeader.getPaidFees();
    Coin rewardBalance = provider.getRewardBalance();
    rewardBalance = rewardBalance.add(processingBlockReward);
    provider.setRewardBalance(rewardBalance);
    if (processingBlockNumber - remascConstants.getSyntheticSpan() < 0) {
        logger.debug("First block has not reached maturity+syntheticSpan yet, current block is {}", executionBlock.getNumber());
        return;
    }
    // Takes from rewardBalance this block's height reward.
    Coin fullBlockReward = rewardBalance.divide(BigInteger.valueOf(remascConstants.getSyntheticSpan()));
    rewardBalance = rewardBalance.subtract(fullBlockReward);
    provider.setRewardBalance(rewardBalance);
    // Pay RSK labs cut
    Coin payToRskLabs = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getRskLabsDivisor()));
    feesPayer.payMiningFees(processingBlockHeader.getHash().getBytes(), payToRskLabs, remascConstants.getRskLabsAddress(), logs);
    fullBlockReward = fullBlockReward.subtract(payToRskLabs);
    // TODO to improve
    // this type choreography is only needed because the RepositoryTrack support the
    // get snapshot to method
    Repository processingRepository = ((RepositoryTrack) repository).getOriginRepository().getSnapshotTo(processingBlockHeader.getStateRoot());
    // TODO to improve
    // and we need a RepositoryTrack to feed RemascFederationProvider
    // because it supports the update of bytes (notably, RepositoryImpl don't)
    // the update of bytes is needed, because BridgeSupport creation could alter
    // the storage when getChainHead is null (specially in production)
    processingRepository = processingRepository.startTracking();
    BridgeSupport bridgeSupport = new BridgeSupport(config, processingRepository, null, PrecompiledContracts.BRIDGE_ADDR, processingBlock);
    RemascFederationProvider federationProvider = new RemascFederationProvider(bridgeSupport);
    Coin payToFederation = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getFederationDivisor()));
    byte[] processingBlockHash = processingBlockHeader.getHash().getBytes();
    int nfederators = federationProvider.getFederationSize();
    Coin payToFederator = payToFederation.divide(BigInteger.valueOf(nfederators));
    Coin restToLastFederator = payToFederation.subtract(payToFederator.multiply(BigInteger.valueOf(nfederators)));
    Coin paidToFederation = Coin.ZERO;
    for (int k = 0; k < nfederators; k++) {
        RskAddress federatorAddress = federationProvider.getFederatorAddress(k);
        if (k == nfederators - 1 && restToLastFederator.compareTo(Coin.ZERO) > 0) {
            feesPayer.payMiningFees(processingBlockHash, payToFederator.add(restToLastFederator), federatorAddress, logs);
        } else {
            feesPayer.payMiningFees(processingBlockHash, payToFederator, federatorAddress, logs);
        }
        paidToFederation = paidToFederation.add(payToFederator);
    }
    fullBlockReward = fullBlockReward.subtract(payToFederation);
    List<Sibling> siblings = provider.getSiblings().get(processingBlockNumber);
    if (CollectionUtils.isNotEmpty(siblings)) {
        // Block has siblings, reward distribution is more complex
        boolean previousBrokenSelectionRule = provider.getBrokenSelectionRule();
        this.payWithSiblings(processingBlockHeader, fullBlockReward, siblings, previousBrokenSelectionRule);
        boolean brokenSelectionRule = SelectionRule.isBrokenSelectionRule(processingBlockHeader, siblings);
        provider.setBrokenSelectionRule(brokenSelectionRule);
    } else {
        if (provider.getBrokenSelectionRule()) {
            // broken selection rule, apply punishment, ie burn part of the reward.
            Coin punishment = fullBlockReward.divide(BigInteger.valueOf(remascConstants.getPunishmentDivisor()));
            fullBlockReward = fullBlockReward.subtract(punishment);
            provider.setBurnedBalance(provider.getBurnedBalance().add(punishment));
        }
        feesPayer.payMiningFees(processingBlockHeader.getHash().getBytes(), fullBlockReward, processingBlockHeader.getCoinbase(), logs);
        provider.setBrokenSelectionRule(Boolean.FALSE);
    }
    this.removeUsedSiblings(processingBlockHeader);
}
Also used : BridgeSupport(co.rsk.peg.BridgeSupport) Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Example 64 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class EthModule method bridgeState.

public Map<String, Object> bridgeState() throws IOException, BlockStoreException {
    Block block = blockchain.getBestBlock();
    Repository repository = blockchain.getRepository().getSnapshotTo(block.getStateRoot()).startTracking();
    BridgeSupport bridgeSupport = new BridgeSupport(config, repository, null, PrecompiledContracts.BRIDGE_ADDR, block);
    byte[] result = bridgeSupport.getStateForDebugging();
    BridgeState state = BridgeState.create(config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), result);
    return state.stateToMap();
}
Also used : Repository(org.ethereum.core.Repository) Block(org.ethereum.core.Block) BridgeState(co.rsk.peg.BridgeState) BridgeSupport(co.rsk.peg.BridgeSupport)

Example 65 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class CommonConfigTest method createRepositoryUsingNewRepository.

@Test
public void createRepositoryUsingNewRepository() {
    CommonConfig config = new CommonConfig();
    Repository repository = config.repository(this.config);
    Assert.assertNotNull(repository);
    Assert.assertTrue(repository instanceof RepositoryImpl);
}
Also used : Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) CommonConfig(org.ethereum.config.CommonConfig) Test(org.junit.Test)

Aggregations

Repository (org.ethereum.core.Repository)136 Test (org.junit.Test)109 RskAddress (co.rsk.core.RskAddress)59 DataWord (org.ethereum.vm.DataWord)43 BigInteger (java.math.BigInteger)31 RepositoryImpl (co.rsk.db.RepositoryImpl)25 Coin (co.rsk.core.Coin)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 HashMapDB (org.ethereum.datasource.HashMapDB)12 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Transaction (org.ethereum.core.Transaction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)10 ArrayList (java.util.ArrayList)10 Program (org.ethereum.vm.program.Program)10 Block (org.ethereum.core.Block)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 RskSystemProperties (co.rsk.config.RskSystemProperties)8 IOException (java.io.IOException)8 List (java.util.List)8