code.vegaprotocol.io/vega@v0.79.0/core/netparams/genesis_state.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 netparams
    17  
    18  import (
    19  	"encoding/json"
    20  	"errors"
    21  )
    22  
    23  var ErrNoNetParamsGenesisState = errors.New("no network parameters genesis state")
    24  
    25  type GenesisState map[string]string
    26  
    27  func DefaultGenesisState() GenesisState {
    28  	state := map[string]string{}
    29  	netp := defaultNetParams()
    30  
    31  	for k, v := range netp {
    32  		if _, ok := Deprecated[k]; ok {
    33  			continue
    34  		}
    35  		state[k] = v.String()
    36  	}
    37  
    38  	return state
    39  }
    40  
    41  func LoadGenesisState(bytes []byte) (GenesisState, error) {
    42  	state := struct {
    43  		NetParams *GenesisState `json:"network_parameters"`
    44  	}{}
    45  	err := json.Unmarshal(bytes, &state)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	if state.NetParams == nil {
    50  		return nil, ErrNoNetParamsGenesisState
    51  	}
    52  	return *state.NetParams, nil
    53  }
    54  
    55  type GenesisStateOverwrite []string
    56  
    57  func LoadGenesisStateOverwrite(bytes []byte) (GenesisStateOverwrite, error) {
    58  	state := struct {
    59  		NetParamsOverwrite *GenesisStateOverwrite `json:"network_parameters_checkpoint_overwrite"`
    60  	}{}
    61  	err := json.Unmarshal(bytes, &state)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	if state.NetParamsOverwrite == nil {
    66  		return nil, nil // not an error, not mandatory to have overwrite list
    67  	}
    68  	return *state.NetParamsOverwrite, nil
    69  }