以太坊源码学习(五)提交交易之交易池

/internet/ethapi/api.go

// submitTransaction is a helper function that submits tx to txPool and logs a message.
func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) {
   if err := b.SendTx(ctx, tx); err != nil {
      return common.Hash{}, err
   }
   if tx.To() == nil {
      signer := types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Number())
      from, err := types.Sender(signer, tx)
      if err != nil {
         return common.Hash{}, err
      }
      addr := crypto.CreateAddress(from, tx.Nonce())
      log.Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", addr.Hex())
   } else {
      log.Info("Submitted transaction", "fullhash", tx.Hash().Hex(), "recipient", tx.To())
   }
   return tx.Hash(), nil
}

先看下交易池 /core/tx_pool.go

type TxPool struct {
   config       TxPoolConfig
   chainconfig  *params.ChainConfig
   chain        blockChain
   gasPrice     *big.Int
   txFeed       event.Feed
   scope        event.SubscriptionScope
   chainHeadCh  chan ChainHeadEvent
   chainHeadSub event.Subscription
   signer       types.Signer
   mu           sync.RWMutex

   currentState  *state.StateDB      // Current state in the blockchain head
   pendingState  *state.ManagedState // Pending state tracking virtual nonces
   currentMaxGas uint64              // Current gas limit for transaction caps

   locals  *accountSet // Set of local transaction to exempt from eviction rules
   journal *txJournal  // Journal of local transaction to back up to disk

   // 从queue到pending到虚拟机执行
   pending map[common.Address]*txList   // All currently processable transactions
   queue   map[common.Address]*txList   // Queued but non-processable transactions
   beats   map[common.Address]time.Time // Last heartbeat from each known account
   all     *txLookup                    // All transactions to allow lookups
   priced  *txPricedList                // All transactions sorted by price

   wg sync.WaitGroup // for shutdown sync

   homestead bool
}
// txList is a "list" of transactions belonging to an account, sorted by account
// nonce. The same type can be used both for storing contiguous transactions for
// the executable/pending queue; and for storing gapped transactions for the non-
// executable/future queue, with minor behavioral changes.
type txList struct {
   strict bool         // Whether nonces are strictly continuous or not
   // 实现按照nonece排序
   txs    *txSortedMap // Heap indexed sorted hash map of the transactions

   costcap *big.Int // Price of the highest costing transaction (reset only if exceeds balance)
   gascap  uint64   // Gas limit of the highest spending transaction (reset only if exceeds block limit)
}
// txSortedMap is a nonce->transaction hash map with a heap based index to allow
// iterating over the contents in a nonce-incrementing way.
type txSortedMap struct {
   // 包含两张表 1.nonce 交易 2.noce最小gas最大的排序
   items map[uint64]*types.Transaction // Hash map storing the transaction data
   index *nonceHeap                    // Heap of nonces of all the stored transactions (non-strict mode)
   cache types.Transactions            // Cache of the transactions already sorted
}

你可能感兴趣的:(区块链)