ceremonyclient/node/main.go

311 lines
8.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"
2024-02-19 00:28:29 +00:00
"log"
2023-08-21 03:50:38 +00:00
"os"
"os/signal"
2023-09-25 02:43:35 +00:00
"path/filepath"
2024-02-19 00:28:29 +00:00
"runtime/pprof"
2023-08-21 03:50:38 +00:00
"syscall"
2024-02-19 00:28:29 +00:00
"time"
2023-08-21 03:50:38 +00:00
2024-02-14 07:11:12 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
2023-08-21 03:50:38 +00:00
"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"
2024-02-13 07:04:56 +00:00
"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"
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",
)
2024-01-29 21:11:40 +00:00
balance = flag.Bool(
"balance",
false,
"print the node's confirmed token balance to stdout and exit",
2023-09-09 23:45:47 +00:00
)
dbConsole = flag.Bool(
"db-console",
false,
"starts the node in database console mode",
)
2024-01-29 21:11:40 +00:00
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",
)
2024-02-19 00:28:29 +00:00
memprofile = flag.String(
"memprofile",
"",
"write memory profile after 20m to this file",
)
2023-08-21 03:50:38 +00:00
)
func main() {
flag.Parse()
2024-02-19 00:28:29 +00:00
if *memprofile != "" {
go func() {
for {
2024-02-20 07:59:03 +00:00
time.Sleep(5 * time.Minute)
2024-02-19 00:28:29 +00:00
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
}()
}
2024-01-29 21:11:40 +00:00
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
}
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...")
2024-02-13 07:04:56 +00:00
kzg.Init()
2023-09-25 02:43:35 +00:00
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-02-20 20:01:10 +00:00
fmt.Println(" Quilibrium Node - v1.2.11 Dawn")
2023-08-21 03:50:38 +00:00
}