Search in sources :

Example 6 with DataWord

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();
}
Also used : DataWord(org.aion.util.types.DataWord) ByteBuffer(java.nio.ByteBuffer)

Example 7 with DataWord

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"));
    }
}
Also used : Arrays(java.util.Arrays) ApiTxResponse(org.aion.api.server.ApiTxResponse) ArgTxCall(org.aion.api.server.types.ArgTxCall) Seal(org.aion.zero.impl.types.BlockHeader.Seal) ListIterator(java.util.ListIterator) FltrTx(org.aion.api.server.types.FltrTx) Version(org.aion.zero.impl.Version) Tx(org.aion.api.server.types.Tx) NodeWrapper(org.aion.zero.impl.sync.NodeWrapper) LRUMap(org.apache.commons.collections4.map.LRUMap) CfgApiZmq(org.aion.zero.impl.config.CfgApiZmq) AionTxReceipt(org.aion.base.AionTxReceipt) AionImpl(org.aion.zero.impl.blockchain.AionImpl) FltrBlk(org.aion.api.server.types.FltrBlk) CfgApiNrg(org.aion.zero.impl.config.CfgApiNrg) BigDecimal(java.math.BigDecimal) JSONObject(org.json.JSONObject) AddressUtils(org.aion.util.types.AddressUtils) Map(java.util.Map) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) ZoneOffset(java.time.ZoneOffset) Block(org.aion.zero.impl.types.Block) AionAddress(org.aion.types.AionAddress) RoundingMode(java.math.RoundingMode) FltrLg(org.aion.api.server.types.FltrLg) HexConvert.hexStringToBytes(org.aion.util.types.HexConvert.hexStringToBytes) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Fltr(org.aion.api.server.types.Fltr) Keystore(org.aion.zero.impl.keystore.Keystore) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Instant(java.time.Instant) ByteUtil(org.aion.util.bytes.ByteUtil) EventCallback(org.aion.evtmgr.impl.callback.EventCallback) TxRecpt(org.aion.api.server.types.TxRecpt) HashUtil(org.aion.crypto.HashUtil) TransactionTypes(org.aion.base.TransactionTypes) Base64(java.util.Base64) List(java.util.List) StringUtils(org.aion.util.string.StringUtils) CfgNet(org.aion.zero.impl.config.CfgNet) Entry(java.util.Map.Entry) Optional(java.util.Optional) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) Hex.toHexString(org.aion.util.conversions.Hex.toHexString) SyncInfo(org.aion.api.server.types.SyncInfo) ECKey(org.aion.crypto.ECKey) CfgAion(org.aion.zero.impl.config.CfgAion) BlockContext(org.aion.zero.impl.types.BlockContext) ArgFltr(org.aion.api.server.types.ArgFltr) CfgApiRpc(org.aion.zero.impl.config.CfgApiRpc) AccountState(org.aion.base.AccountState) CacheLoader(com.github.benmanes.caffeine.cache.CacheLoader) HashMap(java.util.HashMap) CfgApi(org.aion.zero.impl.config.CfgApi) CfgNetP2p(org.aion.zero.impl.config.CfgNetP2p) Function(java.util.function.Function) NumericalValue(org.aion.api.server.types.NumericalValue) ArrayList(java.util.ArrayList) DataWord(org.aion.util.types.DataWord) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) AccountManager(org.aion.api.server.account.AccountManager) IEventMgr(org.aion.evtmgr.IEventMgr) CfgTx(org.aion.zero.impl.config.CfgTx) IAionChain(org.aion.zero.impl.blockchain.IAionChain) StakingBlock(org.aion.zero.impl.types.StakingBlock) CfgSsl(org.aion.zero.impl.config.CfgSsl) CfgSync(org.aion.zero.impl.config.CfgSync) LinkedList(java.util.LinkedList) CfgEnergyStrategy(org.aion.zero.impl.config.CfgEnergyStrategy) ApiAion(org.aion.api.server.ApiAion) IHandler(org.aion.evtmgr.IHandler) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Blk(org.aion.api.server.types.Blk) BLOCKS_QUERY_MAX(org.aion.api.server.types.FltrLg.BLOCKS_QUERY_MAX) Evt(org.aion.api.server.types.Evt) Log(org.aion.types.Log) ImportResult(org.aion.zero.impl.core.ImportResult) CompiledContr(org.aion.api.server.types.CompiledContr) AionBlockchainImpl(org.aion.zero.impl.blockchain.AionBlockchainImpl) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) EMPTY_BYTE_ARRAY(org.aion.util.bytes.ByteUtil.EMPTY_BYTE_ARRAY) CfgConsensusUnity(org.aion.zero.impl.config.CfgConsensusUnity) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) StakingBlockHeader(org.aion.zero.impl.types.StakingBlockHeader) AionTransaction(org.aion.base.AionTransaction) Collections(java.util.Collections) JSONArray(org.json.JSONArray) AionAddress(org.aion.types.AionAddress) JSONObject(org.json.JSONObject) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) DataWord(org.aion.util.types.DataWord) Hex.toHexString(org.aion.util.conversions.Hex.toHexString)

Example 8 with DataWord

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());
}
Also used : Compiler(org.aion.solidity.Compiler) AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) MiningBlock(org.aion.zero.impl.types.MiningBlock) RepositoryCache(org.aion.base.db.RepositoryCache) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 9 with DataWord

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);
}
Also used : AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 10 with DataWord

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);
}
Also used : AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) AionTxExecSummary(org.aion.base.AionTxExecSummary) RepositoryCache(org.aion.base.db.RepositoryCache) BigInteger(java.math.BigInteger) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) InternalTransaction(org.aion.types.InternalTransaction) Test(org.junit.Test)

Aggregations

DataWord (org.aion.util.types.DataWord)49 Test (org.junit.Test)37 AionAddress (org.aion.types.AionAddress)36 AionTransaction (org.aion.base.AionTransaction)29 BigInteger (java.math.BigInteger)23 RepositoryCache (org.aion.base.db.RepositoryCache)23 AionTxExecSummary (org.aion.base.AionTxExecSummary)18 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)12 BlockContext (org.aion.zero.impl.types.BlockContext)12 ECKey (org.aion.crypto.ECKey)7 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)7 Log (org.aion.types.Log)4 HashMap (java.util.HashMap)3 AionTxReceipt (org.aion.base.AionTxReceipt)3 ImportResult (org.aion.zero.impl.core.ImportResult)3 Map (java.util.Map)2 InternalTransaction (org.aion.types.InternalTransaction)2 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)2