Java Code Examples for org.ethereum.core.Block#getNumber()

The following examples show how to use org.ethereum.core.Block#getNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexedBlockStore.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public synchronized BigInteger getTotalDifficultyForHash(byte[] hash) {
    Block block = this.getBlockByHash(hash);
    if (block == null) {
        return ZERO;
    }

    Long level = block.getNumber();
    List<BlockInfo> blockInfos = index.get(level.intValue());
    for (BlockInfo blockInfo : blockInfos) {
        if (areEqual(blockInfo.getHash(), hash)) {
            return blockInfo.totalDifficulty;
        }
    }

    return ZERO;
}
 
Example 2
Source File: IndexedBlockStore.java    From nuls with MIT License 6 votes vote down vote up
@Override
public synchronized BigInteger getTotalDifficultyForHash(byte[] hash) {
    Block block = this.getBlockByHash(hash);
    if (block == null) {
        return ZERO;
    }

    Long level = block.getNumber();
    List<BlockInfo> blockInfos = index.get(level.intValue());
    for (BlockInfo blockInfo : blockInfos) {
        if (areEqual(blockInfo.getHash(), hash)) {
            return blockInfo.totalDifficulty;
        }
    }

    return ZERO;
}
 
Example 3
Source File: BlockQueue.java    From ethereumj with MIT License 6 votes vote down vote up
/**
 * Add a list of blocks to the processing queue.
 * The list is validated by making sure the first block in the received list of blocks
 * is the next expected block number of the queue.
 * 
 * The queue is configured to contain a maximum number of blocks to avoid memory issues
 * If the list exceeds that, the rest of the received blocks in the list are discarded.
 * 
 * @param blockList - the blocks received from a peer to be added to the queue
 */
public void addBlocks(List<Block> blockList) {

	Block lastReceivedBlock = blockList.get(0);
	if (lastReceivedBlock.getNumber() != getLastBlock().getNumber() + 1){
           logger.error("Block download out of sync: lastBlock.index: [{}], receivedBlock.index: [{}]",
                   getLastBlock().getNumber(), lastReceivedBlock.getNumber());
           return;
       }

       blockReceivedQueue.addAll(blockList);
       lastBlock = blockList.get(blockList.size() - 1);

	logger.debug("Blocks waiting to be proceed:  queue.size: [{}] lastBlock.number: [{}]" ,
			blockReceivedQueue.size(),
               lastBlock.getNumber());
}
 
Example 4
Source File: BlockQueue.java    From ethereumj with MIT License 6 votes vote down vote up
/**
 * adding single block to the queue usually
 *        a result of a NEW_BLOCK message announce.
 * @param block - new block
 */
public void addBlock(Block block){

    if (block.getNumber() != getLastBlock().getNumber() + 1){
        logger.error("Block download out of sync: lastBlock.index: [{}], receivedBlock.index: [{}]",
                getLastBlock().getNumber(), block.getNumber());
        return;
    }

    blockReceivedQueue.add(block);
    lastBlock = block;

    logger.debug("Blocks waiting to be proceed:  queue.size: [{}] lastBlock.number: [{}]" ,
            blockReceivedQueue.size(),
            lastBlock.getNumber());
}
 
Example 5
Source File: EthListener.java    From tutorials with MIT License 6 votes vote down vote up
private String calcNetHashRate(Block block) {
    String response = "Net hash rate not available";
    if (block.getNumber() > thou) {
        long timeDelta = 0;
        for (int i = 0; i < thou; ++i) {
            Block parent = ethereum
              .getBlockchain()
              .getBlockByHash(block.getParentHash());
            timeDelta += Math.abs(block.getTimestamp() - parent.getTimestamp());
        }
        response = String.valueOf(block
          .getDifficultyBI()
          .divide(BIUtil.toBI(timeDelta / thou))
          .divide(new BigInteger("1000000000"))
          .doubleValue()) + " GH/s";
    }
    return response;
}
 
Example 6
Source File: IndexedBlockStore.java    From nuls-v2 with MIT License 5 votes vote down vote up
private void addInternalBlock(Block block, BigInteger totalDifficulty, boolean mainChain) {

        List<BlockInfo> blockInfos = block.getNumber() >= index.size() ? null : index.get((int) block.getNumber());
        blockInfos = blockInfos == null ? new ArrayList<BlockInfo>() : blockInfos;

        BlockInfo blockInfo = new BlockInfo();
        blockInfo.setTotalDifficulty(totalDifficulty);
        blockInfo.setHash(block.getHash());
        blockInfo.setMainChain(mainChain); // FIXME:maybe here I should force reset main chain for all uncles on that level

        putBlockInfo(blockInfos, blockInfo);
        index.set((int) block.getNumber(), blockInfos);

        blocks.put(block.getHash(), block);
    }
 
Example 7
Source File: AbstractBlockstore.java    From nuls-v2 with MIT License 5 votes vote down vote up
@Override
public byte[] getBlockHashByNumber(long blockNumber, byte[] branchBlockHash) {
    Block branchBlock = getBlockByHash(branchBlockHash);
    if (branchBlock.getNumber() < blockNumber) {
        throw new IllegalArgumentException("Requested block number > branch hash number: " + blockNumber + " < " + branchBlock.getNumber());
    }
    while (branchBlock.getNumber() > blockNumber) {
        branchBlock = getBlockByHash(branchBlock.getParentHash());
    }
    return branchBlock.getHash();
}
 
Example 8
Source File: IndexedBlockStore.java    From nuls with MIT License 5 votes vote down vote up
private void addInternalBlock(Block block, BigInteger totalDifficulty, boolean mainChain) {

        List<BlockInfo> blockInfos = block.getNumber() >= index.size() ? null : index.get((int) block.getNumber());
        blockInfos = blockInfos == null ? new ArrayList<BlockInfo>() : blockInfos;

        BlockInfo blockInfo = new BlockInfo();
        blockInfo.setTotalDifficulty(totalDifficulty);
        blockInfo.setHash(block.getHash());
        blockInfo.setMainChain(mainChain); // FIXME:maybe here I should force reset main chain for all uncles on that level

        putBlockInfo(blockInfos, blockInfo);
        index.set((int) block.getNumber(), blockInfos);

        blocks.put(block.getHash(), block);
    }
 
Example 9
Source File: AbstractBlockstore.java    From nuls with MIT License 5 votes vote down vote up
@Override
public byte[] getBlockHashByNumber(long blockNumber, byte[] branchBlockHash) {
    Block branchBlock = getBlockByHash(branchBlockHash);
    if (branchBlock.getNumber() < blockNumber) {
        throw new IllegalArgumentException("Requested block number > branch hash number: " + blockNumber + " < " + branchBlock.getNumber());
    }
    while (branchBlock.getNumber() > blockNumber) {
        branchBlock = getBlockByHash(branchBlock.getParentHash());
    }
    return branchBlock.getHash();
}
 
Example 10
Source File: BlockQueue.java    From ethereumj with MIT License 5 votes vote down vote up
@Override
public int compare(Block o1, Block o2) {

	if (o1 == null || o2 == null)
		throw new NullPointerException();

	if (o1.getNumber() > o2.getNumber())
		return 1;
	if (o1.getNumber() < o2.getNumber())
		return -1;

	return 0;
}
 
Example 11
Source File: ChainItem.java    From nuls-v2 with MIT License 4 votes vote down vote up
ChainItem(Block block) {
    this.number = block.getNumber();
    this.hash = block.getHash();
    this.parentHash = block.getParentHash();
}
 
Example 12
Source File: ChainItem.java    From nuls with MIT License 4 votes vote down vote up
ChainItem(Block block) {
    this.number = block.getNumber();
    this.hash = block.getHash();
    this.parentHash = block.getParentHash();
}
 
Example 13
Source File: ProgramInvokeFactory.java    From ethereumj with MIT License 4 votes vote down vote up
public static ProgramInvoke createProgramInvoke(Transaction tx, Block block, Repository repository) {

        // https://ethereum.etherpad.mozilla.org/26
        Block lastBlock = WorldManager.getInstance().getBlockchain().getLastBlock();

        /***         ADDRESS op       ***/
        // YP: Get address of currently executing account.
        byte[] address  =  tx.isContractCreation() ? tx.getContractAddress(): tx.getReceiveAddress();

        /***         ORIGIN op       ***/
        // YP: This is the sender of original transaction; it is never a contract.
        byte[] origin  = tx.getSender();

        /***         CALLER op       ***/
        // YP: This is the address of the account that is directly responsible for this execution.
        byte[] caller = tx.getSender();

        /***         BALANCE op       ***/
        byte[] balance = repository.getBalance(address).toByteArray();

        /***         GASPRICE op       ***/
        byte[] gasPrice = tx.getGasPrice();

        /*** GAS op ***/
        byte[] gas = tx.getGasLimit();

        /***        CALLVALUE op      ***/
        byte[] callValue = tx.getValue() == null ? new byte[]{0} : tx.getValue();

        /***     CALLDATALOAD  op   ***/
        /***     CALLDATACOPY  op   ***/
        /***     CALLDATASIZE  op   ***/
        byte[] data = tx.getData() == null ? ByteUtil.EMPTY_BYTE_ARRAY : tx.getData();

        /***    PREVHASH  op  ***/
        byte[] lastHash = lastBlock.getHash();

        /***   COINBASE  op ***/
        byte[] coinbase = block.getCoinbase();

        /*** TIMESTAMP  op  ***/
        long timestamp = block.getTimestamp();

        /*** NUMBER  op  ***/
        long number = block.getNumber();

        /*** DIFFICULTY  op  ***/
        byte[] difficulty = block.getDifficulty();

        /*** GASLIMIT op ***/
        long gaslimit = block.getGasLimit();

        if (logger.isInfoEnabled()) {
            logger.info("Top level call: \n" +
                    "address={}\n" +
                    "origin={}\n"  +
                    "caller={}\n"  +
                    "balance={}\n" +
                    "gasPrice={}\n" +
                    "gas={}\n" +
                    "callValue={}\n" +
                    "data={}\n" +
                    "lastHash={}\n" +
                    "coinbase={}\n" +
                    "timestamp={}\n" +
                    "blockNumber={}\n" +
                    "difficulty={}\n" +
                    "gaslimit={}\n",

                    Hex.toHexString(address),
                    Hex.toHexString(origin),
                    Hex.toHexString(caller),
                    new BigInteger(balance).longValue(),
                    new BigInteger(gasPrice).longValue(),
                    new BigInteger(gas).longValue(),
                    new BigInteger(callValue).longValue(),
                    Hex.toHexString(data),
                    Hex.toHexString(lastHash),
                    Hex.toHexString(coinbase),
                    timestamp,
                    number,
                    Hex.toHexString(difficulty),
                    gaslimit);
        }

        ProgramInvoke programInvoke =
            new ProgramInvokeImpl(address, origin, caller, balance, gasPrice, gas, callValue, data,
                    lastHash,  coinbase,  timestamp,  number,  difficulty,  gaslimit,
                    repository);

        return programInvoke;
    }