-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
582 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/cartesi/rollups-node/internal/node/config" | ||
"github.com/cartesi/rollups-node/internal/node/model" | ||
"github.com/cartesi/rollups-node/internal/node/startup" | ||
"github.com/cartesi/rollups-node/internal/nodemachine" | ||
"github.com/cartesi/rollups-node/internal/repository" | ||
"github.com/cartesi/rollups-node/pkg/emulator" | ||
"github.com/cartesi/rollups-node/pkg/rollupsmachine" | ||
"github.com/cartesi/rollups-node/pkg/rollupsmachine/cartesimachine" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const CMD_NAME = "advancer" | ||
|
||
var ( | ||
buildVersion = "devel" | ||
Cmd = &cobra.Command{ | ||
Use: CMD_NAME, | ||
Short: "Runs the Advancer", | ||
Long: "Runs the Advancer in standalone mode", | ||
Run: run, | ||
} | ||
) | ||
|
||
func init() { | ||
flags := Cmd.Flags() | ||
flags.BytesHex("application-address", nil, "") | ||
flags.String("server-address", "", "") | ||
flags.String("snapshot", "", "") | ||
flags.Int64("snapshot-input-index", -1, "") | ||
flags.Uint64("machine-inc-cycles", 50_000_000, "") | ||
flags.Uint64("machine-max-cycles", 5_000_000_000, "") | ||
flags.Uint64("machine-advance-timeout", 60, "") | ||
flags.Uint64("machine-inspect-timeout", 10, "") | ||
} | ||
|
||
func main() { | ||
err := Cmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
// startTime := time.Now() | ||
|
||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
|
||
c := config.FromEnv() | ||
|
||
startup.ConfigLogs(c) | ||
|
||
slog.Info("Starting the Cartesi Rollups Node Advancer", "version", buildVersion, "config", c) | ||
|
||
// Validate Schema | ||
err := startup.ValidateSchema(c) | ||
if err != nil { | ||
slog.Error("failed to validate database schema", "error", err) | ||
os.Exit(1) | ||
} | ||
|
||
database, err := repository.Connect(ctx, c.PostgresEndpoint.Value) | ||
if err != nil { | ||
slog.Error("failed to connect to the database", "error", err) | ||
os.Exit(1) | ||
} | ||
defer database.Close() | ||
|
||
_, err = startup.SetupNodePersistentConfig(ctx, database, c) | ||
if err != nil { | ||
slog.Error("configuration error", "error", err) | ||
os.Exit(1) | ||
} | ||
|
||
// // instantiate advancer | ||
// | ||
// ready := make(chan struct{}, 1) | ||
// | ||
// go func() { | ||
// select { | ||
// case <-ready: | ||
// duration := time.Since(startTime) | ||
// slog.Info("advancer is ready", "after", duration) | ||
// case <-ctx.Done(): | ||
// } | ||
// }() | ||
// | ||
// // start service | ||
// | ||
// if err := service.Start(ctx, ready); err != nil { | ||
// slog.Error("validator exited with an error", "error", err) | ||
// os.Exit(1) | ||
// } | ||
} | ||
|
||
// ------------------------------------------------------------------------------------------------ | ||
|
||
type AdvancerConfig struct { | ||
PostgresEndpoint string | ||
PollingInterval uint64 | ||
MaxConcurrentInspects uint8 | ||
|
||
Machines map[model.Address]MachineConfig | ||
} | ||
|
||
type MachineConfig struct { | ||
ServerVerbosity cartesimachine.ServerVerbosity | ||
SnapshotPath string | ||
SnapshotInputIndex uint64 | ||
MachineInc uint64 | ||
MachineMax uint64 | ||
MachineAdvanceTimeout time.Duration | ||
MachineInspectTimeout time.Duration | ||
} | ||
|
||
func CreateMachine(ctx context.Context, c *MachineConfig) (*nodemachine.NodeMachine, error) { | ||
address, err := cartesimachine.StartServer(c.ServerVerbosity, 0, os.Stdout, os.Stderr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := &emulator.MachineRuntimeConfig{} | ||
cartesiMachine, err := cartesimachine.Load(ctx, c.SnapshotPath, address, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rollupsMachine, err := rollupsmachine.New(ctx, cartesiMachine, c.MachineInc, c.MachineMax) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nodemachine.New(rollupsMachine, | ||
c.SnapshotInputIndex, | ||
c.MachineAdvanceTimeout, | ||
c.MachineInspectTimeout, | ||
0) | ||
} | ||
|
||
// // Creates the machine pool. | ||
// machines := advancer.Machines{applicationContractAddress: nodeMachine} | ||
// | ||
// // Creates the advancer's repository. | ||
// repository := &repository.AdvancerRepository{Database: database} | ||
// | ||
// // Creates the advancer. | ||
// advancer, err := advancer.New(machines, repository) | ||
// | ||
// // Creates the poller from the advancer. | ||
// poller, err := advancer.Poller(5 * time.Second) | ||
// | ||
// // Starts the advancer in a separate goroutine. | ||
// done := make(chan struct{}, 1) | ||
// go func() { | ||
// ready := make(chan struct{}, 1) | ||
// err = poller.Start(ctx, ready) | ||
// <-ready | ||
// require.Nil(err, "%v", err) | ||
// done <- struct{}{} | ||
// }() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.