ceremonyclient/node/main.go
Cassandra Heart a0a8ffc4e7
v1.2.13 (#64)
2024-02-22 00:07:17 -06:00

311 lines
8.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build !js && !wasm
package main
import (
"encoding/hex"
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/signal"
"path/filepath"
"runtime/pprof"
"syscall"
"time"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/pkg/errors"
"source.quilibrium.com/quilibrium/monorepo/node/app"
"source.quilibrium.com/quilibrium/monorepo/node/config"
"source.quilibrium.com/quilibrium/monorepo/node/crypto/kzg"
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/ceremony/application"
"source.quilibrium.com/quilibrium/monorepo/node/rpc"
)
var (
configDirectory = flag.String(
"config",
filepath.Join(".", ".config"),
"the configuration directory",
)
balance = flag.Bool(
"balance",
false,
"print the node's confirmed token balance to stdout and exit",
)
dbConsole = flag.Bool(
"db-console",
false,
"starts the node in database console mode",
)
importPrivKey = flag.String(
"import-priv-key",
"",
"creates a new config using a specific key from the phase one ceremony",
)
peerId = flag.Bool(
"peer-id",
false,
"print the peer id to stdout from the config and exit",
)
memprofile = flag.String(
"memprofile",
"",
"write memory profile after 20m to this file",
)
)
func main() {
flag.Parse()
if *memprofile != "" {
go func() {
for {
time.Sleep(5 * time.Minute)
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}()
}
if *balance {
config, err := config.LoadConfig(*configDirectory, "")
if err != nil {
panic(err)
}
if config.ListenGRPCMultiaddr == "" {
_, _ = fmt.Fprintf(os.Stderr, "gRPC Not Enabled, Please Configure\n")
os.Exit(1)
}
conn, err := app.ConnectToNode(config)
if err != nil {
panic(err)
}
defer conn.Close()
client := protobufs.NewNodeServiceClient(conn)
balance, err := app.FetchTokenBalance(client)
if err != nil {
panic(err)
}
fmt.Println("Confirmed balance:", balance.Owned, "QUIL")
return
}
if *peerId {
config, err := config.LoadConfig(*configDirectory, "")
if err != nil {
panic(err)
}
printPeerID(config.P2P)
return
}
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(" ")
}
nodeConfig, err := config.LoadConfig(*configDirectory, "")
if err != nil {
panic(err)
}
clearIfTestData(*configDirectory, nodeConfig)
migrate(*configDirectory, nodeConfig)
if *dbConsole {
console, err := app.NewDBConsole(nodeConfig)
if err != nil {
panic(err)
}
console.Run()
return
}
fmt.Println("Loading ceremony state and starting node...")
kzg.Init()
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(),
node.GetKeyManager(),
node.GetPubSub(),
node.GetExecutionEngines(),
)
if err != nil {
panic(err)
}
go func() {
err := srv.Start()
if err != nil {
panic(err)
}
}()
}
node.Start()
<-done
node.Stop()
}
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)
}
}
}
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(" ")
fmt.Println(" Quilibrium Node - v1.2.13 Dawn")
}