handlers.go raw

   1  package launchers
   2  
   3  import (
   4  	"fmt"
   5  	"io/ioutil"
   6  	"os"
   7  	"path/filepath"
   8  
   9  	"github.com/p9c/p9/pkg/interrupt"
  10  	"github.com/p9c/p9/pkg/qu"
  11  
  12  	"github.com/p9c/p9/cmd/ctl"
  13  	"github.com/p9c/p9/cmd/node"
  14  	"github.com/p9c/p9/cmd/wallet"
  15  	"github.com/p9c/p9/pkg/constant"
  16  	"github.com/p9c/p9/pod/state"
  17  
  18  	"github.com/p9c/p9/pkg/apputil"
  19  )
  20  
  21  // NodeHandle runs the ParallelCoin blockchain node
  22  func NodeHandle(ifc interface{}) (e error) {
  23  	var cx *state.State
  24  	var ok bool
  25  	if cx, ok = ifc.(*state.State); !ok {
  26  		return fmt.Errorf("cannot run without a state")
  27  	}
  28  	I.Ln("running node handler")
  29  	cx.NodeReady = qu.T()
  30  	cx.Node.Store(false)
  31  	// // serviceOptions defines the configuration options for the daemon as a service on Windows.
  32  	// type serviceOptions struct {
  33  	// 	ServiceCommand string `short:"s" long:"service" description:"Service command {install, remove, start, stop}"`
  34  	// }
  35  	// // runServiceCommand is only set to a real function on Windows. It is used to parse and execute service commands
  36  	// // specified via the -s flag.
  37  	// runServiceCommand := func(string) (e error) { return nil }
  38  	// // Service options which are only added on Windows.
  39  	// serviceOpts := serviceOptions{}
  40  	// // Perform service command and exit if specified. Invalid service commands show an appropriate error. Only runs
  41  	// // on Windows since the runServiceCommand function will be nil when not on Windows.
  42  	// if serviceOpts.ServiceCommand != "" && runServiceCommand != nil {
  43  	// 	if e = runServiceCommand(serviceOpts.ServiceCommand); E.Chk(e) {
  44  	// 		return e
  45  	// 	}
  46  	// 	return nil
  47  	// }
  48  	go func() {
  49  		if e := node.NodeMain(cx); E.Chk(e) {
  50  			E.Ln("error starting node ", e)
  51  		}
  52  	}()
  53  	I.Ln("starting node")
  54  	if cx.Config.DisableRPC.False() {
  55  		cx.RPCServer = <-cx.NodeChan
  56  		cx.NodeReady.Q()
  57  		cx.Node.Store(true)
  58  		I.Ln("node started")
  59  	}
  60  	// }
  61  	cx.WaitWait()
  62  	I.Ln("node is now fully shut down")
  63  	cx.WaitGroup.Wait()
  64  	<-cx.KillAll
  65  	return nil
  66  }
  67  
  68  // WalletHandle runs the wallet server
  69  func WalletHandle(ifc interface{}) (e error) {
  70  	var cx *state.State
  71  	var ok bool
  72  	if cx, ok = ifc.(*state.State); !ok {
  73  		return fmt.Errorf("cannot run without a state")
  74  	}
  75  	cx.Config.WalletFile.Set(filepath.Join(cx.Config.DataDir.V(), cx.ActiveNet.Name, constant.DbName))
  76  	// dbFilename := *cx.Config.DataDir + slash + cx.ActiveNet.
  77  	// 	Params.Name + slash + wallet.WalletDbName
  78  	if !apputil.FileExists(cx.Config.WalletFile.V()) && !cx.IsGUI {
  79  		// D.Ln(cx.ActiveNet.Name, *cx.Config.WalletFile)
  80  		if e = wallet.CreateWallet(cx.ActiveNet, cx.Config); E.Chk(e) {
  81  			E.Ln("failed to create wallet", e) /**/
  82  			return e
  83  		}
  84  		fmt.Println("restart to complete initial setup")
  85  		// os.Exit(0)
  86  		interrupt.RequestRestart()
  87  	}
  88  	// for security with apps launching the wallet, the public password can be set with a file that is deleted after
  89  	walletPassPath := filepath.Join(cx.Config.DataDir.V(), cx.ActiveNet.Name, "wp.txt")
  90  	D.Ln("reading password from", walletPassPath)
  91  	if apputil.FileExists(walletPassPath) {
  92  		var b []byte
  93  		if b, e = ioutil.ReadFile(walletPassPath); !E.Chk(e) {
  94  			cx.Config.WalletPass.SetBytes(b)
  95  			D.Ln("read password '" + string(b) + "'", cx.Config.WalletPass.V())
  96  			if e = ioutil.WriteFile(walletPassPath, make([]byte, len(b)),
  97  				0700); E.Chk(e) {
  98  			}
  99  			if e = os.Remove(walletPassPath); E.Chk(e) {
 100  			}
 101  			D.Ln("wallet cookie deleted", cx.Config.WalletPass.V())
 102  		}
 103  	}
 104  	cx.WalletKill = qu.T()
 105  	if e = wallet.Main(cx); E.Chk(e) {
 106  		E.Ln("failed to start up wallet", e)
 107  	}
 108  	// if !*cx.Config.DisableRPC {
 109  	// 	cx.WalletServer = <-cx.WalletChan
 110  	// }
 111  	// cx.WaitGroup.Wait()
 112  	cx.WaitWait()
 113  	return
 114  }
 115  
 116  func CtlHandleList(ifc interface{}) (e error) {
 117  	fmt.Println(ctl.ListCommands())
 118  	return nil
 119  }
 120  
 121  func CtlHandle(ifc interface{}) (e error) {
 122  	var cx *state.State
 123  	var ok bool
 124  	if cx, ok = ifc.(*state.State); !ok {
 125  		return fmt.Errorf("cannot run without a state")
 126  	}
 127  	cx.Config.LogLevel.Set("off")
 128  	ctl.CtlMain(cx.Config)
 129  	return nil
 130  }
 131