2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
import
|
2024-05-16 20:29:11 +00:00
|
|
|
std/[options, bitops, sequtils, net],
|
2024-07-09 11:14:28 +00:00
|
|
|
stew/endians2,
|
|
|
|
results,
|
2023-04-17 13:21:20 +00:00
|
|
|
chronicles,
|
|
|
|
eth/keys,
|
|
|
|
libp2p/[multiaddress, multicodec],
|
|
|
|
libp2p/crypto/crypto
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../common/enr, ../waku_core
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "waku enr sharding"
|
|
|
|
|
2024-09-10 21:07:12 +00:00
|
|
|
const MaxShardIndex*: uint16 = 1023
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
ShardingIndicesListEnrField* = "rs"
|
2023-08-23 13:53:17 +00:00
|
|
|
ShardingIndicesListMaxLength* = 64
|
2023-04-17 13:21:20 +00:00
|
|
|
ShardingBitVectorEnrField* = "rsv"
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
type RelayShards* = object
|
|
|
|
clusterId*: uint16
|
|
|
|
shardIds*: seq[uint16]
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-08-19 09:29:35 +00:00
|
|
|
func topics*(rs: RelayShards): seq[RelayShard] =
|
2024-09-10 21:07:12 +00:00
|
|
|
rs.shardIds.mapIt(RelayShard(clusterId: rs.clusterId, shardId: it))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
func init*(T: type RelayShards, clusterId, shardId: uint16): Result[T, string] =
|
|
|
|
if shardId > MaxShardIndex:
|
|
|
|
return err("invalid shard Id")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
ok(RelayShards(clusterId: clusterId, shardIds: @[shardId]))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func init*(
|
|
|
|
T: type RelayShards, clusterId: uint16, shardIds: varargs[uint16]
|
|
|
|
): Result[T, string] =
|
2023-10-05 12:37:05 +00:00
|
|
|
if toSeq(shardIds).anyIt(it > MaxShardIndex):
|
|
|
|
return err("invalid shard")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let indicesSeq = deduplicate(@shardIds)
|
|
|
|
if shardIds.len < 1:
|
|
|
|
return err("invalid shard count")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
ok(RelayShards(clusterId: clusterId, shardIds: indicesSeq))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func init*(
|
|
|
|
T: type RelayShards, clusterId: uint16, shardIds: seq[uint16]
|
|
|
|
): Result[T, string] =
|
2023-10-05 12:37:05 +00:00
|
|
|
if shardIds.anyIt(it > MaxShardIndex):
|
|
|
|
return err("invalid shard")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let indicesSeq = deduplicate(shardIds)
|
|
|
|
if shardIds.len < 1:
|
|
|
|
return err("invalid shard count")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
ok(RelayShards(clusterId: clusterId, shardIds: indicesSeq))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-06-22 20:58:14 +00:00
|
|
|
func topicsToRelayShards*(topics: seq[string]): Result[Option[RelayShards], string] =
|
|
|
|
if topics.len < 1:
|
|
|
|
return ok(none(RelayShards))
|
|
|
|
|
2024-08-19 09:29:35 +00:00
|
|
|
let parsedTopicsRes = topics.mapIt(RelayShard.parse(it))
|
2023-06-22 20:58:14 +00:00
|
|
|
|
|
|
|
for res in parsedTopicsRes:
|
|
|
|
if res.isErr():
|
|
|
|
return err("failed to parse topic: " & $res.error)
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
if parsedTopicsRes.anyIt(it.get().clusterId != parsedTopicsRes[0].get().clusterId):
|
|
|
|
return err("use shards with the same cluster Id.")
|
2023-06-22 20:58:14 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let relayShard =
|
|
|
|
?RelayShards.init(
|
|
|
|
parsedTopicsRes[0].get().clusterId, parsedTopicsRes.mapIt(it.get().shardId)
|
|
|
|
)
|
2023-08-23 13:53:17 +00:00
|
|
|
|
|
|
|
return ok(some(relayShard))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
func contains*(rs: RelayShards, clusterId, shardId: uint16): bool =
|
2024-07-09 15:36:12 +00:00
|
|
|
return rs.clusterId == clusterId and rs.shardIds.contains(shardId)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-08-19 09:29:35 +00:00
|
|
|
func contains*(rs: RelayShards, shard: RelayShard): bool =
|
|
|
|
return rs.contains(shard.clusterId, shard.shardId)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-07-09 15:36:12 +00:00
|
|
|
func contains*(rs: RelayShards, topic: PubsubTopic): bool =
|
2024-08-19 09:29:35 +00:00
|
|
|
let parseRes = RelayShard.parse(topic)
|
2023-04-17 13:21:20 +00:00
|
|
|
if parseRes.isErr():
|
|
|
|
return false
|
|
|
|
|
|
|
|
rs.contains(parseRes.value)
|
|
|
|
|
|
|
|
# ENR builder extension
|
|
|
|
|
2023-08-23 13:53:17 +00:00
|
|
|
func toIndicesList*(rs: RelayShards): EnrResult[seq[byte]] =
|
2023-10-05 12:37:05 +00:00
|
|
|
if rs.shardIds.len > high(uint8).int:
|
|
|
|
return err("shards list too long")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
var res: seq[byte]
|
2023-10-05 12:37:05 +00:00
|
|
|
res.add(rs.clusterId.toBytesBE())
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
res.add(rs.shardIds.len.uint8)
|
|
|
|
for shardId in rs.shardIds:
|
|
|
|
res.add(shardId.toBytesBE())
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
ok(res)
|
|
|
|
|
2024-03-14 18:01:13 +00:00
|
|
|
func fromIndicesList*(buf: seq[byte]): Result[RelayShards, string] =
|
2023-04-17 13:21:20 +00:00
|
|
|
if buf.len < 3:
|
2024-03-15 23:08:47 +00:00
|
|
|
return
|
|
|
|
err("insufficient data: expected at least 3 bytes, got " & $buf.len & " bytes")
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let clusterId = uint16.fromBytesBE(buf[0 .. 1])
|
2023-04-17 13:21:20 +00:00
|
|
|
let length = int(buf[2])
|
|
|
|
|
|
|
|
if buf.len != 3 + 2 * length:
|
2024-03-15 23:08:47 +00:00
|
|
|
return err(
|
|
|
|
"invalid data: `length` field is " & $length & " but " & $buf.len &
|
|
|
|
" bytes were provided"
|
|
|
|
)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
var shardIds: seq[uint16]
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< length:
|
|
|
|
shardIds.add(uint16.fromBytesBE(buf[3 + 2 * i ..< 5 + 2 * i]))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
ok(RelayShards(clusterId: clusterId, shardIds: shardIds))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-08-23 13:53:17 +00:00
|
|
|
func toBitVector*(rs: RelayShards): seq[byte] =
|
2023-10-05 12:37:05 +00:00
|
|
|
## The value is comprised of a two-byte cluster id in network byte
|
2023-04-17 13:21:20 +00:00
|
|
|
## order concatenated with a 128-byte wide bit vector. The bit vector
|
2023-10-05 12:37:05 +00:00
|
|
|
## indicates which shard ids of the respective cluster id the node is part
|
|
|
|
## of. The right-most bit in the bit vector represents shard id 0, the left-most
|
|
|
|
## bit represents shard id 1023.
|
2023-04-17 13:21:20 +00:00
|
|
|
var res: seq[byte]
|
2023-10-05 12:37:05 +00:00
|
|
|
res.add(rs.clusterId.toBytesBE())
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
var vec = newSeq[byte](128)
|
2023-10-05 12:37:05 +00:00
|
|
|
for shardId in rs.shardIds:
|
|
|
|
vec[shardId div 8].setBit(shardId mod 8)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
res.add(vec)
|
|
|
|
|
|
|
|
res
|
|
|
|
|
|
|
|
func fromBitVector(buf: seq[byte]): EnrResult[RelayShards] =
|
|
|
|
if buf.len != 130:
|
|
|
|
return err("invalid data: expected 130 bytes")
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let clusterId = uint16.fromBytesBE(buf[0 .. 1])
|
2023-10-05 12:37:05 +00:00
|
|
|
var shardIds: seq[uint16]
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0u16 ..< 128u16:
|
|
|
|
for j in 0u16 ..< 8u16:
|
2023-04-17 13:21:20 +00:00
|
|
|
if not buf[2 + i].testBit(j):
|
|
|
|
continue
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
shardIds.add(j + 8 * i)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
ok(RelayShards(clusterId: clusterId, shardIds: shardIds))
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func withWakuRelayShardingIndicesList*(
|
|
|
|
builder: var EnrBuilder, rs: RelayShards
|
|
|
|
): EnrResult[void] =
|
|
|
|
let value = ?rs.toIndicesList()
|
2023-04-17 13:21:20 +00:00
|
|
|
builder.addFieldPair(ShardingIndicesListEnrField, value)
|
|
|
|
ok()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func withWakuRelayShardingBitVector*(
|
|
|
|
builder: var EnrBuilder, rs: RelayShards
|
|
|
|
): EnrResult[void] =
|
2023-04-17 13:21:20 +00:00
|
|
|
let value = rs.toBitVector()
|
|
|
|
builder.addFieldPair(ShardingBitVectorEnrField, value)
|
|
|
|
ok()
|
|
|
|
|
|
|
|
func withWakuRelaySharding*(builder: var EnrBuilder, rs: RelayShards): EnrResult[void] =
|
2023-10-05 12:37:05 +00:00
|
|
|
if rs.shardIds.len >= ShardingIndicesListMaxLength:
|
2023-04-17 13:21:20 +00:00
|
|
|
builder.withWakuRelayShardingBitVector(rs)
|
|
|
|
else:
|
|
|
|
builder.withWakuRelayShardingIndicesList(rs)
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func withShardedTopics*(
|
|
|
|
builder: var EnrBuilder, topics: seq[string]
|
|
|
|
): Result[void, string] =
|
2023-10-05 12:37:05 +00:00
|
|
|
let relayShardOp = topicsToRelayShards(topics).valueOr:
|
|
|
|
return err("building ENR with relay sharding failed: " & $error)
|
2023-06-29 19:59:53 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let relayShard = relayShardOp.valueOr:
|
|
|
|
return ok()
|
2023-06-29 19:59:53 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
builder.withWakuRelaySharding(relayShard).isOkOr:
|
|
|
|
return err($error)
|
2023-06-29 19:59:53 +00:00
|
|
|
|
|
|
|
return ok()
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
# ENR record accessors (e.g., Record, TypedRecord, etc.)
|
|
|
|
|
|
|
|
proc relayShardingIndicesList*(record: TypedRecord): Option[RelayShards] =
|
2023-10-05 12:37:05 +00:00
|
|
|
let field = record.tryGet(ShardingIndicesListEnrField, seq[byte]).valueOr:
|
2023-04-17 13:21:20 +00:00
|
|
|
return none(RelayShards)
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let indexList = fromIndicesList(field).valueOr:
|
|
|
|
debug "invalid shards list", error = error
|
2023-04-17 13:21:20 +00:00
|
|
|
return none(RelayShards)
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
some(indexList)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
proc relayShardingBitVector*(record: TypedRecord): Option[RelayShards] =
|
2023-10-05 12:37:05 +00:00
|
|
|
let field = record.tryGet(ShardingBitVectorEnrField, seq[byte]).valueOr:
|
2023-04-17 13:21:20 +00:00
|
|
|
return none(RelayShards)
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let bitVector = fromBitVector(field).valueOr:
|
|
|
|
debug "invalid shards bit vector", error = error
|
2023-04-17 13:21:20 +00:00
|
|
|
return none(RelayShards)
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
some(bitVector)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
proc relaySharding*(record: TypedRecord): Option[RelayShards] =
|
2023-10-05 12:37:05 +00:00
|
|
|
let indexList = record.relayShardingIndicesList().valueOr:
|
|
|
|
return record.relayShardingBitVector()
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
return some(indexList)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
## Utils
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
proc containsShard*(r: Record, clusterId, shardId: uint16): bool =
|
|
|
|
if shardId > MaxShardIndex:
|
2023-04-17 13:21:20 +00:00
|
|
|
return false
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let record = r.toTyped().valueOr:
|
2024-09-20 11:22:10 +00:00
|
|
|
trace "invalid ENR record", error = error
|
2023-04-17 13:21:20 +00:00
|
|
|
return false
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
let rs = record.relaySharding().valueOr:
|
2023-04-17 13:21:20 +00:00
|
|
|
return false
|
|
|
|
|
2023-10-05 12:37:05 +00:00
|
|
|
rs.contains(clusterId, shardId)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-08-19 09:29:35 +00:00
|
|
|
proc containsShard*(r: Record, shard: RelayShard): bool =
|
|
|
|
return containsShard(r, shard.clusterId, shard.shardId)
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-07-09 15:36:12 +00:00
|
|
|
proc containsShard*(r: Record, topic: PubsubTopic): bool =
|
2024-08-19 09:29:35 +00:00
|
|
|
let parseRes = RelayShard.parse(topic)
|
2023-04-17 13:21:20 +00:00
|
|
|
if parseRes.isErr():
|
|
|
|
debug "invalid static sharding topic", topic = topic, error = parseRes.error
|
|
|
|
return false
|
|
|
|
|
|
|
|
containsShard(r, parseRes.value)
|
2024-01-30 12:15:23 +00:00
|
|
|
|
2024-06-05 13:32:35 +00:00
|
|
|
proc isClusterMismatched*(record: Record, clusterId: uint16): bool =
|
2024-01-30 12:15:23 +00:00
|
|
|
## Check the ENR sharding info for matching cluster id
|
|
|
|
if (let typedRecord = record.toTyped(); typedRecord.isOk()):
|
|
|
|
if (let relayShard = typedRecord.get().relaySharding(); relayShard.isSome()):
|
|
|
|
return relayShard.get().clusterId != clusterId
|
2024-03-15 23:08:47 +00:00
|
|
|
|
|
|
|
return false
|