1

I'm working on a Go app that launches a blockchain p2p nodes. For now the user has to manually start the p2p nodes and submit data via terminal. I'm trying to add a HTTP server to add/read data from the blockchain via REST. The problem is that I'm handling the starting of the p2p nodes with Cobra CLI, so when I added another command to start a go-gin HTTP server. Starting the first p2p node returns a reference to the blockchain, and I'm struggling to pass that reference to the function that starts the http server.

structs:

type P2pConfig struct {
    ListenF int
    Target string
    Secio bool
    Seed int64
}

type HttpConfig struct {
    HttpPort string
}

type Block struct {
    Index     int
    Timestamp string
    BPM       string
    Hash      string
    PrevHash  string
}

type Chain struct {
    BlockChain []Block
    P2pConfig *P2pConfig
    HttpConfig *HttpConfig
}

func NewBlockChain(c *P2pConfig, h *HttpConfig) *Chain {
    return &Chain {
        P2pConfig:c,
        HttpConfig:h,
    }
}

Cobra command to start a p2p node

func init() {
    CommandServe = &cobra.Command{
        Use:   "bc",
        Short: "Start the BlockChain",
        Long:  ``,
        Run: func(CommandServe *cobra.Command, args []string) {

            if _, err := serve(); err != nil {
                fmt.Fprintln(os.Stderr, err)
            }
        },
    }

    CommandServe.Flags().IntVar(&p2pPort, "l", 8199, "port: wait for incoming connections")
    CommandServe.Flags().StringVar(&target, "d", "", "target peer to dial")
    CommandServe.Flags().BoolVar(&secio, "secio", true, "enable secio")
    CommandServe.Flags().Int64Var(&seed, "seed", 0, "set random seed for id generation")
    CommandServe.Flags().StringVar(&httpPort, "p", ":8090", "port of the http ledger")

}

func serve() (*core.Chain, error) {

    c := core.NewBlockChain(&core.P2pConfig{
        ListenF: p2pPort,
        Target:  target,
        Secio:   secio,
        Seed:    seed,
    }, &core.HttpConfig{
        HttpPort: httpPort})

    log.Println("running p2p server")

    nc, err := core.Launch(c)
    if err != nil {
        return nil, err
    }

    return nc, nil

}

Starting the HTTP server

func init() {
    CommandServe = &cobra.Command{
        Use:   "hs",
        Short: "Start the http server",
        Long:  ``,
        Run: func(CommandServe *cobra.Command, args []string) {
            if err := serve(); err != nil {
                fmt.Fprintln(os.Stderr, err)
            }
        },
    }
}

func serve() error {

    log.Println("running http server")
    go func() error {
        err := nc.Run(":8090") //causing error here because nc is not recognizable yet
        if err != nil {
            return err
        }
        return nil
    }()

    return nil
}

The problem is passing the variable nc from the first command to the second command. It cannot be returned because it's inside Init() method, so I tried creating a global variable in the fist command's package, and then access it from the second's command package, but it's always nil and I don't understand why (probably a scope issue).

I tried mixing the two commands into a single one (start the p2p nodes and http server), use go routines and channels to pass nc, but that caused a problem because go routines run in the background, and starting the p2p nodes requires user interactivity.

Is there a better/smoother way to achieve this ?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
med.b
  • 465
  • 2
  • 12
  • 21

0 Answers0