From 49e80a760e46979b58581c8b0cdd57f4c69694ed Mon Sep 17 00:00:00 2001 From: otherview Date: Tue, 4 Feb 2025 10:56:52 +0000 Subject: [PATCH] Ensure thorbuilder doesnt colide on the same process --- preset/LocalSixNodes.go | 2 +- thorbuilder/thorbuilder.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/preset/LocalSixNodes.go b/preset/LocalSixNodes.go index 60297cf..7ef7c13 100644 --- a/preset/LocalSixNodes.go +++ b/preset/LocalSixNodes.go @@ -98,7 +98,7 @@ func LocalSixNodesNetworkGenesis() *genesis.CustomGenesis { ExtraData: "Local Six Nodes Network", Accounts: []thorgenesis.Account{ { - Address: thor.MustParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed"), + Address: thor.MustParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed"), // dce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65 Balance: convToHexOrDecimal256(consts.LargeBigValue), Energy: convToHexOrDecimal256(consts.LargeBigValue), }, diff --git a/thorbuilder/thorbuilder.go b/thorbuilder/thorbuilder.go index 6bba882..dcd3e7c 100644 --- a/thorbuilder/thorbuilder.go +++ b/thorbuilder/thorbuilder.go @@ -2,6 +2,8 @@ package thorbuilder import ( "bytes" + "crypto/rand" + "encoding/hex" "fmt" "log/slog" "os" @@ -18,7 +20,13 @@ type Builder struct { // New creates a new Builder instance for the specified branch. // If reusable is true, it skips cloning if the directory exists and checks for the binary. func New(branch string, reusable bool) *Builder { - downloadPath := filepath.Join(os.TempDir(), fmt.Sprintf("thor_%s_%d", branch, os.Getpid())) + suffix, err := generateRandomSuffix(4) + if err != nil { + // handle error appropriately + } + downloadPath := filepath.Join(os.TempDir(), fmt.Sprintf("thor_%s_%d_%s", branch, os.Getpid(), suffix)) + + //downloadPath := filepath.Join(os.TempDir(), fmt.Sprintf("thor_%s_%d", branch, os.Getpid())) if reusable { downloadPath = filepath.Join(os.TempDir(), fmt.Sprintf("thor_%s_reusable", branch)) } @@ -97,3 +105,12 @@ func (b *Builder) Build() (string, error) { return thorBinaryPath, nil } + +// generateRandomSuffix returns a random hexadecimal string. +func generateRandomSuffix(n int) (string, error) { + bytes := make([]byte, n) + if _, err := rand.Read(bytes); err != nil { + return "", err + } + return hex.EncodeToString(bytes), nil +}