mirror of
https://github.com/basicswap/basicswap.git
synced 2026-06-08 04:01:41 +02:00
Fix: Median time one call.
This commit is contained in:
@@ -504,11 +504,21 @@ class BTCInterface(Secp256k1Interface):
|
|||||||
|
|
||||||
def getChainMedianTime(self) -> int:
|
def getChainMedianTime(self) -> int:
|
||||||
if self.useBackend():
|
if self.useBackend():
|
||||||
height = self._backend.getBlockHeight()
|
import struct
|
||||||
times = []
|
|
||||||
for h in range(max(0, height - 10), height + 1):
|
backend = self.getBackend()
|
||||||
header = self._getBlockHeaderFromHeightElectrum(h)
|
if not backend:
|
||||||
times.append(header["time"])
|
raise ValueError("No electrum backend available")
|
||||||
|
height = backend.getBlockHeight()
|
||||||
|
start = max(0, height - 10)
|
||||||
|
count = height - start + 1
|
||||||
|
result = backend._server.call("blockchain.block.headers", [start, count])
|
||||||
|
header_bytes = bytes.fromhex(result["hex"])
|
||||||
|
returned = result.get("count", count)
|
||||||
|
times = [
|
||||||
|
struct.unpack("<I", header_bytes[i * 80 + 68 : i * 80 + 72])[0]
|
||||||
|
for i in range(returned)
|
||||||
|
]
|
||||||
times.sort()
|
times.sort()
|
||||||
return times[len(times) // 2]
|
return times[len(times) // 2]
|
||||||
return self.rpc("getblockchaininfo")["mediantime"]
|
return self.rpc("getblockchaininfo")["mediantime"]
|
||||||
|
|||||||
Reference in New Issue
Block a user