本文整理汇总了Java中org.ethereum.core.Block类的典型用法代码示例。如果您正苦于以下问题:Java Block类的具体用法?Java Block怎么用?Java Block使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Block类属于org.ethereum.core包,在下文中一共展示了Block类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bridgeState
import org.ethereum.core.Block; //导入依赖的package包/类
public Map<String, Object> bridgeState(Blockchain blockchain) throws IOException, BlockStoreException {
Block block = blockchain.getBestBlock();
Repository repository = blockchain.getRepository().getSnapshotTo(block.getStateRoot()).startTracking();
BridgeSupport bridgeSupport = new BridgeSupport(
config,
repository,
null,
PrecompiledContracts.BRIDGE_ADDR,
block);
byte[] result = bridgeSupport.getStateForDebugging();
BridgeState state = BridgeState.create(config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), result);
return state.stateToMap();
}
示例2: createSampleNewPendingStateWithAccounts
import org.ethereum.core.Block; //导入依赖的package包/类
private static PendingStateImpl createSampleNewPendingStateWithAccounts(int naccounts, BigInteger balance) {
BlockChainImpl blockChain = createBlockchain();
Block best = blockChain.getStatus().getBestBlock();
Repository repository = blockChain.getRepository();
Repository track = repository.startTracking();
for (int k = 1; k <= naccounts; k++) {
Account account = createAccount(k);
track.createAccount(account.getAddress().getBytes());
track.addBalance(account.getAddress().getBytes(), balance);
}
track.commit();
best.setStateRoot(repository.getRoot());
best.flushRLP();
PendingStateImpl pendingState = new PendingStateImpl(ConfigHelper.CONFIG, blockChain, blockChain.getRepository(), blockChain.getBlockStore(), new ProgramInvokeFactoryImpl(), new BlockExecutorTest.SimpleEthereumListener(), 10, 100);
blockChain.setPendingState(pendingState);
return pendingState;
}
示例3: isValid
import org.ethereum.core.Block; //导入依赖的package包/类
@Override
public boolean isValid(Block block, Block parent) {
if(block == null || parent == null) {
logger.warn("BlockDifficultyRule - block or parent are null");
return false;
}
BlockHeader header = block.getHeader();
BigInteger calcDifficulty = difficultyCalculator.calcDifficulty(header, parent.getHeader());
BigInteger difficulty = header.getDifficultyBI();
if (!isEqual(difficulty, calcDifficulty)) {
logger.warn(String.format("#%d: difficulty != calcDifficulty", header.getNumber()));
return false;
}
return true;
}
示例4: processTenBlocksAddingToBlockchain
import org.ethereum.core.Block; //导入依赖的package包/类
@Test
public void processTenBlocksAddingToBlockchain() {
Blockchain blockchain = BlockChainBuilder.ofSize(0);
BlockStore store = new BlockStore();
Block genesis = blockchain.getBestBlock();
List<Block> blocks = BlockGenerator.getInstance().getBlockChain(genesis, 10);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
processor.processBlock(null, genesis);
Assert.assertEquals(0, store.size());
for (Block b : blocks)
processor.processBlock(null, b);
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Assert.assertEquals(0, store.size());
}
示例5: getBlocksByNumber
import org.ethereum.core.Block; //导入依赖的package包/类
public synchronized List<Block> getBlocksByNumber(long number){
List<Block> result = new ArrayList<>();
if (number >= index.size()) {
return result;
}
List<BlockInfo> blockInfos = index.get((int) number);
if (blockInfos == null) {
return result;
}
for (BlockInfo blockInfo : blockInfos){
byte[] hash = blockInfo.getHash();
Block block = blocks.get(hash);
result.add(block);
}
return result;
}
示例6: isValid
import org.ethereum.core.Block; //导入依赖的package包/类
@Override
public boolean isValid(Block block) {
List<Transaction> txs = block.getTransactionsList();
if(block.getMinGasPriceAsInteger() == null) {
logger.warn("Could not retrieve block min gas priceß");
return false;
}
BigInteger blockMgp = block.getMinGasPriceAsInteger();
if(CollectionUtils.isNotEmpty(block.getTransactionsList())) {
for (Transaction tx : txs) {
if (!(tx instanceof RemascTransaction) && tx.getGasPriceAsInteger().compareTo(blockMgp) < 0) {
logger.warn("Tx gas price is under the Min gas Price of the block");
panicProcessor.panic("invalidmingasprice", "Tx gas price is under the Min gas Price of the block");
return false;
}
}
}
return true;
}
示例7: prepareData
import org.ethereum.core.Block; //导入依赖的package包/类
@Ignore("long stress test")
@Test // loads blocks from file and store them into disk DB
public void prepareData() throws URISyntaxException, IOException {
URL dataURL = ClassLoader.getSystemResource("blockstore/big_data.dmp");
File file = new File(dataURL.toURI());
BufferedReader reader = new BufferedReader(new FileReader(file));
String blockRLP;
while(null != (blockRLP = reader.readLine())) {
Block block = new Block(
Hex.decode(blockRLP)
);
blockSource.put(block.getHash(), block);
if (block.getNumber() % 10000 == 0)
logger.info(
"adding block.hash: [{}] block.number: [{}]",
block.getShortHash(),
block.getNumber()
);
}
logger.info("total blocks loaded: {}", blockSource.size());
}
示例8: blockInSomeBlockChain
import org.ethereum.core.Block; //导入依赖的package包/类
@Test
public void blockInSomeBlockChain() {
BlockChainImpl blockChain = new BlockChainBuilder().build();
Block genesis = BlockGenerator.getInstance().getGenesisBlock();
genesis.setStateRoot(blockChain.getRepository().getRoot());
genesis.flushRLP();
Block block1 = new BlockBuilder().parent(genesis).build();
Block block1b = new BlockBuilder().parent(genesis).build();
Block block2 = new BlockBuilder().parent(block1).build();
Block block3 = new BlockBuilder().parent(block2).build();
blockChain.getBlockStore().saveBlock(block3, BigInteger.ONE, false);
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
blockChain.tryToConnect(block1);
blockChain.tryToConnect(block1b);
Assert.assertTrue(BlockUtils.blockInSomeBlockChain(genesis, blockChain));
Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1, blockChain));
Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block1b, blockChain));
Assert.assertFalse(BlockUtils.blockInSomeBlockChain(block2, blockChain));
Assert.assertTrue(BlockUtils.blockInSomeBlockChain(block3, blockChain));
}
示例9: processBlockBuildCommandWithUncles
import org.ethereum.core.Block; //导入依赖的package包/类
@Test
public void processBlockBuildCommandWithUncles() throws DslProcessorException {
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
DslParser parser = new DslParser("block_chain g00 b01\nblock_chain g00 u01\nblock_chain g00 u02\nblock_build b02\nparent b01\nuncles u01 u02\nbuild");
processor.processCommands(parser);
Block block = world.getBlockByName("b02");
Assert.assertNotNull(block);
Assert.assertEquals(2, block.getNumber());
Assert.assertNotNull(block.getUncleList());
Assert.assertFalse(block.getUncleList().isEmpty());
Assert.assertEquals(2, block.getUncleList().size());
Assert.assertArrayEquals(world.getBlockByName("u01").getHash(), block.getUncleList().get(0).getHash());
Assert.assertArrayEquals(world.getBlockByName("u02").getHash(), block.getUncleList().get(1).getHash());
}
示例10: processBlockSavingInStore
import org.ethereum.core.Block; //导入依赖的package包/类
@Test
public void processBlockSavingInStore() throws UnknownHostException {
final BlockStore store = new BlockStore();
final MessageChannel sender = new SimpleMessageChannel();
final Blockchain blockchain = BlockChainBuilder.ofSize(0);
final Block parent = BlockGenerator.getInstance().createChildBlock(BlockGenerator.getInstance().getGenesisBlock());
final Block orphan = BlockGenerator.getInstance().createChildBlock(parent);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
processor.processBlock(sender, orphan);
Assert.assertTrue(processor.getNodeInformation().getNodesByBlock(orphan.getHash()).size() == 1);
Assert.assertTrue(store.hasBlock(orphan));
Assert.assertEquals(1, store.size());
}
示例11: newBody
import org.ethereum.core.Block; //导入依赖的package包/类
@Override
public void newBody(BodyResponseMessage message, MessageChannel peer) {
NodeID peerId = peer.getPeerNodeID();
if (!isExpectedBody(message.getId(), peerId)) {
handleUnexpectedBody(peerId);
return;
}
// we already checked that this message was expected
BlockHeader header = pendingBodyResponses.remove(message.getId()).header;
Block block = Block.fromValidData(header, message.getTransactions(), message.getUncles());
if (!blockUnclesHashValidationRule.isValid(block) || !blockTransactionsValidationRule.isValid(block)) {
handleInvalidMessage(peerId, header);
return;
}
// handle block
if (syncInformation.processBlock(block).isInvalidBlock()){
handleInvalidBlock(peerId, header);
return;
}
// updates peer downloading information
tryRequestNextBody(peerId);
// check if this was the last block to download
verifyDownloadIsFinished();
}
示例12: getBestBlock
import org.ethereum.core.Block; //导入依赖的package包/类
public synchronized Block getBestBlock(){
Long maxLevel = getMaxNumber();
if (maxLevel < 0) return null;
Block bestBlock = getChainBlockByNumber(maxLevel);
if (bestBlock != null) return bestBlock;
// That scenario can happen
// if there is a fork branch that is
// higher than main branch but has
// less TD than the main branch TD
while (bestBlock == null){
--maxLevel;
bestBlock = getChainBlockByNumber(maxLevel);
}
return bestBlock;
}
示例13: removeBlockByNumber
import org.ethereum.core.Block; //导入依赖的package包/类
private void removeBlockByNumber(ByteArrayWrapper key, Long nkey) {
Set<Block> bynumber = this.blocksbynumber.get(nkey);
if (bynumber != null && !bynumber.isEmpty()) {
Block toremove = null;
for (Block blk : bynumber) {
if (new ByteArrayWrapper(blk.getHash()).equals(key)) {
toremove = blk;
break;
}
}
if (toremove != null) {
bynumber.remove(toremove);
if (bynumber.isEmpty()) {
this.blocksbynumber.remove(nkey);
}
}
}
}
示例14: exportBlocks
import org.ethereum.core.Block; //导入依赖的package包/类
private List<Block> exportBlocks() {
List<Block> ret = new ArrayList<>();
for (long i = minNum; i <= maxNum; i++) {
Map<ByteArrayWrapper, HeaderElement> gen = headers.get(i);
if (gen == null) break;
boolean hasAny = false;
for (HeaderElement element : gen.values()) {
HeaderElement parent = element.getParent();
if (element.block != null && (i == minNum || parent != null && parent.exported)) {
if (!element.exported) {
exportNewBlock(element.block);
ret.add(element.block);
element.exported = true;
}
hasAny = true;
}
}
if (!hasAny) break;
}
trimExported();
return ret;
}
示例15: blockTimeEqualsParentTime
import org.ethereum.core.Block; //导入依赖的package包/类
@Test
public void blockTimeEqualsParentTime() {
int validPeriod = 540;
BlockTimeStampValidationRule validationRule = new BlockTimeStampValidationRule(validPeriod);
Block block = Mockito.mock(Block.class);
Block parent = Mockito.mock(Block.class);
Mockito.when(block.getTimestamp())
.thenReturn(System.currentTimeMillis() / 1000);
Mockito.when(parent.getTimestamp())
.thenReturn(System.currentTimeMillis() / 1000);
Assert.assertFalse(validationRule.isValid(block, parent));
}