Search in sources :

Example 1 with IBlockStoreBase

use of org.aion.mcf.db.IBlockStoreBase in project aion by aionnetwork.

the class RecoveryUtils method pruneAndCorrect.

/**
 * Used by the CLI call.
 */
public static void pruneAndCorrect() {
    // ensure mining is disabled
    CfgAion cfg = CfgAion.inst();
    cfg.dbFromXML();
    cfg.getConsensus().setMining(false);
    cfg.getDb().setHeapCacheEnabled(false);
    Map<String, String> cfgLog = new HashMap<>();
    cfgLog.put("DB", "INFO");
    cfgLog.put("GEN", "INFO");
    AionLoggerFactory.init(cfgLog);
    // get the current blockchain
    AionBlockchainImpl blockchain = AionBlockchainImpl.inst();
    IBlockStoreBase store = blockchain.getBlockStore();
    IBlock bestBlock = store.getBestBlock();
    if (bestBlock == null) {
        System.out.println("Empty database. Nothing to do.");
        return;
    }
    // revert to block number and flush changes
    store.pruneAndCorrect();
    store.flush();
    // compact database after the changes were applied
    blockchain.getRepository().compact();
    blockchain.getRepository().close();
}
Also used : CfgAion(org.aion.zero.impl.config.CfgAion) IBlock(org.aion.base.type.IBlock) HashMap(java.util.HashMap) AionBlockchainImpl(org.aion.zero.impl.AionBlockchainImpl) IBlockStoreBase(org.aion.mcf.db.IBlockStoreBase)

Example 2 with IBlockStoreBase

use of org.aion.mcf.db.IBlockStoreBase in project aion by aionnetwork.

the class AionRepositoryImplTest method testRepoTrackUpdateStorageRow.

/**
 * Repo track test suite
 */
/**
 * This test confirms that updates done on the repo track are successfully translated
 * into the root repository.
 */
@Test
public void testRepoTrackUpdateStorageRow() {
    final AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    final IRepositoryCache<AccountState, DataWord, IBlockStoreBase<?, ?>> repoTrack = repository.startTracking();
    final Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    final byte[] key = HashUtil.blake128("hello".getBytes());
    final byte[] value = HashUtil.blake128("world".getBytes());
    repoTrack.addBalance(defaultAccount, BigInteger.valueOf(1));
    final byte[] originalRoot = repository.getRoot();
    repoTrack.addStorageRow(defaultAccount, new DataWord(key), new DataWord(value));
    DataWord retrievedStorageValue = repoTrack.getStorageValue(defaultAccount, new DataWord(key));
    assertThat(retrievedStorageValue).isEqualTo(new DataWord(value));
    // commit changes, then check that the root has updated
    repoTrack.flush();
    assertThat(repository.getStorageValue(defaultAccount, new DataWord(key))).isEqualTo(retrievedStorageValue);
    final byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
}
Also used : Address(org.aion.base.type.Address) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState) IBlockStoreBase(org.aion.mcf.db.IBlockStoreBase) Test(org.junit.Test)

Example 3 with IBlockStoreBase

use of org.aion.mcf.db.IBlockStoreBase in project aion by aionnetwork.

the class RecoveryUtils method revertTo.

/**
 * Used by internal world state recovery method.
 */
public static Status revertTo(AionBlockchainImpl blockchain, long nbBlock) {
    IBlockStoreBase store = blockchain.getBlockStore();
    IBlock bestBlock = store.getBestBlock();
    if (bestBlock == null) {
        System.out.println("Empty database. Nothing to do.");
        return Status.ILLEGAL_ARGUMENT;
    }
    long nbBestBlock = bestBlock.getNumber();
    System.out.println("Attempting to revert best block from " + nbBestBlock + " to " + nbBlock + " ...");
    // exit with warning if the given block is larger or negative
    if (nbBlock < 0) {
        System.out.println("Negative values <" + nbBlock + "> cannot be interpreted as block numbers. Nothing to do.");
        return Status.ILLEGAL_ARGUMENT;
    }
    if (nbBestBlock == 0) {
        System.out.println("Only genesis block in database. Nothing to do.");
        return Status.ILLEGAL_ARGUMENT;
    }
    if (nbBlock == nbBestBlock) {
        System.out.println("The block " + nbBlock + " is the current best block stored in the database. Nothing to do.");
        return Status.ILLEGAL_ARGUMENT;
    }
    if (nbBlock > nbBestBlock) {
        System.out.println("The block #" + nbBlock + " is greater than the current best block #" + nbBestBlock + " stored in the database. " + "Cannot move to that block without synchronizing with peers. Start Aion instance to sync.");
        return Status.ILLEGAL_ARGUMENT;
    }
    // revert to block number and flush changes
    store.revert(nbBlock);
    store.flush();
    nbBestBlock = store.getBestBlock().getNumber();
    // ok if we managed to get down to the expected block
    return (nbBestBlock == nbBlock) ? Status.SUCCESS : Status.FAILURE;
}
Also used : IBlock(org.aion.base.type.IBlock) IBlockStoreBase(org.aion.mcf.db.IBlockStoreBase)

Aggregations

IBlockStoreBase (org.aion.mcf.db.IBlockStoreBase)3 IBlock (org.aion.base.type.IBlock)2 HashMap (java.util.HashMap)1 Address (org.aion.base.type.Address)1 AccountState (org.aion.mcf.core.AccountState)1 DataWord (org.aion.mcf.vm.types.DataWord)1 AionBlockchainImpl (org.aion.zero.impl.AionBlockchainImpl)1 CfgAion (org.aion.zero.impl.config.CfgAion)1 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)1 Test (org.junit.Test)1