Skip to content

Commit

Permalink
Merge pull request #52 from aionnetwork/step-11
Browse files Browse the repository at this point in the history
Step 11: kernel integration (put / delete split in contract details)
  • Loading branch information
AionJayT authored Feb 6, 2019
2 parents 071e2b9 + a4e9f26 commit 4cbe2bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
35 changes: 24 additions & 11 deletions modFastVM/src/org/aion/fastvm/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import java.util.List;
import org.aion.base.type.AionAddress;
import org.aion.base.util.ByteUtil;
import org.aion.base.vm.IDataWord;
import org.aion.crypto.HashUtil;
import org.aion.mcf.vm.Constants;
import org.aion.mcf.vm.types.DataWord;
import org.aion.mcf.vm.types.KernelInterfaceForFastVM;
import org.aion.mcf.vm.types.Log;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.type.PrecompiledContract;
import org.aion.mcf.vm.types.KernelInterfaceForFastVM;
import org.aion.vm.api.interfaces.Address;
import org.aion.base.vm.IDataWord;
import org.aion.vm.api.interfaces.TransactionContext;
import org.aion.vm.api.interfaces.TransactionResult;
import org.aion.zero.types.AionInternalTx;
Expand Down Expand Up @@ -139,7 +139,21 @@ public static void putStorage(byte[] address, byte[] key, byte[] value) {
// System.err.println("PUT_STORAGE: address = " + Hex.toHexString(address) + ", key = " +
// Hex.toHexString(key) + ", value = " + Hex.toHexString(value));

kernelRepo().putStorage(AionAddress.wrap(address), key, value);
if (value == null || value.length == 0 || isZero(value)) {
kernelRepo().removeStorage(AionAddress.wrap(address), key);
} else {
kernelRepo().putStorage(AionAddress.wrap(address), key, value);
}
}

private static boolean isZero(byte[] value) {
int length = value.length;
for (int i = 0; i < length; i++) {
if (value[length - 1 - i] != 0) {
return false;
}
}
return true;
}

/**
Expand Down Expand Up @@ -192,7 +206,7 @@ public static void log(byte[] address, byte[] topics, byte[] data) {
/**
* This method only exists so that FastVM and ContractFactory can be mocked for testing. This
* method was formerly called call and now the call method simply invokes this method with new
* istances of the fast vm and contract factory.
* instances of the fast vm and contract factory.
*/
static byte[] performCall(byte[] message, FastVM vm, ContractFactory factory) {
ExecutionContext ctx = parseMessage(message);
Expand Down Expand Up @@ -255,13 +269,13 @@ private static TransactionResult doCall(

// Check that the destination address is safe to call from this VM.
if (!kernelRepo().destinationAddressIsSafeForThisVM(codeAddress)) {
return new FastVmTransactionResult(FastVmResultCode.INCOMPATIBLE_CONTRACT_CALL, ctx.getTransactionEnergy());
return new FastVmTransactionResult(
FastVmResultCode.INCOMPATIBLE_CONTRACT_CALL, ctx.getTransactionEnergy());
}

KernelInterfaceForFastVM track = kernelRepo().makeChildKernelInterface();
TransactionResult result =
new FastVmTransactionResult(
FastVmResultCode.SUCCESS, ctx.getTransactionEnergy());
new FastVmTransactionResult(FastVmResultCode.SUCCESS, ctx.getTransactionEnergy());

// add internal transaction
AionInternalTx internalTx =
Expand Down Expand Up @@ -321,8 +335,7 @@ private static TransactionResult doCall(
private static FastVmTransactionResult doCreate(ExecutionContext ctx, FastVM jit) {
KernelInterfaceForFastVM track = kernelRepo().makeChildKernelInterface();
FastVmTransactionResult result =
new FastVmTransactionResult(
FastVmResultCode.SUCCESS, ctx.getTransactionEnergy());
new FastVmTransactionResult(FastVmResultCode.SUCCESS, ctx.getTransactionEnergy());

// compute new address
byte[] nonce = track.getNonce(ctx.getSenderAddress()).toByteArray();
Expand Down Expand Up @@ -439,9 +452,9 @@ protected static ExecutionContext parseMessage(byte[] message) {
long blockNrgLimit = prev.getBlockEnergyLimit();
IDataWord blockDifficulty = new DataWord(prev.getBlockDifficulty());

//TODO: properly construct a transaction first
// TODO: properly construct a transaction first
return new ExecutionContext(
null,
null,
txHash,
AionAddress.wrap(address),
origin,
Expand Down
6 changes: 6 additions & 0 deletions modFastVM/test/org/aion/vm/DummyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public void addStorageRow(Address addr, ByteArrayWrapper key, ByteArrayWrapper v
map.put(key.toString(), value.getData());
}

@Override
public void removeStorageRow(Address addr, ByteArrayWrapper key) {
Map<String, byte[]> map = storage.computeIfAbsent(addr, k -> new HashMap<>());
map.remove(key.toString());
}

@Override
public ByteArrayWrapper getStorageValue(Address addr, ByteArrayWrapper key) {
Map<String, byte[]> map = storage.get(addr);
Expand Down

0 comments on commit 4cbe2bd

Please sign in to comment.