-
Notifications
You must be signed in to change notification settings - Fork 32
feat: add QUIC port #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add QUIC port #835
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,8 @@ type | |
| udp*: Opt[int] | ||
| tcp6*: Opt[int] | ||
| udp6*: Opt[int] | ||
| quic*: Opt[int] | ||
| quic6*: Opt[int] | ||
|
|
||
| EnrResult*[T] = Result[T, cstring] | ||
|
|
||
|
|
@@ -218,7 +220,7 @@ macro initRecord*( | |
| func insertAddress( | ||
| fields: var seq[FieldPair], | ||
| ip: Opt[IpAddress], | ||
| tcpPort, udpPort: Opt[Port]) = | ||
| tcpPort, udpPort, quicPort: Opt[Port]) = | ||
| ## Insert address data. | ||
| ## Incomplete address information is allowed (example: Port but not IP) as | ||
| ## that information might be already in the ENR or added later. | ||
|
|
@@ -233,24 +235,27 @@ func insertAddress( | |
| fields.insert(("tcp", tcpPort.get().uint16.toField)) | ||
| if udpPort.isSome(): | ||
| fields.insert(("udp", udpPort.get().uint16.toField)) | ||
| if quicPort.isSome(): | ||
| fields.insert(("quic", quicPort.get().uint16.toField)) | ||
|
|
||
| func init*( | ||
| T: type Record, | ||
| seqNum: uint64, pk: PrivateKey, | ||
| ip: Opt[IpAddress] = Opt.none(IpAddress), | ||
| tcpPort: Opt[Port] = Opt.none(Port), | ||
| udpPort: Opt[Port] = Opt.none(Port), | ||
| quicPort: Opt[Port] = Opt.none(Port), | ||
| extraFields: openArray[FieldPair] = []): | ||
| EnrResult[T] = | ||
| ## Initialize a `Record` with given sequence number, private key, optional | ||
| ## ip address, tcp port, udp port, and optional custom k:v pairs. | ||
| ## ip address, tcp port, udp port, quic port, and optional custom k:v pairs. | ||
| ## | ||
| ## Can fail in case the record exceeds the `maxEnrSize`. | ||
| doAssert(not hasPredefinedKey(extraFields), "Predefined key in custom pairs") | ||
|
|
||
| var fields = newSeq[FieldPair]() | ||
|
|
||
| fields.insertAddress(ip, tcpPort, udpPort) | ||
| fields.insertAddress(ip, tcpPort, udpPort, quicPort) | ||
| fields.insert extraFields | ||
| makeEnrAux(seqNum, "v4", pk, fields) | ||
|
|
||
|
|
@@ -326,16 +331,17 @@ func update*( | |
| ip: Opt[IpAddress] = Opt.none(IpAddress), | ||
| tcpPort: Opt[Port] = Opt.none(Port), | ||
| udpPort: Opt[Port] = Opt.none(Port), | ||
| quicPort: Opt[Port] = Opt.none(Port), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. idem ^ |
||
| extraFields: openArray[FieldPair] = []): | ||
| EnrResult[void] = | ||
| ## Update a `Record` with given ip address, tcp port, udp port and optional | ||
| ## custom k:v pairs. | ||
| ## Update a `Record` with given ip address, tcp port, udp port, quic port | ||
| ## and optional custom k:v pairs. | ||
| ## | ||
| ## If none of the k:v pairs are changed, the sequence number of the `Record` | ||
| ## will still be incremented and a new signature will be applied. | ||
| ## | ||
| ## Providing an `Opt.none` for `ip`, `tcpPort` or `udpPort` will leave the | ||
| ## corresponding field untouched. | ||
| ## Providing an `Opt.none` for `ip`, `tcpPort`, `udpPort` or `quicPort` will | ||
| ## leave the corresponding field untouched. | ||
| ## | ||
| ## Can fail in case of wrong `PrivateKey`, if the size of the resulting record | ||
| ## exceeds `maxEnrSize` or if maximum sequence number is reached. The `Record` | ||
|
|
@@ -349,7 +355,7 @@ func update*( | |
| if pubkey.isNone() or pubkey.get() != pk.toPublicKey(): | ||
| return err("Public key does not correspond with given private key") | ||
|
|
||
| r.pairs.insertAddress(ip, tcpPort, udpPort) | ||
| r.pairs.insertAddress(ip, tcpPort, udpPort, quicPort) | ||
| r.pairs.insert extraFields | ||
|
|
||
| if r.seqNum == high(type r.seqNum): # highly unlikely | ||
|
|
@@ -376,7 +382,9 @@ func fromRecord*(T: type TypedRecord, r: Record): T = | |
| tcp: r.tryGet("tcp", int), | ||
| tcp6: r.tryGet("tcp6", int), | ||
| udp: r.tryGet("udp", int), | ||
| udp6: r.tryGet("udp6", int) | ||
| udp6: r.tryGet("udp6", int), | ||
| quic: r.tryGet("quic", int), | ||
| quic6: r.tryGet("quic6", int) | ||
| ) | ||
|
|
||
| func toTypedRecord*(r: Record): EnrResult[TypedRecord] {.deprecated: "Please use TypedRecord.fromRecord instead".} = | ||
|
|
@@ -462,7 +470,7 @@ func fromBytesAux(T: type Record, s: openArray[byte]): EnrResult[T] = | |
| of "secp256k1": | ||
| pkRaw = Opt.some rlpResult rlp.read(seq[byte]) | ||
| pairs.add((k, Field(kind: kBytes, bytes: pkRaw.value()))) | ||
| of "tcp", "udp", "tcp6", "udp6": | ||
| of "tcp", "udp", "tcp6", "udp6", "quic", "quic6": | ||
| let v = rlpResult rlp.read(uint16) | ||
| pairs.add((k, Field(kind: kNum, num: v))) | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1059,7 +1059,7 @@ func init*( | |
| proc newProtocol*( | ||
| privKey: PrivateKey, | ||
| enrIp: Opt[IpAddress], | ||
| enrTcpPort, enrUdpPort: Opt[Port], | ||
| enrTcpPort, enrUdpPort, enrQuicPort: Opt[Port], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also do without altering this one (afaik it is not needed for nimbus-eth2 and I'd like to deprecate it). |
||
| localEnrFields: openArray[FieldPair] = [], | ||
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
|
|
@@ -1083,14 +1083,14 @@ proc newProtocol*( | |
| record = previousRecord.get() | ||
| # TODO: this is faulty in case the intent is to remove a field with | ||
| # opt.none | ||
| record.update(privKey, enrIp, enrTcpPort, enrUdpPort, | ||
| record.update(privKey, enrIp, enrTcpPort, enrUdpPort, enrQuicPort, | ||
| localEnrFields).expect("Record within size limits and correct key") | ||
| else: | ||
| record = enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort, | ||
| record = enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort, enrQuicPort, | ||
| localEnrFields).expect("Record within size limits") | ||
|
|
||
| info "Discovery ENR initialized", enrAutoUpdate, seqNum = record.seqNum, | ||
| ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort, | ||
| ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort, quicPort = enrQuicPort, | ||
| localEnrFields, uri = toURI(record) | ||
| if enrIp.isNone(): | ||
| if enrAutoUpdate: | ||
|
|
@@ -1124,6 +1124,26 @@ proc newProtocol*( | |
| privKey: PrivateKey, | ||
| enrIp: Opt[IpAddress], | ||
| enrTcpPort, enrUdpPort: Opt[Port], | ||
| localEnrFields: openArray[FieldPair] = [], | ||
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
| bindPort: Port, | ||
| bindIp = IPv4_any(), | ||
| enrAutoUpdate = false, | ||
| banNodes = false, | ||
| config = defaultDiscoveryConfig, | ||
| rng = newRng()): | ||
| Protocol = | ||
| newProtocol( | ||
| privKey, enrIp, enrTcpPort, enrUdpPort, Opt.none(Port), localEnrFields, | ||
| bootstrapRecords, previousRecord, bindPort, bindIp, enrAutoUpdate, | ||
| banNodes, config, rng | ||
| ) | ||
|
|
||
| proc newProtocol*( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the diff between the two
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The difference is the One accepts an I want to deprecate the original one, and keep just the newer one with proper comment explaining this. |
||
| privKey: PrivateKey, | ||
| enrIp: Opt[IpAddress], | ||
| enrTcpPort, enrUdpPort, enrQuicPort: Opt[Port], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem for this one |
||
| localEnrFields: openArray[(string, seq[byte])] = [], | ||
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
|
|
@@ -1136,7 +1156,7 @@ proc newProtocol*( | |
| ): Protocol = | ||
| let customEnrFields = mapIt(localEnrFields, toFieldPair(it[0], it[1])) | ||
| newProtocol( | ||
| privKey, enrIp, enrTcpPort, enrUdpPort, customEnrFields, bootstrapRecords, | ||
| privKey, enrIp, enrTcpPort, enrUdpPort, enrQuicPort, customEnrFields, bootstrapRecords, | ||
| previousRecord, bindPort, bindIp, enrAutoUpdate, banNodes, config, rng, | ||
| ) | ||
|
|
||
|
|
@@ -1148,6 +1168,26 @@ proc newProtocol*( | |
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
| bindPort: Port, | ||
| bindIp = IPv4_any(), | ||
| enrAutoUpdate = false, | ||
| banNodes = false, | ||
| config = defaultDiscoveryConfig, | ||
| rng = newRng(), | ||
| ): Protocol = | ||
| newProtocol( | ||
| privKey, enrIp, enrTcpPort, enrUdpPort, Opt.none(Port), localEnrFields, | ||
| bootstrapRecords, previousRecord, bindPort, bindIp, enrAutoUpdate, banNodes, | ||
| config, rng | ||
| ) | ||
|
|
||
| proc newProtocol*( | ||
| privKey: PrivateKey, | ||
| enrIp: Opt[IpAddress], | ||
| enrTcpPort, enrUdpPort, enrQuicPort: Opt[Port], | ||
| localEnrFields: openArray[(string, seq[byte])] = [], | ||
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
| bindPort: Port, | ||
| bindIp: Opt[IpAddress], | ||
| enrAutoUpdate = false, | ||
| config = defaultDiscoveryConfig, | ||
|
|
@@ -1159,15 +1199,15 @@ proc newProtocol*( | |
| var res = previousRecord.get() | ||
| # TODO: this is faulty in case the intent is to remove a field with | ||
| # opt.none | ||
| res.update(privKey, enrIp, enrTcpPort, enrUdpPort, | ||
| res.update(privKey, enrIp, enrTcpPort, enrUdpPort, enrQuicPort, | ||
| customEnrFields).expect("Record within size limits and correct key") | ||
| res | ||
| else: | ||
| enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort, | ||
| enr.Record.init(1, privKey, enrIp, enrTcpPort, enrUdpPort, enrQuicPort, | ||
| customEnrFields).expect("Record within size limits") | ||
|
|
||
| info "Discovery ENR initialized", enrAutoUpdate, seqNum = record.seqNum, | ||
| ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort, | ||
| ip = enrIp, tcpPort = enrTcpPort, udpPort = enrUdpPort, quicPort = enrQuicPort, | ||
| customEnrFields, uri = toURI(record) | ||
|
|
||
| if enrIp.isNone(): | ||
|
|
@@ -1198,6 +1238,24 @@ proc newProtocol*( | |
| responseTimeout: config.responseTimeout, | ||
| rng: rng) | ||
|
|
||
| proc newProtocol*( | ||
| privKey: PrivateKey, | ||
| enrIp: Opt[IpAddress], | ||
| enrTcpPort, enrUdpPort: Opt[Port], | ||
| localEnrFields: openArray[(string, seq[byte])] = [], | ||
| bootstrapRecords: openArray[Record] = [], | ||
| previousRecord = Opt.none(enr.Record), | ||
| bindPort: Port, | ||
| bindIp: Opt[IpAddress], | ||
| enrAutoUpdate = false, | ||
| config = defaultDiscoveryConfig, | ||
| rng = newRng()): Protocol = | ||
| newProtocol( | ||
| privKey, enrIp, enrTcpPort, enrUdpPort, Opt.none(Port), localEnrFields, | ||
| bootstrapRecords, previousRecord, bindPort, bindIp, enrAutoUpdate, | ||
| config, rng | ||
| ) | ||
|
|
||
| proc `$`*(a: OptAddress): string = | ||
| if a.ip.isNone(): | ||
| "*:" & $a.port | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way of adding a parameter does have the potential of breaking current usages. E.g. when
extraFieldsis passed as parameter but not specifically set withextraFields = ....But perhaps fine as we control most/all of its usage? Not sure.