@@ -5,6 +5,7 @@ package localnet
55import (
66 "context"
77 "encoding/base64"
8+ "encoding/json"
89 "errors"
910 "fmt"
1011 "net/netip"
@@ -20,6 +21,7 @@ import (
2021 "github.com/ava-labs/avalanchego/api/admin"
2122 "github.com/ava-labs/avalanchego/api/info"
2223 "github.com/ava-labs/avalanchego/config"
24+ "github.com/ava-labs/avalanchego/config/node"
2325 "github.com/ava-labs/avalanchego/genesis"
2426 "github.com/ava-labs/avalanchego/ids"
2527 "github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
@@ -153,6 +155,28 @@ func GetTmpNetNetwork(networkDir string) (*tmpnet.Network, error) {
153155 if err != nil {
154156 return network , err
155157 }
158+ for i := range network .Nodes {
159+ // ensure that URI and StakingAddress are empty if the process does not exists
160+ processPath := filepath .Join (networkDir , network .Nodes [i ].NodeID .String (), "process.json" )
161+ if bytes , err := os .ReadFile (processPath ); errors .Is (err , os .ErrNotExist ) {
162+ network .Nodes [i ].URI = ""
163+ network .Nodes [i ].StakingAddress = netip.AddrPort {}
164+ } else if err != nil {
165+ return network , fmt .Errorf ("failed to read node process context: %w" , err )
166+ } else {
167+ processContext := node.ProcessContext {}
168+ if err := json .Unmarshal (bytes , & processContext ); err != nil {
169+ return network , fmt .Errorf ("failed to unmarshal node process context: %w" , err )
170+ }
171+ if _ , err := utils .GetProcess (processContext .PID ); err != nil {
172+ network .Nodes [i ].URI = ""
173+ network .Nodes [i ].StakingAddress = netip.AddrPort {}
174+ if err := os .Remove (processPath ); err != nil {
175+ return network , fmt .Errorf ("failed to clean up node process context: %w" , err )
176+ }
177+ }
178+ }
179+ }
156180 networkID , err := GetTmpNetNetworkID (network )
157181 if err != nil {
158182 return network , err
@@ -194,9 +218,13 @@ func TmpNetLoad(
194218func TmpNetStop (
195219 networkDir string ,
196220) error {
221+ network , err := GetTmpNetNetwork (networkDir )
222+ if err != nil {
223+ return err
224+ }
197225 ctx , cancel := sdkutils .GetTimedContext (2 * time .Minute )
198226 defer cancel ()
199- return tmpnet . StopNetwork (ctx , networkDir )
227+ return network . Stop (ctx )
200228}
201229
202230// Indicates whether the given network has all, part, or none of its nodes running
0 commit comments