Skip to content

Commit

Permalink
Merge pull request #405 from 1200wd/release-v07b
Browse files Browse the repository at this point in the history
Release v07b
  • Loading branch information
mccwdev authored Jun 3, 2024
2 parents 2d7e2f2 + 0a07438 commit e554492
Show file tree
Hide file tree
Showing 75 changed files with 10,796 additions and 4,892 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/unittests-mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Bitcoinlib Tests Ubuntu - MySQL
on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Set up MySQL
env:
DB_DATABASE: bitcoinlib_test
DB_USER: root
DB_PASSWORD: root
run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
- name: Install dependencies
run: |
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
UNITTEST_DATABASE: mysql
run: coverage run --source=bitcoinlib -m unittest -v
9 changes: 4 additions & 5 deletions .github/workflows/unittests-noscrypt.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu - No scrypt
name: Bitcoinlib Tests Ubuntu - No scrypt
on: [push]

jobs:
Expand All @@ -7,9 +7,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
Expand All @@ -19,7 +19,6 @@ jobs:
pip uninstall -y scrypt
- name: Test with coverage
env:
BCL_CONFIG_FILE: config_encryption.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config_encryption.ini.unittest
DB_FIELD_ENCRYPTION_KEY: 11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff
run: coverage run --source=bitcoinlib -m unittest -v
40 changes: 40 additions & 0 deletions .github/workflows/unittests-postgresql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Bitcoinlib Tests Ubuntu - PostgreSQL
on: [push]

jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres

env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: bitcoinlib_test

options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
UNITTEST_DATABASE: postgresql
DB_FIELD_ENCRYPTION_PASSWORD: verybadpassword
run: coverage run --source=bitcoinlib -m unittest -v
11 changes: 5 additions & 6 deletions .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu
name: Bitcoinlib Tests Ubuntu
on: [push]

jobs:
Expand All @@ -7,12 +7,12 @@ jobs:

strategy:
matrix:
python: ["3.8", "3.10", "3.11"]
python: ["3.8", "3.11"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: 'x64'
Expand All @@ -21,8 +21,7 @@ jobs:
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: config.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
run: coverage run --source=bitcoinlib -m unittest -v

- name: Coveralls
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/unittests_windows.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Windows Unittests
name: Bitcoinlib Tests Windows
on: [push]

jobs:
Expand All @@ -7,9 +7,9 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.11'
architecture: 'x64'
Expand All @@ -18,7 +18,6 @@ jobs:
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: config.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
PYTHONUTF8: 1
run: coverage run --source=bitcoinlib -m unittest -v
86 changes: 8 additions & 78 deletions bitcoinlib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ def parse_bytesio(cls, raw, block_hash=None, height=None, parse_transactions=Fal
raw.seek(tx_start_pos)
transactions = []

index = 0
while parse_transactions and raw.tell() < txs_data_size:
if limit != 0 and len(transactions) >= limit:
break
t = Transaction.parse_bytesio(raw, strict=False)
t = Transaction.parse_bytesio(raw, strict=False, index=index)
transactions.append(t)
index += 1
# TODO: verify transactions, need input value from previous txs
# if verify and not t.verify():
# raise ValueError("Could not verify transaction %s in block %s" % (t.txid, block_hash))
Expand All @@ -270,81 +272,6 @@ def parse_bytesio(cls, raw, block_hash=None, height=None, parse_transactions=Fal
block.tx_count = tx_count
return block

@classmethod
@deprecated
def from_raw(cls, raw, block_hash=None, height=None, parse_transactions=False, limit=0, network=DEFAULT_NETWORK): # pragma: no cover
"""
Create Block object from raw serialized block in bytes.
Get genesis block:
>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
:param raw: Raw serialize block
:type raw: bytes
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.
:type block_hash: bytes
:param height: Specify height if known. Will be derived from coinbase transaction if not provided.
:type height: int
:param parse_transactions: Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False
:type parse_transactions: bool
:param limit: Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True
:type limit: int
:param network: Name of network
:type network: str
:return Block:
"""
block_hash_calc = double_sha256(raw[:80])[::-1]
if not block_hash:
block_hash = block_hash_calc
elif block_hash != block_hash_calc:
raise ValueError("Provided block hash does not correspond to calculated block hash %s" %
block_hash_calc.hex())

version = raw[0:4][::-1]
prev_block = raw[4:36][::-1]
merkle_root = raw[36:68][::-1]
time = raw[68:72][::-1]
bits = raw[72:76][::-1]
nonce = raw[76:80][::-1]
tx_count, size = varbyteint_to_int(raw[80:89])
txs_data = BytesIO(raw[80+size:])

# Parse coinbase transaction so we can extract extra information
# transactions = [Transaction.parse(txs_data, network=network)]
# txs_data = BytesIO(txs_data[transactions[0].size:])
# block_txs_data = txs_data.read()
txs_data_size = txs_data.seek(0, 2)
txs_data.seek(0)
transactions = []

while parse_transactions and txs_data and txs_data.tell() < txs_data_size:
if limit != 0 and len(transactions) >= limit:
break
t = Transaction.parse_bytesio(txs_data, strict=False)
transactions.append(t)
# t = transaction_deserialize(txs_data, network=network, check_size=False)
# transactions.append(t)
# txs_data = txs_data[t.size:]
# TODO: verify transactions, need input value from previous txs
# if verify and not t.verify():
# raise ValueError("Could not verify transaction %s in block %s" % (t.txid, block_hash))

if parse_transactions and limit == 0 and tx_count != len(transactions):
raise ValueError("Number of found transactions %d is not equal to expected number %d" %
(len(transactions), tx_count))

block = cls(block_hash, version, prev_block, merkle_root, time, bits, nonce, transactions, height,
network=network)
block.txs_data = txs_data
block.tx_count = tx_count
return block

def parse_transactions(self, limit=0):
"""
Parse raw transactions from Block, if transaction data is available in txs_data attribute. Creates
Expand Down Expand Up @@ -373,11 +300,13 @@ def parse_transactions_dict(self):
"""
transactions_dict = []
txs_data_orig = deepcopy(self.txs_data)
index = 0
while self.txs_data and len(self.transactions) < self.tx_count:
tx = self.parse_transaction_dict()
tx = self.parse_transaction_dict(index)
if not tx:
break
transactions_dict.append(tx)
index += 1
self.txs_data = txs_data_orig
return transactions_dict

Expand All @@ -394,7 +323,7 @@ def parse_transaction(self):
return t
return False

def parse_transaction_dict(self):
def parse_transaction_dict(self, index=None):
"""
Parse a single transaction from Block, if transaction data is available in txs_data attribute. Add
Transaction object in Block and return the transaction
Expand Down Expand Up @@ -480,6 +409,7 @@ def parse_transaction_dict(self):
tx['txid'] = double_sha256(tx['version'][::-1] + raw_n_inputs + inputs_raw + raw_n_outputs + outputs_raw
+ tx_locktime)[::-1]
tx['size'] = len(tx['rawtx'])
tx['index'] = index
# TODO: tx['vsize'] = len(tx['rawtx'])
return tx
return False
Expand Down
Loading

0 comments on commit e554492

Please sign in to comment.