github.com/mavryk-network/mvgo@v1.19.9/rpc/constants.go (about)

     1  // Copyright (c) 2020-2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package rpc
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"strconv"
    10  	"time"
    11  
    12  	"github.com/mavryk-network/mvgo/mavryk"
    13  )
    14  
    15  // Constants represents only a limited subset of Tezos chain configuration params
    16  // which are required by MvGo. Users must define custom structs to read other
    17  // constants as needed.
    18  type Constants struct {
    19  	PreservedCycles              int64    `json:"preserved_cycles"`
    20  	BlocksPerCycle               int64    `json:"blocks_per_cycle"`
    21  	BlocksPerRollSnapshot        int64    `json:"blocks_per_roll_snapshot"`
    22  	TimeBetweenBlocks            []string `json:"time_between_blocks"`
    23  	HardGasLimitPerOperation     int64    `json:"hard_gas_limit_per_operation,string"`
    24  	HardGasLimitPerBlock         int64    `json:"hard_gas_limit_per_block,string"`
    25  	MichelsonMaximumTypeSize     int      `json:"michelson_maximum_type_size"`
    26  	OriginationSize              int64    `json:"origination_size"`
    27  	OriginationBurn              int64    `json:"origination_burn,string"`
    28  	CostPerByte                  int64    `json:"cost_per_byte,string"`
    29  	HardStorageLimitPerOperation int64    `json:"hard_storage_limit_per_operation,string"`
    30  	MaxOperationDataLength       int      `json:"max_operation_data_length"`
    31  
    32  	// New in v10
    33  	MinimalBlockDelay int `json:"minimal_block_delay,string"`
    34  
    35  	// New in v12
    36  	MaxOperationsTimeToLive int64 `json:"max_operations_time_to_live"`
    37  	BlocksPerStakeSnapshot  int64 `json:"blocks_per_stake_snapshot"`
    38  
    39  	// New in v19
    40  	ConsensusRightsDelay int64 `json:"consensus_rights_delay"`
    41  }
    42  
    43  // GetConstants returns chain configuration constants at block id
    44  // https://protocol.mavryk.org/tezos/api/rpc.html#get-block-id-context-constants
    45  func (c *Client) GetConstants(ctx context.Context, id BlockID) (con Constants, err error) {
    46  	u := fmt.Sprintf("chains/main/blocks/%s/context/constants", id)
    47  	err = c.Get(ctx, u, &con)
    48  	return
    49  }
    50  
    51  // GetCustomConstants returns chain configuration constants at block id
    52  // marshaled into a user-defined structure.
    53  // https://protocol.mavryk.org/tezos/api/rpc.html#get-block-id-context-constants
    54  func (c *Client) GetCustomConstants(ctx context.Context, id BlockID, resp any) error {
    55  	u := fmt.Sprintf("chains/main/blocks/%s/context/constants", id)
    56  	return c.Get(ctx, u, resp)
    57  }
    58  
    59  // GetParams returns a translated parameters structure for the current
    60  // network at block id.
    61  func (c *Client) GetParams(ctx context.Context, id BlockID) (*mavryk.Params, error) {
    62  	if !c.ChainId.IsValid() {
    63  		id, err := c.GetChainId(ctx)
    64  		if err != nil {
    65  			return nil, err
    66  		}
    67  		c.ChainId = id
    68  	}
    69  	meta, err := c.GetBlockMetadata(ctx, id)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	con, err := c.GetConstants(ctx, id)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	ver, err := c.GetVersionInfo(ctx)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	p := con.MapToChainParams().
    82  		WithChainId(c.ChainId).
    83  		WithProtocol(meta.Protocol).
    84  		WithNetwork(ver.NetworkVersion.ChainName).
    85  		WithBlock(meta.GetLevel())
    86  	return p, nil
    87  }
    88  
    89  func (c Constants) MapToChainParams() *mavryk.Params {
    90  	p := &mavryk.Params{
    91  		BlocksPerCycle:               c.BlocksPerCycle,
    92  		ConsensusRightsDelay:         c.ConsensusRightsDelay,
    93  		BlocksPerSnapshot:            c.BlocksPerRollSnapshot + c.BlocksPerStakeSnapshot,
    94  		OriginationSize:              c.OriginationSize + c.OriginationBurn,
    95  		CostPerByte:                  c.CostPerByte,
    96  		HardGasLimitPerOperation:     c.HardGasLimitPerOperation,
    97  		HardGasLimitPerBlock:         c.HardGasLimitPerBlock,
    98  		HardStorageLimitPerOperation: c.HardStorageLimitPerOperation,
    99  		MaxOperationDataLength:       c.MaxOperationDataLength,
   100  		MaxOperationsTTL:             c.MaxOperationsTimeToLive,
   101  		MinimalBlockDelay:            time.Duration(c.MinimalBlockDelay) * time.Second,
   102  	}
   103  
   104  	// Paris blocks per snapshot
   105  	if p.BlocksPerSnapshot == 0 {
   106  		p.BlocksPerSnapshot = c.BlocksPerCycle
   107  	}
   108  
   109  	// backport preserved cycles
   110  	if p.ConsensusRightsDelay == 0 {
   111  		p.ConsensusRightsDelay = c.PreservedCycles
   112  	}
   113  
   114  	// default for old protocols
   115  	if p.MaxOperationsTTL == 0 {
   116  		p.MaxOperationsTTL = 120
   117  	}
   118  
   119  	// timing on old protocols
   120  	if len(c.TimeBetweenBlocks) > 0 {
   121  		if val, err := strconv.ParseInt(c.TimeBetweenBlocks[0], 10, 64); err == nil {
   122  			p.MinimalBlockDelay = time.Duration(val) * time.Second
   123  		}
   124  	}
   125  
   126  	return p
   127  }