increase nonce size to 32 byte and make it hex in event
This commit is contained in:
@@ -1413,7 +1413,7 @@ class NostrTransport(Logger):
|
|||||||
'min_amount': sm._min_amount,
|
'min_amount': sm._min_amount,
|
||||||
'max_amount': sm._max_amount,
|
'max_amount': sm._max_amount,
|
||||||
'relays': sm.config.NOSTR_RELAYS,
|
'relays': sm.config.NOSTR_RELAYS,
|
||||||
'pow_nonce': str(sm.config.SWAPSERVER_ANN_POW_NONCE),
|
'pow_nonce': hex(sm.config.SWAPSERVER_ANN_POW_NONCE),
|
||||||
}
|
}
|
||||||
# the first value of a single letter tag is indexed and can be filtered for
|
# the first value of a single letter tag is indexed and can be filtered for
|
||||||
tags = [['d', f'electrum-swapserver-{self.NOSTR_EVENT_VERSION}'],
|
tags = [['d', f'electrum-swapserver-{self.NOSTR_EVENT_VERSION}'],
|
||||||
@@ -1470,7 +1470,13 @@ class NostrTransport(Logger):
|
|||||||
if event.created_at <= ts:
|
if event.created_at <= ts:
|
||||||
#print('skipping old event', pubkey[0:10], event.id)
|
#print('skipping old event', pubkey[0:10], event.id)
|
||||||
continue
|
continue
|
||||||
pow_bits = get_nostr_ann_pow_amount(bytes.fromhex(pubkey), int(content.get('pow_nonce', 0)))
|
try:
|
||||||
|
pow_bits = get_nostr_ann_pow_amount(
|
||||||
|
bytes.fromhex(pubkey),
|
||||||
|
int(content.get('pow_nonce', "0"), 16)
|
||||||
|
)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
if pow_bits < self.config.SWAPSERVER_POW_TARGET:
|
if pow_bits < self.config.SWAPSERVER_POW_TARGET:
|
||||||
self.logger.debug(f"too low pow: {pubkey}: pow: {pow_bits} nonce: {content.get('pow_nonce', 0)}")
|
self.logger.debug(f"too low pow: {pubkey}: pow: {pow_bits} nonce: {content.get('pow_nonce', 0)}")
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -2127,7 +2127,7 @@ def nostr_pow_worker(nonce, nostr_pubk, target_bits, hash_function, hash_len_bit
|
|||||||
# we cannot check is_set on each iteration as it has a lot of overhead, this way we can check
|
# we cannot check is_set on each iteration as it has a lot of overhead, this way we can check
|
||||||
# it with low overhead (just the additional range counter)
|
# it with low overhead (just the additional range counter)
|
||||||
for i in range(1000000):
|
for i in range(1000000):
|
||||||
digest = hash_function(hash_preimage + nonce.to_bytes(8, 'big')).digest()
|
digest = hash_function(hash_preimage + nonce.to_bytes(32, 'big')).digest()
|
||||||
if int.from_bytes(digest, 'big') < (1 << (hash_len_bits - target_bits)):
|
if int.from_bytes(digest, 'big') < (1 << (hash_len_bits - target_bits)):
|
||||||
shutdown.set()
|
shutdown.set()
|
||||||
return hash, nonce
|
return hash, nonce
|
||||||
@@ -2141,7 +2141,7 @@ async def gen_nostr_ann_pow(nostr_pubk: bytes, target_bits: int) -> Tuple[int, i
|
|||||||
import multiprocessing # not available on Android, so we import it here
|
import multiprocessing # not available on Android, so we import it here
|
||||||
hash_function = hashlib.sha256
|
hash_function = hashlib.sha256
|
||||||
hash_len_bits = 256
|
hash_len_bits = 256
|
||||||
max_nonce = 0xFFFFFFFFFFFFFFFF # 8 byte
|
max_nonce: int = (1 << (32 * 8)) - 1 # 32-byte nonce
|
||||||
start_nonce = 0
|
start_nonce = 0
|
||||||
|
|
||||||
max_workers = max(multiprocessing.cpu_count() - 1, 1) # use all but one CPU
|
max_workers = max(multiprocessing.cpu_count() - 1, 1) # use all but one CPU
|
||||||
@@ -2181,6 +2181,6 @@ def get_nostr_ann_pow_amount(nostr_pubk: bytes, nonce: Optional[int]) -> int:
|
|||||||
hash_len_bits = 256
|
hash_len_bits = 256
|
||||||
hash_preimage = b'electrum-' + nostr_pubk
|
hash_preimage = b'electrum-' + nostr_pubk
|
||||||
|
|
||||||
digest = hash_function(hash_preimage + nonce.to_bytes(8, 'big')).digest()
|
digest = hash_function(hash_preimage + nonce.to_bytes(32, 'big')).digest()
|
||||||
digest = int.from_bytes(digest, 'big')
|
digest = int.from_bytes(digest, 'big')
|
||||||
return hash_len_bits - digest.bit_length()
|
return hash_len_bits - digest.bit_length()
|
||||||
|
|||||||
Reference in New Issue
Block a user