code.vegaprotocol.io/vega@v0.79.0/core/stats/blockchain.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package stats
    17  
    18  // Blockchain hold stats over all the vega node.
    19  type Blockchain struct {
    20  	hash                   string
    21  	height                 uint64
    22  	averageTxSizeBytes     uint64
    23  	averageTxPerBatch      uint64
    24  	totalTxCurrentBatch    uint64
    25  	totalTxLastBatch       uint64
    26  	totalOrdersLastBatch   uint64
    27  	totalTradesLastBatch   uint64
    28  	averageOrdersPerBatch  uint64
    29  	currentOrdersInBatch   uint64
    30  	currentTradesInBatch   uint64
    31  	totalBatches           uint64
    32  	ordersPerSecond        uint64
    33  	tradesPerSecond        uint64
    34  	totalAmendOrder        uint64
    35  	totalCancelOrder       uint64
    36  	totalCreateOrder       uint64
    37  	totalOrders            uint64
    38  	totalTrades            uint64
    39  	blockDuration          uint64 // nanoseconds
    40  	lastBlockEventCount    uint64
    41  	currentBlockEventCount uint64
    42  	eventsPerSecond        uint64
    43  }
    44  
    45  // NewBlockchain instantiate a new Blockchain.
    46  func NewBlockchain() *Blockchain {
    47  	return &Blockchain{}
    48  }
    49  
    50  func (b *Blockchain) IncrementEventCount(count uint64) {
    51  	b.currentBlockEventCount += count
    52  }
    53  
    54  // IncTotalBatches increment total batches.
    55  func (b *Blockchain) IncTotalBatches() {
    56  	b.totalBatches++
    57  }
    58  
    59  // TotalBatches get total batches.
    60  func (b Blockchain) TotalBatches() uint64 {
    61  	return b.totalBatches
    62  }
    63  
    64  func (b *Blockchain) NewBatch() {
    65  	b.totalOrdersLastBatch = b.currentOrdersInBatch
    66  	b.totalTradesLastBatch = b.currentTradesInBatch
    67  	b.currentOrdersInBatch = 0
    68  	b.currentTradesInBatch = 0
    69  	b.lastBlockEventCount = b.currentBlockEventCount
    70  	b.currentBlockEventCount = 0
    71  }
    72  
    73  func (b *Blockchain) ResetBatchTotals() {
    74  	b.currentOrdersInBatch = 0
    75  	b.currentTradesInBatch = 0
    76  }
    77  
    78  func (b *Blockchain) IncCurrentOrdersInBatch() {
    79  	b.currentOrdersInBatch++
    80  }
    81  
    82  func (b *Blockchain) AddCurrentTradesInBatch(i uint64) {
    83  	b.currentTradesInBatch += i
    84  }
    85  
    86  func (b Blockchain) CurrentOrdersInBatch() uint64 {
    87  	return b.currentOrdersInBatch
    88  }
    89  
    90  func (b Blockchain) CurrentTradesInBatch() uint64 {
    91  	return b.currentTradesInBatch
    92  }
    93  
    94  func (b Blockchain) CurrentEventsInBatch() uint64 {
    95  	return b.currentBlockEventCount
    96  }
    97  
    98  func (b Blockchain) Hash() string {
    99  	return b.hash
   100  }
   101  
   102  // Height returns the current heights of the chain.
   103  func (b Blockchain) Height() uint64 {
   104  	return b.height
   105  }
   106  
   107  // IncHeight increment the height of the chain.
   108  func (b *Blockchain) IncHeight() {
   109  	b.height++
   110  }
   111  
   112  func (b *Blockchain) SetHeight(height uint64) {
   113  	b.height = height
   114  }
   115  
   116  func (b *Blockchain) SetHash(hash string) {
   117  	b.hash = hash
   118  }
   119  
   120  // AverageTxSizeBytes return the average size in bytes of the
   121  // transaction sent to vega.
   122  func (b *Blockchain) AverageTxSizeBytes() uint64 {
   123  	return b.averageTxSizeBytes
   124  }
   125  
   126  func (b *Blockchain) SetAverageTxSizeBytes(i uint64) {
   127  	b.averageTxSizeBytes = i
   128  }
   129  
   130  // AverageTxPerBatch return the average number of
   131  // transaction per block.
   132  func (b *Blockchain) AverageTxPerBatch() uint64 {
   133  	return b.averageTxPerBatch
   134  }
   135  
   136  func (b *Blockchain) SetAverageTxPerBatch(i uint64) {
   137  	b.averageTxPerBatch = i
   138  }
   139  
   140  func (b *Blockchain) TotalEventsLastBatch() uint64 {
   141  	return b.lastBlockEventCount
   142  }
   143  
   144  func (b *Blockchain) EventsPerSecond() uint64 {
   145  	return b.eventsPerSecond
   146  }
   147  
   148  // TotalTxLastBatch return the number of transaction
   149  // processed in the last accepted block in the chain.
   150  func (b *Blockchain) TotalTxLastBatch() uint64 {
   151  	return b.totalTxLastBatch
   152  }
   153  
   154  func (b *Blockchain) SetTotalTxLastBatch(i uint64) {
   155  	b.totalTxLastBatch = i
   156  }
   157  
   158  func (b *Blockchain) SetTotalTxCurrentBatch(i uint64) {
   159  	b.totalTxCurrentBatch = i
   160  }
   161  
   162  func (b *Blockchain) TotalTxCurrentBatch() uint64 {
   163  	return b.totalTxCurrentBatch
   164  }
   165  
   166  func (b *Blockchain) IncTotalTxCurrentBatch() {
   167  	b.totalTxCurrentBatch++
   168  }
   169  
   170  // SetTotalOrdersLastBatch assing total orders.
   171  func (b *Blockchain) SetTotalOrdersLastBatch(i uint64) {
   172  	b.totalOrdersLastBatch = i
   173  }
   174  
   175  // TotalOrdersLastBatch returns the number of orders
   176  // accepted in the last block in the chain.
   177  func (b Blockchain) TotalOrdersLastBatch() uint64 {
   178  	return b.totalOrdersLastBatch
   179  }
   180  
   181  // SetTotalTradesLastBatch set total trades.
   182  func (b *Blockchain) SetTotalTradesLastBatch(i uint64) {
   183  	b.totalTradesLastBatch = i
   184  }
   185  
   186  // TotalTradesLastBatch returns the number of trades
   187  // created during the last block in the chain.
   188  func (b Blockchain) TotalTradesLastBatch() uint64 {
   189  	return b.totalTradesLastBatch
   190  }
   191  
   192  // SetAverageOrdersPerBatch sets new average orders per batch.
   193  func (b *Blockchain) SetAverageOrdersPerBatch(i uint64) {
   194  	b.averageOrdersPerBatch = i
   195  }
   196  
   197  // AverageOrdersPerBatch returns the average number
   198  // of orders accepted per blocks.
   199  func (b Blockchain) AverageOrdersPerBatch() uint64 {
   200  	return b.averageOrdersPerBatch
   201  }
   202  
   203  // TotalAmendOrder returns the total amount of order
   204  // amended processed by the vega node.
   205  func (b Blockchain) TotalAmendOrder() uint64 {
   206  	return b.totalAmendOrder
   207  }
   208  
   209  // TotalCancelOrder return the total number of orders
   210  // cancel by the vega node.
   211  func (b Blockchain) TotalCancelOrder() uint64 {
   212  	return b.totalCancelOrder
   213  }
   214  
   215  // TotalCreateOrder return the total amount of
   216  // request to create a new order.
   217  func (b Blockchain) TotalCreateOrder() uint64 {
   218  	return b.totalCreateOrder
   219  }
   220  
   221  // TotalOrders return the total amount of
   222  // orders placed in the system.
   223  func (b Blockchain) TotalOrders() uint64 {
   224  	return b.totalOrders
   225  }
   226  
   227  // TotalTrades return the total amount of trades
   228  // in the system.
   229  func (b Blockchain) TotalTrades() uint64 {
   230  	return b.totalTrades
   231  }
   232  
   233  // OrdersPerSecond return the total number of orders
   234  // processed during the last second.
   235  func (b Blockchain) OrdersPerSecond() uint64 {
   236  	return b.ordersPerSecond
   237  }
   238  
   239  // TradesPerSecond return the total number of trades
   240  // generated during the last second.
   241  func (b Blockchain) TradesPerSecond() uint64 {
   242  	return b.tradesPerSecond
   243  }
   244  
   245  // BlockDuration return the duration it took
   246  // to generate the last block.
   247  func (b Blockchain) BlockDuration() uint64 {
   248  	return b.blockDuration
   249  }
   250  
   251  func (b *Blockchain) IncTotalAmendOrder() {
   252  	b.totalAmendOrder++
   253  }
   254  
   255  func (b *Blockchain) AddTotalAmendOrder(val uint64) uint64 {
   256  	r := val + b.totalAmendOrder
   257  	b.totalAmendOrder = r
   258  	return r
   259  }
   260  
   261  func (b *Blockchain) IncTotalCancelOrder() {
   262  	b.totalCancelOrder++
   263  }
   264  
   265  func (b *Blockchain) AddTotalCancelOrder(val uint64) uint64 {
   266  	r := b.totalCancelOrder + val
   267  	b.totalCancelOrder = r
   268  	return r
   269  }
   270  
   271  func (b *Blockchain) IncTotalCreateOrder() {
   272  	b.totalCreateOrder++
   273  }
   274  
   275  // AddTotalCreateOrder - increment total created orders.
   276  func (b *Blockchain) AddTotalCreateOrder(val uint64) uint64 {
   277  	r := b.totalCreateOrder + val
   278  	b.totalCreateOrder = r
   279  	return r
   280  }
   281  
   282  func (b *Blockchain) IncTotalOrders() {
   283  	b.totalOrders++
   284  }
   285  
   286  // AddTotalOrders increment total orders.
   287  func (b *Blockchain) AddTotalOrders(val uint64) uint64 {
   288  	r := b.totalOrders + val
   289  	b.totalOrders = r
   290  	return r
   291  }
   292  
   293  // AddTotalTrades increment total trades.
   294  func (b *Blockchain) AddTotalTrades(val uint64) uint64 {
   295  	r := b.totalTrades + val
   296  	b.totalTrades = r
   297  	return r
   298  }
   299  
   300  func (b *Blockchain) SetOrdersPerSecond(val uint64) {
   301  	b.ordersPerSecond = val
   302  }
   303  
   304  func (b *Blockchain) SetTradesPerSecond(val uint64) {
   305  	b.tradesPerSecond = val
   306  }
   307  
   308  func (b *Blockchain) SetBlockDuration(val uint64) {
   309  	b.blockDuration = val
   310  }
   311  
   312  func (b *Blockchain) SetEventsPerSecond(val uint64) {
   313  	b.eventsPerSecond = val
   314  }