github.com/Gessiux/neatchain@v1.3.1/chain/neatchain/chain.go (about)

     1  package main
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	cfg "github.com/Gessiux/go-config"
     7  	"github.com/Gessiux/neatchain/chain/accounts/keystore"
     8  	ntcTypes "github.com/Gessiux/neatchain/chain/consensus/neatcon/types"
     9  	"github.com/Gessiux/neatchain/chain/log"
    10  	neatnode "github.com/Gessiux/neatchain/network/node"
    11  	"github.com/Gessiux/neatchain/utilities/utils"
    12  	"gopkg.in/urfave/cli.v1"
    13  )
    14  
    15  const (
    16  	MainChain    = "neatchain"
    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  	//log.Infof("Starting Chain: %s", chain.Id)
    61  	go func() {
    62  		utils.StartNode(ctx, chain.NeatNode)
    63  
    64  		if startDone != nil {
    65  			startDone <- struct{}{}
    66  		}
    67  	}()
    68  
    69  	return nil
    70  }
    71  
    72  func CreateSideChain(ctx *cli.Context, chainId string, validator ntcTypes.PrivValidator, keyJson []byte, validators []ntcTypes.GenesisValidator) error {
    73  
    74  	config := utils.GetNeatConConfig(chainId, ctx)
    75  
    76  	if len(keyJson) > 0 {
    77  		keystoreDir := config.GetString("keystore")
    78  		keyJsonFilePath := filepath.Join(keystoreDir, keystore.KeyFileName(validator.Address))
    79  		saveKeyError := keystore.WriteKeyStore(keyJsonFilePath, keyJson)
    80  		if saveKeyError != nil {
    81  			return saveKeyError
    82  		}
    83  	}
    84  
    85  	privValFile := config.GetString("priv_validator_file_root")
    86  	validator.SetFile(privValFile + ".json")
    87  	validator.Save()
    88  
    89  	err := initEthGenesisFromExistValidator(chainId, config, validators)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	init_neatchain(chainId, config.GetString("neat_genesis_file"), ctx)
    95  
    96  	init_em_files(config, chainId, config.GetString("neat_genesis_file"), validators)
    97  
    98  	return nil
    99  }