use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class TotalCurrencyContractTest method constructUpdateInput.
/**
* Constructs the input for an update request on the TotalCurrencyContract using AMT as the
* value to update by and signs it using ownerKey.
*
* @param chainID The chain to update.
* @param signum 0 for addition, 1 for subtraction.
* @return the input byte array.
*/
private byte[] constructUpdateInput(byte chainID, byte signum) {
ByteBuffer buffer = ByteBuffer.allocate(18);
buffer.put(chainID).put(signum).put(new DataWord(AMT.toByteArray()).getData());
byte[] payload = buffer.array();
buffer = ByteBuffer.allocate(18 + 96);
return buffer.put(payload).put(ownerKey.sign(payload).toBytes()).array();
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getStorageAt.
public RpcMsg eth_getStorageAt(Object _params) {
String _address;
String _index;
Object _bnOrId;
if (_params instanceof JSONArray) {
_address = ((JSONArray) _params).get(0) + "";
_index = ((JSONArray) _params).get(1) + "";
_bnOrId = ((JSONArray) _params).opt(2);
} else if (_params instanceof JSONObject) {
_address = ((JSONObject) _params).get("address") + "";
_index = ((JSONObject) _params).get("index") + "";
_bnOrId = ((JSONObject) _params).opt("block");
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
String bnOrId = formalizeBlockNumberOrId(_bnOrId);
Long bn = parseBnOrId(bnOrId);
if (bn == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid block number");
} else {
DataWord key;
try {
key = new DataWord(ByteUtil.hexStringToBytes(_index));
} catch (Exception e) {
// invalid key
LOG.debug("eth_getStorageAt: invalid storageIndex. Must be <= 16 bytes.");
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid storageIndex. Must be <= 16 bytes.");
}
AionAddress address = AddressUtils.wrapAddress(_address);
Optional<ByteArrayWrapper> storageValue = (bn == BEST_PENDING_BLOCK ? pendingState.getStorageValue(address, key.toWrapper()) : ac.getStorageValue(address, key.toWrapper()));
return storageValue.map(byteArrayWrapper -> new RpcMsg(StringUtils.toJsonHex(byteArrayWrapper.toBytes()))).orElseGet(() -> new RpcMsg(null, RpcError.EXECUTION_ERROR, "Storage value not found"));
}
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class OldTxExecutorTest method testCallTransaction.
@Test
public void testCallTransaction() throws Exception {
Compiler.Result r = Compiler.getInstance().compile(ContractUtils.readContract("Ticker.sol"), Options.ABI, Options.BIN);
CompilationResult cr = CompilationResult.parse(r.output);
// deployer
String deployer = cr.contracts.get("Ticker").bin;
// contract
String contract = deployer.substring(deployer.indexOf("60506040", 1));
byte[] txNonce = BigInteger.ZERO.toByteArray();
AionAddress to = AddressUtils.wrapAddress("2222222222222222222222222222222222222222222222222222222222222222");
byte[] value = BigInteger.ZERO.toByteArray();
byte[] data = Hex.decode("c0004213");
long nrg = new DataWord(100000L).longValue();
long nrgPrice = DataWord.ONE.longValue();
AionTransaction tx = AionTransaction.create(deployerKey, txNonce, to, value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
MiningBlock block = createDummyBlock();
AionRepositoryImpl repo = blockchain.getRepository();
RepositoryCache cache = repo.startTracking();
cache.addBalance(tx.getSenderAddress(), BigInteger.valueOf(100_000).multiply(BigInteger.valueOf(tx.getEnergyPrice())));
cache.createAccount(to);
cache.saveCode(to, Hex.decode(contract));
cache.saveVmType(to, InternalVmType.FVM);
cache.flushTo(repo, true);
AionTxReceipt receipt = executeTransaction(repo, block, tx).getReceipt();
System.out.println(receipt);
assertArrayEquals(Hex.decode("00000000000000000000000000000000"), receipt.getTransactionOutput());
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class OldTxExecutorTest method createDummyBlock.
private static MiningBlock createDummyBlock() {
byte[] parentHash = new byte[32];
byte[] coinbase = RandomUtils.nextBytes(AionAddress.LENGTH);
byte[] logsBloom = new byte[256];
byte[] difficulty = new DataWord(0x1000000L).getData();
long number = 1;
long timestamp = System.currentTimeMillis() / 1000;
byte[] extraData = new byte[0];
byte[] nonce = new byte[32];
byte[] receiptsRoot = new byte[32];
byte[] transactionsRoot = new byte[32];
byte[] stateRoot = new byte[32];
List<AionTransaction> transactionsList = Collections.emptyList();
byte[] solutions = new byte[1408];
// TODO: set a dummy limit of 5000000 for now
return new MiningBlock(parentHash, new AionAddress(coinbase), logsBloom, difficulty, number, timestamp, extraData, nonce, receiptsRoot, transactionsRoot, stateRoot, transactionsList, solutions, 0, 5000000);
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class OpcodeIntegTest method testCallcodeActors.
@Test
public void testCallcodeActors() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
// Deployer calls contract D which performs CALLCODE to call contract E. From the
// perspective
// of the internal transaction, however, it looks like D calls D.
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger nonce = BigInteger.TWO;
byte[] input = // use CALLCODE on E.
ByteUtil.merge(Hex.decode("5cce9fc2"), E.toByteArray());
// pass in 'n' also.
input = ByteUtil.merge(input, new DataWord(0).getData());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
// We expect that the internal transaction is sent from D to D.
List<InternalTransaction> internalTxs = summary.getInternalTransactions();
assertEquals(1, internalTxs.size());
assertEquals(D, internalTxs.get(0).sender);
assertEquals(D, internalTxs.get(0).destination);
}
Aggregations