예제 #1
0
  @Test
  public void test1() {

    byte[] result = HashUtil.sha3("horse".getBytes());

    assertEquals(
        "c87f65ff3f271bf5dc8643484f66b200109caffe4bf98c4cb393dc35740b28c0",
        Hex.toHexString(result));

    result = HashUtil.sha3("cow".getBytes());

    assertEquals(
        "c85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4",
        Hex.toHexString(result));
  }
예제 #2
0
  // 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());
  }
예제 #3
0
  @Test /* real tx hash calc */
  public void test7() {

    String txRaw =
        "F89D80809400000000000000000000000000000000000000008609184E72A000822710B3606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C5884336069571CA07F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4A06D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC";
    byte[] txHashB = HashUtil.sha3(Hex.decode(txRaw));
    String txHash = Hex.toHexString(txHashB);
    assertEquals("4b7d9670a92bf120d5b43400543b69304a14d767cf836a7f6abff4edde092895", txHash);
  }
예제 #4
0
  @Test /* real block hash calc */
  public void test8() {

    String blockRaw =
        "F885F8818080A01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D49347940000000000000000000000000000000000000000A0BCDDD284BF396739C224DBA0411566C891C32115FEB998A3E2B4E61F3F35582AA01DCC4DE8DEC75D7AAB85B567B6CCD41AD312451B948A7413F0A142FD40D4934783800000808080C0C0";

    byte[] blockHashB = HashUtil.sha3(Hex.decode(blockRaw));
    String blockHash = Hex.toHexString(blockHashB);
    System.out.println(blockHash);
  }
예제 #5
0
파일: TrieImpl.java 프로젝트: fjl/ethereumj
 @Override
 public byte[] getRootHash() {
   if (root == null
       || (root instanceof byte[] && ((byte[]) root).length == 0)
       || (root instanceof String && "".equals(root))) {
     return EMPTY_TRIE_HASH;
   } else if (root instanceof byte[]) {
     return (byte[]) this.getRoot();
   } else {
     Value rootValue = new Value(this.getRoot());
     byte[] val = rootValue.encode();
     return HashUtil.sha3(val);
   }
 }
예제 #6
0
  @Test /* performance test */
  public void test6() {

    long firstTime = System.currentTimeMillis();
    System.out.println(firstTime);
    for (int i = 0; i < 1000; ++i) {

      byte[] horseBytes = HashUtil.sha3("horse".getBytes());
      byte[] addr = ECKey.fromPrivate(horseBytes).getAddress();
      assertEquals("13978AEE95F38490E9769C39B2773ED763D9CD5F", Hex.toHexString(addr).toUpperCase());
    }
    long secondTime = System.currentTimeMillis();
    System.out.println(secondTime);
    System.out.println(secondTime - firstTime + " millisec");
    // 1) result: ~52 address calculation every second
  }
예제 #7
0
  @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());
  }
예제 #8
0
  public void createContract(DataWord value, DataWord memStart, DataWord memSize) {

    if (invokeData.byTestingSuite()) {
      logger.info("[testing suite] - omit real create");
      return;
    }

    // [1] FETCH THE CODE FROM THE MEMORY
    ByteBuffer programCode = memoryChunk(memStart, memSize);

    byte[] senderAddress = this.getOwnerAddress().getLast20Bytes();
    if (logger.isInfoEnabled())
      logger.info(
          "creating a new contract inside contract run: [{}]", Hex.toHexString(senderAddress));

    //  actual gas subtract
    int gas = this.getGas().intValue();
    this.spendGas(gas, "internal call");

    // [2] CREATE THE CONTRACT ADDRESS
    byte[] nonce = result.getRepository().getNonce(senderAddress).toByteArray();
    byte[] newAddress = HashUtil.calcNewAddr(this.getOwnerAddress().getLast20Bytes(), nonce);
    result.getRepository().createAccount(newAddress);

    // [3] UPDATE THE NONCE
    // (THIS STAGE IS NOT REVERTED BY ANY EXCEPTION)
    result.getRepository().increaseNonce(senderAddress);

    // [4] TRANSFER THE BALANCE
    BigInteger endowment = value.value();
    BigInteger senderBalance = result.getRepository().getBalance(senderAddress);
    if (senderBalance.compareTo(endowment) < 0) {
      stackPushZero();
      return;
    }
    result.getRepository().addBalance(senderAddress, endowment.negate());
    result.getRepository().addBalance(newAddress, endowment);

    RepositoryImpl trackRepositoryImpl = result.getRepository().getTrack();
    trackRepositoryImpl.startTracking();

    // [5] COOK THE INVOKE AND EXECUTE
    ProgramInvoke programInvoke =
        ProgramInvokeFactory.createProgramInvoke(
            this,
            new DataWord(newAddress),
            DataWord.ZERO,
            new DataWord(gas),
            BigInteger.ZERO,
            null,
            trackRepositoryImpl,
            this.invokeData.getCallDeep() + 1);

    VM vm = new VM();
    Program program = new Program(programCode.array(), programInvoke);
    vm.play(program);
    ProgramResult result = program.getResult();
    this.result.addDeleteAccounts(result.getDeleteAccounts());

    if (result.getException() != null
        && result.getException() instanceof Program.OutOfGasException) {
      logger.info(
          "contract run halted by OutOfGas: new contract init ={}", Hex.toHexString(newAddress));

      trackRepositoryImpl.rollback();
      stackPushZero();
      return;
    }

    // 4. CREATE THE CONTRACT OUT OF RETURN
    byte[] code = result.getHReturn().array();
    trackRepositoryImpl.saveCode(newAddress, code);

    // IN SUCCESS PUSH THE ADDRESS INTO THE STACK
    stackPush(new DataWord(newAddress));
    trackRepositoryImpl.commit();

    // 5. REFUND THE REMAIN GAS
    long refundGas = gas - result.getGasUsed();
    if (refundGas > 0) {
      this.refundGas(refundGas, "remain gas from the internal call");
      if (logger.isInfoEnabled()) {

        logger.info(
            "The remaining gas is refunded, account: [ {} ], gas: [ {} ] ",
            Hex.toHexString(this.getOwnerAddress().getLast20Bytes()),
            refundGas);
      }
    }
  }
예제 #9
0
 public byte[] getContractAddress() {
   if (!isContractCreation()) return null;
   return HashUtil.calcNewAddr(this.getSender(), this.getNonce());
 }
예제 #10
0
 public byte[] getRawHash() {
   if (!parsed) rlpParse();
   byte[] plainMsg = this.getEncodedRaw();
   return HashUtil.sha3(plainMsg);
 }
예제 #11
0
 @Test
 public void test5() {
   byte[] horseBytes = HashUtil.sha3("horse".getBytes());
   byte[] addr = ECKey.fromPrivate(horseBytes).getAddress();
   assertEquals("13978AEE95F38490E9769C39B2773ED763D9CD5F", Hex.toHexString(addr).toUpperCase());
 }
예제 #12
0
 @Test
 public void test4() {
   byte[] cowBytes = HashUtil.sha3("cow".getBytes());
   byte[] addr = ECKey.fromPrivate(cowBytes).getAddress();
   assertEquals("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826", Hex.toHexString(addr).toUpperCase());
 }