@@ -238,4 +238,96 @@ BitTorrent 旨在解决依赖中心化服务器下载大文件导致的网络堵
238
238
这下他十分生气,决定做接收者并且再做坏事,当他母亲给他打生活费时,他复制了这笔交易企图获得更多,
239
239
但也被拒绝了,因为Nonce被使用过了,防御重放成功。
240
240
241
+ ### 2025.02.15
242
+
243
+ #### 执行层
244
+ 简而言之,执行层相当于比特币区块链去除PoW的部分,仅起到验证区块和更新区块的作用。
245
+
246
+ ##### 验证区块
247
+ 客户端需要:
248
+ - 上一个区块
249
+ - 当前区块
250
+ - StateDB 状态数据库:账户信息
251
+ 一旦发生错误将会唤起错误,简单代码体现:
252
+ ``` python
253
+ def stf (parent_block : Block, current_block : Block, state : StateDB) -> StateDB:
254
+ # something validations
255
+ try :
256
+ verify_headers(parent_block, current_block)
257
+ except Error as error:
258
+ return (state, error) # the state no updated
259
+
260
+ state, error = verify_transactions(current_block, state)
261
+ if error:
262
+ return (state, error) # the state no updated
263
+ return state
264
+ ```
265
+
266
+ ###### 标头验证
267
+ ``` python
268
+ def verify_headers (parent_block : Block, current_block : Block) -> Optional[error]:
269
+ # something validations
270
+ # error = None or others
271
+ if error:
272
+ raise Error(" ..." )
273
+ ```
274
+ 一个有意思的错误是gas超过上限,而gas上限是被硬编码的,这也制约了智能合约的复杂性。
275
+
276
+ ###### 交易验证
277
+ ``` python
278
+ def verify_transactions (current_block : Block, state : StateDB) -> Tuple[state, Optional[error]]:
279
+ origin_state = state.copy()
280
+ updated_state = state.copy()
281
+ good_tx = []
282
+
283
+ for n, tx in enumerate (current_block):
284
+ try :
285
+ # some validations
286
+ except Error as error:
287
+ return origin_state, error
288
+ else :
289
+ good_tx.append(tx)
290
+ updated_state = update_state(updated_state, good_tx)
291
+ return (updated_state, None )
292
+ ```
293
+ 错了就返回原状态和错误原因,正确就更新。
294
+
295
+ ###### 检验区块验证
296
+ 这个东东负责告诉信标链当前区块是否验证成功
297
+ ``` python
298
+ def new_payload (exec_payload : ExecutionPaylaod) -> bool :
299
+ _, error = stf(exec_payload.parent_block, exec_payload.current_block, exec_payload.state)
300
+
301
+ if error:
302
+ return False
303
+
304
+ return True
305
+ ```
306
+
307
+ ##### 构建区块
308
+ 构建区块需要:
309
+ - Environment 环境:包括一些信息,比如标头(时间戳、gas多少)、共识层的信息等
310
+ - TxPool 交易池:一个包含交易的列表,通过其价值降序排列。这个池也是根据广播中优先费最高的组成的。换句话说这些Gas信息没加密
311
+ - StateDB 状态数据库
312
+
313
+ ``` python
314
+ def build (env : Environment, pool : TxPool, state : StateDB) -> Tuple[Block, StateDB, error]:
315
+ gas_used = 0 # 确定gas用量
316
+ txs = []
317
+
318
+ # Gas达到上限或池是非空
319
+ while gas_used < env.gas_limit or not pool.empty():
320
+ tx = pool.pop()
321
+ updated_state, gas, err = vm.run(env, tx, state)
322
+ if err is not None :
323
+ # tx invalid
324
+ continue
325
+ gas_used += gas
326
+ txs.append(tx)
327
+ state = updated_state
328
+
329
+ # 通过一个外包的函数回传,用于计算生产完整的块,比如一些数据压缩计算
330
+ return finalize(env, txs, state)
331
+ ```
332
+
241
333
<!-- Content_END -->
0 commit comments