Search in sources :

Example 26 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PingPacketData method readFrom.

public static PingPacketData readFrom(final RLPInput in) {
    in.enterList();
    // The first element signifies the "version", but this value is ignored as of EIP-8
    in.readBigIntegerScalar();
    Optional<Endpoint> from = Optional.empty();
    Optional<Endpoint> to = Optional.empty();
    if (in.nextIsList()) {
        to = Endpoint.maybeDecodeStandalone(in);
        // https://github.com/ethereum/devp2p/blob/master/discv4.md#ping-packet-0x01
        if (in.nextIsList()) {
            // if there are two, the first is the from address, next is the to
            // address
            from = to;
            to = Endpoint.maybeDecodeStandalone(in);
        }
    } else {
        throw new DevP2PException("missing address in ping packet");
    }
    final long expiration = in.readLongScalar();
    UInt64 enrSeq = null;
    if (!in.isEndOfCurrentList()) {
        try {
            enrSeq = UInt64.valueOf(in.readBigIntegerScalar());
            LOG.debug("read PING enr as long scalar");
        } catch (MalformedRLPInputException malformed) {
            LOG.debug("failed to read PING enr as scalar, trying to read bytes instead");
            enrSeq = UInt64.fromBytes(in.readBytes());
        }
    }
    in.leaveListLenient();
    return new PingPacketData(from, to.get(), expiration, enrSeq);
}
Also used : Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) MalformedRLPInputException(org.hyperledger.besu.ethereum.rlp.MalformedRLPInputException) UInt64(org.apache.tuweni.units.bigints.UInt64)

Example 27 with UInt64

use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.

the class PongPacketData method legacyReadFrom.

/**
 * Used by test classes to read legacy encodes of Pongs which used a byte array for the ENR field
 *
 * @deprecated Only to be used by internal tests to confirm backward compatibility.
 * @param in input stream being read from
 * @return PongPacketData parsed from input, using legacy encode
 */
@Deprecated
public static PongPacketData legacyReadFrom(final RLPInput in) {
    in.enterList();
    final Endpoint to = Endpoint.decodeStandalone(in);
    final Bytes hash = in.readBytes();
    final long expiration = in.readLongScalar();
    UInt64 enrSeq = null;
    if (!in.isEndOfCurrentList()) {
        enrSeq = UInt64.fromBytes(in.readBytes());
    }
    in.leaveListLenient();
    return new PongPacketData(to, hash, expiration, enrSeq);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) UInt64(org.apache.tuweni.units.bigints.UInt64)

Aggregations

UInt64 (org.apache.tuweni.units.bigints.UInt64)27 Bytes (org.apache.tuweni.bytes.Bytes)20 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)19 Test (org.junit.Test)16 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)13 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 Test (org.junit.jupiter.api.Test)5 InterchangeV5Format (dsl.InterchangeV5Format)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Suppliers (com.google.common.base.Suppliers)1 InetAddresses (com.google.common.net.InetAddresses)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1