2023-11-21 15:16:15 +00:00
|
|
|
# Fluffy
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-11-21 15:16:15 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
import stew/ptrops, stint, sqlite3_abi, eth/db/kvstore_sqlite3
|
2023-11-21 15:16:15 +00:00
|
|
|
|
|
|
|
func xorDistance(a: openArray[byte], b: openArray[byte]): seq[byte] =
|
|
|
|
doAssert(a.len == b.len)
|
|
|
|
|
|
|
|
let length = a.len
|
|
|
|
var distance: seq[byte] = newSeq[byte](length)
|
2024-02-28 17:31:45 +00:00
|
|
|
for i in 0 ..< length:
|
2023-11-21 15:16:15 +00:00
|
|
|
distance[i] = a[i] xor b[i]
|
|
|
|
|
|
|
|
return distance
|
|
|
|
|
|
|
|
proc xorDistance*(
|
2024-02-28 17:31:45 +00:00
|
|
|
ctx: SqliteContext, n: cint, v: SqliteValue
|
|
|
|
) {.cdecl, gcsafe, raises: [].} =
|
2023-11-21 15:16:15 +00:00
|
|
|
doAssert(n == 2)
|
|
|
|
|
|
|
|
let
|
|
|
|
ptrs = makeUncheckedArray(v)
|
|
|
|
blob1Len = sqlite3_value_bytes(ptrs[][0])
|
|
|
|
blob2Len = sqlite3_value_bytes(ptrs[][1])
|
|
|
|
|
|
|
|
bytes = xorDistance(
|
|
|
|
makeOpenArray(sqlite3_value_blob(ptrs[][0]), byte, blob1Len),
|
2024-02-28 17:31:45 +00:00
|
|
|
makeOpenArray(sqlite3_value_blob(ptrs[][1]), byte, blob2Len),
|
2023-11-21 15:16:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
sqlite3_result_blob(ctx, baseAddr bytes, cint bytes.len, SQLITE_TRANSIENT)
|
|
|
|
|
|
|
|
func isInRadius(contentId: UInt256, localId: UInt256, radius: UInt256): bool =
|
|
|
|
let distance = contentId xor localId
|
|
|
|
|
|
|
|
radius > distance
|
|
|
|
|
|
|
|
func isInRadius*(
|
2024-02-28 17:31:45 +00:00
|
|
|
ctx: SqliteContext, n: cint, v: SqliteValue
|
|
|
|
) {.cdecl, gcsafe, raises: [].} =
|
2023-11-21 15:16:15 +00:00
|
|
|
doAssert(n == 3)
|
|
|
|
|
|
|
|
let
|
|
|
|
ptrs = makeUncheckedArray(v)
|
|
|
|
blob1Len = sqlite3_value_bytes(ptrs[][0])
|
|
|
|
blob2Len = sqlite3_value_bytes(ptrs[][1])
|
|
|
|
blob3Len = sqlite3_value_bytes(ptrs[][2])
|
|
|
|
|
|
|
|
doAssert(blob1Len == 32 and blob2Len == 32 and blob3Len == 32)
|
|
|
|
|
|
|
|
let
|
2024-02-28 17:31:45 +00:00
|
|
|
localId =
|
|
|
|
UInt256.fromBytesBE(makeOpenArray(sqlite3_value_blob(ptrs[][0]), byte, blob1Len))
|
|
|
|
contentId =
|
|
|
|
UInt256.fromBytesBE(makeOpenArray(sqlite3_value_blob(ptrs[][1]), byte, blob2Len))
|
|
|
|
radius =
|
|
|
|
UInt256.fromBytesBE(makeOpenArray(sqlite3_value_blob(ptrs[][2]), byte, blob3Len))
|
2023-11-21 15:16:15 +00:00
|
|
|
|
|
|
|
if isInRadius(contentId, localId, radius):
|
|
|
|
ctx.sqlite3_result_int(cint 1)
|
|
|
|
else:
|
|
|
|
ctx.sqlite3_result_int(cint 0)
|