ceremonyclient/node/main.go

254 lines
7.2 KiB
Go
Raw Normal View History

//go:build !js && !wasm
2023-08-21 03:50:38 +00:00
package main
import (
"encoding/hex"
"flag"
"fmt"
2023-09-25 02:43:35 +00:00
"io/fs"
2023-08-21 03:50:38 +00:00
"os"
"os/signal"
2023-09-25 02:43:35 +00:00
"path/filepath"
2023-08-21 03:50:38 +00:00
"syscall"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pkg/errors"
2023-09-03 23:47:09 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/app"
2023-08-21 03:50:38 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/config"
2023-09-25 02:43:35 +00:00
qcrypto "source.quilibrium.com/quilibrium/monorepo/node/crypto"
"source.quilibrium.com/quilibrium/monorepo/node/execution/ceremony/application"
"source.quilibrium.com/quilibrium/monorepo/node/rpc"
2023-08-21 03:50:38 +00:00
)
var (
2023-09-09 23:45:47 +00:00
configDirectory = flag.String(
"config",
2024-01-03 07:31:42 +00:00
filepath.Join(".", ".config"),
2023-09-09 23:45:47 +00:00
"the configuration directory",
)
importPrivKey = flag.String(
"import-priv-key",
"",
"creates a new config using a specific key from the phase one ceremony",
)
dbConsole = flag.Bool(
"db-console",
false,
"starts the node in database console mode",
)
peerId = flag.Bool(
"peer-id",
false,
"print the peer id to stdout from the config and exit",
)
2023-08-21 03:50:38 +00:00
)
func main() {
flag.Parse()
if *peerId {
config, err := config.LoadConfig(*configDirectory, "")
if err != nil {
panic(err)
}
printPeerID(config.P2P)
return
}
2023-08-21 03:50:38 +00:00
if *importPrivKey != "" {
config, err := config.LoadConfig(*configDirectory, *importPrivKey)
if err != nil {
panic(err)
}
printPeerID(config.P2P)
fmt.Println("Import completed, you are ready for the launch.")
return
}
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
if !*dbConsole {
printLogo()
printVersion()
fmt.Println(" ")
}
2023-08-21 03:50:38 +00:00
2023-09-03 23:47:09 +00:00
nodeConfig, err := config.LoadConfig(*configDirectory, "")
if err != nil {
panic(err)
}
2023-09-25 02:43:35 +00:00
clearIfTestData(*configDirectory, nodeConfig)
migrate(*configDirectory, nodeConfig)
2023-09-25 02:43:35 +00:00
2023-09-09 23:45:47 +00:00
if *dbConsole {
console, err := app.NewDBConsole(nodeConfig)
if err != nil {
panic(err)
}
console.Run()
return
}
2023-09-25 02:43:35 +00:00
fmt.Println("Loading ceremony state and starting node...")
qcrypto.Init()
2023-09-03 23:47:09 +00:00
node, err := app.NewNode(nodeConfig)
if err != nil {
panic(err)
}
if nodeConfig.ListenGRPCMultiaddr != "" {
srv, err := rpc.NewRPCServer(
nodeConfig.ListenGRPCMultiaddr,
nodeConfig.ListenRestMultiaddr,
node.GetLogger(),
node.GetClockStore(),
2023-10-28 02:23:55 +00:00
node.GetKeyManager(),
node.GetPubSub(),
node.GetExecutionEngines(),
)
if err != nil {
panic(err)
}
go func() {
err := srv.Start()
if err != nil {
panic(err)
}
}()
}
2023-09-03 23:47:09 +00:00
node.Start()
<-done
node.Stop()
2023-08-21 03:50:38 +00:00
}
2023-09-25 02:43:35 +00:00
func clearIfTestData(configDir string, nodeConfig *config.Config) {
_, err := os.Stat(filepath.Join(configDir, "RELEASE_VERSION"))
if os.IsNotExist(err) {
fmt.Println("Clearing test data...")
err := os.RemoveAll(nodeConfig.DB.Path)
if err != nil {
panic(err)
}
versionFile, err := os.OpenFile(
filepath.Join(configDir, "RELEASE_VERSION"),
os.O_CREATE|os.O_RDWR,
fs.FileMode(0700),
)
if err != nil {
panic(err)
}
_, err = versionFile.Write([]byte{0x01, 0x00, 0x00})
if err != nil {
panic(err)
}
err = versionFile.Close()
if err != nil {
panic(err)
}
}
}
func migrate(configDir string, nodeConfig *config.Config) {
_, err := os.Stat(filepath.Join(configDir, "MIGRATIONS"))
if os.IsNotExist(err) {
fmt.Println("Deduplicating and compressing clock frame data...")
clock, err := app.NewClockStore(nodeConfig)
if err := clock.Deduplicate(application.CEREMONY_ADDRESS); err != nil {
panic(err)
}
migrationFile, err := os.OpenFile(
filepath.Join(configDir, "MIGRATIONS"),
os.O_CREATE|os.O_RDWR,
fs.FileMode(0700),
)
if err != nil {
panic(err)
}
_, err = migrationFile.Write([]byte{0x00, 0x00, 0x01})
if err != nil {
panic(err)
}
err = migrationFile.Close()
if err != nil {
panic(err)
}
}
}
2023-08-21 03:50:38 +00:00
func printPeerID(p2pConfig *config.P2PConfig) {
peerPrivKey, err := hex.DecodeString(p2pConfig.PeerPrivKey)
if err != nil {
panic(errors.Wrap(err, "error unmarshaling peerkey"))
}
privKey, err := crypto.UnmarshalEd448PrivateKey(peerPrivKey)
if err != nil {
panic(errors.Wrap(err, "error unmarshaling peerkey"))
}
pub := privKey.GetPublic()
id, err := peer.IDFromPublicKey(pub)
if err != nil {
panic(errors.Wrap(err, "error getting peer id"))
}
fmt.Println("Peer ID: " + id.String())
}
func printLogo() {
fmt.Println(" %#########")
fmt.Println(" #############################")
fmt.Println(" ########################################&")
fmt.Println(" ###############################################")
fmt.Println(" &#####################% %######################")
fmt.Println(" ################# #################")
fmt.Println(" ############### ###############")
fmt.Println(" ############# ##############")
fmt.Println(" ############# ############&")
fmt.Println(" ############ ############")
fmt.Println(" ########### ########## &###########")
fmt.Println(" ########### ############## ###########")
fmt.Println(" ########### ############## ##########&")
fmt.Println(" ########## ############## ##########")
fmt.Println("%########## ########## ##########")
fmt.Println("##########& ##########")
fmt.Println("########## &#########")
fmt.Println("##########& ####### ####### ##########")
fmt.Println(" ########## &######################### ##########")
fmt.Println(" ########## ##############% ############## &##########")
fmt.Println(" %########## &############## ############### ##########")
fmt.Println(" ########### ############### ##############% ###########")
fmt.Println(" ###########& ########## ############### ########")
fmt.Println(" ############ ##### ##############% ####")
fmt.Println(" ############ ###############")
fmt.Println(" ############## ##############%")
fmt.Println(" ############### ###############")
fmt.Println(" #################& ##############%")
fmt.Println(" #########################&&&############# ###############")
fmt.Println(" ########################################% ############")
fmt.Println(" ####################################### ########")
fmt.Println(" ############################# ##")
}
func printVersion() {
fmt.Println(" ")
2024-01-10 06:58:38 +00:00
fmt.Println(" Quilibrium Node - v1.2.2 Dawn")
2023-08-21 03:50:38 +00:00
}