Blockchain API¶
The blockchain module provides immutable trust records and verification.
BlockchainTrustNetwork¶
qfzz.blockchain.trust_network.BlockchainTrustNetwork
¶
Blockchain-based trust network for immutable content verification.
Creates tamper-proof records of trust scores for content and creators.
Source code in qfzz/blockchain/trust_network.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
__init__(difficulty=2)
¶
Initialize blockchain trust network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
difficulty
|
int
|
Mining difficulty (number of leading zeros) |
2
|
Source code in qfzz/blockchain/trust_network.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
add_trust_record(content_id, creator_id, initial_score=0.5, metadata=None)
¶
Add a new trust record to pending records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_id
|
str
|
Content identifier |
required |
creator_id
|
str
|
Creator identifier |
required |
initial_score
|
float
|
Initial trust score (default: 0.5) |
0.5
|
metadata
|
Optional[dict[str, Any]]
|
Optional metadata |
None
|
Returns:
| Type | Description |
|---|---|
TrustRecord
|
Created TrustRecord |
Source code in qfzz/blockchain/trust_network.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
export_chain()
¶
Export entire blockchain as list of dictionaries.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of block dictionaries |
Source code in qfzz/blockchain/trust_network.py
264 265 266 267 268 269 270 271 | |
get_block(index)
¶
Get block by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Block index |
required |
Returns:
| Type | Description |
|---|---|
Optional[Block]
|
Block if found, None otherwise |
Source code in qfzz/blockchain/trust_network.py
228 229 230 231 232 233 234 235 236 237 238 239 240 | |
get_chain_length()
¶
Get length of the blockchain.
Source code in qfzz/blockchain/trust_network.py
224 225 226 | |
get_creator_trust(creator_id)
¶
Get average trust score for a creator across all content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
creator_id
|
str
|
Creator identifier |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average trust score |
Source code in qfzz/blockchain/trust_network.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
get_latest_block()
¶
Get the latest block in the chain.
Source code in qfzz/blockchain/trust_network.py
242 243 244 | |
get_statistics()
¶
Get blockchain statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of statistics |
Source code in qfzz/blockchain/trust_network.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
get_trust_record(content_id, creator_id)
¶
Get trust record for content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_id
|
str
|
Content identifier |
required |
creator_id
|
str
|
Creator identifier |
required |
Returns:
| Type | Description |
|---|---|
Optional[TrustRecord]
|
TrustRecord if found, None otherwise |
Source code in qfzz/blockchain/trust_network.py
167 168 169 170 171 172 173 174 175 176 177 178 179 | |
get_trust_score(content_id, creator_id)
¶
Get trust score for content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_id
|
str
|
Content identifier |
required |
creator_id
|
str
|
Creator identifier |
required |
Returns:
| Type | Description |
|---|---|
float
|
Trust score (0.0-1.0), default 0.5 if not found |
Source code in qfzz/blockchain/trust_network.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
is_chain_valid()
¶
Validate the entire blockchain.
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise |
Source code in qfzz/blockchain/trust_network.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
mine_pending_records()
¶
Mine pending records into a new block.
Returns:
| Type | Description |
|---|---|
Optional[Block]
|
Newly mined block, or None if no pending records |
Source code in qfzz/blockchain/trust_network.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
report_content(content_id, creator_id)
¶
Add report to content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_id
|
str
|
Content identifier |
required |
creator_id
|
str
|
Creator identifier |
required |
Source code in qfzz/blockchain/trust_network.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
verify_content(content_id, creator_id)
¶
Add verification to content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_id
|
str
|
Content identifier |
required |
creator_id
|
str
|
Creator identifier |
required |
Source code in qfzz/blockchain/trust_network.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
TrustRecord¶
qfzz.blockchain.trust_record.TrustRecord
dataclass
¶
Record of trust transaction
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
str
|
User identifier |
action |
str
|
Action type (e.g., "interaction", "rating", "verification") |
target |
str
|
Target of the action |
trust_delta |
float
|
Change in trust score |
timestamp |
datetime
|
Record creation timestamp |
Source code in qfzz/blockchain/trust_record.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
Block¶
qfzz.blockchain.trust_record.Block
dataclass
¶
Represents a block in the trust chain
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
Block index in the chain |
timestamp |
datetime
|
Block creation timestamp |
data |
dict[str, Any]
|
Block data payload |
previous_hash |
str
|
Hash of the previous block |
hash |
str
|
Hash of this block |
Source code in qfzz/blockchain/trust_record.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
calculate_hash()
¶
Calculate block hash using SHA-256
Returns:
| Type | Description |
|---|---|
str
|
Calculated hash string |
Source code in qfzz/blockchain/trust_record.py
27 28 29 30 31 32 33 34 | |
Usage Example¶
from qfzz import BlockchainTrustNetwork, TrustRecord
# Create blockchain
blockchain = BlockchainTrustNetwork()
# Add trust records
record = TrustRecord("user_001", "interaction", "dj", 0.05)
blockchain.add_trust_record(record)
# Mine block
block = blockchain.mine_block()
print(f"Mined block #{block.index}")
# Verify chain
is_valid = blockchain.verify_chain()
print(f"Chain valid: {is_valid}")
# Get trust score
score = blockchain.get_trust_score("user_001")
print(f"Trust score: {score}")