mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 19:05:22 +00:00
Add NIP-13 PoW for geohash with body-only mining animation\n\n- Implement PoW core and defaults (16/15/14 by precision)\n- Wire PoW into geohash send path; hide timestamp while mining\n- Use combined animated text so long messages wrap under name\n- Enforce receive-side PoW filtering for geohash events\n- Filter geohash peer list by observed PoW bits; track per pubkey\n- Stabilize miner memory (autoreleasepool + sched_yield)\n- Add unit tests for PoW
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import XCTest
|
||||
import CryptoKit
|
||||
@testable import bitchat
|
||||
|
||||
final class NostrPoWTests: XCTestCase {
|
||||
func testLeadingZeroBits() {
|
||||
// 0x00 -> 8, 0x00 -> 8, 0xF0 -> 0 leading zeros
|
||||
let data = Data([0x00, 0x00, 0xF0]) + Data(repeating: 0x00, count: 29)
|
||||
let lz = NostrPoW.leadingZeroBits(data)
|
||||
XCTAssertEqual(lz, 16)
|
||||
}
|
||||
|
||||
func testMineLowDifficulty() {
|
||||
let pubkey = String(repeating: "a", count: 64)
|
||||
let createdAt = Int(Date().timeIntervalSince1970)
|
||||
let kind = 20000
|
||||
let tags = [["g", "u4pruydqqvj"]]
|
||||
let content = "hello"
|
||||
let targetBits = 8 // keep it very low for test speed
|
||||
let (nonce, idHex) = NostrPoW.mine(pubkey: pubkey, createdAt: createdAt, kind: kind, baseTags: tags, content: content, targetBits: targetBits)
|
||||
XCTAssertGreaterThan(nonce, 0)
|
||||
// Verify difficulty
|
||||
let powTags = tags + [["nonce", String(nonce), String(targetBits)]]
|
||||
// Recompute ID
|
||||
let serialized: [Any] = [0, pubkey, createdAt, kind, powTags, content]
|
||||
let json = try! JSONSerialization.data(withJSONObject: serialized, options: [.withoutEscapingSlashes])
|
||||
let digest = CryptoKit.SHA256.hash(data: json)
|
||||
let idData = Data(digest)
|
||||
XCTAssertEqual(idHex.count, 64)
|
||||
XCTAssertGreaterThanOrEqual(NostrPoW.leadingZeroBits(idData), targetBits)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user