Merge pull request #24 from nicksavers/master

Remove obsolete state code and placeholder for processing txs in block
This commit is contained in:
romanman 2014-06-07 10:09:35 +01:00
commit dcf19fba08
6 changed files with 48 additions and 410 deletions

View File

@ -88,7 +88,7 @@ public class AccountState {
this.codeHash = codeHash;
}
public BigInteger getBalance() {
public BigInteger getBalance() {
return balance;
}
@ -96,6 +96,11 @@ public BigInteger getBalance() {
if (value.signum() != 0) rlpEncoded = null;
this.balance = balance.add(value);
}
public void subFromBalance(BigInteger value) {
if (value.signum() != 0) rlpEncoded = null;
this.balance = balance.subtract(value);
}
public byte[] getEncoded() {
if(rlpEncoded == null) {

View File

@ -77,29 +77,10 @@ public class Block {
// Parse Header
RLPList header = (RLPList) block.get(0);
this.header = new BlockHeader(header);
// Parse Transactions
this.txsState = new Trie(null);
RLPList txReceipts = (RLPList) block.get(1);
for (int i = 0; i < txReceipts.size(); i++) {
RLPElement rlpTxReceipt = txReceipts.get(i);
RLPElement txData = ((RLPList)rlpTxReceipt).get(0);
Transaction tx = new Transaction(txData.getRLPData());
this.transactionsList.add(tx);
this.txsState.update(RLP.encodeInt(i), tx.getEncoded());
// this.accountState.update();
// YP 4.3.1
RLPElement cummGas = ((RLPList)rlpTxReceipt).get(1);
RLPElement pstTxState = ((RLPList)rlpTxReceipt).get(2);
TransactionReceipt txReceipt =
new TransactionReceipt(tx, cummGas.getRLPData(), pstTxState.getRLPData());
txReceiptList.add(txReceipt);
}
this.header.setTxTrieRoot(txsState.getRootHash());
this.processTxs(txReceipts);
// Parse Uncles
RLPList uncleBlocks = (RLPList) block.get(2);
@ -259,6 +240,46 @@ public class Block {
return toStringBuff.toString();
}
private void processTxs(RLPList txReceipts) {
this.txsState = new Trie(null);
for (int i = 0; i < txReceipts.size(); i++) {
RLPElement rlpTxReceipt = txReceipts.get(i);
RLPElement txData = ((RLPList)rlpTxReceipt).get(0);
Transaction tx = new Transaction(txData.getRLPData());
this.addAndProcessTransaction(i, tx);
// YP 4.3.1
RLPElement cummGas = ((RLPList)rlpTxReceipt).get(1);
RLPElement pstTxState = ((RLPList)rlpTxReceipt).get(2);
TransactionReceipt txReceipt =
new TransactionReceipt(tx, cummGas.getRLPData(), pstTxState.getRLPData());
txReceiptList.add(txReceipt);
}
this.header.setTxTrieRoot(txsState.getRootHash());
}
private void addAndProcessTransaction(int counter, Transaction tx) {
this.transactionsList.add(tx);
this.txsState.update(RLP.encodeInt(counter), tx.getEncoded());
/* Figure out type of tx
* 1. Contract creation
* - perform code
* - create state object
* - add contract body to DB,
* 2. Contract call
* - perform code
* - update state object
* 3. Account to account -
* - update state object
*/
// this.accountState.update();
}
public byte[] updateState(byte[] key, byte[] value) {
this.accountState.update(key, value);
byte[] stateRoot = this.accountState.getRootHash();

View File

@ -1,160 +0,0 @@
package org.ethereum.core;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import org.ethereum.trie.Trie;
public class GoState {
// States within the ethereum protocol are used to store anything
// within the merkle trie. States take care of caching and storing
// nested states. It's the general query interface to retrieve:
// * Contracts
// * Accounts
// The trie for this structure
private Trie trie;
// Nested states
private Map<String, GoState> states;
// Create a new state from a given trie
public GoState(Trie trie) {
this.trie = trie;
states = new HashMap<String, GoState>();
}
public void add(String key, GoState state) {
this.states.put(key, state);
}
// Resets the trie and all siblings
public void reset() {
this.trie.undo();
// Reset all nested states
for (GoState state : states.values()) {
state.reset();
}
}
// Syncs the trie and all siblings
public void sync() {
this.trie.sync();
// Sync all nested states
for (GoState state : states.values()) {
state.sync();
}
}
// Purges the current trie.
public int purge() {
return this.trie.getIterator().purge();
}
public StateObject getContract(byte[] address) {
byte[] data = this.trie.get(new String(address));
if (data == null || data.length == 0) {
return null;
}
// build contract
StateObject contract = new StateObject(address, data);
// Check if there's a cached state for this contract
GoState cachedState = this.states.get(new String(address));
if (cachedState != null) {
contract.setState( cachedState );
} else {
// If it isn't cached, cache the state
this.states.put(new String(address), contract.getState());
}
return contract;
}
public StateObject getAccount(byte[] address) {
byte[] data = this.trie.get(new String(address));
if (data == null || data.length == 0) {
return StateObject.createAccount(address, BigInteger.ZERO);
} else {
return new StateObject(address, data);
}
}
public boolean cmp(GoState other) {
return this.trie.cmp(other.getTrie());
}
public GoState copy() {
return new GoState(this.trie.copy());
}
// type ObjType byte
//
// enum (
// NullTy ObjType = iota,
// AccountTy,
// ContractTy,
// UnknownTy
// )
// Returns the object stored at key and the type stored at key
// Returns null if nothing is stored
// public (*ethutil.Value, ObjType) getStateObject(byte[] key) {
//
// // Fetch data from the trie
// String data = this.trie.get(new String(key));
// // Returns the null type, indicating nothing could be retrieved.
// // Anything using this function should check for this ret val
// if (data == "") {
// return (null, NullTy)
// }
//
// var enum ObjType
// Value val = new Value(data.getBytes());
// // Check the length of the retrieved value.
// // Len 2 = Account
// // Len 3 = Contract
// // Other = invalid for now. If other types emerge, add them here
// if (val.length() == 2) {
// typ = AccountTy
// } else if (val.length == 3) {
// typ = ContractTy
// } else {
// typ = UnknownTy
// }
//
// return (val, typ);
// }
// Updates any given state object
public void updateStateObject(StateObject stateObject) {
byte[] addr = stateObject.getAddress();
if (stateObject.getState() != null) {
this.states.put(new String(addr), stateObject.getState());
}
this.trie.update(addr, stateObject.rlpEncode());
}
public void put(byte[] key, byte[] object) {
this.trie.update(key, object);
}
/**
* Instead of calling this method, call state.getTrie().getRoot()
* @return
*/
@Deprecated()
public Object getRoot() {
return this.trie.getRoot();
}
public Trie getTrie() {
return this.trie;
}
}

View File

@ -1,12 +0,0 @@
package org.ethereum.core;
import java.util.Map;
import org.ethereum.trie.Trie;
public class State {
Trie trie;
Map<String, State> states;
}

View File

@ -1,180 +0,0 @@
package org.ethereum.core;
import java.math.BigInteger;
import org.ethereum.db.Config;
import org.ethereum.trie.Trie;
import org.ethereum.util.RLP;
import org.ethereum.util.Value;
import static java.util.Arrays.copyOfRange;
public class StateObject {
// Address of the object
private byte[] address;
// Shared attributes
private BigInteger amount;
private long nonce;
// Contract related attributes
private GoState state;
private byte[] init;
private byte[] body;
// Returns a newly created contract at root
public static StateObject createContract(byte[] address, BigInteger amount, byte[] root) {
StateObject contract = new StateObject(address, amount);
contract.setState(new GoState(new Trie(Config.STATE_DB.getDb(), new String(root))));
return contract;
}
// Returns a newly created account
public static StateObject createAccount(byte[] address, BigInteger amount) {
return new StateObject(address, amount);
}
public StateObject(byte[] address, BigInteger amount) {
this.address = address;
this.amount = amount;
}
public StateObject(byte[] address, byte[] data) {
this.address = address;
this.rlpDecode(data);
}
public void setState(GoState state) {
this.state = state;
}
public void setBody(byte[] body) {
this.body = body;
}
public void setInit(byte[] init) {
this.init = init;
}
public Value getAddress(byte[] address) {
return new Value(this.state.getTrie().get(new String(address)));
}
public void setAddress(byte[] address, Object value) {
this.state.getTrie().update(address, new Value(value).encode());
}
public GoState getState() {
return this.state;
}
public Value getMem(BigInteger num) {
byte[] nb = num.toByteArray();
return this.getAddress(nb);
}
/**
* Get the instruction
*
* @param pc
* @return byte wrapped in a Value object
*/
public Value getInstr(BigInteger pc) {
if (this.body.length-1 < pc.longValue()) {
return new Value(0);
}
return new Value( new byte[] { this.body[pc.intValue()] } );
}
public void setMem(BigInteger num, Value val) {
byte[] address = num.toByteArray();
this.state.getTrie().update(address, val.encode());
}
// Return the gas back to the origin. Used by the Virtual machine or Closures
public void returnGas(BigInteger gas, BigInteger gasPrice, GoState state) {
BigInteger remainder = gas.multiply(gasPrice);
this.addAmount(remainder);
}
public BigInteger getAmount() {
return this.amount;
}
public void addAmount(BigInteger amount) {
this.amount = this.amount.add(amount);
}
public void subAmount(BigInteger amount) {
this.amount = this.amount.subtract(amount);
}
public void convertGas(BigInteger gas, BigInteger gasPrice) throws RuntimeException {
BigInteger total = gas.multiply(gasPrice);
if (total.compareTo(this.amount) > 0) {
throw new RuntimeException("insufficient amount: " + this.amount + ", " + total);
}
this.subAmount(total);
}
// Returns the address of the contract/account
public byte[] getAddress() {
return this.address;
}
public long getNonce() {
return this.nonce;
}
// Returns the main script body
public byte[] getBody() {
return this.body;
}
// Returns the initialization script
public byte[] getInit() {
return this.init;
}
// State object encoding methods
public byte[] rlpEncode() {
Object root;
if (this.state != null) {
root = this.state.getTrie().getRoot();
} else {
root = null;
}
return RLP.encode( new Object[] {this.amount, this.nonce, root, this.body});
}
public void rlpDecode(byte[] data) {
Value decoder = new Value(data);
this.amount = decoder.get(0).asBigInt();
this.nonce = decoder.get(1).asInt();
this.state = new GoState(new Trie(Config.STATE_DB.getDb(), decoder.get(2).asObj()));
this.body = decoder.get(3).asBytes();
}
// Converts an transaction in to a state object
public static StateObject createContract(Transaction tx, GoState state) {
// Create contract if there's no recipient
if (tx.isContractCreation()) {
// FIXME
byte[] txHash = tx.getHash();
byte[] contractAddress = copyOfRange(txHash, 12, txHash.length);
BigInteger value = new BigInteger(1, tx.getValue());
StateObject contract = StateObject.createContract(contractAddress, value, "".getBytes());
state.updateStateObject(contract);
contract.setBody(tx.getData());
state.updateStateObject(contract);
return contract;
}
return null;
}
}

View File

@ -1,36 +0,0 @@
package org.ethereum.core;
import java.util.HashMap;
import java.util.Map;
public class StateObjectCache {
// The cached state and state object cache are helpers which will give you somewhat
// control over the nonce. When creating new transactions you're interested in the 'next'
// nonce rather than the current nonce. This to avoid creating invalid-nonce transactions.
Map<String, CachedStateObject> cachedObjects;
public StateObjectCache() {
this.cachedObjects = new HashMap<String, CachedStateObject>();
}
public CachedStateObject add(byte[] address, StateObject stateObject) {
CachedStateObject state = new CachedStateObject(stateObject.getNonce(), stateObject);
this.cachedObjects.put(new String(address), state);
return state;
}
public CachedStateObject get(byte[] address) {
return this.cachedObjects.get(new String(address));
}
public class CachedStateObject {
private long nonce;
private StateObject stateObject;
public CachedStateObject(long nonce, StateObject stateObject) {
this.nonce = nonce;
this.stateObject = stateObject;
}
}
}