use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.
the class PingPacketDataTest method serializeDeserialize.
@Test
public void serializeDeserialize() {
final long currentTimeSec = Instant.now().getEpochSecond();
final Endpoint from = new Endpoint("127.0.0.1", 30303, Optional.of(30303));
final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
final UInt64 enrSeq = UInt64.ONE;
final PingPacketData packet = PingPacketData.create(Optional.of(from), to, enrSeq);
final Bytes serialized = RLP.encode(packet::writeTo);
final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized));
assertThat(deserialized.getFrom()).contains(from);
assertThat(deserialized.getTo()).isEqualTo(to);
assertThat(deserialized.getExpiration()).isGreaterThan(currentTimeSec);
assertThat(deserialized.getEnrSeq().isPresent()).isTrue();
assertThat(deserialized.getEnrSeq().get()).isEqualTo(enrSeq);
}
use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.
the class PongPacketDataTest method serializeDeserialize.
@Test
public void serializeDeserialize() {
final long currentTimeSec = Instant.now().getEpochSecond();
final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");
final UInt64 enrSeq = UInt64.ONE;
final PongPacketData packet = PongPacketData.create(to, hash, enrSeq);
final Bytes serialized = RLP.encode(packet::writeTo);
final PongPacketData deserialized = PongPacketData.readFrom(RLP.input(serialized));
assertThat(deserialized.getTo()).isEqualTo(to);
assertThat(deserialized.getPingHash()).isEqualTo(hash);
assertThat(deserialized.getExpiration()).isGreaterThan(currentTimeSec);
assertThat(deserialized.getEnrSeq().isPresent()).isTrue();
assertThat(deserialized.getEnrSeq().get()).isEqualTo(enrSeq);
}
use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.
the class PongPacketDataTest method legacyHandlesScalar.
@Test
public void legacyHandlesScalar() {
final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
final UInt64 enrSeq = UInt64.MAX_VALUE;
final Bytes32 hash = Bytes32.fromHexStringLenient("0x1234");
final PongPacketData pong = PongPacketData.create(to, hash, enrSeq);
final BytesValueRLPOutput out = new BytesValueRLPOutput();
pong.writeTo(out);
final PongPacketData legacyPong = PongPacketData.legacyReadFrom(RLP.input(out.encoded()));
assertThat(legacyPong.getTo()).isEqualTo(to);
assertThat(legacyPong.getPingHash()).isEqualTo(hash);
assertThat(legacyPong.getEnrSeq().isPresent()).isTrue();
assertThat(legacyPong.getEnrSeq().get()).isEqualTo(enrSeq);
}
use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.
the class PeerDiscoveryAgent method updateNodeRecord.
public void updateNodeRecord() {
if (!config.isActive()) {
return;
}
final KeyValueStorage keyValueStorage = storageProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.BLOCKCHAIN);
final NodeRecordFactory nodeRecordFactory = NodeRecordFactory.DEFAULT;
final Optional<NodeRecord> existingNodeRecord = keyValueStorage.get(Bytes.of(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray()).map(Bytes::of).map(nodeRecordFactory::fromBytes);
final Bytes addressBytes = Bytes.of(InetAddresses.forString(advertisedAddress).getAddress());
final Optional<EnodeURL> maybeEnodeURL = localNode.map(DiscoveryPeer::getEnodeURL);
final Integer discoveryPort = maybeEnodeURL.flatMap(EnodeURL::getDiscoveryPort).orElse(0);
final Integer listeningPort = maybeEnodeURL.flatMap(EnodeURL::getListeningPort).orElse(0);
final String forkIdEnrField = "eth";
final NodeRecord newNodeRecord = existingNodeRecord.filter(nodeRecord -> id.equals(nodeRecord.get(EnrField.PKEY_SECP256K1)) && addressBytes.equals(nodeRecord.get(EnrField.IP_V4)) && discoveryPort.equals(nodeRecord.get(EnrField.UDP)) && listeningPort.equals(nodeRecord.get(EnrField.TCP)) && forkIdSupplier.get().equals(nodeRecord.get(forkIdEnrField))).orElseGet(() -> {
final UInt64 sequenceNumber = existingNodeRecord.map(NodeRecord::getSeq).orElse(UInt64.ZERO).add(1);
final NodeRecord nodeRecord = nodeRecordFactory.createFromValues(sequenceNumber, new EnrField(EnrField.ID, IdentitySchema.V4), new EnrField(SIGNATURE_ALGORITHM.get().getCurveName(), SIGNATURE_ALGORITHM.get().compressPublicKey(SIGNATURE_ALGORITHM.get().createPublicKey(id))), new EnrField(EnrField.IP_V4, addressBytes), new EnrField(EnrField.TCP, listeningPort), new EnrField(EnrField.UDP, discoveryPort), new EnrField(forkIdEnrField, Collections.singletonList(forkIdSupplier.get())));
nodeRecord.setSignature(nodeKey.sign(Hash.keccak256(nodeRecord.serializeNoSignature())).encodedBytes().slice(0, 64));
LOG.info("Writing node record to disk. {}", nodeRecord);
final KeyValueStorageTransaction keyValueStorageTransaction = keyValueStorage.startTransaction();
keyValueStorageTransaction.put(Bytes.wrap(SEQ_NO_STORE_KEY.getBytes(UTF_8)).toArray(), nodeRecord.serialize().toArray());
keyValueStorageTransaction.commit();
return nodeRecord;
});
localNode.orElseThrow(() -> new IllegalStateException("Local node should be set here")).setNodeRecord(newNodeRecord);
}
use of org.apache.tuweni.units.bigints.UInt64 in project besu by hyperledger.
the class PongPacketData method readFrom.
public static PongPacketData readFrom(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()) {
try {
enrSeq = UInt64.valueOf(in.readBigIntegerScalar());
LOG.debug("read PONG enr from scalar");
} catch (MalformedRLPInputException malformed) {
LOG.debug("failed to read PONG enr from scalar, trying as byte array");
enrSeq = UInt64.fromBytes(in.readBytes());
}
}
in.leaveListLenient();
return new PongPacketData(to, hash, expiration, enrSeq);
}
Aggregations