private BlockchainImpl createBlockchain(Genesis genesis) {
    IndexedBlockStore blockStore = new IndexedBlockStore();
    blockStore.init(new HashMapDB(), new HashMapDB());

    Repository repository = new RepositoryImpl(new HashMapDB(), new HashMapDB());

    ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
    EthereumListenerAdapter listener = new EthereumListenerAdapter();

    BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository);
    blockchain.setParentHeaderValidator(new DependentBlockHeaderRuleAdapter());
    blockchain.setProgramInvokeFactory(programInvokeFactory);
    programInvokeFactory.setBlockchain(blockchain);

    blockchain.byTest = true;

    PendingStateImpl pendingState = new PendingStateImpl(listener, blockchain);

    pendingState.init();

    pendingState.setBlockchain(blockchain);
    blockchain.setPendingState(pendingState);

    Repository track = repository.startTracking();
    for (ByteArrayWrapper key : genesis.getPremine().keySet()) {
      track.createAccount(key.getData());
      track.addBalance(key.getData(), genesis.getPremine().get(key).getBalance());
    }
    for (Pair<byte[], BigInteger> acc : initialBallances) {
      track.createAccount(acc.getLeft());
      track.addBalance(acc.getLeft(), acc.getRight());
    }

    track.commit();

    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);

    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());

    return blockchain;
  }
示例#2
0
  /**
   * Insert/delete operations on a Trie structure leaves the old nodes in cache, this method scans
   * the cache and removes them. The method is not thread safe, the tree should not be modified
   * during the cleaning process.
   */
  public void cleanCache() {

    CollectFullSetOfNodes collectAction = new CollectFullSetOfNodes();
    long startTime = System.currentTimeMillis();

    this.scanTree(this.getRootHash(), collectAction);

    Set<byte[]> hashSet = collectAction.getCollectedHashes();
    Map<ByteArrayWrapper, Node> nodes = this.getCache().getNodes();
    Set<ByteArrayWrapper> toRemoveSet = new HashSet<>();

    for (ByteArrayWrapper key : nodes.keySet())
      if (!hashSet.contains(key.getData())) toRemoveSet.add(key);

    for (ByteArrayWrapper key : toRemoveSet) {
      this.getCache().delete(key.getData());

      if (logger.isTraceEnabled())
        logger.trace("Garbage collected node: [{}]", Hex.toHexString(key.getData()));
    }
    logger.info("Garbage collected node list, size: [{}]", toRemoveSet.size());
    logger.info("Garbage collection time: [{}ms]", System.currentTimeMillis() - startTime);
  }