From 196be7cf447f2c2bffa0d72d812d858a0b3d91f9 Mon Sep 17 00:00:00 2001 From: Faiz Khan Date: Mon, 2 Mar 2015 16:23:21 -0600 Subject: [PATCH] More of CJ's tests --- .../java/test/ethereum/vm/VMComplexTest.java | 188 ++++++++++++++++-- 1 file changed, 174 insertions(+), 14 deletions(-) diff --git a/ethereumj-core/src/test/java/test/ethereum/vm/VMComplexTest.java b/ethereumj-core/src/test/java/test/ethereum/vm/VMComplexTest.java index f45221f1..0eddda79 100644 --- a/ethereumj-core/src/test/java/test/ethereum/vm/VMComplexTest.java +++ b/ethereumj-core/src/test/java/test/ethereum/vm/VMComplexTest.java @@ -429,20 +429,6 @@ public class VMComplexTest { @Test // contract call quadratic memory use public void test7() { - /** - * #The code will run - * ------------------ - - a = contract.storage[999] - if a > 0: - contract.storage[999] = a - 1 - - # call to contract: 77045e71a7a2c50903d88e564cd72fab11e82051 - send((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0) - else: - stop - */ - int expectedGas = 357; DataWord key1 = new DataWord(999); @@ -496,4 +482,178 @@ public class VMComplexTest { repository.close(); assertEquals(expectedGas, program.getResult().getGasUsed()); } + + //sha3_memSizeQuadraticCost31 + @Test // contract call quadratic memory use + 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"; + + byte[] contractAddrB = Hex.decode(contractAddr); + byte[] callerAddrB = Hex.decode(callerAddr); + byte[] codeB = Hex.decode(code); + + byte[] codeKey = HashUtil.sha3(codeB); + AccountState accountState = new AccountState(); + 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()); + } + + //sha3_memSizeQuadraticCost32 + @Test // contract call quadratic memory use + 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"; + + byte[] contractAddrB = Hex.decode(contractAddr); + byte[] callerAddrB = Hex.decode(callerAddr); + byte[] codeB = Hex.decode(code); + + byte[] codeKey = HashUtil.sha3(codeB); + AccountState accountState = new AccountState(); + 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()); + } + + //sha3_memSizeQuadraticCost32_zeroSize + @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(); + 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()); + } }