租房甲醛检测存证程序,检测报告上链,房东无法造假,保障租客健康。
用 Python 构建一个“租房甲醛检测存证区块链原型系统”将「甲醛检测报告」上链实现检测数据不可篡改、房东无法事后造假、租客可自证。整体定位为课程级区块链应用示例非商业级产品。一、实际应用场景描述在城市租房市场尤其是长租公寓、二房东转租场景中常出现- 刚装修完就出租- 甲醛等空气质量指标超标- 租客入住后出现头晕、过敏等症状传统模式下- 检测报告多为 PDF / 纸质- 房东可自行找机构“美化数据”- 报告日期可伪造- 发生纠纷时难以举证本示例设计一个 甲醛检测报告上链存证系统- 每次检测生成一份“检测交易”- 交易被打包进区块- 区块通过哈希链式存储- 租客、平台、第三方均可验证报告真伪二、引入痛点为什么要用区块链传统方式 区块链方式PDF 报告可 PS 哈希上链无法篡改检测时间可伪造 时间戳不可更改房东单方委托 多方可验证纠纷举证难 链上即为证据监管滞后 实时可查说明本示例不替代正规检测机构流程而是用于理解区块链在“健康权益存证”中的技术价值。三、核心逻辑讲解简化模型1️⃣ 数据模型检测报告Transaction房源ID房东ID检测机构ID检测时间甲醛浓度mg/m³是否超标报告原文哈希注真实系统中应存 PDF 哈希 IPFS 地址本示例简化为结构化数据。2️⃣ 核心流程检测机构现场采样↓生成检测报告↓计算报告哈希↓构造交易↓上链存证↓租客/平台查询验证3️⃣ 防造假逻辑- 报告内容 → 哈希- 哈希写入区块- 区块链接前后区块- 修改任何字段 → 哈希变化 → 验证失败四、代码模块化Python 项目结构rental_formaldehyde_trace/│├── transaction.py # 检测报告交易├── block.py # 区块├── blockchain.py # 区块链逻辑├── verify.py # 验证模块├── main.py # 示例运行入口├── README.md└── USAGE.md1️⃣ transaction.py检测报告import hashlibimport jsonfrom datetime import datetimeclass FormaldehydeReport:甲醛检测报告交易def __init__(self,house_id,landlord_id,agency_id,formaldehyde_value,threshold0.08,timestampNone):self.house_id house_idself.landlord_id landlord_idself.agency_id agency_idself.formaldehyde_value formaldehyde_valueself.threshold thresholdself.is_exceeded formaldehyde_value thresholdself.timestamp timestamp or datetime.utcnow().isoformat()def to_dict(self):return {house_id: self.house_id,landlord_id: self.landlord_id,agency_id: self.agency_id,formaldehyde_value: self.formaldehyde_value,is_exceeded: self.is_exceeded,timestamp: self.timestamp}def compute_hash(self):report_string json.dumps(self.to_dict(), sort_keysTrue)return hashlib.sha256(report_string.encode()).hexdigest()2️⃣ block.py区块import hashlibimport jsonclass Block:区块链中的区块def __init__(self, index, transactions, previous_hash, nonce0):self.index indexself.transactions transactionsself.previous_hash previous_hashself.nonce nonceself.hash self.compute_hash()def compute_hash(self):block_content {index: self.index,transactions: [tx.to_dict() for tx in self.transactions],previous_hash: self.previous_hash,nonce: self.nonce}block_string json.dumps(block_content, sort_keysTrue)return hashlib.sha256(block_string.encode()).hexdigest()3️⃣ blockchain.py链逻辑from block import Blockclass FormaldehydeBlockchain:甲醛检测存证区块链def __init__(self):self.chain []self.pending_transactions []self.create_genesis_block()def create_genesis_block(self):genesis_block Block(0, [], 0)self.chain.append(genesis_block)def add_report(self, report):self.pending_transactions.append(report)def mine_reports(self):if not self.pending_transactions:return Nonelast_block self.chain[-1]new_block Block(indexlen(self.chain),transactionsself.pending_transactions,previous_hashlast_block.hash)self.pending_transactions []self.chain.append(new_block)return new_blockdef is_chain_valid(self):for i in range(1, len(self.chain)):current self.chain[i]previous self.chain[i - 1]if current.previous_hash ! previous.hash:return Falseif current.hash ! current.compute_hash():return Falsereturn True4️⃣ verify.py验证模块def verify_report(chain, report_hash):根据报告哈希验证是否存在且未被篡改for block in chain:for tx in block.transactions:if tx.compute_hash() report_hash:return True, tx.to_dict()return False, None5️⃣ main.py示例运行from blockchain import FormaldehydeBlockchainfrom transaction import FormaldehydeReportfrom verify import verify_reportblockchain FormaldehydeBlockchain()# 模拟一次甲醛检测report FormaldehydeReport(house_idHZ202401,landlord_idL001,agency_idAGENCY_A,formaldehyde_value0.12)blockchain.add_report(report)blockchain.mine_reports()# 租客拿到报告哈希report_hash report.compute_hash()# 验证valid, data verify_report(blockchain.chain, report_hash)print(验证结果, valid)print(报告内容, data)五、README.md# 租房甲醛检测存证区块链原型## 项目简介本项目是一个基于 Python 的区块链教学示例用于演示如何将甲醛检测报告上链存证防止房东造假。## 功能- 甲醛检测报告建模- 报告哈希上链- 区块链完整性校验- 报告真实性验证## 适用场景- 区块链与创新思维课程- 租房权益保护教学案例- 存证系统设计练习## 运行方式bashpython main.py六、使用说明USAGE.md1. 安装 Python 3.82. 进入项目目录3. 执行python main.py4. 查看控制台输出的验证结果七、核心知识点卡片知识点 说明交易 单份检测报告哈希 报告唯一指纹区块 批量存证链式结构 防篡改时间戳 确定检测时间验证函数 租客自证八、总结本示例展示了- 如何用 Python 构建健康权益存证区块链- 如何将甲醛检测报告关键数据上链- 如何通过哈希验证防止房东事后造假✅ 优点- 逻辑简单- 易于教学- 贴近现实社会问题⚠️ 局限- 未接入真实检测设备- 未处理隐私数据- 未引入多方签名 / 智能合约利用AI解决实际问题如果你觉得这个工具好用欢迎关注长安牧笛