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

     1  package schema
     2  
     3  import (
     4  	cstypes "github.com/badrootd/celestia-core/consensus/types"
     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  // ConsensusTables returns the list of tables that are used for consensus
    11  // tracing.
    12  func ConsensusTables() []string {
    13  	return []string{
    14  		RoundStateTable,
    15  		BlockPartsTable,
    16  		BlockTable,
    17  		VoteTable,
    18  	}
    19  }
    20  
    21  // Schema constants for the consensus round state tracing database.
    22  const (
    23  	// RoundStateTable is the name of the table that stores the consensus
    24  	// state traces. Follows this schema:
    25  	//
    26  	// | time | height | round | step |
    27  	RoundStateTable = "consensus_round_state"
    28  
    29  	// StepFieldKey is the name of the field that stores the consensus step. The
    30  	// value is a string.
    31  	StepFieldKey = "step"
    32  )
    33  
    34  // WriteRoundState writes a tracing point for a tx using the predetermined
    35  // schema for consensus state tracing. This is used to create a table in the following
    36  // schema:
    37  //
    38  // | time | height | round | step |
    39  func WriteRoundState(client *trace.Client, height int64, round int32, step cstypes.RoundStepType) {
    40  	client.WritePoint(RoundStateTable, map[string]interface{}{
    41  		HeightFieldKey: height,
    42  		RoundFieldKey:  round,
    43  		StepFieldKey:   step.String(),
    44  	})
    45  }
    46  
    47  // Schema constants for the "consensus_block_parts" table.
    48  const (
    49  	// BlockPartsTable is the name of the table that stores the consensus block
    50  	// parts.
    51  	// following schema:
    52  	//
    53  	// | time | height | round | index | peer | transfer type |
    54  	BlockPartsTable = "consensus_block_parts"
    55  
    56  	// BlockPartIndexFieldKey is the name of the field that stores the block
    57  	// part
    58  	BlockPartIndexFieldKey = "index"
    59  )
    60  
    61  // WriteBlockPart writes a tracing point for a BlockPart using the predetermined
    62  // schema for consensus state tracing. This is used to create a table in the
    63  // following schema:
    64  //
    65  // | time | height | round | index | peer | transfer type |
    66  func WriteBlockPart(
    67  	client *trace.Client,
    68  	height int64,
    69  	round int32,
    70  	peer p2p.ID,
    71  	index uint32,
    72  	transferType string,
    73  ) {
    74  	// this check is redundant to what is checked during WritePoint, although it
    75  	// is an optimization to avoid allocations from the map of fields.
    76  	if !client.IsCollecting(BlockPartsTable) {
    77  		return
    78  	}
    79  	client.WritePoint(BlockPartsTable, map[string]interface{}{
    80  		HeightFieldKey:         height,
    81  		RoundFieldKey:          round,
    82  		BlockPartIndexFieldKey: index,
    83  		PeerFieldKey:           peer,
    84  		TransferTypeFieldKey:   transferType,
    85  	})
    86  }
    87  
    88  const (
    89  	// BlockTable is the name of the table that stores metadata about consensus blocks.
    90  	// following schema:
    91  	//
    92  	//  | time  | height | timestamp |
    93  	BlockTable = "consensus_block"
    94  
    95  	// UnixMillisecondTimestampFieldKey is the name of the field that stores the timestamp in
    96  	// the last commit in unix milliseconds.
    97  	UnixMillisecondTimestampFieldKey = "unix_millisecond_timestamp"
    98  
    99  	// TxCountFieldKey is the name of the field that stores the number of
   100  	// transactions in the block.
   101  	TxCountFieldKey = "tx_count"
   102  
   103  	// SquareSizeFieldKey is the name of the field that stores the square size
   104  	// of the block. SquareSize is the number of shares in a single row or
   105  	// column of the original data square.
   106  	SquareSizeFieldKey = "square_size"
   107  
   108  	// BlockSizeFieldKey is the name of the field that stores the size of
   109  	// the block data in bytes.
   110  	BlockSizeFieldKey = "block_size"
   111  
   112  	// ProposerFieldKey is the name of the field that stores the proposer of
   113  	// the block.
   114  	ProposerFieldKey = "proposer"
   115  
   116  	// LastCommitRoundFieldKey is the name of the field that stores the round
   117  	// of the last commit.
   118  	LastCommitRoundFieldKey = "last_commit_round"
   119  )
   120  
   121  func WriteBlock(client *trace.Client, block *types.Block, size int) {
   122  	client.WritePoint(BlockTable, map[string]interface{}{
   123  		HeightFieldKey:                   block.Height,
   124  		UnixMillisecondTimestampFieldKey: block.Time.UnixMilli(),
   125  		TxCountFieldKey:                  len(block.Data.Txs),
   126  		SquareSizeFieldKey:               block.SquareSize,
   127  		BlockSizeFieldKey:                size,
   128  		ProposerFieldKey:                 block.ProposerAddress.String(),
   129  		LastCommitRoundFieldKey:          block.LastCommit.Round,
   130  	})
   131  }
   132  
   133  // Schema constants for the consensus votes tracing database.
   134  const (
   135  	// VoteTable is the name of the table that stores the consensus
   136  	// voting traces. Follows this schema:
   137  	//
   138  	// | time | height | round | vote_type | vote_height | vote_round
   139  	// | vote_block_id| vote_unix_millisecond_timestamp
   140  	// | vote_validator_address | vote_validator_index | peer
   141  	// | transfer_type |
   142  	VoteTable = "consensus_vote"
   143  
   144  	VoteTypeFieldKey         = "vote_type"
   145  	VoteHeightFieldKey       = "vote_height"
   146  	VoteRoundFieldKey        = "vote_round"
   147  	VoteBlockIDFieldKey      = "vote_block_id"
   148  	VoteTimestampFieldKey    = "vote_unix_millisecond_timestamp"
   149  	ValidatorAddressFieldKey = "vote_validator_address"
   150  	ValidatorIndexFieldKey   = "vote_validator_index"
   151  )
   152  
   153  // WriteVote writes a tracing point for a vote using the predetermined
   154  // schema for consensus vote tracing.
   155  // This is used to create a table in the following
   156  // schema:
   157  //
   158  // | time | height | round | vote_type | vote_height | vote_round
   159  // | vote_block_id| vote_unix_millisecond_timestamp
   160  // | vote_validator_address | vote_validator_index | peer
   161  // | transfer_type |
   162  func WriteVote(client *trace.Client,
   163  	height int64, // height of the current peer when it received/sent the vote
   164  	round int32, // round of the current peer when it received/sent the vote
   165  	vote *types.Vote, // vote received by the current peer
   166  	peer p2p.ID, // the peer from which it received the vote or the peer to which it sent the vote
   167  	transferType string, // download (received) or upload(sent)
   168  ) {
   169  	client.WritePoint(VoteTable, map[string]interface{}{
   170  		HeightFieldKey:           height,
   171  		RoundFieldKey:            round,
   172  		VoteTypeFieldKey:         vote.Type.String(),
   173  		VoteHeightFieldKey:       vote.Height,
   174  		VoteRoundFieldKey:        vote.Round,
   175  		VoteBlockIDFieldKey:      vote.BlockID.Hash.String(),
   176  		VoteTimestampFieldKey:    vote.Timestamp.UnixMilli(),
   177  		ValidatorAddressFieldKey: vote.ValidatorAddress.String(),
   178  		ValidatorIndexFieldKey:   vote.ValidatorIndex,
   179  		PeerFieldKey:             peer,
   180  		TransferTypeFieldKey:     transferType,
   181  	})
   182  }