写点什么

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

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

    阅读完需:约 4 分钟

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


我们先看 single 类型的交易池实现

// AddTx add tx form RPC or P2P to pool//添加一笔交易到交易池func (pool *txPoolImpl) AddTx(tx *commonPb.Transaction, source protocol.TxSource) error {	// should not be stopped  //检查交易池状态是否停止	if atomic.LoadInt32(&pool.stat) == 0 {		return commonErrors.ErrTxPoolHasStopped	}	// should not be nil //交易不能为空	if tx == nil || tx.Payload == nil {		return commonErrors.ErrStructEmpty	}	// the transaction from RPC or P2P can put into pool by AddTx  //交易必须是rpc或者p2p来源	if source == protocol.INTERNAL {		return commonErrors.ErrTxSource	}	// verify whether the tx exist in pool  //检查交易是否已经存在池的队列中了	if pool.queue.has(tx, true) {		pool.log.Warnf("transaction exists in pool, txId:%s", tx.Payload.TxId)		return commonErrors.ErrTxIdExist	}	// verify whether the tx is out of date, signature/format is right(P2P), tx exist in db	//验证交易是否超时,签名格式是否正确,交易是否存在于db中了  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.isFull(tx) {    //如果满了,是rpc来源,把交易广播出去		if source == protocol.RPC {			pool.broadcastTx(tx.Payload.TxId, mustMarshal(tx))		}		return commonErrors.ErrTxPoolLimit	}	pool.log.Debugw("AddTx", "txId", tx.Payload.TxId, "source", source)	// attempt to add transaction to pool  //定义一个内存交易池	memTxs := &mempoolTxs{isConfigTxs: false, mtxs: []*memTx{mtx}, source: source}  //是否是配置交易	if isConfigTx(tx, pool.chainConf) {		memTxs.isConfigTxs = true	}  //定时器	t := time.NewTimer(time.Second)	defer t.Stop()  	select {  //交易池添加通道收到内存池	case pool.addTxsCh <- memTxs:  //超时	case <-t.C:		pool.log.Warnf("add transaction timeout, txId:%s, source:%v", tx.Payload.TxId, source)		return fmt.Errorf("add transaction timeout, txId:%s", tx.Payload.TxId)	}	// broadcast the transaction to other nodes  //如果来源是rpc模块	if source == protocol.RPC {		// simulate no broadcast transaction to test recover mechanism		if localconf.ChainMakerConfig.DebugConfig.IsNoBroadcastTx {			pool.log.Warnf("simulate no broadcast transaction, txId:%s", tx.Payload.TxId)		} else {			// broadcast transaction			// attention: must shallow copy transaction!!!			// While marshaling transaction, VM module may be adding execution result to the transaction for proposer,			// which can cause panic.      //交易池广播交易			pool.broadcastTx(tx.Payload.TxId, mustMarshal(copyTx(tx)))		}	}	return nil}
复制代码


用户头像

关注

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

还未添加个人简介

评论

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