code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/node/fetch_genesis.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 node
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"time"
    24  
    25  	"code.vegaprotocol.io/vega/core/genesis"
    26  
    27  	tmtypes "github.com/cometbft/cometbft/types"
    28  )
    29  
    30  func genesisDocHTTPFromURL(genesisFilePath string) (*tmtypes.GenesisDoc, error) {
    31  	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    32  	defer cancel()
    33  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, genesisFilePath, nil)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("couldn't load genesis file from %s: %w", genesisFilePath, err)
    36  	}
    37  	resp, err := http.DefaultClient.Do(req)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("couldn't load genesis file from %s: %w", genesisFilePath, err)
    40  	}
    41  	defer resp.Body.Close()
    42  	jsonGenesis, err := ioutil.ReadAll(resp.Body)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	doc, _, err := genesis.FromJSON(jsonGenesis)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("invalid genesis file from %s: %w", genesisFilePath, err)
    50  	}
    51  
    52  	return doc, nil
    53  }
    54  
    55  func httpGenesisDocProvider(networkSelect string) (*tmtypes.GenesisDoc, error) {
    56  	genesisFilesRootPath := fmt.Sprintf("https://raw.githubusercontent.com/vegaprotocol/networks/master/%s", networkSelect)
    57  
    58  	doc, _, err := getGenesisFromRemote(genesisFilesRootPath)
    59  
    60  	return doc, err
    61  }
    62  
    63  func getGenesisFromRemote(genesisFilesRootPath string) (*tmtypes.GenesisDoc, *genesis.State, error) {
    64  	jsonGenesis, err := fetchData(fmt.Sprintf("%s/genesis.json", genesisFilesRootPath))
    65  	if err != nil {
    66  		return nil, nil, fmt.Errorf("couldn't get remote genesis file: %w", err)
    67  	}
    68  	doc, state, err := genesis.FromJSON(jsonGenesis)
    69  	if err != nil {
    70  		return nil, nil, fmt.Errorf("couldn't parse genesis file: %w", err)
    71  	}
    72  	return doc, state, nil
    73  }
    74  
    75  func fetchData(path string) ([]byte, error) {
    76  	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
    77  	defer cancel()
    78  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("couldn't build request for %s: %w", path, err)
    81  	}
    82  	sigResp, err := http.DefaultClient.Do(req)
    83  	if err != nil {
    84  		return nil, fmt.Errorf("couldn't get response for %s: %w", path, err)
    85  	}
    86  	defer sigResp.Body.Close()
    87  	data, err := ioutil.ReadAll(sigResp.Body)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("couldn't read response body: %w", err)
    90  	}
    91  	return data, nil
    92  }