Replace GasLedger with Gas enum class

This commit is contained in:
nicksavers 2014-06-07 16:13:05 +02:00
parent 890d06c995
commit 76175bca90
5 changed files with 53 additions and 53 deletions

View File

@ -9,6 +9,8 @@ import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPList;
import org.spongycastle.util.BigIntegers;
import edu.emory.mathcs.backport.java.util.Arrays;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
@ -320,7 +322,7 @@ public class Block {
}
public byte[] calcDifficulty() {
if (this.header.getParentHash() == null)
if (Arrays.equals(this.header.getParentHash(), Genesis.PARENT_HASH))
return Genesis.DIFFICULTY;
else {
Block parent = this.getParent();

View File

@ -0,0 +1,33 @@
package org.ethereum.vm;
/**
* The fundamental network cost unit. Paid for exclusively by Ether, which is converted
* freely to and from Gas as required. Gas does not exist outside of the internal Ethereum
* 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 {
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;
}
}

View File

@ -1,41 +0,0 @@
package org.ethereum.vm;
import java.util.HashMap;
/**
* www.ethereumJ.com
* User: Roman Mandeleil
* Created on: 04/06/2014 23:58
*/
public class GasLedger {
/* YP Appendix B.
Gstep 1 Default amount of gas to pay for execution cycle.
Gstop 0 Nothing paid for the STOP operation.
Gsuicide 0 Nothing paid for the SUICIDE operation.
Gsha3 20 Paid for a SHA3 operation.
Gsload 20 Paid for a SLOAD operation.
Gsstore 100 Paid for a normal SSTORE operation (doubled or waived sometimes).
Gbalance 20 Paid for a BALANCE operation.
Gcreate 100 Paid for a CREATE operation.
Gcall 20 Paid for a CALL operation.
Gmemory 1 Paid for every additional word when expanding memory.
Gtxdata 5 Paid for every byte of data or code for a transaction.
Gtransaction 500 Paid for every transaction.
*/
public static int G_STEP = 1;
public static int G_STOP = 0;
public static int G_SUICIDE = 0;
public static int G_SLOAD = 20;
public static int G_SHA3 = 20;
public static int G_SSTORE = 100;
public static int G_BALANCE = 20;
public static int G_CREATE = 100;
public static int G_CALL = 20;
public static int G_MEMORY = 1;
public static int G_TXDATA = 5;
public static int G_TRANSACTION = 500;
}

View File

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

View File

@ -1,5 +1,7 @@
package org.ethereum.core;
import java.math.BigInteger;
import org.ethereum.net.message.BlocksMessage;
import org.ethereum.net.message.StaticMessages;
import org.ethereum.util.RLPList;
@ -89,7 +91,10 @@ public class BlockTest {
@Test
public void testCalcDifficulty() {
// Block.calcDifficulty()
Block genesis = Genesis.getInstance();
byte[] diffBytes = genesis.calcDifficulty();
BigInteger difficulty = new BigInteger(1, diffBytes);
System.out.println(difficulty.toString());
fail("Yet to be implemented.");
}