Skip to content
Snippets Groups Projects
Commit 8499f73d authored by Vincent Texier's avatar Vincent Texier
Browse files

Merge branch '#798' into dev

parents ffb68a11 a883607d
Branches
Tags
1 merge request!778Release 0.51.0
Showing
with 1421 additions and 535 deletions
pypeg2
aiohttp==3.6.2
async-timeout==3.0.1
asynctest==0.13.0
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -3,6 +3,10 @@ import attr
@attr.s(hash=True)
class Source:
TYPE_TRANSACTION = "T"
TYPE_DIVIDEND = "D"
currency = attr.ib(converter=str)
pubkey = attr.ib(converter=str)
identifier = attr.ib(converter=str)
......@@ -10,3 +14,5 @@ class Source:
type = attr.ib(converter=str, validator=lambda i, a, s: s == "T" or s == "D")
amount = attr.ib(converter=int, hash=False)
base = attr.ib(converter=int, hash=False)
conditions = attr.ib(converter=str, hash=False)
used_by = attr.ib(hash=False, default=None)
......@@ -111,18 +111,21 @@ class Transaction:
Transaction entity
:param str currency: the currency of the transaction
:param str pubkey: the pubkey of the issuer
:param str sha_hash: the hash of the transaction
:param int written_block: the number of the block
:param duniterpy.documents.BlockUID blockstamp: the blockstamp of the transaction
:param int timestamp: the timestamp of the transaction
:param str signature: the signature
:param str issuer: the pubkey of the issuer
:param str receiver: the pubkey of the receiver
:param str signatures: the signature
:param tuple issuers: tuple of pubkey of the issuers
:param tuple receivers: tuple of pubkey of the receivers
:param int amount: the amount
:param int amount_base: the amount base
:param str comment: a comment
:param str txid: the transaction id to sort transctions
:param int txid: the transaction id to sort transctions
:param int state: the state of the transaction
:param bool local: is the transaction local
:param str raw: the raw string of the transaction
"""
TO_SEND = 0
......
import attr
import logging
from ..entities import Dividend
from ..entities import Dividend, Source
from .nodes import NodesProcessor
from ..connectors import BmaConnector
from duniterpy.api import bma
......@@ -82,7 +82,7 @@ class DividendsProcessor:
txdoc = Transaction.from_signed_raw(tx.raw)
for input in txdoc.inputs:
if (
input.source == "D"
input.source == Source.TYPE_DIVIDEND
and input.origin_id == connection.pubkey
and input.index not in block_numbers
and input.index > start
......
......@@ -35,6 +35,9 @@ class SourcesProcessor:
except sqlite3.IntegrityError:
self._logger.debug("Source already known: {0}".format(source.identifier))
def get_one(self, **search):
return self._repo.get_one(**search)
def amount(self, currency, pubkey):
"""
Get the amount value of the sources for a given pubkey
......@@ -53,15 +56,27 @@ class SourcesProcessor:
"""
return self._repo.get_all(currency=currency, pubkey=pubkey)
def consume(self, sources):
def consume(self, sources, tx_sha_hash):
"""
Consume sources in db by setting the used_by column to tx hash
:param List(Source) sources: Source instances list
:param str tx_sha_hash: Hash of tx
:param currency:
:param sources:
:return:
"""
for s in sources:
self._repo.drop(s)
self._repo.consume(s, tx_sha_hash)
def restore_all(self, tx_sha_hash):
"""
Restore sources in db by setting the used_by column to null
:param str tx_sha_hash: Hash of tx
:return:
"""
self._repo.restore_all(tx_sha_hash)
def insert(self, source):
try:
......
......@@ -92,9 +92,12 @@ class TransactionsProcessor:
except sqlite3.IntegrityError:
self._repo.update(tx)
def find_by_hash(self, pubkey, sha_hash):
def find_by_pubkey_and_hash(self, pubkey: str, sha_hash: str) -> Transaction:
return self._repo.get_one(pubkey=pubkey, sha_hash=sha_hash)
def find_one_by_hash(self, sha_hash: str) -> Transaction:
return self._repo.get_one(sha_hash=sha_hash)
def awaiting(self, currency):
return self._repo.get_all(currency=currency, state=Transaction.AWAITING)
......
BEGIN TRANSACTION;
ALTER TABLE sources ADD COLUMN conditions VARCHAR(255);
COMMIT;
BEGIN TRANSACTION;
ALTER TABLE sources ADD COLUMN used_by VARCHAR(255) default null;
COMMIT;
......@@ -7,7 +7,7 @@ from .connections import ConnectionsRepo
from .identities import IdentitiesRepo
from .blockchains import BlockchainsRepo
from .certifications import CertificationsRepo
from .transactions import TransactionsRepo, Transaction
from .transactions import TransactionsRepo
from .dividends import DividendsRepo
from .nodes import NodesRepo, Node
from .sources import SourcesRepo
......@@ -89,6 +89,8 @@ class SakiaDatabase:
self.refactor_transactions,
self.drop_incorrect_nodes,
self.insert_last_mass_attribute,
self.add_sources_conditions_property,
self.add_sources_used_by_property,
]
def upgrade_database(self, to=0):
......@@ -211,6 +213,28 @@ class SakiaDatabase:
with self.conn:
self.conn.executescript(sql_file.read())
def add_sources_conditions_property(self):
self._logger.debug("Add sources conditions property")
sql_file = open(
os.path.join(
os.path.dirname(__file__), "006_add_sources_conditions_property.sql"
),
"r",
)
with self.conn:
self.conn.executescript(sql_file.read())
def add_sources_used_by_property(self):
self._logger.debug("Add sources used_by property")
sql_file = open(
os.path.join(
os.path.dirname(__file__), "007_add_sources_used_by_property.sql"
),
"r",
)
with self.conn:
self.conn.executescript(sql_file.read())
def version(self):
with self.conn:
c = self.conn.execute("SELECT * FROM meta WHERE id=1")
......
......@@ -106,7 +106,7 @@ CREATE TABLE IF NOT EXISTS nodes(
PRIMARY KEY (currency, pubkey)
);
-- Cnnections TABLE
-- CONNECTIONS TABLE
CREATE TABLE IF NOT EXISTS connections(
currency VARCHAR(30),
pubkey VARCHAR(50),
......@@ -118,7 +118,7 @@ CREATE TABLE IF NOT EXISTS connections(
PRIMARY KEY (currency, pubkey)
);
-- Cnnections TABLE
-- SOURCES TABLE
CREATE TABLE IF NOT EXISTS sources(
currency VARCHAR(30),
pubkey VARCHAR(50),
......@@ -130,6 +130,7 @@ CREATE TABLE IF NOT EXISTS sources(
PRIMARY KEY (currency, pubkey, identifier, noffset)
);
-- DIVIDENDS TABLE
CREATE TABLE IF NOT EXISTS dividends(
currency VARCHAR(30),
pubkey VARCHAR(50),
......
......@@ -29,8 +29,9 @@ class SourcesRepo:
def get_one(self, **search):
"""
Get an existing source in the database
:param dict search: the criterions of the lookup
Get an existing not consumed source in the database
:param ** search: the criterions of the lookup
:rtype: sakia.data.entities.Source
"""
filters = []
......@@ -39,7 +40,7 @@ class SourcesRepo:
filters.append("{k}=?".format(k=k))
values.append(v)
request = "SELECT * FROM sources WHERE {filters}".format(
request = "SELECT * FROM sources WHERE used_by is NULL AND {filters}".format(
filters=" AND ".join(filters)
)
......@@ -50,9 +51,10 @@ class SourcesRepo:
def get_all(self, **search):
"""
Get all existing source in the database corresponding to the search
:param dict search: the criterions of the lookup
:rtype: sakia.data.entities.Source
Get all existing not consumed source in the database corresponding to the search
:param ** search: the criterions of the lookup
:rtype: [sakia.data.entities.Source]
"""
filters = []
values = []
......@@ -61,7 +63,7 @@ class SourcesRepo:
filters.append("{key} = ?".format(key=k))
values.append(value)
request = "SELECT * FROM sources WHERE {filters}".format(
request = "SELECT * FROM sources WHERE used_by is NULL AND {filters}".format(
filters=" AND ".join(filters)
)
......@@ -101,3 +103,40 @@ class SourcesRepo:
filters=" AND ".join(filters)
)
self._conn.execute(request, tuple(values))
def consume(self, source, tx_hash):
"""
Consume a source by setting the used_by column with the tx hash
:param Source source: Source instance to consume
:param str tx_hash: Hash of tx
:return:
"""
where_fields = attr.astuple(
source, filter=attr.filters.include(*SourcesRepo._primary_keys)
)
fields = (tx_hash,) + where_fields
self._conn.execute(
"""UPDATE sources SET used_by=?
WHERE
currency=? AND
pubkey=? AND
identifier=? AND
noffset=?""",
fields,
)
def restore_all(self, tx_hash):
"""
Restore all sources released by tx_hash setting the used_by column with null
:param Source source: Source instance to consume
:param str tx_hash: Hash of tx
:return:
"""
self._conn.execute(
"""UPDATE sources SET used_by=NULL
WHERE
used_by=?""",
(tx_hash,),
)
......@@ -75,7 +75,7 @@ class TransactionsRepo:
def get_one(self, **search):
"""
Get an existing transaction in the database
:param dict search: the criterions of the lookup
:param ** search: the criterions of the lookup
:rtype: sakia.data.entities.Transaction
"""
filters = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment