本文整理汇总了Java中org.ethereum.core.BlockHeaderWrapper类的典型用法代码示例。如果您正苦于以下问题:Java BlockHeaderWrapper类的具体用法?Java BlockHeaderWrapper怎么用?Java BlockHeaderWrapper使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BlockHeaderWrapper类属于org.ethereum.core包,在下文中一共展示了BlockHeaderWrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addBatch
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public void addBatch(Collection<BlockHeaderWrapper> headers) {
awaitInit();
synchronized (mutex) {
List<Long> numbers = new ArrayList<>(headers.size());
for (BlockHeaderWrapper b : headers) {
if(!index.contains(b.getNumber()) &&
!numbers.contains(b.getNumber())) {
this.headers.put(b.getNumber(), b);
numbers.add(b.getNumber());
}
}
index.addAll(numbers);
}
dbCommit("addBatch: " + headers.size());
}
示例2: pollBatch
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public List<BlockHeaderWrapper> pollBatch(int qty) {
awaitInit();
if (index.isEmpty()) {
return Collections.emptyList();
}
List<BlockHeaderWrapper> headers = new ArrayList<>(qty > size() ? qty : size());
while (headers.size() < qty) {
BlockHeaderWrapper header = pollInner();
if(header == null) {
break;
}
headers.add(header);
}
dbCommit("pollBatch: " + headers.size());
return headers;
}
示例3: pollInner
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
private BlockHeaderWrapper pollInner() {
synchronized (mutex) {
if (index.isEmpty()) {
return null;
}
Long idx = index.poll();
BlockHeaderWrapper header = headers.get(idx);
headers.remove(idx);
if (header == null) {
logger.error("Header for index {} is null", idx);
panicProcessor.panic("headerstore", String.format("Header for index %d is null", idx));
}
return header;
}
}
示例4: addHeaderPriv
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
private boolean addHeaderPriv(BlockHeaderWrapper header) {
long num = header.getNumber();
Map<ByteArrayWrapper, HeaderElement> genHeaders = headers.get(num);
if (genHeaders == null) {
genHeaders = new HashMap<>();
putGenHeaders(num, genHeaders);
}
ByteArrayWrapper wHash = new ByteArrayWrapper(header.getHash());
HeaderElement headerElement = genHeaders.get(wHash);
if (headerElement != null) return false;
headerElement = new HeaderElement(header);
genHeaders.put(wHash, headerElement);
return true;
}
示例5: add
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public void add(BlockHeaderWrapper header) {
awaitInit();
synchronized (mutex) {
if (index.contains(header.getNumber())) {
return;
}
headers.put(header.getNumber(), header);
index.add(header.getNumber());
}
dbCommit("add");
}
示例6: peek
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public BlockHeaderWrapper peek() {
awaitInit();
synchronized (mutex) {
if(index.isEmpty()) {
return null;
}
Long idx = index.peek();
return headers.get(idx);
}
}
示例7: poll
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public BlockHeaderWrapper poll() {
awaitInit();
BlockHeaderWrapper header = pollInner();
dbCommit("poll");
return header;
}
示例8: drop
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public void drop(byte[] nodeId) {
awaitInit();
int i = 0;
List<Long> removed = new ArrayList<>();
synchronized (index) {
for (Long idx : index) {
BlockHeaderWrapper h = headers.get(idx);
if (h.sentBy(nodeId)) {
removed.add(idx);
}
}
headers.keySet().removeAll(removed);
index.removeAll(removed);
}
db.commit();
if (logger.isDebugEnabled()) {
if (removed.isEmpty()) {
logger.debug("0 headers are dropped out");
} else {
logger.debug("[{}..{}] headers are dropped out", removed.get(0), removed.get(removed.size() - 1));
}
}
}
示例9: createHeaders
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
static BlockHeaderWrapper[] createHeaders(int headCnt) {
BlockHeaderWrapper[] ret = new BlockHeaderWrapper[headCnt];
for (int i = 0; i < ret.length; i++) {
ret[i] = new BlockHeaderWrapper(new BlockHeader(new byte[0], new byte[0], new byte[0], new byte[0], new byte[0],
cnt.getAndIncrement(), new byte[] {0}, 0, 0, new byte[0], new byte[0], 0), null);
}
return ret;
}
示例10: test1
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Test
public void test1() {
hs.addBatch(Arrays.asList(createHeaders(10)));
List<BlockHeaderWrapper> bhs = hs.pollBatch(1000);
Assert.assertEquals(10, bhs.size());
BlockHeaderWrapper[] headers = createHeaders(2);
hs.add(headers[1]);
hs.add(headers[0]);
BlockHeaderWrapper bh = hs.poll();
Assert.assertEquals(headers[0].getNumber(), bh.getNumber());
bh = hs.poll();
Assert.assertEquals(headers[1].getNumber(), bh.getNumber());
}
示例11: concurrentTest1
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Test
public void concurrentTest1() throws InterruptedException {
final CountDownLatch waiter = new CountDownLatch(1);
new Thread() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
hs.addBatch(Arrays.asList(createHeaders(10)));
System.out.println("Added 10");
}
}
}.start();
new Thread() {
int cnt = 0;
@Override
public void run() {
while (cnt < 10 * 10) {
List<BlockHeaderWrapper> ret = hs.pollBatch(100000);
if (ret.size() > 0) {
System.out.println("Polled " + ret.size());
}
cnt += ret.size();
}
waiter.countDown();
}
}.start();
waiter.await(20, TimeUnit.SECONDS);
Assert.assertTrue(waiter.getCount() == 0);
}
示例12: requestHeaders
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public synchronized List<HeadersRequest> requestHeaders(int maxSize, int maxRequests, int maxTotalHeaders) {
List<HeadersRequest> ret = new ArrayList<>();
if (minValidated < 0) {
ret.add(new SyncQueueImpl.HeadersRequestImpl(curHeaderHash, maxSize, true, maxSize - 1));
} else if (minValidated == 0) {
// genesis reached
return null;
} else {
if (minValidated - headers.getMin() < maxSize * maxSize && minValidated > maxSize) {
ret.add(new SyncQueueImpl.HeadersRequestImpl(
headers.get(headers.getMin()).getHash(), maxSize, true, maxSize - 1));
maxRequests--;
}
Set<Map.Entry<Long, BlockHeaderWrapper>> entries =
headers.descendingMap().subMap(minValidated, true, headers.getMin(), true).entrySet();
Iterator<Map.Entry<Long, BlockHeaderWrapper>> it = entries.iterator();
BlockHeaderWrapper prevEntry = it.next().getValue();
while(maxRequests > 0 && it.hasNext()) {
BlockHeaderWrapper entry = it.next().getValue();
if (prevEntry.getNumber() - entry.getNumber() > 1) {
ret.add(new SyncQueueImpl.HeadersRequestImpl(prevEntry.getHash(), maxSize, true));
maxRequests--;
}
prevEntry = entry;
}
if (maxRequests > 0) {
ret.add(new SyncQueueImpl.HeadersRequestImpl(prevEntry.getHash(), maxSize, true));
}
}
return ret;
}
示例13: requestBlocks
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
public synchronized BlocksRequest requestBlocks(int maxSize) {
List<BlockHeaderWrapper> reqHeaders = new ArrayList<>();
for (BlockHeaderWrapper header : headers.descendingMap().values()) {
if (maxSize == 0) break;
if (blocks.get(header.getHash()) == null) {
reqHeaders.add(header);
maxSize--;
}
}
return new SyncQueueImpl.BlocksRequestImpl(reqHeaders);
}
示例14: pushHeaders
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
@Override
protected void pushHeaders(List<BlockHeaderWrapper> headers) {
if (headers.get(headers.size() - 1).getNumber() == 0) {
genesisHash = headers.get(headers.size() - 1).getHash();
}
if (headers.get(headers.size() - 1).getNumber() == 1) {
genesisHash = headers.get(headers.size() - 1).getHeader().getParentHash();
}
logger.info(headers.size() + " headers loaded: " + headers.get(0).getNumber() + " - " + headers.get(headers.size() - 1).getNumber());
for (BlockHeaderWrapper header : headers) {
headerStore.set((int) header.getNumber(), header.getHeader());
headersLoaded++;
}
dbFlushManager.commit();
}
示例15: init
import org.ethereum.core.BlockHeaderWrapper; //导入依赖的package包/类
private void init(List<Block> initBlocks) {
if (initBlocks.size() < MAX_CHAIN_LEN && initBlocks.get(0).getNumber() != 0) {
throw new RuntimeException("Queue should be initialized with a chain of at least " + MAX_CHAIN_LEN + " size or with the first genesis block");
}
for (Block block : initBlocks) {
addHeaderPriv(new BlockHeaderWrapper(block.getHeader(), null));
addBlock(block).exported = true;
}
darkZoneNum = initBlocks.get(0).getNumber();
}