Merge small changes
This commit is contained in:
parent
11cbb3c1b6
commit
a3f2206d51
|
@ -53,7 +53,7 @@ public class AccountState {
|
|||
this.rlpEncoded = rlpData;
|
||||
|
||||
RLPList items = (RLPList) RLP.decode2(rlpEncoded).get(0);
|
||||
this.nonce = new BigInteger(1, ((items.get(0).getRLPData()) == null ? new byte[]{} : items.get(0).getRLPData()));
|
||||
this.nonce = new BigInteger(1, ((items.get(0).getRLPData()) == null ? new byte[0] : items.get(0).getRLPData()));
|
||||
this.balance = new BigInteger(1, items.get(1).getRLPData());
|
||||
this.stateRoot = items.get(2).getRLPData();
|
||||
this.codeHash = items.get(3).getRLPData();
|
||||
|
|
|
@ -89,6 +89,8 @@ public class Block {
|
|||
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);
|
||||
|
@ -184,6 +186,14 @@ public class Block {
|
|||
return this.header.getNonce();
|
||||
}
|
||||
|
||||
public Trie getAccountState() {
|
||||
return this.accountState;
|
||||
}
|
||||
|
||||
public Trie getTxsState() {
|
||||
return this.txsState;
|
||||
}
|
||||
|
||||
public List<Transaction> getTransactionsList() {
|
||||
if (!parsed) parseRLP();
|
||||
if (transactionsList == null) {
|
||||
|
|
|
@ -104,9 +104,7 @@ public class Trie {
|
|||
* @return value
|
||||
*/
|
||||
public byte[] get(String key) {
|
||||
byte[] k = binToNibbles(key.getBytes());
|
||||
Value c = new Value( this.get(this.root, k) );
|
||||
return c.asBytes();
|
||||
return this.get(key.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,7 +35,8 @@ public class Program {
|
|||
|
||||
public Program(byte[] ops, ProgramInvoke invokeData) {
|
||||
|
||||
if (ops == null) throw new RuntimeException("program can not run with ops: null");
|
||||
if (ops == null)
|
||||
throw new RuntimeException("program can not run with ops: null");
|
||||
|
||||
this.invokeData = invokeData;
|
||||
this.ops = ops;
|
||||
|
@ -45,7 +46,6 @@ public class Program {
|
|||
return ops[pc];
|
||||
}
|
||||
|
||||
|
||||
public void stackPush(byte[] data) {
|
||||
DataWord stackWord = new DataWord(data);
|
||||
stack.push(stackWord);
|
||||
|
@ -86,19 +86,22 @@ public class Program {
|
|||
|
||||
public void step() {
|
||||
++pc;
|
||||
if (pc >= ops.length) stop();
|
||||
if (pc >= ops.length)
|
||||
stop();
|
||||
}
|
||||
|
||||
public byte[] sweep(int n) {
|
||||
|
||||
if (pc + n > ops.length) {
|
||||
stop();
|
||||
throw new RuntimeException("pc overflow sweep n: " + n + " pc: " + pc);
|
||||
throw new RuntimeException("pc overflow sweep n: " + n + " pc: "
|
||||
+ pc);
|
||||
}
|
||||
|
||||
byte[] data = Arrays.copyOfRange(ops, pc, pc + n);
|
||||
pc += n;
|
||||
if (pc >= ops.length) stop();
|
||||
if (pc >= ops.length)
|
||||
stop();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
@ -115,7 +118,8 @@ public class Program {
|
|||
public int getMemSize() {
|
||||
|
||||
int memSize = 0;
|
||||
if (memory != null) memSize = memory.limit();
|
||||
if (memory != null)
|
||||
memSize = memory.limit();
|
||||
return memSize;
|
||||
}
|
||||
|
||||
|
@ -149,18 +153,19 @@ public class Program {
|
|||
|
||||
byte[] chunk = new byte[size];
|
||||
|
||||
if (memory.limit() < offset + size) size = memory.limit() - offset;
|
||||
if (memory.limit() < offset + size)
|
||||
size = memory.limit() - offset;
|
||||
|
||||
System.arraycopy(memory.array(), offset, chunk, 0, size);
|
||||
|
||||
return ByteBuffer.wrap(chunk);
|
||||
}
|
||||
|
||||
|
||||
private void allocateMemory(int address, byte[] value) {
|
||||
|
||||
int memSize = 0;
|
||||
if (memory != null) memSize = memory.limit();
|
||||
if (memory != null)
|
||||
memSize = memory.limit();
|
||||
|
||||
// check if you need to allocate
|
||||
if (memSize < (address + value.length)) {
|
||||
|
@ -174,20 +179,20 @@ public class Program {
|
|||
}
|
||||
|
||||
// complete to 32
|
||||
sizeToAllocate = (sizeToAllocate % 32)==0 ? sizeToAllocate :
|
||||
sizeToAllocate + (32 - sizeToAllocate % 32);
|
||||
sizeToAllocate = (sizeToAllocate % 32) == 0 ? sizeToAllocate
|
||||
: sizeToAllocate + (32 - sizeToAllocate % 32);
|
||||
|
||||
sizeToAllocate = (sizeToAllocate == 0) ? 32 : sizeToAllocate;
|
||||
|
||||
ByteBuffer tmpMem = ByteBuffer.allocate(sizeToAllocate);
|
||||
if (memory != null)
|
||||
System.arraycopy(memory.array(), 0, tmpMem.array(), 0, memory.limit());
|
||||
System.arraycopy(memory.array(), 0, tmpMem.array(), 0,
|
||||
memory.limit());
|
||||
|
||||
memory = tmpMem;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void spendGas(int gasValue) {
|
||||
// todo: check it against avail gas
|
||||
// todo: out of gas will revert the changes [YP 5, 6 ]
|
||||
|
@ -204,50 +209,57 @@ public class Program {
|
|||
storage.put(keyWord, valWord);
|
||||
}
|
||||
|
||||
|
||||
public DataWord getOwnerAddress() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getOwnerAddress();
|
||||
}
|
||||
|
||||
public DataWord getBalance() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getBalance();
|
||||
}
|
||||
|
||||
public DataWord getOriginAddress() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getOriginAddress();
|
||||
}
|
||||
|
||||
public DataWord getCallerAddress() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getCallerAddress();
|
||||
}
|
||||
|
||||
public DataWord getMinGasPrice() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getMinGasPrice();
|
||||
}
|
||||
|
||||
public DataWord getCallValue() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getCallValue();
|
||||
}
|
||||
|
||||
|
||||
public DataWord getDataSize() {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getDataSize();
|
||||
}
|
||||
|
||||
public DataWord getDataValue(DataWord index) {
|
||||
if (invokeData == null) return new DataWord( new byte[0]);
|
||||
if (invokeData == null)
|
||||
return new DataWord(new byte[0]);
|
||||
return invokeData.getDataValue(index);
|
||||
}
|
||||
|
||||
public byte[] getDataCopy(DataWord offset, DataWord length) {
|
||||
if (invokeData == null) return new byte[0];
|
||||
if (invokeData == null)
|
||||
return new byte[0];
|
||||
return invokeData.getDataCopy(offset, length);
|
||||
}
|
||||
|
||||
|
@ -255,7 +267,6 @@ public class Program {
|
|||
return storage.get(key);
|
||||
}
|
||||
|
||||
|
||||
public void fullTrace() {
|
||||
|
||||
// todo: add gas to full trace calc
|
||||
|
@ -266,16 +277,20 @@ public class Program {
|
|||
for (int i = 0; i < stack.size(); ++i) {
|
||||
|
||||
stackData.append(" ").append(stack.get(i));
|
||||
if (i < stack.size() - 1) stackData.append("\n");
|
||||
if (i < stack.size() - 1)
|
||||
stackData.append("\n");
|
||||
}
|
||||
if (stackData.length() > 0) stackData.insert(0, "\n");
|
||||
if (stackData.length() > 0)
|
||||
stackData.insert(0, "\n");
|
||||
|
||||
StringBuilder storageData = new StringBuilder();
|
||||
for (DataWord key : storage.keySet()) {
|
||||
|
||||
storageData.append(" ").append(key).append(" -> ").append(storage.get(key)).append("\n");
|
||||
storageData.append(" ").append(key).append(" -> ")
|
||||
.append(storage.get(key)).append("\n");
|
||||
}
|
||||
if (storageData.length() > 0) storageData.insert(0, "\n");
|
||||
if (storageData.length() > 0)
|
||||
storageData.insert(0, "\n");
|
||||
|
||||
StringBuilder memoryData = new StringBuilder();
|
||||
StringBuilder oneLine = new StringBuilder();
|
||||
|
@ -286,21 +301,25 @@ public class Program {
|
|||
|
||||
if ((i + 1) % 16 == 0) {
|
||||
|
||||
String tmp = String.format("[%4s]-[%4s]", Integer.toString(i - 15, 16),
|
||||
String tmp = String.format("[%4s]-[%4s]",
|
||||
Integer.toString(i - 15, 16),
|
||||
Integer.toString(i, 16)).replace(" ", "0");
|
||||
memoryData.append("").append(tmp).append(" ");
|
||||
memoryData.append(oneLine);
|
||||
if (i < memory.limit()) memoryData.append("\n");
|
||||
if (i < memory.limit())
|
||||
memoryData.append("\n");
|
||||
oneLine.setLength(0);
|
||||
}
|
||||
}
|
||||
if (memoryData.length() > 0) memoryData.insert(0, "\n");
|
||||
if (memoryData.length() > 0)
|
||||
memoryData.insert(0, "\n");
|
||||
|
||||
StringBuilder opsString = new StringBuilder();
|
||||
for (int i = 0; i < ops.length; ++i) {
|
||||
|
||||
String tmpString = Integer.toString(ops[i] & 0xFF, 16);
|
||||
tmpString = tmpString.length() == 1? "0" + tmpString : tmpString;
|
||||
tmpString = tmpString.length() == 1 ? "0" + tmpString
|
||||
: tmpString;
|
||||
|
||||
if (i != pc)
|
||||
opsString.append(tmpString);
|
||||
|
@ -308,17 +327,19 @@ public class Program {
|
|||
opsString.append(" >>").append(tmpString).append("");
|
||||
|
||||
}
|
||||
if (pc >= ops.length) opsString.append(" >>");
|
||||
if (opsString.length() > 0) opsString.insert(0, "\n ");
|
||||
if (pc >= ops.length)
|
||||
opsString.append(" >>");
|
||||
if (opsString.length() > 0)
|
||||
opsString.insert(0, "\n ");
|
||||
|
||||
logger.debug(" -- OPS -- {}", opsString);
|
||||
logger.debug(" -- STACK -- {}", stackData);
|
||||
logger.debug(" -- MEMORY -- {}", memoryData);
|
||||
logger.debug(" -- STORAGE -- {}\n", storageData);
|
||||
|
||||
|
||||
StringBuilder global = new StringBuilder("\n");
|
||||
if (stackData.length() > 0) stackData.append("\n");
|
||||
if (stackData.length() > 0)
|
||||
stackData.append("\n");
|
||||
|
||||
global.append(" -- OPS -- ").append(opsString).append("\n");
|
||||
global.append(" -- STACK -- ").append(stackData).append("\n");
|
||||
|
@ -326,14 +347,14 @@ public class Program {
|
|||
global.append(" -- STORAGE -- ").append(storageData).append("\n");
|
||||
|
||||
if (hReturn != null) {
|
||||
global.append("\n HReturn: ").append(Hex.toHexString(hReturn.array()));
|
||||
global.append("\n HReturn: ").append(
|
||||
Hex.toHexString(hReturn.array()));
|
||||
}
|
||||
|
||||
if (listener != null) {
|
||||
listener.output(global.toString());
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void addListener(ProgramListener listener) {
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package org.ethereum.core;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.ethereum.crypto.HashUtil;
|
||||
import org.ethereum.trie.MockDB;
|
||||
import org.ethereum.trie.Trie;
|
||||
|
@ -7,10 +11,6 @@ import org.ethereum.util.RLP;
|
|||
import org.junit.Test;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StateTest {
|
||||
|
||||
@Test
|
||||
|
@ -19,7 +19,6 @@ public class StateTest {
|
|||
assertEquals("23b503734ff34ddb7bd5e478f1645680ec778ab3f90007cb1c854653693e5adc", Hex.toHexString(trie.getRootHash()));
|
||||
}
|
||||
|
||||
|
||||
@Test // right way to calc tx trie hash
|
||||
public void testCalculatePostTxState() {
|
||||
|
||||
|
@ -82,10 +81,6 @@ public class StateTest {
|
|||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Test // calc state after applying first tx on genesis
|
||||
public void test2() {
|
||||
|
||||
|
@ -99,24 +94,28 @@ public class StateTest {
|
|||
Trie trie = generateGenesis();
|
||||
String expected = "69c21ff84a5af0b53b11c61110a16d6ad43dad37b3eb29ae8e88c936eb06456a";
|
||||
|
||||
byte[] data = trie.get(Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"));
|
||||
AccountState account_1 = new AccountState(data);
|
||||
// Get and update sender in world state
|
||||
byte[] cowAddress = Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
|
||||
byte[] rlpEncodedState = trie.get(cowAddress);
|
||||
AccountState account_1 = new AccountState(rlpEncodedState);
|
||||
account_1.addToBalance(new BigInteger("-6260000000001000"));
|
||||
account_1.incrementNonce();
|
||||
trie.update(Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"), account_1.getEncoded());
|
||||
trie.update(cowAddress, account_1.getEncoded());
|
||||
|
||||
String code = "61778e600054";
|
||||
byte[] codeData = Hex.decode(code);
|
||||
AccountState account_2 = new AccountState(BigInteger.ZERO, new BigInteger("1000"));
|
||||
// Add contract to world state
|
||||
byte[] codeData = Hex.decode("61778e600054");
|
||||
AccountState account_2 = new AccountState(BigInteger.ZERO, BigInteger.valueOf(1000));
|
||||
account_2.setCodeHash(HashUtil.sha3(codeData));
|
||||
trie.update(Hex.decode("77045e71a7a2c50903d88e564cd72fab11e82051"), account_2.getEncoded());
|
||||
byte[] contractAddress = Hex.decode("77045e71a7a2c50903d88e564cd72fab11e82051"); // generated based on sender + nonce
|
||||
trie.update(contractAddress, account_2.getEncoded());
|
||||
|
||||
// this is saved in the db
|
||||
// trie.update(HashUtil.sha3(codeData), codeData);
|
||||
|
||||
// Update miner in world state
|
||||
byte[] minerAddress = Hex.decode("4c5f4d519dff3c16f0d54b6866e256fbbbc1a600");
|
||||
AccountState account_3 = new AccountState(BigInteger.ZERO, new BigInteger("1506260000000000000"));
|
||||
trie.update(Hex.decode("4c5f4d519dff3c16f0d54b6866e256fbbbc1a600"), account_3.getEncoded());
|
||||
|
||||
trie.update(minerAddress, account_3.getEncoded());
|
||||
|
||||
assertEquals(expected, Hex.toHexString(trie.getRootHash()));
|
||||
|
||||
|
@ -155,10 +154,9 @@ public class StateTest {
|
|||
* cd2a3d9f938e13cd947ec05abc7fe734df8dd826: #1 1606938044258990275541962092341162602522202987522792835300376 (-6260000000001000)
|
||||
*/
|
||||
|
||||
|
||||
assertEquals("69c21ff84a5af0b53b11c61110a16d6ad43dad37b3eb29ae8e88c936eb06456a", Hex.toHexString(trie.getRootHash()));
|
||||
}
|
||||
|
||||
|
||||
private Trie generateGenesis() {
|
||||
|
||||
Trie trie = new Trie(new MockDB());
|
||||
|
@ -195,8 +193,5 @@ public class StateTest {
|
|||
trie.update(Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826"), acct6.getEncoded());
|
||||
return trie;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ public class ProgramInvokeMockImpl implements ProgramInvoke{
|
|||
this.msgData = msgDataRaw;
|
||||
}
|
||||
|
||||
ProgramInvokeMockImpl() {
|
||||
public ProgramInvokeMockImpl() {
|
||||
}
|
||||
|
||||
/* ADDRESS op */
|
||||
|
|
Loading…
Reference in New Issue