Change my mind on using constants instead of enums for Gas

This commit is contained in:
nicksavers 2014-06-07 16:19:32 +02:00
parent 76175bca90
commit dcd69f26ea
2 changed files with 23 additions and 34 deletions

View File

@ -6,28 +6,18 @@ package org.ethereum.vm;
* computation engine; its price is set by the Transaction and miners are free to
* ignore Transactions whose Gas price is too low.
*/
public enum Gas {
public class Gas {
G_STEP(1),
G_STOP(0),
G_SUICIDE(0),
G_SLOAD(20),
G_SHA3(20),
G_SSTORE(100),
G_BALANCE(20),
G_CREATE(100),
G_CALL(20),
G_MEMORY(1),
G_TXDATA(5),
G_TRANSACTION(500);
private int cost;
private Gas(int value) {
this.cost = value;
}
public int cost() {
return cost;
}
public static int STEP = 1;
public static int STOP = 0;
public static int SUICIDE = 0;
public static int SLOAD = 20;
public static int SHA3 = 20;
public static int SSTORE = 100;
public static int BALANCE = 20;
public static int CREATE = 100;
public static int CALL = 20;
public static int MEMORY = 1;
public static int TXDATA = 5;
public static int TRANSACTION = 500;
}

View File

@ -8,7 +8,6 @@ import java.math.BigInteger;
import java.nio.ByteBuffer;
import static org.ethereum.vm.OpCode.PUSH1;
import static org.ethereum.vm.Gas.*;
/**
* www.ethereumJ.com
@ -35,23 +34,23 @@ public class VM {
switch (OpCode.code(op)) {
case SHA3:
program.spendGas(G_SHA3.cost());
program.spendGas(Gas.SHA3);
break;
case SLOAD:
program.spendGas(G_SLOAD.cost());
program.spendGas(Gas.SLOAD);
break;
case SSTORE:
// todo: calc gas in the execution
// todo: according to the size
break;
case BALANCE:
program.spendGas(G_BALANCE.cost());
program.spendGas(Gas.BALANCE);
break;
case CREATE:
program.spendGas(G_CREATE.cost());
program.spendGas(Gas.CREATE);
break;
case CALL:
program.spendGas(G_CALL.cost());
program.spendGas(Gas.CALL);
break;
case MSTORE8:
case MSTORE:
@ -59,7 +58,7 @@ public class VM {
// todo: according to the size
break;
default:
program.spendGas( G_STEP.cost() );
program.spendGas(Gas.STEP);
break;
}
@ -473,11 +472,11 @@ public class VM {
DataWord oldValue = program.storageLoad(addr);
program.storageSave(addr, value);
if (oldValue == null && !value.isZero()){
program.spendGas(G_SSTORE.cost() * 2);
program.spendGas(Gas.SSTORE * 2);
} else if (oldValue != null && value.isZero()){
program.spendGas(G_SSTORE.cost() * 0);
program.spendGas(Gas.SSTORE * 0);
} else
program.spendGas(G_SSTORE.cost());
program.spendGas(Gas.SSTORE);
program.step();
}
@ -555,7 +554,7 @@ public class VM {
// memory gas calc
int newMemSize = program.getMemSize();
program.spendGas(G_MEMORY.cost() * (newMemSize - oldMemSize) /32);
program.spendGas(Gas.MEMORY * (newMemSize - oldMemSize) /32);
}
program.fullTrace();
} catch (RuntimeException e) {