github.com/0chain/gosdk@v1.17.11/core/block/block.go (about)

     1  // Provides the data structures and methods to work with the block data structure.
     2  // The block data structure is the core data structure in the 0chain protocol.
     3  // It is used to store the transactions and the state of the system at a given point in time.
     4  // The block data structure is used to create the blockchain, which is a chain of blocks that are linked together using the hash of the previous block.
     5  package block
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/0chain/gosdk/core/common"
    11  	"github.com/0chain/gosdk/core/encryption"
    12  	"github.com/0chain/gosdk/core/transaction"
    13  )
    14  
    15  type Key []byte
    16  
    17  type Header struct {
    18  	Version               string `json:"version,omitempty"`
    19  	CreationDate          int64  `json:"creation_date,omitempty"`
    20  	Hash                  string `json:"hash,omitempty"`
    21  	MinerID               string `json:"miner_id,omitempty"`
    22  	Round                 int64  `json:"round,omitempty"`
    23  	RoundRandomSeed       int64  `json:"round_random_seed,omitempty"`
    24  	MerkleTreeRoot        string `json:"merkle_tree_root,omitempty"`
    25  	StateHash             string `json:"state_hash,omitempty"`
    26  	ReceiptMerkleTreeRoot string `json:"receipt_merkle_tree_root,omitempty"`
    27  	NumTxns               int64  `json:"num_txns,omitempty"`
    28  }
    29  
    30  // IsBlockExtends - check if the block extends the previous block
    31  //   - prevHash is the hash of the previous block
    32  func (h *Header) IsBlockExtends(prevHash string) bool {
    33  	var data = fmt.Sprintf("%s:%s:%d:%d:%d:%s:%s", h.MinerID, prevHash,
    34  		h.CreationDate, h.Round, h.RoundRandomSeed, h.MerkleTreeRoot,
    35  		h.ReceiptMerkleTreeRoot)
    36  	return encryption.Hash(data) == h.Hash
    37  }
    38  
    39  /*Block - data structure that holds the block data */
    40  type Block struct {
    41  	Header *Header `json:"-"`
    42  
    43  	MinerID           common.Key `json:"miner_id"`
    44  	Round             int64      `json:"round"`
    45  	RoundRandomSeed   int64      `json:"round_random_seed"`
    46  	RoundTimeoutCount int        `json:"round_timeout_count"`
    47  
    48  	Hash            common.Key `json:"hash"`
    49  	Signature       string     `json:"signature"`
    50  	ChainID         common.Key `json:"chain_id"`
    51  	ChainWeight     float64    `json:"chain_weight"`
    52  	RunningTxnCount int64      `json:"running_txn_count"`
    53  
    54  	Version      string           `json:"version"`
    55  	CreationDate common.Timestamp `json:"creation_date"`
    56  
    57  	MagicBlockHash string `json:"magic_block_hash"`
    58  	PrevHash       string `json:"prev_hash"`
    59  
    60  	ClientStateHash Key                        `json:"state_hash"`
    61  	Txns            []*transaction.Transaction `json:"transactions,omitempty"`
    62  
    63  	// muted
    64  
    65  	// VerificationTickets []*VerificationTicket `json:"verification_tickets,omitempty"`
    66  	// PrevBlockVerificationTickets []*VerificationTicket `json:"prev_verification_tickets,omitempty"`
    67  }
    68  
    69  type ChainStats struct {
    70  	BlockSize            int     `json:"block_size"`
    71  	Count                int     `json:"count"`
    72  	CurrentRound         int     `json:"current_round"`
    73  	Delta                int     `json:"delta"`
    74  	LatestFinalizedRound int     `json:"latest_finalized_round"`
    75  	Max                  float64 `json:"max"`
    76  	Mean                 float64 `json:"mean"`
    77  	Min                  float64 `json:"min"`
    78  	Percentile50         float64 `json:"percentile_50"`
    79  	Percentile90         float64 `json:"percentile_90"`
    80  	Percentile95         float64 `json:"percentile_95"`
    81  	Percentile99         float64 `json:"percentile_99"`
    82  	Rate15Min            float64 `json:"rate_15_min"`
    83  	Rate1Min             float64 `json:"rate_1_min"`
    84  	Rate5Min             float64 `json:"rate_5_min"`
    85  	RateMean             float64 `json:"rate_mean"`
    86  	StdDev               float64 `json:"std_dev"`
    87  	TotalTxns            int     `json:"total_txns"`
    88  }
    89  
    90  type FeeStats struct {
    91  	MaxFees  common.Balance `json:"max_fees"`
    92  	MinFees  common.Balance `json:"min_fees"`
    93  	MeanFees common.Balance `json:"mean_fees"`
    94  }