use of org.aion.base.AccountState in project aion by aionnetwork.
the class AionImpl method getAccountState.
@Override
public Optional<AccountState> getAccountState(AionAddress address) {
try {
byte[] stateRoot = this.aionHub.getBlockchain().getBestBlock().getStateRoot();
AccountState account = this.aionHub.getRepository().getSnapshotTo(stateRoot).getAccountState(address);
if (account == null)
return Optional.empty();
return Optional.of(account);
} catch (Exception e) {
LOG_GEN.debug("query request failed", e);
return Optional.empty();
}
}
use of org.aion.base.AccountState in project aion by aionnetwork.
the class AionImpl method getAccountState.
// assumes a correctly formatted blockHash
public Optional<AccountState> getAccountState(AionAddress address, byte[] blockHash) {
try {
byte[] stateRoot = this.aionHub.getBlockchain().getBlockByHash(blockHash).getStateRoot();
AccountState account = this.aionHub.getRepository().getSnapshotTo(stateRoot).getAccountState(address);
if (account == null)
return Optional.empty();
return Optional.of(account);
} catch (Exception e) {
LOG_GEN.debug("query request failed", e);
return Optional.empty();
}
}
use of org.aion.base.AccountState in project aion by aionnetwork.
the class GenesisTestNetJsonTest method testNetJsonLoad.
@Test
public void testNetJsonLoad() throws Exception {
if (genesis == null)
throw new Exception("did not properly load genesis file");
Map<AionAddress, AccountState> premineAccounts = genesis.getPremine();
AionAddress[] accString = new AionAddress[] { AddressUtils.wrapAddress("0xa0483412e8c8e769df037231d336e96f7f6d843cf7224c3d8fbe0ec7cdc12ac6"), AddressUtils.wrapAddress("0xa0353561f8b6b5a8b56d535647a4ddd7278e80c2494e3314d1f0605470f56925"), AddressUtils.wrapAddress("0xa0274c1858ca50576f4d4d18b719787b76bb454c33749172758a620555bf4586"), AddressUtils.wrapAddress("0xa06691a968e8fe22dc79b6dd503d44cb96d9a523ae265b63c6752389be90185d"), AddressUtils.wrapAddress("0xa0a95b372efe55c77a75364407f0403dfefd3131519ca980b2d92b1d2d7297a7"), AddressUtils.wrapAddress("0xa07262e1d026fca027e5f4f38668303b74a2bba8bd07470fa39d0f3b03f882f1"), AddressUtils.wrapAddress("0xa08f92c80e34c95b8b2e8cb208d47e3e6048b7e0ea44d866e33e865c365b1417"), AddressUtils.wrapAddress("0xa022045f22772463c94e08d1fd32f7b251d4c9cfd1c92e039f1517b906776283"), AddressUtils.wrapAddress("0xa04c282f636feff4d6b35174fc2d8e05c23df4b1d59508712712627184dd8a93"), AddressUtils.wrapAddress("0xa0f8654c63ae53598cf42435f53b1ebd9b7df6cbceba10af235aa2393f03034c"), AddressUtils.wrapAddress("0xa0f3defb01b531c5a28680eb928e79ea18bc155f1060e1d923d660d74883518b") };
AionAddress tokenAddress = AddressUtils.wrapAddress("0xa02198c9192bb89e9b2e8536d96cbf287fde80625afcf1a5e5632e23c1260d61");
for (AionAddress a : accString) {
assertThat(premineAccounts.containsKey(a)).isTrue();
AccountState acc = premineAccounts.get(a);
assertThat(acc).isNotNull();
assertThat(acc.getBalance()).isEqualTo(new BigInteger("314159000000000000000000"));
}
assertThat(premineAccounts.containsKey(tokenAddress)).isTrue();
AccountState tokenAcc = premineAccounts.get(tokenAddress);
assertThat(tokenAcc.getBalance()).isEqualTo(new BigInteger("465934586660000000000000000"));
// assert that the loaded fields are correct
assertThat(genesis.getChainId()).isEqualTo(2);
assertThat(genesis.getDifficultyBI()).isEqualTo(BigInteger.valueOf(16));
assertThat(genesis.getCoinbase()).isEqualTo(AddressUtils.wrapAddress("0x0000000000000000000000000000000000000000000000000000000000000000"));
assertThat(genesis.getTimestamp()).isEqualTo(1497536993L);
assertThat(genesis.getParentHash()).isEqualTo(ByteUtil.hexStringToBytes("0x0000000000000000000000000000000000000000000000000000000000000000"));
assertThat(genesis.getNrgLimit()).isEqualTo(10000000L);
assertThat(genesis.getNrgConsumed()).isEqualTo(0L);
}
use of org.aion.base.AccountState in project aion by aionnetwork.
the class TransactionCreateSpecificationTests method deployAvmContractOnTopOfAddressWithStorageUsingAvmVersion1.
@Test
public void deployAvmContractOnTopOfAddressWithStorageUsingAvmVersion1() throws VmFatalException {
AvmTestConfig.clearConfigurations();
AvmTestConfig.supportOnlyAvmVersion1();
// Deploy AVM contract.
AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.HELLO_WORLD, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
// Manipulate the repository to have a non-default storage value.
RepositoryCache cache = blockchain.getRepository().startTracking();
cache.addStorageRow(contract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
cache.saveVmType(contract, InternalVmType.AVM);
cache.flushTo(cache.getParent(), true);
// Check assumptions about contract state.
AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(contract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
byte[] oldRoot = contractState.getStateRoot();
// Next, process the deploy transaction with fork040 enabled.
AionTxExecSummary result = executeTransaction(deployTxAvm, true);
assertThat(result.isFailed()).isFalse();
assertThat(result.isRejected()).isFalse();
assertThat(result.getReceipt().getError()).isEmpty();
assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployTxAvm.getEnergyLimit()));
assertThat(result.getLogs()).isEmpty();
contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(contract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getStateRoot()).isNotEqualTo(oldRoot);
assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
use of org.aion.base.AccountState in project aion by aionnetwork.
the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1.
@Test
public void deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1() throws VmFatalException {
AvmTestConfig.clearConfigurations();
AvmTestConfig.supportOnlyAvmVersion1();
// Deploy AVM contract.
AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
// Call AVM contract to deploy new internal AVM contract (version without required success).
long internalLimit = 1_000_000;
AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
// Manipulate the repository to have a non-default storage value.
RepositoryCache cache = blockchain.getRepository().startTracking();
cache.addStorageRow(internalContract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
cache.saveVmType(internalContract, InternalVmType.AVM);
cache.flushTo(cache.getParent(), true);
// Check assumptions about contract state.
AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
byte[] oldRoot = contractState.getStateRoot();
// Next, process the deploy transaction with fork040 enabled.
AionTxExecSummary result = executeTransaction(deployInternal, true);
assertThat(result.isFailed()).isFalse();
assertThat(result.isRejected()).isFalse();
assertThat(result.getReceipt().getError()).isEmpty();
assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
assertThat(result.getLogs()).isEmpty();
InternalTransaction itx = result.getInternalTransactions().get(0);
assertThat(itx.isCreate).isTrue();
assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
assertThat(itx.isRejected).isFalse();
assertThat(itx.energyLimit).isEqualTo(internalLimit);
assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getStateRoot()).isNotEqualTo(oldRoot);
assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Aggregations