Exemple #1
0
  private Trie parseGenesis() {
    JSONParser parser = new JSONParser();
    JSONObject genesisMap = null;
    try {
      genesisMap = (JSONObject) parser.parse(GENESIS_JSON);
    } catch (ParseException e) {
      e.printStackTrace();
      System.exit(-1);
    }

    Set<String> keys = genesisMap.keySet();

    Trie state = new SecureTrie(null);

    for (String key : keys) {

      JSONObject val = (JSONObject) genesisMap.get(key);
      String denom = (String) val.keySet().toArray()[0];
      String value = (String) val.values().toArray()[0];

      BigInteger wei =
          Denomination.valueOf(denom.toUpperCase()).value().multiply(new BigInteger(value));

      AccountState acctState = new AccountState(BigInteger.ZERO, wei);
      byte[] keyB = Hex.decode(key);
      state.update(keyB, acctState.getEncoded());
      premine.put(wrap(keyB), acctState);
    }

    return state;
  }
  // sha3_memSizeQuadraticCost32_zeroSize
  @Ignore // TODO #POC9
  @Test // contract call quadratic memory use
  public void test10() {

    int expectedGas = 313;

    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);

    // Set contract into Database
    String callerAddr = "cd1722f3947def4cf144679da39c4c32bdc35681";
    String contractAddr = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6";
    String code = "600061040020600055";

    byte[] contractAddrB = Hex.decode(contractAddr);
    byte[] callerAddrB = Hex.decode(callerAddr);
    byte[] codeB = Hex.decode(code);

    byte[] codeKey = HashUtil.sha3(codeB);
    AccountState accountState = new AccountState(SystemProperties.getDefault());
    accountState.setCodeHash(codeKey);

    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();

    repository.createAccount(callerAddrB);
    repository.addBalance(
        callerAddrB,
        new BigInteger(
            "115792089237316195423570985008687907853269984665640564039457584007913129639935"));

    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);

    // Play the program
    VM vm = new VM();
    Program program = new Program(codeB, pi);

    try {
      while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
      program.setRuntimeFailure(e);
    }

    System.out.println();
    System.out.println("============ Results ============");

    BigInteger 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());
  }
  public AccountState clone() {
    AccountState accountState = new AccountState();

    accountState.addToBalance(this.getBalance());
    accountState.setNonce(this.getNonce());
    accountState.setCodeHash(this.getCodeHash());
    accountState.setStateRoot(this.getStateRoot());
    accountState.setDirty(false);

    return accountState;
  }
  @Ignore // TODO #POC9
  @Test // contract call recursive
  public void test1() {

    /**
     * #The code will run ------------------
     *
     * <p>a = contract.storage[999] if a > 0: contract.storage[999] = a - 1
     *
     * <p># call to contract: 77045e71a7a2c50903d88e564cd72fab11e82051 send((tx.gas / 10 * 8),
     * 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0) else: stop
     */
    int expectedGas = 436;

    DataWord key1 = new DataWord(999);
    DataWord value1 = new DataWord(3);

    // Set contract into Database
    String callerAddr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826";
    String contractAddr = "77045e71a7a2c50903d88e564cd72fab11e82051";
    String code =
        "6103e75460005260006000511115630000004c576001600051036103e755600060006000600060007377045e71a7a2c50903d88e564cd72fab11e820516008600a5a0402f1630000004c00565b00";

    byte[] contractAddrB = Hex.decode(contractAddr);
    byte[] callerAddrB = Hex.decode(callerAddr);
    byte[] codeB = Hex.decode(code);

    byte[] codeKey = HashUtil.sha3(codeB);
    AccountState accountState = new AccountState(SystemProperties.getDefault());
    accountState.setCodeHash(codeKey);

    ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
    pi.setOwnerAddress(contractAddrB);
    Repository repository = pi.getRepository();

    repository.createAccount(callerAddrB);
    repository.addBalance(callerAddrB, new BigInteger("100000000000000000000"));

    repository.createAccount(contractAddrB);
    repository.saveCode(contractAddrB, codeB);
    repository.addStorageRow(contractAddrB, key1, value1);

    // Play the program
    VM vm = new VM();
    Program program = new Program(codeB, pi);

    try {
      while (!program.isStopped()) vm.step(program);
    } catch (RuntimeException e) {
      program.setRuntimeFailure(e);
    }

    System.out.println();
    System.out.println("============ Results ============");

    BigInteger 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());
  }