写点什么

长安链源码分析之交易过程分析(6)

作者:
  • 2022-10-25
    湖南
  • 本文字数:1028 字

    阅读完需:约 3 分钟

本文已参与「开源摘星计划」,欢迎正在阅读的你加入。活动链接:https://github.com/weopenprojects/WeOpen-Star

继续看 normal 实现


func (pool *normalPool) AddTx(tx *commonPb.Transaction, source protocol.TxSource) error {   // should not be stopped   //检查交易池不能停止   if atomic.LoadInt32(&pool.stat) == 0 {      return commonErr.ErrTxPoolHasStopped   }   // should not be nil   //交易不能为空   if tx == nil || tx.Payload == nil {      return commonErr.ErrStructEmpty   }   // verify tx source   //验证交易来源   if source == protocol.INTERNAL {      return commonErr.ErrTxSource   }   // verify whether timestamp is out of date, signature and format(from P2P) is valid, and exist in db   // attention: whether tx exist in pool is verified when add to txList   //验证交易是否超时,签名格式,   mTx, err := pool.validateTx(tx, source)   if err != nil {      return err   }   // verify whether the tx pool is full, even if tx pool is full, broadcast valid tx from RPC to other nodes  //判断交易池的队列是否满了  if pool.queue.isFull(tx) {      if source == protocol.RPC {      //如果满了,并且是rpc来源,则广播交易         pool.broadcastTx(tx.Payload.TxId, mustMarshal(tx))      }      return commonErr.ErrTxPoolLimit   }   pool.log.Debugw("AddTx", "txId", tx.Payload.GetTxId(), "source", source)   // attempt add tx to addTxCh   t := time.NewTimer(time.Second)   defer t.Stop()   select {   //交易池添加通道是否收到交易   case pool.addTxCh <- mTx:   //超时   case <-t.C:      pool.log.Warnf("add transaction timeout, txId:%s, source:%v", mTx.getTxId(), source)      return fmt.Errorf("add transaction timeout")   }   // notify txBatchBuilder to build and broadcast tx batch   // attention: must shallow copy transaction!!!   // While batchBuilder serializing transaction, VM module may be adding execution result to the transaction   // for proposer, which can cause panic.   if source == protocol.RPC {   //复制一笔交易放到batch通道里      pool.batchBuilder.getBuildBatchCh() <- copyTx(tx)   }   return nil}
复制代码


用户头像

关注

还未添加个人签名 2018-05-04 加入

还未添加个人简介

评论

发布
暂无评论
长安链源码分析之交易过程分析(6)_李_InfoQ写作社区