wip sync fixing
This commit is contained in:
parent
c36b4626a9
commit
2b8c06bda7
5 changed files with 84 additions and 13 deletions
|
@ -4,10 +4,45 @@ from torba.testcase import IntegrationTestCase
|
||||||
from torba.client.constants import COIN
|
from torba.client.constants import COIN
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
log.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
class BasicTransactionTests(IntegrationTestCase):
|
class BasicTransactionTests(IntegrationTestCase):
|
||||||
|
|
||||||
VERBOSITY = logging.WARN
|
VERBOSITY = logging.WARN
|
||||||
|
|
||||||
|
async def test_stressing(self):
|
||||||
|
await self.blockchain.generate(1000)
|
||||||
|
await self.assertBalance(self.account, '0.0')
|
||||||
|
address1 = await self.account.receiving.get_or_create_usable_address()
|
||||||
|
hash1 = self.ledger.address_to_hash160(address1)
|
||||||
|
|
||||||
|
tasks = []
|
||||||
|
for _ in range(10):
|
||||||
|
sendtxid = await self.blockchain.send_to_address(address1, 100)
|
||||||
|
tasks.append(self.on_transaction_id(sendtxid))
|
||||||
|
await asyncio.wait(tasks)
|
||||||
|
await self.assertBalance(self.account, '1000.0')
|
||||||
|
|
||||||
|
tasks = []
|
||||||
|
for _ in range(10):
|
||||||
|
tx = await self.ledger.transaction_class.create(
|
||||||
|
[],
|
||||||
|
[self.ledger.transaction_class.output_class.pay_pubkey_hash(1*COIN, hash1)],
|
||||||
|
[self.account], self.account
|
||||||
|
)
|
||||||
|
await self.broadcast(tx)
|
||||||
|
tasks.append(asyncio.create_task(self.ledger.wait(tx)))
|
||||||
|
|
||||||
|
await asyncio.wait(tasks)
|
||||||
|
|
||||||
|
#await asyncio.sleep(5)
|
||||||
|
|
||||||
|
await self.assertBalance(self.account, '1000.0')
|
||||||
|
|
||||||
|
await self.blockchain.generate(1)
|
||||||
|
|
||||||
async def test_sending_and_receiving(self):
|
async def test_sending_and_receiving(self):
|
||||||
account1, account2 = self.account, self.wallet.generate_account(self.ledger)
|
account1, account2 = self.account, self.wallet.generate_account(self.ledger)
|
||||||
await self.ledger.subscribe_account(account2)
|
await self.ledger.subscribe_account(account2)
|
||||||
|
|
|
@ -500,7 +500,7 @@ class BaseLedger(metaclass=LedgerRegistry):
|
||||||
def broadcast(self, tx):
|
def broadcast(self, tx):
|
||||||
return self.network.broadcast(hexlify(tx.raw).decode())
|
return self.network.broadcast(hexlify(tx.raw).decode())
|
||||||
|
|
||||||
async def wait(self, tx: basetransaction.BaseTransaction, height=0):
|
async def wait(self, tx: basetransaction.BaseTransaction, height=-1):
|
||||||
addresses = set()
|
addresses = set()
|
||||||
for txi in tx.inputs:
|
for txi in tx.inputs:
|
||||||
if txi.txo_ref.txo is not None:
|
if txi.txo_ref.txo is not None:
|
||||||
|
|
|
@ -247,7 +247,7 @@ class BaseTransaction:
|
||||||
output_class = BaseOutput
|
output_class = BaseOutput
|
||||||
|
|
||||||
def __init__(self, raw=None, version: int = 1, locktime: int = 0, is_verified: bool = False,
|
def __init__(self, raw=None, version: int = 1, locktime: int = 0, is_verified: bool = False,
|
||||||
height: int = -1, position: int = -1) -> None:
|
height: int = -2, position: int = -1) -> None:
|
||||||
self._raw = raw
|
self._raw = raw
|
||||||
self.ref = TXRefMutable(self)
|
self.ref = TXRefMutable(self)
|
||||||
self.version = version
|
self.version = version
|
||||||
|
@ -255,11 +255,28 @@ class BaseTransaction:
|
||||||
self._inputs: List[BaseInput] = []
|
self._inputs: List[BaseInput] = []
|
||||||
self._outputs: List[BaseOutput] = []
|
self._outputs: List[BaseOutput] = []
|
||||||
self.is_verified = is_verified
|
self.is_verified = is_verified
|
||||||
|
# Height Progression
|
||||||
|
# -2: not broadcast
|
||||||
|
# -1: in mempool but has unconfirmed inputs
|
||||||
|
# 0: in mempool and all inputs confirmed
|
||||||
|
# +num: confirmed in a specific block (height)
|
||||||
self.height = height
|
self.height = height
|
||||||
self.position = position
|
self.position = position
|
||||||
if raw is not None:
|
if raw is not None:
|
||||||
self._deserialize()
|
self._deserialize()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_broadcast(self):
|
||||||
|
return self.height > -2
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_mempool(self):
|
||||||
|
return self.height in (-1, 0)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_confirmed(self):
|
||||||
|
return self.height > 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
return self.ref.id
|
return self.ref.id
|
||||||
|
|
|
@ -49,6 +49,7 @@ def get_blockchain_node_from_ledger(ledger_module):
|
||||||
|
|
||||||
def set_logging(ledger_module, level):
|
def set_logging(ledger_module, level):
|
||||||
logging.getLogger('torba').setLevel(level)
|
logging.getLogger('torba').setLevel(level)
|
||||||
|
logging.getLogger('torba.client').setLevel(logging.INFO)
|
||||||
logging.getLogger('torba.server').setLevel(level)
|
logging.getLogger('torba.server').setLevel(level)
|
||||||
#logging.getLogger('asyncio').setLevel(level)
|
#logging.getLogger('asyncio').setLevel(level)
|
||||||
logging.getLogger('blockchain').setLevel(level)
|
logging.getLogger('blockchain').setLevel(level)
|
||||||
|
@ -213,8 +214,6 @@ class BlockchainProcess(asyncio.SubprocessProtocol):
|
||||||
raise SystemError(data.decode('ascii'))
|
raise SystemError(data.decode('ascii'))
|
||||||
elif b'Done loading' in data:
|
elif b'Done loading' in data:
|
||||||
self.ready.set()
|
self.ready.set()
|
||||||
elif b'Shutdown: done' in data:
|
|
||||||
self.stopped.set()
|
|
||||||
|
|
||||||
def process_exited(self):
|
def process_exited(self):
|
||||||
self.stopped.set()
|
self.stopped.set()
|
||||||
|
@ -298,6 +297,7 @@ class BlockchainNode:
|
||||||
try:
|
try:
|
||||||
self.transport.terminate()
|
self.transport.terminate()
|
||||||
await self.protocol.stopped.wait()
|
await self.protocol.stopped.wait()
|
||||||
|
self.transport.close()
|
||||||
finally:
|
finally:
|
||||||
if cleanup:
|
if cleanup:
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import unittest
|
import unittest
|
||||||
|
import asyncio
|
||||||
|
from asyncio.runners import _cancel_all_tasks # type: ignore
|
||||||
from unittest.case import _Outcome
|
from unittest.case import _Outcome
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from torba.orchstr8 import Conductor
|
from torba.orchstr8 import Conductor
|
||||||
|
@ -12,17 +14,34 @@ from torba.client.wallet import Wallet
|
||||||
from torba.client.util import satoshis_to_coins
|
from torba.client.util import satoshis_to_coins
|
||||||
|
|
||||||
|
|
||||||
try:
|
class ColorHandler(logging.StreamHandler):
|
||||||
import asyncio
|
|
||||||
from asyncio.runners import _cancel_all_tasks # type: ignore
|
|
||||||
except ImportError:
|
|
||||||
import asyncio
|
|
||||||
|
|
||||||
# this is only available in py3.7
|
level_color = {
|
||||||
def _cancel_all_tasks(loop):
|
logging.DEBUG: "black",
|
||||||
pass
|
logging.INFO: "black",
|
||||||
|
logging.WARNING: "yellow",
|
||||||
|
logging.ERROR: "red"
|
||||||
|
}
|
||||||
|
|
||||||
HANDLER = logging.StreamHandler(sys.stdout)
|
color_code = dict(
|
||||||
|
black=30, red=31, green=32, yellow=33,
|
||||||
|
blue=34, magenta=35, cyan=36, white=37
|
||||||
|
)
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
try:
|
||||||
|
msg = self.format(record)
|
||||||
|
color_name = self.level_color.get(record.levelno, "black")
|
||||||
|
color_code = self.color_code[color_name]
|
||||||
|
stream = self.stream
|
||||||
|
stream.write('\x1b[%sm%s\x1b[0m' % (color_code, msg))
|
||||||
|
stream.write(self.terminator)
|
||||||
|
self.flush()
|
||||||
|
except Exception:
|
||||||
|
self.handleError(record)
|
||||||
|
|
||||||
|
|
||||||
|
HANDLER = ColorHandler(sys.stdout)
|
||||||
HANDLER.setFormatter(
|
HANDLER.setFormatter(
|
||||||
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue