org.bitcoinj.core.Block.unsafeBitcoinSerialize()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(129)

本文整理了Java中org.bitcoinj.core.Block.unsafeBitcoinSerialize()方法的一些代码示例,展示了Block.unsafeBitcoinSerialize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Block.unsafeBitcoinSerialize()方法的具体详情如下:
包路径:org.bitcoinj.core.Block
类名称:Block
方法名:unsafeBitcoinSerialize

Block.unsafeBitcoinSerialize介绍

暂无

代码示例

代码示例来源:origin: fr.acinq/bitcoinj-core

/** Serializes the stored block to a custom packed format. Used by {@link CheckpointManager}. */
public void serializeCompact(ByteBuffer buffer) {
  byte[] chainWorkBytes = getChainWork().toByteArray();
  checkState(chainWorkBytes.length <= CHAIN_WORK_BYTES, "Ran out of space to store chain work!");
  if (chainWorkBytes.length < CHAIN_WORK_BYTES) {
    // Pad to the right size.
    buffer.put(EMPTY_BYTES, 0, CHAIN_WORK_BYTES - chainWorkBytes.length);
  }
  buffer.put(chainWorkBytes);
  buffer.putInt(getHeight());
  // Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
  // avoiding serialization round-trips.
  byte[] bytes = getHeader().unsafeBitcoinSerialize();
  buffer.put(bytes, 0, Block.HEADER_SIZE);  // Trim the trailing 00 byte (zero transactions).
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

/** Serializes the stored block to a custom packed format. Used by {@link CheckpointManager}. */
public void serializeCompact(ByteBuffer buffer) {
  byte[] chainWorkBytes = getChainWork().toByteArray();
  checkState(chainWorkBytes.length <= CHAIN_WORK_BYTES, "Ran out of space to store chain work!");
  if (chainWorkBytes.length < CHAIN_WORK_BYTES) {
    // Pad to the right size.
    buffer.put(EMPTY_BYTES, 0, CHAIN_WORK_BYTES - chainWorkBytes.length);
  }
  buffer.put(chainWorkBytes);
  buffer.putInt(getHeight());
  // Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
  // avoiding serialization round-trips.
  byte[] bytes = getHeader().unsafeBitcoinSerialize();
  buffer.put(bytes, 0, Block.HEADER_SIZE);  // Trim the trailing 00 byte (zero transactions).
}

代码示例来源:origin: greenaddress/GreenBits

/** Serializes the stored block to a custom packed format. Used by {@link CheckpointManager}. */
public void serializeCompact(ByteBuffer buffer) {
  byte[] chainWorkBytes = getChainWork().toByteArray();
  checkState(chainWorkBytes.length <= CHAIN_WORK_BYTES, "Ran out of space to store chain work!");
  if (chainWorkBytes.length < CHAIN_WORK_BYTES) {
    // Pad to the right size.
    buffer.put(EMPTY_BYTES, 0, CHAIN_WORK_BYTES - chainWorkBytes.length);
  }
  buffer.put(chainWorkBytes);
  buffer.putInt(getHeight());
  // Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
  // avoiding serialization round-trips.
  byte[] bytes = getHeader().unsafeBitcoinSerialize();
  buffer.put(bytes, 0, Block.HEADER_SIZE);  // Trim the trailing 00 byte (zero transactions).
}

代码示例来源:origin: HashEngineering/dashj

/** Serializes the stored block to a custom packed format. Used by {@link CheckpointManager}. */
public void serializeCompact(ByteBuffer buffer) {
  byte[] chainWorkBytes = getChainWork().toByteArray();
  checkState(chainWorkBytes.length <= CHAIN_WORK_BYTES, "Ran out of space to store chain work!");
  if (chainWorkBytes.length < CHAIN_WORK_BYTES) {
    // Pad to the right size.
    buffer.put(EMPTY_BYTES, 0, CHAIN_WORK_BYTES - chainWorkBytes.length);
  }
  buffer.put(chainWorkBytes);
  buffer.putInt(getHeight());
  // Using unsafeBitcoinSerialize here can give us direct access to the same bytes we read off the wire,
  // avoiding serialization round-trips.
  byte[] bytes = getHeader().unsafeBitcoinSerialize();
  buffer.put(bytes, 0, Block.HEADER_SIZE);  // Trim the trailing 00 byte (zero transactions).
}

代码示例来源:origin: fr.acinq/bitcoinj-core

protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws SQLException {
  try {
    PreparedStatement s =
        conn.get().prepareStatement(getInsertHeadersSQL());
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(1, hashBytes);
    s.setBytes(2, storedBlock.getChainWork().toByteArray());
    s.setInt(3, storedBlock.getHeight());
    s.setBytes(4, storedBlock.getHeader().cloneAsHeader().unsafeBitcoinSerialize());
    s.setBoolean(5, wasUndoable);
    s.executeUpdate();
    s.close();
  } catch (SQLException e) {
    // It is possible we try to add a duplicate StoredBlock if we upgraded
    // In that case, we just update the entry to mark it wasUndoable
    if  (!(e.getSQLState().equals(getDuplicateKeyErrorCode())) || !wasUndoable)
      throw e;
    PreparedStatement s = conn.get().prepareStatement(getUpdateHeadersSQL());
    s.setBoolean(1, true);
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(2, hashBytes);
    s.executeUpdate();
    s.close();
  }
}

代码示例来源:origin: cash.bitcoinj/bitcoinj-core

protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws SQLException {
  try {
    PreparedStatement s =
        conn.get().prepareStatement(getInsertHeadersSQL());
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(1, hashBytes);
    s.setBytes(2, storedBlock.getChainWork().toByteArray());
    s.setInt(3, storedBlock.getHeight());
    s.setBytes(4, storedBlock.getHeader().cloneAsHeader().unsafeBitcoinSerialize());
    s.setBoolean(5, wasUndoable);
    s.executeUpdate();
    s.close();
  } catch (SQLException e) {
    // It is possible we try to add a duplicate StoredBlock if we upgraded
    // In that case, we just update the entry to mark it wasUndoable
    if  (!(e.getSQLState().equals(getDuplicateKeyErrorCode())) || !wasUndoable)
      throw e;
    PreparedStatement s = conn.get().prepareStatement(getUpdateHeadersSQL());
    s.setBoolean(1, true);
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(2, hashBytes);
    s.executeUpdate();
    s.close();
  }
}

代码示例来源:origin: greenaddress/GreenBits

protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws SQLException {
  try {
    PreparedStatement s =
        conn.get().prepareStatement(getInsertHeadersSQL());
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(1, hashBytes);
    s.setBytes(2, storedBlock.getChainWork().toByteArray());
    s.setInt(3, storedBlock.getHeight());
    s.setBytes(4, storedBlock.getHeader().cloneAsHeader().unsafeBitcoinSerialize());
    s.setBoolean(5, wasUndoable);
    s.executeUpdate();
    s.close();
  } catch (SQLException e) {
    // It is possible we try to add a duplicate StoredBlock if we upgraded
    // In that case, we just update the entry to mark it wasUndoable
    if  (!(e.getSQLState().equals(getDuplicateKeyErrorCode())) || !wasUndoable)
      throw e;
    PreparedStatement s = conn.get().prepareStatement(getUpdateHeadersSQL());
    s.setBoolean(1, true);
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(2, hashBytes);
    s.executeUpdate();
    s.close();
  }
}

代码示例来源:origin: HashEngineering/dashj

protected void putUpdateStoredBlock(StoredBlock storedBlock, boolean wasUndoable) throws SQLException {
  try {
    PreparedStatement s =
        conn.get().prepareStatement(getInsertHeadersSQL());
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(1, hashBytes);
    s.setBytes(2, storedBlock.getChainWork().toByteArray());
    s.setInt(3, storedBlock.getHeight());
    s.setBytes(4, storedBlock.getHeader().cloneAsHeader().unsafeBitcoinSerialize());
    s.setBoolean(5, wasUndoable);
    s.executeUpdate();
    s.close();
  } catch (SQLException e) {
    // It is possible we try to add a duplicate StoredBlock if we upgraded
    // In that case, we just update the entry to mark it wasUndoable
    if  (!(e.getSQLState().equals(getDuplicateKeyErrorCode())) || !wasUndoable)
      throw e;
    PreparedStatement s = conn.get().prepareStatement(getUpdateHeadersSQL());
    s.setBoolean(1, true);
    // We skip the first 4 bytes because (on mainnet) the minimum target has 4 0-bytes
    byte[] hashBytes = new byte[28];
    System.arraycopy(storedBlock.getHeader().getHash().getBytes(), 4, hashBytes, 0, 28);
    s.setBytes(2, hashBytes);
    s.executeUpdate();
    s.close();
  }
}

代码示例来源:origin: greenaddress/GreenBits

assertEquals(origTxLength, tx.length);
block.addTransaction(tx);
assertEquals(block.unsafeBitcoinSerialize().length, block.length);
assertEquals(origBlockLen + tx.length, block.length);
block.getTransactions().get(1).getInputs().get(0).setScriptBytes(new byte[] {(byte) ScriptOpCodes.OP_FALSE, (byte) ScriptOpCodes.OP_FALSE});
assertEquals(tx.length, origTxLength + 1);
block.getTransactions().get(1).getInputs().get(0).clearScriptBytes();
assertEquals(block.length, block.unsafeBitcoinSerialize().length);
assertEquals(block.length, origBlockLen + tx.length);
assertEquals(tx.length, origTxLength - 1);

相关文章