Skip to content

Commit af427b8

Browse files
authored
Merge pull request #800 from jshufro/jms/prunestarter
Remove invocations to nethermind prune starter container, preferring docker-based curl
2 parents b182a9d + 658e6f3 commit af427b8

2 files changed

Lines changed: 68 additions & 11 deletions

File tree

rocketpool-cli/service/service.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const (
3333
ValidatorContainerSuffix string = "_validator"
3434
BeaconContainerSuffix string = "_eth2"
3535
ExecutionContainerSuffix string = "_eth1"
36-
PruneStarterContainerSuffix string = "_nm_prune_starter"
3736
NodeContainerSuffix string = "_node"
3837
ApiContainerSuffix string = "_api"
3938
WatchtowerContainerSuffix string = "_watchtower"
@@ -1012,8 +1011,6 @@ func pruneExecutionClient(c *cli.Context) error {
10121011
// Get the execution container name
10131012
executionContainerName := prefix + ExecutionContainerSuffix
10141013

1015-
pruneStarterContainerName := prefix + PruneStarterContainerSuffix
1016-
10171014
// Check for enough free space
10181015
volumePath, err := rp.GetClientVolumeSource(executionContainerName, clientDataVolumeName)
10191016
if err != nil {
@@ -1046,7 +1043,7 @@ func pruneExecutionClient(c *cli.Context) error {
10461043

10471044
if selectedEc == cfgtypes.ExecutionClient_Nethermind {
10481045
// Restarting NM is not needed anymore
1049-
err = rp.RunNethermindPruneStarter(executionContainerName, pruneStarterContainerName)
1046+
err = rp.RunNethermindPruneStarter(executionContainerName)
10501047
if err != nil {
10511048
return fmt.Errorf("Error starting Nethermind prune starter: %w", err)
10521049
}

shared/services/rocketpool/client.go

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const (
4949
templateSuffix string = ".tmpl"
5050
composeFileSuffix string = ".yml"
5151

52-
nethermindAdminUrl string = "http://127.0.0.1:7434"
52+
nethermindAdminUrl string = "http://127.0.0.1:7434"
53+
pruneStarterContainerSuffix string = "_nm_prune_starter"
5354

5455
DebugColor = color.FgYellow
5556
)
@@ -830,13 +831,72 @@ func (c *Client) RunPruneProvisioner(container, volume string) error {
830831

831832
}
832833

833-
// Executes a Go program that triggers NM pruning
834-
func (c *Client) RunNethermindPruneStarter(executionContainerName string, pruneStarterContainerName string) error {
835-
cmd := fmt.Sprintf(`docker run --rm --name %s --network container:%s rocketpool/nm-prune-starter %s`, pruneStarterContainerName, executionContainerName, nethermindAdminUrl)
834+
// Curls the Nethermind admin URL to trigger pruning
835+
func (c *Client) RunNethermindPruneStarter(executionContainerName string) error {
836+
retryCount := 5
837+
retryTime := 3 * time.Second
838+
839+
for i := 0; i < retryCount; i++ {
840+
command := fmt.Sprintf(`-m 30 -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"admin_prune","params":[],"id":%d}' %s`, i+1, nethermindAdminUrl)
841+
cmdText := fmt.Sprintf(`docker run --quiet --rm --name curl%s --network container:%s curlimages/curl -Ss %s`, pruneStarterContainerSuffix, executionContainerName, command)
842+
843+
if i != 0 {
844+
fmt.Printf("Trying again in %v... (%d/%d)\n", retryTime, i+1, retryCount)
845+
time.Sleep(retryTime)
846+
}
847+
848+
cmd, err := c.newCommand(cmdText)
849+
if err != nil {
850+
return fmt.Errorf("error creating command for prune starter: %w", err)
851+
}
852+
853+
stdOut, stdErr, err := cmd.OutputPipes()
854+
if err != nil {
855+
return fmt.Errorf("error getting output pipes for prune starter: %w", err)
856+
}
857+
858+
err = cmd.Start()
859+
if err != nil {
860+
return fmt.Errorf("error running prune starter: %w", err)
861+
}
862+
defer func() {
863+
_ = cmd.Wait()
864+
}()
865+
866+
// Check for a curl error
867+
stdErrTextBytes, err := io.ReadAll(stdErr)
868+
if err != nil {
869+
return fmt.Errorf("error reading error from prune starter: %w", err)
870+
}
871+
stdErrText := string(stdErrTextBytes)
872+
if stdErrText != "" {
873+
fmt.Printf("Error while curling the Nethermind admin URL: %s\n", stdErrText)
874+
continue
875+
}
876+
877+
// Grab the response
878+
stdOutText, err := io.ReadAll(stdOut)
879+
if err != nil {
880+
return fmt.Errorf("error reading response from prune starter: %w", err)
881+
}
882+
// Parse the response as JSON
883+
var response map[string]any
884+
err = json.Unmarshal(stdOutText, &response)
885+
if err != nil {
886+
return fmt.Errorf("error parsing response from prune starter: %w", err)
887+
}
888+
889+
if errObject, ok := response["error"].(map[string]any); ok {
890+
fmt.Printf("Error starting prune: code %d, message = %s, data = %s\n", errObject["code"], errObject["message"], errObject["data"])
891+
continue
892+
} else {
893+
fmt.Printf("Success: Pruning is now \"%s\"\n", response["result"])
894+
fmt.Println("Your main execution client is now pruning. You can follow its progress with `rocketpool service logs eth1`.")
895+
fmt.Println("NOTE: While pruning, you **cannot** interrupt the client (e.g. by restarting) or you risk corrupting the database!")
896+
fmt.Println("You must let it run to completion!")
897+
break
898+
}
836899

837-
err := c.printOutput(cmd)
838-
if err != nil {
839-
return err
840900
}
841901
return nil
842902
}

0 commit comments

Comments
 (0)