github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/pkg/trace/schema/mempool.go (about)

     1  package schema
     2  
     3  import (
     4  	"github.com/badrootd/celestia-core/libs/bytes"
     5  	"github.com/badrootd/celestia-core/p2p"
     6  	"github.com/badrootd/celestia-core/pkg/trace"
     7  	"github.com/badrootd/celestia-core/types"
     8  )
     9  
    10  // MempoolTables returns the list of tables for mempool tracing.
    11  func MempoolTables() []string {
    12  	return []string{
    13  		MempoolTxTable,
    14  		MempoolPeerStateTable,
    15  		MempoolRejectedTable,
    16  	}
    17  }
    18  
    19  // Schema constants for the mempool_tx table
    20  const (
    21  	// MempoolTxTable is the tracing "measurement" (aka table) for the mempool
    22  	// that stores tracing data related to gossiping transactions.
    23  	//
    24  	// The schema for this table is:
    25  	// | time | peerID | tx size | tx hash | transfer type | mempool version |
    26  	MempoolTxTable = "mempool_tx"
    27  
    28  	// TxFieldKey is the tracing field key for receiving for sending a
    29  	// tx. This should take the form of a tx hash as the value.
    30  	TxFieldKey = "tx"
    31  
    32  	// SizeFieldKey is the tracing field key for the size of a tx. This
    33  	// should take the form of the size of the tx as the value.
    34  	SizeFieldKey = "size"
    35  
    36  	// VersionFieldKey is the tracing field key for the version of the mempool.
    37  	// This is used to distinguish between versions of the mempool.
    38  	VersionFieldKey = "version"
    39  
    40  	// V1VersionFieldValue is a tracing field value for the version of
    41  	// the mempool. This value is used by the "version" field key.
    42  	V1VersionFieldValue = "v1"
    43  
    44  	// CatVersionFieldValue is a tracing field value for the version of
    45  	// the mempool. This value is used by the "version" field key.
    46  	CatVersionFieldValue = "cat"
    47  )
    48  
    49  // WriteMempoolTx writes a tracing point for a tx using the predetermined
    50  // schema for mempool tracing. This is used to create a table in the following
    51  // schema:
    52  //
    53  // | time | peerID | tx size | tx hash | transfer type | mempool version |
    54  func WriteMempoolTx(client *trace.Client, peer p2p.ID, tx []byte, transferType, version string) {
    55  	// this check is redundant to what is checked during WritePoint, although it
    56  	// is an optimization to avoid allocations from the map of fields.
    57  	if !client.IsCollecting(MempoolTxTable) {
    58  		return
    59  	}
    60  	client.WritePoint(MempoolTxTable, map[string]interface{}{
    61  		TxFieldKey:           bytes.HexBytes(types.Tx(tx).Hash()).String(),
    62  		PeerFieldKey:         peer,
    63  		SizeFieldKey:         len(tx),
    64  		TransferTypeFieldKey: transferType,
    65  		VersionFieldKey:      version,
    66  	})
    67  }
    68  
    69  const (
    70  	// MempoolPeerState is the tracing "measurement" (aka table) for the mempool
    71  	// that stores tracing data related to mempool state, specifically
    72  	// the gossipping of "SeenTx" and "WantTx".
    73  	//
    74  	// The schema for this table is:
    75  	// | time | peerID | update type | mempool version |
    76  	MempoolPeerStateTable = "mempool_peer_state"
    77  
    78  	// StateUpdateFieldKey is the tracing field key for state updates of the mempool.
    79  	StateUpdateFieldKey = "update"
    80  
    81  	// SeenTxStateUpdateFieldValue is a tracing field value for the state
    82  	// update of the mempool. This value is used by the "update" field key.
    83  	SeenTxStateUpdateFieldValue = "seen_tx"
    84  
    85  	// WantTxStateUpdateFieldValue is a tracing field value for the state
    86  	// update of the mempool. This value is used by the "update" field key.
    87  	WantTxStateUpdateFieldValue = "want_tx"
    88  
    89  	// RemovedTxStateUpdateFieldValue is a tracing field value for the local
    90  	// state update of the mempool. This value is used by the "update" field
    91  	// key.
    92  	RemovedTxStateUpdateFieldValue = "removed_tx"
    93  
    94  	// AddedTxStateUpdateFieldValue is a tracing field value for the local state
    95  	// update of the mempool. This value is used by the "update" field key.
    96  	AddedTxStateUpdateFieldValue = "added_tx"
    97  )
    98  
    99  // WriteMempoolPeerState writes a tracing point for the mempool state using
   100  // the predetermined schema for mempool tracing. This is used to create a table
   101  // in the following schema:
   102  //
   103  // | time | peerID | transfer type | state update | mempool version |
   104  func WriteMempoolPeerState(client *trace.Client, peer p2p.ID, stateUpdate, transferType, version string) {
   105  	// this check is redundant to what is checked during WritePoint, although it
   106  	// is an optimization to avoid allocations from creating the map of fields.
   107  	if !client.IsCollecting(MempoolPeerStateTable) {
   108  		return
   109  	}
   110  	client.WritePoint(MempoolPeerStateTable, map[string]interface{}{
   111  		PeerFieldKey:         peer,
   112  		TransferTypeFieldKey: transferType,
   113  		StateUpdateFieldKey:  stateUpdate,
   114  		VersionFieldKey:      version,
   115  	})
   116  }
   117  
   118  const (
   119  	MempoolRejectedTable = "mempool_rejected"
   120  	ReasonFieldKey       = "reason"
   121  )
   122  
   123  // WriteMempoolRejected records why a transaction was rejected.
   124  func WriteMempoolRejected(client *trace.Client, reason string) {
   125  	// this check is redundant to what is checked during WritePoint, although it
   126  	// is an optimization to avoid allocations from creating the map of fields.
   127  	if !client.IsCollecting(MempoolRejectedTable) {
   128  		return
   129  	}
   130  	client.WritePoint(MempoolRejectedTable, map[string]interface{}{
   131  		ReasonFieldKey: reason,
   132  	})
   133  }