github.com/vipernet-xyz/tm@v0.34.24/mempool/v1/tx.go (about)

     1  package v1
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/vipernet-xyz/tm/types"
     8  )
     9  
    10  // WrappedTx defines a wrapper around a raw transaction with additional metadata
    11  // that is used for indexing.
    12  type WrappedTx struct {
    13  	tx        types.Tx    // the original transaction data
    14  	hash      types.TxKey // the transaction hash
    15  	height    int64       // height when this transaction was initially checked (for expiry)
    16  	timestamp time.Time   // time when transaction was entered (for TTL)
    17  
    18  	mtx       sync.Mutex
    19  	gasWanted int64           // app: gas required to execute this transaction
    20  	priority  int64           // app: priority value for this transaction
    21  	sender    string          // app: assigned sender label
    22  	peers     map[uint16]bool // peer IDs who have sent us this transaction
    23  }
    24  
    25  // Size reports the size of the raw transaction in bytes.
    26  func (w *WrappedTx) Size() int64 { return int64(len(w.tx)) }
    27  
    28  // SetPeer adds the specified peer ID as a sender of w.
    29  func (w *WrappedTx) SetPeer(id uint16) {
    30  	w.mtx.Lock()
    31  	defer w.mtx.Unlock()
    32  	if w.peers == nil {
    33  		w.peers = map[uint16]bool{id: true}
    34  	} else {
    35  		w.peers[id] = true
    36  	}
    37  }
    38  
    39  // HasPeer reports whether the specified peer ID is a sender of w.
    40  func (w *WrappedTx) HasPeer(id uint16) bool {
    41  	w.mtx.Lock()
    42  	defer w.mtx.Unlock()
    43  	_, ok := w.peers[id]
    44  	return ok
    45  }
    46  
    47  // SetGasWanted sets the application-assigned gas requirement of w.
    48  func (w *WrappedTx) SetGasWanted(gas int64) {
    49  	w.mtx.Lock()
    50  	defer w.mtx.Unlock()
    51  	w.gasWanted = gas
    52  }
    53  
    54  // GasWanted reports the application-assigned gas requirement of w.
    55  func (w *WrappedTx) GasWanted() int64 {
    56  	w.mtx.Lock()
    57  	defer w.mtx.Unlock()
    58  	return w.gasWanted
    59  }
    60  
    61  // SetSender sets the application-assigned sender of w.
    62  func (w *WrappedTx) SetSender(sender string) {
    63  	w.mtx.Lock()
    64  	defer w.mtx.Unlock()
    65  	w.sender = sender
    66  }
    67  
    68  // Sender reports the application-assigned sender of w.
    69  func (w *WrappedTx) Sender() string {
    70  	w.mtx.Lock()
    71  	defer w.mtx.Unlock()
    72  	return w.sender
    73  }
    74  
    75  // SetPriority sets the application-assigned priority of w.
    76  func (w *WrappedTx) SetPriority(p int64) {
    77  	w.mtx.Lock()
    78  	defer w.mtx.Unlock()
    79  	w.priority = p
    80  }
    81  
    82  // Priority reports the application-assigned priority of w.
    83  func (w *WrappedTx) Priority() int64 {
    84  	w.mtx.Lock()
    85  	defer w.mtx.Unlock()
    86  	return w.priority
    87  }