2024-04-25 13:09:52 +00:00
|
|
|
## Waku Store protocol for historical messaging support.
|
|
|
|
## See spec for more details:
|
|
|
|
## https://github.com/vacp2p/specs/blob/master/specs/waku/v2/waku-store.md
|
2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
import
|
2024-09-20 11:23:53 +00:00
|
|
|
std/[options, times],
|
2024-07-09 11:14:28 +00:00
|
|
|
results,
|
2024-04-25 13:09:52 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
|
|
|
bearssl/rand,
|
|
|
|
libp2p/crypto/crypto,
|
|
|
|
libp2p/protocols/protocol,
|
|
|
|
libp2p/protobuf/minprotobuf,
|
|
|
|
libp2p/stream/connection,
|
|
|
|
metrics
|
|
|
|
import
|
|
|
|
../waku_core,
|
|
|
|
../node/peer_manager,
|
|
|
|
./common,
|
|
|
|
./rpc,
|
|
|
|
./rpc_codec,
|
|
|
|
./protocol_metrics,
|
2024-07-16 13:46:21 +00:00
|
|
|
../common/rate_limit/request_limiter
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "waku legacy store"
|
|
|
|
|
|
|
|
type HistoryQueryHandler* =
|
|
|
|
proc(req: HistoryQuery): Future[HistoryResult] {.async, gcsafe.}
|
|
|
|
|
|
|
|
type WakuStore* = ref object of LPProtocol
|
|
|
|
peerManager: PeerManager
|
|
|
|
rng: ref rand.HmacDrbgContext
|
|
|
|
queryHandler*: HistoryQueryHandler
|
2024-07-16 13:46:21 +00:00
|
|
|
requestRateLimiter*: RequestRateLimiter
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
## Protocol
|
|
|
|
|
2024-10-10 09:57:57 +00:00
|
|
|
type StoreResp = tuple[resp: seq[byte], requestId: string]
|
|
|
|
|
2024-05-09 18:07:49 +00:00
|
|
|
proc handleLegacyQueryRequest(
|
|
|
|
self: WakuStore, requestor: PeerId, raw_request: seq[byte]
|
2024-10-10 09:57:57 +00:00
|
|
|
): Future[StoreResp] {.async.} =
|
2024-05-09 18:07:49 +00:00
|
|
|
let decodeRes = HistoryRPC.decode(raw_request)
|
|
|
|
if decodeRes.isErr():
|
2024-10-10 09:57:57 +00:00
|
|
|
error "failed to decode rpc", peerId = requestor, error = $decodeRes.error
|
2024-05-09 18:07:49 +00:00
|
|
|
waku_legacy_store_errors.inc(labelValues = [decodeRpcFailure])
|
2024-10-10 09:57:57 +00:00
|
|
|
return (newSeq[byte](), "failed to decode rpc")
|
2024-05-09 18:07:49 +00:00
|
|
|
|
|
|
|
let reqRpc = decodeRes.value
|
|
|
|
|
|
|
|
if reqRpc.query.isNone():
|
|
|
|
error "empty query rpc", peerId = requestor, requestId = reqRpc.requestId
|
|
|
|
waku_legacy_store_errors.inc(labelValues = [emptyRpcQueryFailure])
|
2024-10-10 09:57:57 +00:00
|
|
|
return (newSeq[byte](), "empty query rpc")
|
2024-05-09 18:07:49 +00:00
|
|
|
|
2024-08-29 20:56:14 +00:00
|
|
|
let requestId = reqRpc.requestId
|
|
|
|
var request = reqRpc.query.get().toAPI()
|
|
|
|
request.requestId = requestId
|
2024-05-09 18:07:49 +00:00
|
|
|
|
|
|
|
info "received history query",
|
|
|
|
peerId = requestor, requestId = requestId, query = request
|
|
|
|
waku_legacy_store_queries.inc()
|
|
|
|
|
|
|
|
var responseRes: HistoryResult
|
|
|
|
try:
|
|
|
|
responseRes = await self.queryHandler(request)
|
|
|
|
except Exception:
|
|
|
|
error "history query failed",
|
|
|
|
peerId = requestor, requestId = requestId, error = getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
let error = HistoryError(kind: HistoryErrorKind.UNKNOWN).toRPC()
|
|
|
|
let response = HistoryResponseRPC(error: error)
|
2024-10-10 09:57:57 +00:00
|
|
|
return (
|
|
|
|
HistoryRPC(requestId: requestId, response: some(response)).encode().buffer,
|
|
|
|
requestId,
|
|
|
|
)
|
2024-05-09 18:07:49 +00:00
|
|
|
|
|
|
|
if responseRes.isErr():
|
|
|
|
error "history query failed",
|
|
|
|
peerId = requestor, requestId = requestId, error = responseRes.error
|
2024-04-25 13:09:52 +00:00
|
|
|
|
2024-05-09 18:07:49 +00:00
|
|
|
let response = responseRes.toRPC()
|
2024-10-10 09:57:57 +00:00
|
|
|
return (
|
|
|
|
HistoryRPC(requestId: requestId, response: some(response)).encode().buffer,
|
|
|
|
requestId,
|
|
|
|
)
|
2024-04-25 13:09:52 +00:00
|
|
|
|
2024-05-09 18:07:49 +00:00
|
|
|
let response = responseRes.toRPC()
|
2024-04-25 13:09:52 +00:00
|
|
|
|
2024-05-09 18:07:49 +00:00
|
|
|
info "sending history response",
|
|
|
|
peerId = requestor, requestId = requestId, messages = response.messages.len
|
2024-04-25 13:09:52 +00:00
|
|
|
|
2024-10-10 09:57:57 +00:00
|
|
|
return (
|
|
|
|
HistoryRPC(requestId: requestId, response: some(response)).encode().buffer,
|
|
|
|
requestId,
|
|
|
|
)
|
2024-04-25 13:09:52 +00:00
|
|
|
|
2024-05-09 18:07:49 +00:00
|
|
|
proc initProtocolHandler(ws: WakuStore) =
|
|
|
|
let rejectResponseBuf = HistoryRPC(
|
|
|
|
## We will not copy and decode RPC buffer from stream only for requestId
|
|
|
|
## in reject case as it is comparably too expensive and opens possible
|
|
|
|
## attack surface
|
|
|
|
requestId: "N/A",
|
|
|
|
response: some(
|
|
|
|
HistoryResponseRPC(
|
|
|
|
error: HistoryError(kind: HistoryErrorKind.TOO_MANY_REQUESTS).toRPC()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
).encode().buffer
|
|
|
|
|
|
|
|
proc handler(conn: Connection, proto: string) {.async, closure.} =
|
2024-09-20 11:23:53 +00:00
|
|
|
var successfulQuery = false ## only consider the correct queries in metrics
|
2024-10-10 09:57:57 +00:00
|
|
|
var resBuf: StoreResp
|
|
|
|
var queryDuration: float
|
2024-05-09 18:07:49 +00:00
|
|
|
ws.requestRateLimiter.checkUsageLimit(WakuLegacyStoreCodec, conn):
|
|
|
|
let readRes = catch:
|
|
|
|
await conn.readLp(DefaultMaxRpcSize.int)
|
|
|
|
|
|
|
|
let reqBuf = readRes.valueOr:
|
|
|
|
error "Connection read error", error = error.msg
|
|
|
|
return
|
|
|
|
|
2024-06-28 00:48:29 +00:00
|
|
|
waku_service_network_bytes.inc(
|
|
|
|
amount = reqBuf.len().int64, labelValues = [WakuLegacyStoreCodec, "in"]
|
2024-05-09 18:07:49 +00:00
|
|
|
)
|
|
|
|
|
2024-09-20 11:23:53 +00:00
|
|
|
let queryStartTime = getTime().toUnixFloat()
|
2024-05-09 18:07:49 +00:00
|
|
|
resBuf = await ws.handleLegacyQueryRequest(conn.peerId, reqBuf)
|
2024-10-10 09:57:57 +00:00
|
|
|
queryDuration = getTime().toUnixFloat() - queryStartTime
|
2024-09-20 11:23:53 +00:00
|
|
|
waku_legacy_store_time_seconds.set(queryDuration, ["query-db-time"])
|
|
|
|
successfulQuery = true
|
2024-05-09 18:07:49 +00:00
|
|
|
do:
|
|
|
|
debug "Legacy store query request rejected due rate limit exceeded",
|
2024-07-16 13:46:21 +00:00
|
|
|
peerId = conn.peerId, limit = $ws.requestRateLimiter.setting
|
2024-10-10 09:57:57 +00:00
|
|
|
resBuf = (rejectResponseBuf, "rejected")
|
2024-05-09 18:07:49 +00:00
|
|
|
|
2024-09-20 11:23:53 +00:00
|
|
|
let writeRespStartTime = getTime().toUnixFloat()
|
2024-05-09 18:07:49 +00:00
|
|
|
let writeRes = catch:
|
2024-10-10 09:57:57 +00:00
|
|
|
await conn.writeLp(resBuf.resp)
|
2024-05-09 18:07:49 +00:00
|
|
|
|
|
|
|
if writeRes.isErr():
|
|
|
|
error "Connection write error", error = writeRes.error.msg
|
2024-04-25 13:09:52 +00:00
|
|
|
return
|
|
|
|
|
2024-09-20 11:23:53 +00:00
|
|
|
if successfulQuery:
|
|
|
|
let writeDuration = getTime().toUnixFloat() - writeRespStartTime
|
|
|
|
waku_legacy_store_time_seconds.set(writeDuration, ["send-store-resp-time"])
|
2024-10-10 09:57:57 +00:00
|
|
|
debug "after sending response",
|
|
|
|
requestId = resBuf.requestId,
|
|
|
|
queryDurationSecs = queryDuration,
|
|
|
|
writeStreamDurationSecs = writeDuration
|
2024-09-20 11:23:53 +00:00
|
|
|
|
2024-06-28 00:48:29 +00:00
|
|
|
waku_service_network_bytes.inc(
|
2024-10-10 09:57:57 +00:00
|
|
|
amount = resBuf.resp.len().int64, labelValues = [WakuLegacyStoreCodec, "out"]
|
2024-05-09 18:07:49 +00:00
|
|
|
)
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
ws.handler = handler
|
2024-05-09 18:07:49 +00:00
|
|
|
ws.codec = WakuLegacyStoreCodec
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type WakuStore,
|
|
|
|
peerManager: PeerManager,
|
|
|
|
rng: ref rand.HmacDrbgContext,
|
|
|
|
queryHandler: HistoryQueryHandler,
|
|
|
|
rateLimitSetting: Option[RateLimitSetting] = none[RateLimitSetting](),
|
|
|
|
): T =
|
|
|
|
# Raise a defect if history query handler is nil
|
|
|
|
if queryHandler.isNil():
|
|
|
|
raise newException(NilAccessDefect, "history query handler is nil")
|
|
|
|
|
|
|
|
let ws = WakuStore(
|
|
|
|
rng: rng,
|
|
|
|
peerManager: peerManager,
|
|
|
|
queryHandler: queryHandler,
|
2024-07-16 13:46:21 +00:00
|
|
|
requestRateLimiter: newRequestRateLimiter(rateLimitSetting),
|
2024-04-25 13:09:52 +00:00
|
|
|
)
|
|
|
|
ws.initProtocolHandler()
|
2024-09-18 13:58:07 +00:00
|
|
|
setServiceLimitMetric(WakuLegacyStoreCodec, rateLimitSetting)
|
2024-04-25 13:09:52 +00:00
|
|
|
ws
|