github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/neatio/chain.go (about)

     1  package main
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	cfg "github.com/neatio-net/config-go"
     7  	"github.com/neatio-net/neatio/chain/accounts/keystore"
     8  	ntcTypes "github.com/neatio-net/neatio/chain/consensus/neatcon/types"
     9  	"github.com/neatio-net/neatio/chain/log"
    10  	neatnode "github.com/neatio-net/neatio/network/node"
    11  	"github.com/neatio-net/neatio/utilities/utils"
    12  	"gopkg.in/urfave/cli.v1"
    13  )
    14  
    15  const (
    16  	MainChain    = "neatio"
    17  	TestnetChain = "testnet"
    18  )
    19  
    20  type Chain struct {
    21  	Id       string
    22  	Config   cfg.Config
    23  	NeatNode *neatnode.Node
    24  }
    25  
    26  func LoadMainChain(ctx *cli.Context, chainId string) *Chain {
    27  
    28  	chain := &Chain{Id: chainId}
    29  	config := utils.GetNeatConConfig(chainId, ctx)
    30  	chain.Config = config
    31  
    32  	log.Info("Starting Neatio full node...")
    33  	stack := makeFullNode(ctx, GetCMInstance(ctx).cch, chainId)
    34  	chain.NeatNode = stack
    35  
    36  	return chain
    37  }
    38  
    39  func LoadSideChain(ctx *cli.Context, chainId string) *Chain {
    40  
    41  	log.Infof("now load side: %s", chainId)
    42  
    43  	chain := &Chain{Id: chainId}
    44  	config := utils.GetNeatConConfig(chainId, ctx)
    45  	chain.Config = config
    46  
    47  	log.Infof("chainId: %s, makeFullNode", chainId)
    48  	cch := GetCMInstance(ctx).cch
    49  	stack := makeFullNode(ctx, cch, chainId)
    50  	if stack == nil {
    51  		return nil
    52  	} else {
    53  		chain.NeatNode = stack
    54  		return chain
    55  	}
    56  }
    57  
    58  func StartChain(ctx *cli.Context, chain *Chain, startDone chan<- struct{}) error {
    59  
    60  	go func() {
    61  		utils.StartNode(ctx, chain.NeatNode)
    62  
    63  		if startDone != nil {
    64  			startDone <- struct{}{}
    65  		}
    66  	}()
    67  
    68  	return nil
    69  }
    70  
    71  func CreateSideChain(ctx *cli.Context, chainId string, validator ntcTypes.PrivValidator, keyJson []byte, validators []ntcTypes.GenesisValidator) error {
    72  
    73  	config := utils.GetNeatConConfig(chainId, ctx)
    74  
    75  	if len(keyJson) > 0 {
    76  		keystoreDir := config.GetString("keystore")
    77  		keyJsonFilePath := filepath.Join(keystoreDir, keystore.KeyFileName(validator.Address))
    78  		saveKeyError := keystore.WriteKeyStore(keyJsonFilePath, keyJson)
    79  		if saveKeyError != nil {
    80  			return saveKeyError
    81  		}
    82  	}
    83  
    84  	privValFile := config.GetString("priv_validator_file_root")
    85  	validator.SetFile(privValFile + ".json")
    86  	validator.Save()
    87  
    88  	err := initEthGenesisFromExistValidator(chainId, config, validators)
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	init_neatchain(chainId, config.GetString("neat_genesis_file"), ctx)
    94  
    95  	init_em_files(config, chainId, config.GetString("neat_genesis_file"), validators)
    96  
    97  	return nil
    98  }