-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AKI-87: add getStorage and putStorage to Blockchain
- Key cannot be null - Key must be 32 bytes - Billing is linear based on the value size
- Loading branch information
Showing
14 changed files
with
350 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
org.aion.avm.core/test/org/aion/avm/core/KeyValueStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package org.aion.avm.core; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.math.BigInteger; | ||
import org.aion.avm.core.blockchainruntime.EmptyCapabilities; | ||
import org.aion.avm.core.dappreading.JarBuilder; | ||
import org.aion.avm.core.util.ABIUtil; | ||
import org.aion.avm.core.util.CodeAndArguments; | ||
import org.aion.avm.core.util.Helpers; | ||
import org.aion.kernel.AvmTransactionResult; | ||
import org.aion.kernel.AvmTransactionResult.Code; | ||
import org.aion.kernel.Block; | ||
import org.aion.kernel.TestingKernel; | ||
import org.aion.kernel.Transaction; | ||
import org.aion.types.Address; | ||
import org.aion.vm.api.interfaces.TransactionResult; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class KeyValueStoreTest { | ||
// transaction | ||
private long energyLimit = 10_000_000L; | ||
private long energyPrice = 1L; | ||
|
||
// block | ||
private Block block = new Block(new byte[32], 1, Helpers.randomAddress(), System.currentTimeMillis(), new byte[0]); | ||
|
||
// kernel & vm | ||
private TestingKernel kernel; | ||
private AvmImpl avm; | ||
|
||
private Address deployer = TestingKernel.PREMINED_ADDRESS; | ||
private Address dappAddress; | ||
|
||
|
||
@Before | ||
public void setup() { | ||
this.kernel = new TestingKernel(block); | ||
|
||
AvmConfiguration avmConfig = new AvmConfiguration(); | ||
this.avm = CommonAvmFactory.buildAvmInstanceForConfiguration(new EmptyCapabilities(), avmConfig); | ||
|
||
byte[] jar = JarBuilder.buildJarForMainAndClassesAndUserlib(KeyValueStoreTestTarget.class); | ||
Transaction tx = Transaction.create(deployer, kernel.getNonce(deployer), BigInteger.ZERO, new CodeAndArguments(jar, null).encodeToBytes(), energyLimit, energyPrice); | ||
TransactionResult txResult = avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
dappAddress = Address.wrap(txResult.getReturnData()); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
this.avm.shutdown(); | ||
} | ||
|
||
@Test | ||
public void testGetStorageKeyNotExist() { | ||
byte[] key = Helpers.randomBytes(32); | ||
byte[] data = ABIUtil.encodeMethodArguments("testAvmGetStorage", key); | ||
Transaction tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
AvmTransactionResult txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(null, txResult.getReturnData()); | ||
} | ||
|
||
@Test | ||
public void testGetStorageWrongSizeKey() { | ||
byte[] key = Helpers.randomBytes(33); | ||
byte[] data = ABIUtil.encodeMethodArguments("testAvmGetStorage", key); | ||
Transaction tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
AvmTransactionResult txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.FAILED_EXCEPTION, txResult.getResultCode()); | ||
} | ||
|
||
@Test | ||
public void testPutStorageWrongSizeKey() { | ||
byte[] key = Helpers.randomBytes(33); | ||
byte[] value = Helpers.randomBytes(32); | ||
|
||
byte[] data = ABIUtil.encodeMethodArguments("testAvmPutStorage", key, value); | ||
Transaction tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
AvmTransactionResult txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.FAILED_EXCEPTION, txResult.getResultCode()); | ||
} | ||
|
||
@Test | ||
public void testPutGetStorageSuccess() { | ||
byte[] key = Helpers.randomBytes(32); | ||
byte[] value = Helpers.randomBytes(32); | ||
|
||
byte[] data = ABIUtil.encodeMethodArguments("testAvmPutStorage", key, value); | ||
Transaction tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
AvmTransactionResult txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(new byte[0], txResult.getReturnData()); | ||
|
||
data = ABIUtil.encodeMethodArguments("testAvmGetStorage", key); | ||
tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(value, txResult.getReturnData()); | ||
} | ||
|
||
@Test | ||
public void testStoragePutNullDelete() { | ||
byte[] key = Helpers.randomBytes(32); | ||
byte[] value = Helpers.randomBytes(32); | ||
|
||
byte[] data = ABIUtil.encodeMethodArguments("testAvmPutStorage", key, value); | ||
Transaction tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
AvmTransactionResult txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(new byte[0], txResult.getReturnData()); | ||
|
||
data = ABIUtil.encodeMethodArguments("testAvmGetStorage", key); | ||
tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(value, txResult.getReturnData()); | ||
|
||
// put null to delete | ||
data = ABIUtil.encodeMethodArguments("testAvmPutStorageNullValue", key); | ||
tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(new byte[0], txResult.getReturnData()); | ||
|
||
data = ABIUtil.encodeMethodArguments("testAvmGetStorage", key); | ||
tx = Transaction.call(deployer, dappAddress, kernel.getNonce(deployer), BigInteger.ZERO, data, energyLimit, energyPrice); | ||
txResult = (AvmTransactionResult) avm.run(this.kernel, new Transaction[] {tx})[0].get(); | ||
|
||
assertEquals(Code.SUCCESS, txResult.getResultCode()); | ||
assertArrayEquals(null, txResult.getReturnData()); | ||
|
||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
org.aion.avm.core/test/org/aion/avm/core/KeyValueStoreTestTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.aion.avm.core; | ||
|
||
import avm.Blockchain; | ||
import org.aion.avm.userlib.abi.ABIDecoder; | ||
|
||
public class KeyValueStoreTestTarget { | ||
|
||
public static byte[] testAvmGetStorage(byte[] key) { | ||
return Blockchain.getStorage(key); | ||
} | ||
|
||
public static void testAvmPutStorage(byte[] key, byte[] value) { | ||
Blockchain.putStorage(key, value); | ||
} | ||
|
||
public static byte[] main() { | ||
ABIDecoder decoder = new ABIDecoder(Blockchain.getData()); | ||
String methodName = decoder.decodeMethodName(); | ||
|
||
if (methodName == null) { | ||
return new byte[0]; | ||
} else { | ||
if (methodName.equals("testAvmGetStorage")) { | ||
byte[] key = decoder.decodeOneByteArray(); | ||
return testAvmGetStorage(key); | ||
} else if (methodName.equals("testAvmPutStorage")) { | ||
byte[] key = decoder.decodeOneByteArray(); | ||
byte[] value = decoder.decodeOneByteArray(); | ||
testAvmPutStorage(key, value); | ||
return new byte[0]; | ||
} else if (methodName.equals("testAvmPutStorageNullValue")) { | ||
byte[] key = decoder.decodeOneByteArray(); | ||
testAvmPutStorage(key, null); | ||
return new byte[0]; | ||
} | ||
else { | ||
return new byte[0]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.