useful_blockchain 2.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- useful_blockchain-2.1.1/LICENSE +21 -0
- useful_blockchain-2.1.1/MANIFEST.in +7 -0
- useful_blockchain-2.1.1/PKG-INFO +296 -0
- useful_blockchain-2.1.1/README.md +259 -0
- useful_blockchain-2.1.1/config/default.yaml +74 -0
- useful_blockchain-2.1.1/config/docker.yaml +47 -0
- useful_blockchain-2.1.1/config/production.yaml +74 -0
- useful_blockchain-2.1.1/pyproject.toml +74 -0
- useful_blockchain-2.1.1/setup.cfg +4 -0
- useful_blockchain-2.1.1/setup.py +58 -0
- useful_blockchain-2.1.1/useful_blockchain/__init__.py +6 -0
- useful_blockchain-2.1.1/useful_blockchain/blockchain.py +220 -0
- useful_blockchain-2.1.1/useful_blockchain/chain_validator.py +116 -0
- useful_blockchain-2.1.1/useful_blockchain/cli.py +86 -0
- useful_blockchain-2.1.1/useful_blockchain/consensus/__init__.py +11 -0
- useful_blockchain-2.1.1/useful_blockchain/consensus/base.py +58 -0
- useful_blockchain-2.1.1/useful_blockchain/consensus/factory.py +36 -0
- useful_blockchain-2.1.1/useful_blockchain/consensus/pos.py +239 -0
- useful_blockchain-2.1.1/useful_blockchain/consensus/pow.py +124 -0
- useful_blockchain-2.1.1/useful_blockchain/hash_utils.py +45 -0
- useful_blockchain-2.1.1/useful_blockchain/network/__init__.py +4 -0
- useful_blockchain-2.1.1/useful_blockchain/network/discovery.py +79 -0
- useful_blockchain-2.1.1/useful_blockchain/network/messages.py +35 -0
- useful_blockchain-2.1.1/useful_blockchain/network/node.py +484 -0
- useful_blockchain-2.1.1/useful_blockchain/network/peer.py +156 -0
- useful_blockchain-2.1.1/useful_blockchain/network/peer_auth.py +69 -0
- useful_blockchain-2.1.1/useful_blockchain/network/rate_limit.py +31 -0
- useful_blockchain-2.1.1/useful_blockchain/network/reconnect.py +55 -0
- useful_blockchain-2.1.1/useful_blockchain/network/server.py +201 -0
- useful_blockchain-2.1.1/useful_blockchain/network/tls.py +64 -0
- useful_blockchain-2.1.1/useful_blockchain/observability/__init__.py +7 -0
- useful_blockchain-2.1.1/useful_blockchain/observability/health_server.py +139 -0
- useful_blockchain-2.1.1/useful_blockchain/observability/logging_config.py +56 -0
- useful_blockchain-2.1.1/useful_blockchain/observability/metrics.py +143 -0
- useful_blockchain-2.1.1/useful_blockchain/persistence/__init__.py +5 -0
- useful_blockchain-2.1.1/useful_blockchain/persistence/constants.py +5 -0
- useful_blockchain-2.1.1/useful_blockchain/persistence/store.py +169 -0
- useful_blockchain-2.1.1/useful_blockchain/py.typed +0 -0
- useful_blockchain-2.1.1/useful_blockchain/settings.py +244 -0
- useful_blockchain-2.1.1/useful_blockchain/signature.py +304 -0
- useful_blockchain-2.1.1/useful_blockchain/types.py +171 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/PKG-INFO +296 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/SOURCES.txt +45 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/dependency_links.txt +1 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/entry_points.txt +2 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/requires.txt +18 -0
- useful_blockchain-2.1.1/useful_blockchain.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Teppei.F
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: useful_blockchain
|
|
3
|
+
Version: 2.1.1
|
|
4
|
+
Summary: Easy Blockchain: A simple blockchain with PoW/PoS consensus and P2P networking
|
|
5
|
+
Home-page: https://github.com/T3pp31/easyblockchain
|
|
6
|
+
Download-URL: https://github.com/T3pp31/easyblockchain
|
|
7
|
+
Author: Teppei Fukutomi
|
|
8
|
+
Author-email: Teppei Fukutomi <ttyn4519@outlook.jp>
|
|
9
|
+
Maintainer: Teppei Fukutomi
|
|
10
|
+
Maintainer-email: ttyn4519@outlook.jp
|
|
11
|
+
License: MIT
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: cryptography>=3.0.0
|
|
16
|
+
Requires-Dist: pyyaml>=6.0
|
|
17
|
+
Requires-Dist: websockets>=12.0
|
|
18
|
+
Provides-Extra: dev
|
|
19
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
20
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
21
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-timeout>=2.2; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.4; extra == "dev"
|
|
24
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: types-PyYAML>=6.0; extra == "dev"
|
|
26
|
+
Provides-Extra: observability
|
|
27
|
+
Requires-Dist: prometheus-client>=0.20; extra == "observability"
|
|
28
|
+
Provides-Extra: mdns
|
|
29
|
+
Requires-Dist: zeroconf>=0.131; extra == "mdns"
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: download-url
|
|
32
|
+
Dynamic: home-page
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: maintainer
|
|
35
|
+
Dynamic: maintainer-email
|
|
36
|
+
Dynamic: requires-python
|
|
37
|
+
|
|
38
|
+
# easy_blockchain
|
|
39
|
+
|
|
40
|
+
you can use blockchain easily.
|
|
41
|
+
please use for prototype
|
|
42
|
+
|
|
43
|
+
簡単にブロックチェーンを使うためのライブラリ.
|
|
44
|
+
IoTに組み込んだりなど,簡易的なプロトタイプ作成に使ってください.
|
|
45
|
+
|
|
46
|
+
## useful_blockchain 概要
|
|
47
|
+
|
|
48
|
+
- 提供クラス:
|
|
49
|
+
- `useful_blockchain.blockchain.BlockChain`
|
|
50
|
+
- `useful_blockchain.signature.SignatureManager`
|
|
51
|
+
- `useful_blockchain.network.node.Node`(v2.0: P2P ノード)
|
|
52
|
+
- 目的: プロトタイプ・学習向けのブロックチェーン実装(PoW/PoS 合意 + P2P 対応)。
|
|
53
|
+
- 主なメソッド:
|
|
54
|
+
- `add_new_block(input_data, output_data)`: 新しいトランザクションを作成し,直前ブロックのハッシュと組み合わせて末尾にブロックを追加します(戻り値は追加されたブロックの辞書)。
|
|
55
|
+
- `dump(block_index=0)`: チェーン全体または指定インデックスのブロックを簡易表示します。
|
|
56
|
+
- `generate_key_pair()`: デジタル署名用の鍵ペアを生成します(署名機能有効時のみ)。
|
|
57
|
+
- `verify_block_signature(block_index)`: 指定ブロックの署名を検証します。
|
|
58
|
+
- ブロック構造(辞書):
|
|
59
|
+
- `block_index`: 1始まりの連番
|
|
60
|
+
- `block_item`: 生成日時(`YYYY-MM-DD HH:MM:SS`)
|
|
61
|
+
- `block_header.prev_hash`: 直前ブロックのトランザクションハッシュ(先頭ブロックは `config/default.yaml` の `genesis.prev_hash` で固定)
|
|
62
|
+
- `block_header.tran_hash`: `sha256(prev_hash + sha256(json(tran_body)))`
|
|
63
|
+
- `tran_counter`: 入力と出力の要素数の合計
|
|
64
|
+
- `tran_body.input_data` / `tran_body.output_data`: 追加時に渡した値
|
|
65
|
+
- ハッシュ化: `sha256` による簡易的な整合性保証(改ざん検知の学習・デモ用途)。
|
|
66
|
+
|
|
67
|
+
### 使い方(例)
|
|
68
|
+
|
|
69
|
+
#### 基本的な使い方
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from useful_blockchain.blockchain import BlockChain
|
|
73
|
+
|
|
74
|
+
bc = BlockChain()
|
|
75
|
+
bc.add_new_block(["a"], ["b"])
|
|
76
|
+
bc.add_new_block(["c"], ["d"])
|
|
77
|
+
print(bc.chain) # チェーン配列(各ブロックは dict)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### 署名機能付きブロックチェーン
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from useful_blockchain.blockchain import BlockChain
|
|
84
|
+
|
|
85
|
+
# 署名機能を有効にしてブロックチェーンを作成
|
|
86
|
+
bc = BlockChain(enable_signature=True)
|
|
87
|
+
|
|
88
|
+
# 鍵ペアを生成
|
|
89
|
+
private_key, public_key = bc.generate_key_pair()
|
|
90
|
+
|
|
91
|
+
# 署名付きブロックを追加
|
|
92
|
+
bc.add_new_block(["sender_a"], ["receiver_b"])
|
|
93
|
+
bc.add_new_block(["sender_c"], ["receiver_d"])
|
|
94
|
+
|
|
95
|
+
# 署名を検証
|
|
96
|
+
print(bc.verify_block_signature(1)) # True(署名が正しい場合)
|
|
97
|
+
print(bc.verify_all_signatures()) # 全ブロックの検証結果
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### デジタル署名単体での使用
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from useful_blockchain.signature import SignatureManager
|
|
104
|
+
|
|
105
|
+
sig_manager = SignatureManager()
|
|
106
|
+
private_key, public_key = sig_manager.generate_key_pair()
|
|
107
|
+
|
|
108
|
+
# データに署名
|
|
109
|
+
data = {"message": "Hello, Blockchain!"}
|
|
110
|
+
signature = sig_manager.sign_data(data)
|
|
111
|
+
|
|
112
|
+
# 署名を検証
|
|
113
|
+
is_valid = sig_manager.verify_signature(data, signature)
|
|
114
|
+
print(f"署名検証結果: {is_valid}")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## デジタル署名機能について
|
|
118
|
+
|
|
119
|
+
- RSA暗号化を使用したデジタル署名機能を提供
|
|
120
|
+
- 各ブロックに署名を付与し、データの完全性と認証を保証
|
|
121
|
+
- 署名の生成・検証・鍵管理機能を含む
|
|
122
|
+
- PEM形式での公開鍵エクスポート/インポートに対応
|
|
123
|
+
|
|
124
|
+
### v2.0: PoW / PoS 分散合意 + P2P
|
|
125
|
+
|
|
126
|
+
P2P ネットワークの詳細は [docs/p2p.md](docs/p2p.md) を参照してください。
|
|
127
|
+
|
|
128
|
+
#### 設定ファイル
|
|
129
|
+
|
|
130
|
+
`config/default.yaml` で合意方式・ネットワーク・ジェネシスを設定します。環境変数 `EASYBLOCKCHAIN_CONFIG` でパスを上書きできます。
|
|
131
|
+
|
|
132
|
+
`genesis.prev_hash` は先頭ブロックの `prev_hash` および P2P の `genesis_hash` 識別子として使われます。同一ネットワーク内の全ノードで同じ値を設定してください。
|
|
133
|
+
|
|
134
|
+
#### ノード起動(PoW)
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
uv run easyblockchain-node --consensus pow --port 8765
|
|
138
|
+
uv run easyblockchain-node --consensus pow --port 8766 --bootstrap ws://127.0.0.1:8765
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
または:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
uv run python examples/run_node.py --consensus pow --port 8765
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### ノード起動(PoS)
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
uv run python examples/run_node.py --consensus pos --port 8770
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Python API
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
import asyncio
|
|
157
|
+
from useful_blockchain.network.node import Node
|
|
158
|
+
|
|
159
|
+
async def main():
|
|
160
|
+
node = Node(overrides={"consensus": {"type": "pow", "pow": {"initial_difficulty": 2}}})
|
|
161
|
+
await node.start()
|
|
162
|
+
await node.add_block(["alice"], ["bob"])
|
|
163
|
+
await node.stop()
|
|
164
|
+
|
|
165
|
+
asyncio.run(main())
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### テスト
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
uv run pytest tests -v
|
|
172
|
+
uv run pytest tests/e2e -v -m slow
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### 運用・デプロイ(v2.1)
|
|
176
|
+
|
|
177
|
+
- 運用ドキュメント: [docs/operations.md](docs/operations.md)
|
|
178
|
+
- 本番設定テンプレート: `config/production.yaml`
|
|
179
|
+
- Docker 3 ノード例:
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
docker compose up --build
|
|
183
|
+
curl -f http://localhost:9090/healthz
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- ヘルスチェック: `GET /healthz`(liveness), `GET /readyz`(readiness)
|
|
187
|
+
- メトリクス: `GET /metrics`(`uv sync --extra observability` が必要)
|
|
188
|
+
|
|
189
|
+
### 注意事項
|
|
190
|
+
|
|
191
|
+
- PoW/PoS/P2P は教育・試作向けの実装です。本番利用には追加のセキュリティ監査が必要です。
|
|
192
|
+
- v1 互換: `BlockChain()` を合意なしで使うと従来どおり即時ブロック追加が可能です。
|
|
193
|
+
|
|
194
|
+
# 変更履歴
|
|
195
|
+
|
|
196
|
+
[CHANGELOG.md](CHANGELOG.md) を参照してください。
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
# English Documentation
|
|
201
|
+
|
|
202
|
+
## useful_blockchain Overview
|
|
203
|
+
|
|
204
|
+
- Provided Classes:
|
|
205
|
+
- `useful_blockchain.blockchain.BlockChain`
|
|
206
|
+
- `useful_blockchain.signature.SignatureManager`
|
|
207
|
+
- Purpose: Minimal blockchain implementation for prototyping and learning.
|
|
208
|
+
- Main Methods:
|
|
209
|
+
- `add_new_block(input_data, output_data)`: Creates a new transaction and adds a block to the end of the chain by combining it with the hash of the previous block (returns the dictionary of the added block).
|
|
210
|
+
- `dump(block_index=0)`: Simple display of the entire chain or a block at the specified index.
|
|
211
|
+
- `generate_key_pair()`: Generates key pairs for digital signatures (only when signature feature is enabled).
|
|
212
|
+
- `verify_block_signature(block_index)`: Verifies the signature of the specified block.
|
|
213
|
+
- Block Structure (dictionary):
|
|
214
|
+
- `block_index`: Sequential number starting from 1
|
|
215
|
+
- `block_item`: Generation timestamp (`YYYY-MM-DD HH:MM:SS`)
|
|
216
|
+
- `block_header.prev_hash`: Transaction hash of the previous block (generated from random seed only for the first block)
|
|
217
|
+
- `block_header.tran_hash`: `sha256(prev_hash + sha256(json(tran_body)))`
|
|
218
|
+
- `tran_counter`: Total number of input and output elements
|
|
219
|
+
- `tran_body.input_data` / `tran_body.output_data`: Values passed during addition
|
|
220
|
+
- Hashing: Simple integrity assurance by `sha256` (for learning and demonstration purposes of tampering detection).
|
|
221
|
+
|
|
222
|
+
### Usage Examples
|
|
223
|
+
|
|
224
|
+
#### Basic Usage
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
from useful_blockchain.blockchain import BlockChain
|
|
228
|
+
|
|
229
|
+
bc = BlockChain()
|
|
230
|
+
bc.add_new_block(["a"], ["b"])
|
|
231
|
+
bc.add_new_block(["c"], ["d"])
|
|
232
|
+
print(bc.chain) # Chain array (each block is a dict)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### Blockchain with Signature Feature
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
from useful_blockchain.blockchain import BlockChain
|
|
239
|
+
|
|
240
|
+
# Create blockchain with signature feature enabled
|
|
241
|
+
bc = BlockChain(enable_signature=True)
|
|
242
|
+
|
|
243
|
+
# Generate key pair
|
|
244
|
+
private_key, public_key = bc.generate_key_pair()
|
|
245
|
+
|
|
246
|
+
# Add signed blocks
|
|
247
|
+
bc.add_new_block(["sender_a"], ["receiver_b"])
|
|
248
|
+
bc.add_new_block(["sender_c"], ["receiver_d"])
|
|
249
|
+
|
|
250
|
+
# Verify signatures
|
|
251
|
+
print(bc.verify_block_signature(1)) # True (if signature is correct)
|
|
252
|
+
print(bc.verify_all_signatures()) # Verification results for all blocks
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
#### Standalone Digital Signature Usage
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
from useful_blockchain.signature import SignatureManager
|
|
259
|
+
|
|
260
|
+
sig_manager = SignatureManager()
|
|
261
|
+
private_key, public_key = sig_manager.generate_key_pair()
|
|
262
|
+
|
|
263
|
+
# Sign data
|
|
264
|
+
data = {"message": "Hello, Blockchain!"}
|
|
265
|
+
signature = sig_manager.sign_data(data)
|
|
266
|
+
|
|
267
|
+
# Verify signature
|
|
268
|
+
is_valid = sig_manager.verify_signature(data, signature)
|
|
269
|
+
print(f"Signature verification result: {is_valid}")
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## About Digital Signature Feature
|
|
273
|
+
|
|
274
|
+
- Provides digital signature functionality using RSA encryption
|
|
275
|
+
- Adds signatures to each block to ensure data integrity and authentication
|
|
276
|
+
- Includes signature generation, verification, and key management functions
|
|
277
|
+
- Supports public key export/import in PEM format
|
|
278
|
+
|
|
279
|
+
### v2.0: PoW / PoS Consensus + P2P
|
|
280
|
+
|
|
281
|
+
See [docs/p2p.md](docs/p2p.md) for P2P networking details, plus `config/default.yaml`, `examples/run_node.py`, and [CHANGELOG.md](CHANGELOG.md).
|
|
282
|
+
|
|
283
|
+
### Important Notes
|
|
284
|
+
|
|
285
|
+
- PoW/PoS/P2P are educational implementations; production use requires additional security review.
|
|
286
|
+
- v1 compatibility: `BlockChain()` without consensus retains legacy instant-add behavior.
|
|
287
|
+
|
|
288
|
+
## PyPI リリース
|
|
289
|
+
|
|
290
|
+
GitHub Release を公開すると `.github/workflows/publish.yml` が PyPI へ自動公開します(Trusted Publishing)。
|
|
291
|
+
|
|
292
|
+
手動ビルド:
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
uv build
|
|
296
|
+
```
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# easy_blockchain
|
|
2
|
+
|
|
3
|
+
you can use blockchain easily.
|
|
4
|
+
please use for prototype
|
|
5
|
+
|
|
6
|
+
簡単にブロックチェーンを使うためのライブラリ.
|
|
7
|
+
IoTに組み込んだりなど,簡易的なプロトタイプ作成に使ってください.
|
|
8
|
+
|
|
9
|
+
## useful_blockchain 概要
|
|
10
|
+
|
|
11
|
+
- 提供クラス:
|
|
12
|
+
- `useful_blockchain.blockchain.BlockChain`
|
|
13
|
+
- `useful_blockchain.signature.SignatureManager`
|
|
14
|
+
- `useful_blockchain.network.node.Node`(v2.0: P2P ノード)
|
|
15
|
+
- 目的: プロトタイプ・学習向けのブロックチェーン実装(PoW/PoS 合意 + P2P 対応)。
|
|
16
|
+
- 主なメソッド:
|
|
17
|
+
- `add_new_block(input_data, output_data)`: 新しいトランザクションを作成し,直前ブロックのハッシュと組み合わせて末尾にブロックを追加します(戻り値は追加されたブロックの辞書)。
|
|
18
|
+
- `dump(block_index=0)`: チェーン全体または指定インデックスのブロックを簡易表示します。
|
|
19
|
+
- `generate_key_pair()`: デジタル署名用の鍵ペアを生成します(署名機能有効時のみ)。
|
|
20
|
+
- `verify_block_signature(block_index)`: 指定ブロックの署名を検証します。
|
|
21
|
+
- ブロック構造(辞書):
|
|
22
|
+
- `block_index`: 1始まりの連番
|
|
23
|
+
- `block_item`: 生成日時(`YYYY-MM-DD HH:MM:SS`)
|
|
24
|
+
- `block_header.prev_hash`: 直前ブロックのトランザクションハッシュ(先頭ブロックは `config/default.yaml` の `genesis.prev_hash` で固定)
|
|
25
|
+
- `block_header.tran_hash`: `sha256(prev_hash + sha256(json(tran_body)))`
|
|
26
|
+
- `tran_counter`: 入力と出力の要素数の合計
|
|
27
|
+
- `tran_body.input_data` / `tran_body.output_data`: 追加時に渡した値
|
|
28
|
+
- ハッシュ化: `sha256` による簡易的な整合性保証(改ざん検知の学習・デモ用途)。
|
|
29
|
+
|
|
30
|
+
### 使い方(例)
|
|
31
|
+
|
|
32
|
+
#### 基本的な使い方
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from useful_blockchain.blockchain import BlockChain
|
|
36
|
+
|
|
37
|
+
bc = BlockChain()
|
|
38
|
+
bc.add_new_block(["a"], ["b"])
|
|
39
|
+
bc.add_new_block(["c"], ["d"])
|
|
40
|
+
print(bc.chain) # チェーン配列(各ブロックは dict)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### 署名機能付きブロックチェーン
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from useful_blockchain.blockchain import BlockChain
|
|
47
|
+
|
|
48
|
+
# 署名機能を有効にしてブロックチェーンを作成
|
|
49
|
+
bc = BlockChain(enable_signature=True)
|
|
50
|
+
|
|
51
|
+
# 鍵ペアを生成
|
|
52
|
+
private_key, public_key = bc.generate_key_pair()
|
|
53
|
+
|
|
54
|
+
# 署名付きブロックを追加
|
|
55
|
+
bc.add_new_block(["sender_a"], ["receiver_b"])
|
|
56
|
+
bc.add_new_block(["sender_c"], ["receiver_d"])
|
|
57
|
+
|
|
58
|
+
# 署名を検証
|
|
59
|
+
print(bc.verify_block_signature(1)) # True(署名が正しい場合)
|
|
60
|
+
print(bc.verify_all_signatures()) # 全ブロックの検証結果
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### デジタル署名単体での使用
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from useful_blockchain.signature import SignatureManager
|
|
67
|
+
|
|
68
|
+
sig_manager = SignatureManager()
|
|
69
|
+
private_key, public_key = sig_manager.generate_key_pair()
|
|
70
|
+
|
|
71
|
+
# データに署名
|
|
72
|
+
data = {"message": "Hello, Blockchain!"}
|
|
73
|
+
signature = sig_manager.sign_data(data)
|
|
74
|
+
|
|
75
|
+
# 署名を検証
|
|
76
|
+
is_valid = sig_manager.verify_signature(data, signature)
|
|
77
|
+
print(f"署名検証結果: {is_valid}")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## デジタル署名機能について
|
|
81
|
+
|
|
82
|
+
- RSA暗号化を使用したデジタル署名機能を提供
|
|
83
|
+
- 各ブロックに署名を付与し、データの完全性と認証を保証
|
|
84
|
+
- 署名の生成・検証・鍵管理機能を含む
|
|
85
|
+
- PEM形式での公開鍵エクスポート/インポートに対応
|
|
86
|
+
|
|
87
|
+
### v2.0: PoW / PoS 分散合意 + P2P
|
|
88
|
+
|
|
89
|
+
P2P ネットワークの詳細は [docs/p2p.md](docs/p2p.md) を参照してください。
|
|
90
|
+
|
|
91
|
+
#### 設定ファイル
|
|
92
|
+
|
|
93
|
+
`config/default.yaml` で合意方式・ネットワーク・ジェネシスを設定します。環境変数 `EASYBLOCKCHAIN_CONFIG` でパスを上書きできます。
|
|
94
|
+
|
|
95
|
+
`genesis.prev_hash` は先頭ブロックの `prev_hash` および P2P の `genesis_hash` 識別子として使われます。同一ネットワーク内の全ノードで同じ値を設定してください。
|
|
96
|
+
|
|
97
|
+
#### ノード起動(PoW)
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
uv run easyblockchain-node --consensus pow --port 8765
|
|
101
|
+
uv run easyblockchain-node --consensus pow --port 8766 --bootstrap ws://127.0.0.1:8765
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
または:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
uv run python examples/run_node.py --consensus pow --port 8765
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### ノード起動(PoS)
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
uv run python examples/run_node.py --consensus pos --port 8770
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Python API
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
import asyncio
|
|
120
|
+
from useful_blockchain.network.node import Node
|
|
121
|
+
|
|
122
|
+
async def main():
|
|
123
|
+
node = Node(overrides={"consensus": {"type": "pow", "pow": {"initial_difficulty": 2}}})
|
|
124
|
+
await node.start()
|
|
125
|
+
await node.add_block(["alice"], ["bob"])
|
|
126
|
+
await node.stop()
|
|
127
|
+
|
|
128
|
+
asyncio.run(main())
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### テスト
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
uv run pytest tests -v
|
|
135
|
+
uv run pytest tests/e2e -v -m slow
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### 運用・デプロイ(v2.1)
|
|
139
|
+
|
|
140
|
+
- 運用ドキュメント: [docs/operations.md](docs/operations.md)
|
|
141
|
+
- 本番設定テンプレート: `config/production.yaml`
|
|
142
|
+
- Docker 3 ノード例:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
docker compose up --build
|
|
146
|
+
curl -f http://localhost:9090/healthz
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- ヘルスチェック: `GET /healthz`(liveness), `GET /readyz`(readiness)
|
|
150
|
+
- メトリクス: `GET /metrics`(`uv sync --extra observability` が必要)
|
|
151
|
+
|
|
152
|
+
### 注意事項
|
|
153
|
+
|
|
154
|
+
- PoW/PoS/P2P は教育・試作向けの実装です。本番利用には追加のセキュリティ監査が必要です。
|
|
155
|
+
- v1 互換: `BlockChain()` を合意なしで使うと従来どおり即時ブロック追加が可能です。
|
|
156
|
+
|
|
157
|
+
# 変更履歴
|
|
158
|
+
|
|
159
|
+
[CHANGELOG.md](CHANGELOG.md) を参照してください。
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
# English Documentation
|
|
164
|
+
|
|
165
|
+
## useful_blockchain Overview
|
|
166
|
+
|
|
167
|
+
- Provided Classes:
|
|
168
|
+
- `useful_blockchain.blockchain.BlockChain`
|
|
169
|
+
- `useful_blockchain.signature.SignatureManager`
|
|
170
|
+
- Purpose: Minimal blockchain implementation for prototyping and learning.
|
|
171
|
+
- Main Methods:
|
|
172
|
+
- `add_new_block(input_data, output_data)`: Creates a new transaction and adds a block to the end of the chain by combining it with the hash of the previous block (returns the dictionary of the added block).
|
|
173
|
+
- `dump(block_index=0)`: Simple display of the entire chain or a block at the specified index.
|
|
174
|
+
- `generate_key_pair()`: Generates key pairs for digital signatures (only when signature feature is enabled).
|
|
175
|
+
- `verify_block_signature(block_index)`: Verifies the signature of the specified block.
|
|
176
|
+
- Block Structure (dictionary):
|
|
177
|
+
- `block_index`: Sequential number starting from 1
|
|
178
|
+
- `block_item`: Generation timestamp (`YYYY-MM-DD HH:MM:SS`)
|
|
179
|
+
- `block_header.prev_hash`: Transaction hash of the previous block (generated from random seed only for the first block)
|
|
180
|
+
- `block_header.tran_hash`: `sha256(prev_hash + sha256(json(tran_body)))`
|
|
181
|
+
- `tran_counter`: Total number of input and output elements
|
|
182
|
+
- `tran_body.input_data` / `tran_body.output_data`: Values passed during addition
|
|
183
|
+
- Hashing: Simple integrity assurance by `sha256` (for learning and demonstration purposes of tampering detection).
|
|
184
|
+
|
|
185
|
+
### Usage Examples
|
|
186
|
+
|
|
187
|
+
#### Basic Usage
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from useful_blockchain.blockchain import BlockChain
|
|
191
|
+
|
|
192
|
+
bc = BlockChain()
|
|
193
|
+
bc.add_new_block(["a"], ["b"])
|
|
194
|
+
bc.add_new_block(["c"], ["d"])
|
|
195
|
+
print(bc.chain) # Chain array (each block is a dict)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Blockchain with Signature Feature
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from useful_blockchain.blockchain import BlockChain
|
|
202
|
+
|
|
203
|
+
# Create blockchain with signature feature enabled
|
|
204
|
+
bc = BlockChain(enable_signature=True)
|
|
205
|
+
|
|
206
|
+
# Generate key pair
|
|
207
|
+
private_key, public_key = bc.generate_key_pair()
|
|
208
|
+
|
|
209
|
+
# Add signed blocks
|
|
210
|
+
bc.add_new_block(["sender_a"], ["receiver_b"])
|
|
211
|
+
bc.add_new_block(["sender_c"], ["receiver_d"])
|
|
212
|
+
|
|
213
|
+
# Verify signatures
|
|
214
|
+
print(bc.verify_block_signature(1)) # True (if signature is correct)
|
|
215
|
+
print(bc.verify_all_signatures()) # Verification results for all blocks
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### Standalone Digital Signature Usage
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from useful_blockchain.signature import SignatureManager
|
|
222
|
+
|
|
223
|
+
sig_manager = SignatureManager()
|
|
224
|
+
private_key, public_key = sig_manager.generate_key_pair()
|
|
225
|
+
|
|
226
|
+
# Sign data
|
|
227
|
+
data = {"message": "Hello, Blockchain!"}
|
|
228
|
+
signature = sig_manager.sign_data(data)
|
|
229
|
+
|
|
230
|
+
# Verify signature
|
|
231
|
+
is_valid = sig_manager.verify_signature(data, signature)
|
|
232
|
+
print(f"Signature verification result: {is_valid}")
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## About Digital Signature Feature
|
|
236
|
+
|
|
237
|
+
- Provides digital signature functionality using RSA encryption
|
|
238
|
+
- Adds signatures to each block to ensure data integrity and authentication
|
|
239
|
+
- Includes signature generation, verification, and key management functions
|
|
240
|
+
- Supports public key export/import in PEM format
|
|
241
|
+
|
|
242
|
+
### v2.0: PoW / PoS Consensus + P2P
|
|
243
|
+
|
|
244
|
+
See [docs/p2p.md](docs/p2p.md) for P2P networking details, plus `config/default.yaml`, `examples/run_node.py`, and [CHANGELOG.md](CHANGELOG.md).
|
|
245
|
+
|
|
246
|
+
### Important Notes
|
|
247
|
+
|
|
248
|
+
- PoW/PoS/P2P are educational implementations; production use requires additional security review.
|
|
249
|
+
- v1 compatibility: `BlockChain()` without consensus retains legacy instant-add behavior.
|
|
250
|
+
|
|
251
|
+
## PyPI リリース
|
|
252
|
+
|
|
253
|
+
GitHub Release を公開すると `.github/workflows/publish.yml` が PyPI へ自動公開します(Trusted Publishing)。
|
|
254
|
+
|
|
255
|
+
手動ビルド:
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
uv build
|
|
259
|
+
```
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
genesis:
|
|
2
|
+
prev_hash: "0000000000000000000000000000000000000000000000000000000000000000"
|
|
3
|
+
|
|
4
|
+
consensus:
|
|
5
|
+
type: pow
|
|
6
|
+
pow:
|
|
7
|
+
initial_difficulty: 4
|
|
8
|
+
adjustment_interval: 10
|
|
9
|
+
max_adjustment_factor: 2.0
|
|
10
|
+
max_mining_iterations: 1000000
|
|
11
|
+
target_block_time_seconds: 10
|
|
12
|
+
pos:
|
|
13
|
+
epoch_length: 10
|
|
14
|
+
min_stake: 100
|
|
15
|
+
block_reward: 10
|
|
16
|
+
|
|
17
|
+
network:
|
|
18
|
+
host: "0.0.0.0"
|
|
19
|
+
port: 8765
|
|
20
|
+
bootstrap_peers: []
|
|
21
|
+
mdns_enabled: false
|
|
22
|
+
mdns_service_name: "_easyblockchain._tcp.local."
|
|
23
|
+
max_peers: 25
|
|
24
|
+
max_message_bytes: 1048576
|
|
25
|
+
chain_sync_batch_size: 100
|
|
26
|
+
ping_interval_seconds: 30
|
|
27
|
+
connection_timeout_seconds: 10
|
|
28
|
+
chain_sync_timeout_seconds: 10
|
|
29
|
+
shutdown_peer_close_timeout_seconds: 2
|
|
30
|
+
shutdown_server_wait_timeout_seconds: 3
|
|
31
|
+
pong_timeout_seconds: 90
|
|
32
|
+
tls:
|
|
33
|
+
enabled: false
|
|
34
|
+
cert_file: ""
|
|
35
|
+
key_file: ""
|
|
36
|
+
ca_file: ""
|
|
37
|
+
verify_peer: false
|
|
38
|
+
peer_auth:
|
|
39
|
+
enabled: true
|
|
40
|
+
max_skew_seconds: 300
|
|
41
|
+
rate_limit:
|
|
42
|
+
max_connections_per_ip_per_minute: 10
|
|
43
|
+
max_messages_per_peer_per_second: 50
|
|
44
|
+
max_decode_errors_before_disconnect: 5
|
|
45
|
+
reconnect:
|
|
46
|
+
enabled: true
|
|
47
|
+
initial_delay_seconds: 1
|
|
48
|
+
max_delay_seconds: 60
|
|
49
|
+
max_attempts: 0
|
|
50
|
+
backoff_multiplier: 2.0
|
|
51
|
+
|
|
52
|
+
node:
|
|
53
|
+
data_dir: "./data"
|
|
54
|
+
node_id: ""
|
|
55
|
+
log_level: "INFO"
|
|
56
|
+
|
|
57
|
+
observability:
|
|
58
|
+
enabled: false
|
|
59
|
+
host: "0.0.0.0"
|
|
60
|
+
port: 9090
|
|
61
|
+
log_format: "text"
|
|
62
|
+
health_path: "/healthz"
|
|
63
|
+
ready_path: "/readyz"
|
|
64
|
+
metrics_path: "/metrics"
|
|
65
|
+
min_peers_for_ready: 0
|
|
66
|
+
|
|
67
|
+
persistence:
|
|
68
|
+
schema_version: 1
|
|
69
|
+
chain_file: chain.json
|
|
70
|
+
meta_file: meta.json
|
|
71
|
+
genesis_stakes_file: genesis_stakes.json
|
|
72
|
+
keys_dir: keys
|
|
73
|
+
private_key_file: node.pem
|
|
74
|
+
p2p_identity_file: p2p_identity.pem
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
genesis:
|
|
2
|
+
prev_hash: "0000000000000000000000000000000000000000000000000000000000000000"
|
|
3
|
+
|
|
4
|
+
consensus:
|
|
5
|
+
type: pow
|
|
6
|
+
pow:
|
|
7
|
+
initial_difficulty: 4
|
|
8
|
+
adjustment_interval: 10
|
|
9
|
+
max_adjustment_factor: 2.0
|
|
10
|
+
max_mining_iterations: 1000000
|
|
11
|
+
target_block_time_seconds: 10
|
|
12
|
+
|
|
13
|
+
network:
|
|
14
|
+
host: "0.0.0.0"
|
|
15
|
+
port: 8765
|
|
16
|
+
bootstrap_peers: []
|
|
17
|
+
mdns_enabled: false
|
|
18
|
+
tls:
|
|
19
|
+
enabled: false
|
|
20
|
+
peer_auth:
|
|
21
|
+
enabled: true
|
|
22
|
+
max_skew_seconds: 300
|
|
23
|
+
rate_limit:
|
|
24
|
+
max_connections_per_ip_per_minute: 5
|
|
25
|
+
max_messages_per_peer_per_second: 30
|
|
26
|
+
max_decode_errors_before_disconnect: 3
|
|
27
|
+
reconnect:
|
|
28
|
+
enabled: true
|
|
29
|
+
|
|
30
|
+
node:
|
|
31
|
+
data_dir: "/data"
|
|
32
|
+
log_level: "INFO"
|
|
33
|
+
|
|
34
|
+
observability:
|
|
35
|
+
enabled: true
|
|
36
|
+
host: "0.0.0.0"
|
|
37
|
+
port: 9090
|
|
38
|
+
log_format: "json"
|
|
39
|
+
|
|
40
|
+
persistence:
|
|
41
|
+
schema_version: 1
|
|
42
|
+
chain_file: chain.json
|
|
43
|
+
meta_file: meta.json
|
|
44
|
+
genesis_stakes_file: genesis_stakes.json
|
|
45
|
+
keys_dir: keys
|
|
46
|
+
private_key_file: node.pem
|
|
47
|
+
p2p_identity_file: p2p_identity.pem
|