Search in sources :

Example 71 with RskAddress

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

the class GenesisLoader method loadGenesis.

public static Genesis loadGenesis(RskSystemProperties config, BigInteger initialNonce, InputStream genesisJsonIS, boolean isRsk) {
    try {
        String json = new String(ByteStreams.toByteArray(genesisJsonIS));
        ObjectMapper mapper = new ObjectMapper();
        JavaType type = mapper.getTypeFactory().constructType(GenesisJson.class);
        GenesisJson genesisJson = new ObjectMapper().readValue(json, type);
        Genesis genesis = new GenesisMapper().mapFromJson(genesisJson, isRsk);
        Map<RskAddress, InitialAddressState> premine = generatePreMine(config, initialNonce, genesisJson.getAlloc());
        genesis.setPremine(premine);
        byte[] rootHash = generateRootHash(premine);
        genesis.setStateRoot(rootHash);
        genesis.flushRLP();
        return genesis;
    } catch (Exception e) {
        System.err.println("Genesis block configuration is corrupted or not found ./resources/genesis/...");
        logger.error("Genesis block configuration is corrupted or not found ./resources/genesis/...", e);
        System.exit(-1);
        return null;
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) RskAddress(co.rsk.core.RskAddress) Genesis(org.ethereum.core.Genesis) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 72 with RskAddress

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

the class BlockGenerator method generatePreMine.

private Map<RskAddress, InitialAddressState> generatePreMine(Map<byte[], BigInteger> alloc) {
    Map<RskAddress, InitialAddressState> premine = new HashMap<>();
    for (byte[] key : alloc.keySet()) {
        AccountState acctState = new AccountState(BigInteger.valueOf(0), new Coin(alloc.get(key)));
        premine.put(new RskAddress(key), new InitialAddressState(acctState, null));
    }
    return premine;
}
Also used : Coin(co.rsk.core.Coin) HashMap(java.util.HashMap) RskAddress(co.rsk.core.RskAddress) InitialAddressState(org.ethereum.core.genesis.InitialAddressState)

Example 73 with RskAddress

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

the class Web3Impl method eth_getBalance.

@Override
public String eth_getBalance(String address, String block) throws Exception {
    /* HEX String  - an integer block number
        *  String "earliest"  for the earliest/genesis block
        *  String "latest"  - for the latest mined block
        *  String "pending"  - for the pending state/transactions
        */
    Repository repository = getRepoByJsonBlockId(block);
    if (repository == null) {
        throw new NullPointerException();
    }
    RskAddress addr = new RskAddress(address);
    BigInteger balance = repository.getBalance(addr).asBigInteger();
    return toJsonHex(balance);
}
Also used : RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 74 with RskAddress

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

the class VM method computeCallGas.

private long computeCallGas(DataWord codeAddress, DataWord value, DataWord inDataOffs, DataWord inDataSize, DataWord outDataOffs, DataWord outDataSize) {
    long callGas = GasCost.CALL;
    // check to see if account does not exist and is not a precompiled contract
    if (op == OpCode.CALL && !program.getStorage().isExist(new RskAddress(codeAddress))) {
        callGas += GasCost.NEW_ACCT_CALL;
    }
    if (op != OpCode.DELEGATECALL && !value.isZero()) {
        callGas += GasCost.VT_CALL;
    }
    long inSizeLong = Program.limitToMaxLong(inDataSize);
    long outSizeLong = Program.limitToMaxLong(outDataSize);
    // in offset+size
    long in = memNeeded(inDataOffs, inSizeLong);
    // out offset+size
    long out = memNeeded(outDataOffs, outSizeLong);
    long newMemSize = Long.max(in, out);
    callGas += calcMemGas(oldMemSize, newMemSize, 0);
    return callGas;
}
Also used : RskAddress(co.rsk.core.RskAddress)

Example 75 with RskAddress

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

the class VM method dumpLine.

/*
     * Dumping the VM state at the current operation in various styles
     *  - standard  Not Yet Implemented
     *  - standard+ (owner address, program counter, operation, gas left)
     *  - pretty (stack, memory, storage, level, contract,
     *              vmCounter, internalSteps, operation
                    gasBefore, gasCost, memWords)
     */
private void dumpLine(OpCode op, long gasBefore, long gasCost, long memWords, Program program) {
    if ("standard+".equals(vmConfig.dumpStyle())) {
        switch(op) {
            case STOP:
            case RETURN:
            case SUICIDE:
                ContractDetails details = program.getStorage().getContractDetails(new RskAddress(program.getOwnerAddress()));
                List<DataWord> storageKeys = new ArrayList<>(details.getStorage().keySet());
                Collections.sort(storageKeys);
                storageKeys.forEach(key -> dumpLogger.trace("{} {}", Hex.toHexString(key.getNoLeadZeroesData()), Hex.toHexString(details.getStorage().get(key).getNoLeadZeroesData())));
                break;
            default:
                break;
        }
        String addressString = Hex.toHexString(program.getOwnerAddress().getLast20Bytes());
        String pcString = Hex.toHexString(new DataWord(program.getPC()).getNoLeadZeroesData());
        String opString = Hex.toHexString(new byte[] { op.val() });
        String gasString = Long.toHexString(program.getRemainingGas());
        dumpLogger.trace("{} {} {} {}", addressString, pcString, opString, gasString);
    } else if ("pretty".equals(vmConfig.dumpStyle())) {
        dumpLogger.trace("-------------------------------------------------------------------------");
        dumpLogger.trace("    STACK");
        program.getStack().forEach(item -> dumpLogger.trace("{}", item));
        dumpLogger.trace("    MEMORY");
        String memoryString = program.memoryToString();
        if (!"".equals(memoryString)) {
            dumpLogger.trace("{}", memoryString);
        }
        dumpLogger.trace("    STORAGE");
        ContractDetails details = program.getStorage().getContractDetails(new RskAddress(program.getOwnerAddress()));
        List<DataWord> storageKeys = new ArrayList<>(details.getStorage().keySet());
        Collections.sort(storageKeys);
        storageKeys.forEach(key -> dumpLogger.trace("{}: {}", key.shortHex(), details.getStorage().get(key).shortHex()));
        int level = program.getCallDeep();
        String contract = Hex.toHexString(program.getOwnerAddress().getLast20Bytes());
        String internalSteps = String.format("%4s", Integer.toHexString(program.getPC())).replace(' ', '0').toUpperCase();
        dumpLogger.trace("{} | {} | #{} | {} : {} | {} | -{} | {}x32", level, contract, vmCounter, internalSteps, op, gasBefore, gasCost, memWords);
    }
}
Also used : Logger(org.slf4j.Logger) MsgType(org.ethereum.vm.MessageCall.MsgType) HashUtil(org.ethereum.crypto.HashUtil) Hex(org.spongycastle.util.encoders.Hex) LoggerFactory(org.slf4j.LoggerFactory) EMPTY_BYTE_ARRAY(org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY) RskAddress(co.rsk.core.RskAddress) ArrayList(java.util.ArrayList) Program(org.ethereum.vm.program.Program) List(java.util.List) VmConfig(co.rsk.config.VmConfig) ContractDetails(org.ethereum.db.ContractDetails) BigInteger(java.math.BigInteger) Stack(org.ethereum.vm.program.Stack) Collections(java.util.Collections) RskAddress(co.rsk.core.RskAddress) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ContractDetails(org.ethereum.db.ContractDetails)

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