Skip to content

Commit

Permalink
Merge pull request #2 from datachainlab/fix-save-last-enclave-key
Browse files Browse the repository at this point in the history
copy enclaveKey from tmp Dir without using os.Rename

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 10, 2023
2 parents f908d1d + 3479913 commit a789dae
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -13,13 +14,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
mapset "github.com/deckarep/golang-set/v2"
"github.com/hyperledger-labs/yui-relayer/core"
oias "github.com/oasisprotocol/oasis-core/go/common/sgx/ias"

lcptypes "github.com/datachainlab/lcp-go/light-clients/lcp/types"
"github.com/datachainlab/lcp-go/relay/elc"
"github.com/datachainlab/lcp-go/relay/enclave"
"github.com/datachainlab/lcp-go/sgx/ias"
mapset "github.com/deckarep/golang-set/v2"
"github.com/hyperledger-labs/yui-relayer/core"
oias "github.com/oasisprotocol/oasis-core/go/common/sgx/ias"
)

const lastEnclaveKeyInfoFile = "last_eki"
Expand All @@ -46,26 +48,35 @@ func (pr *Prover) loadLastEnclaveKey(ctx context.Context) (*enclave.EnclaveKeyIn
}

func (pr *Prover) saveLastEnclaveKey(ctx context.Context, eki *enclave.EnclaveKeyInfo) error {
path, err := pr.lastEnclaveKeyInfoFilePath()
src, err := os.CreateTemp(os.TempDir(), lastEnclaveKeyInfoFile)
if err != nil {
return err
}
f, err := os.CreateTemp(os.TempDir(), lastEnclaveKeyInfoFile)
defer src.Close()

bz, err := json.Marshal(eki)
if err != nil {
return err
}
defer f.Close()

bz, err := json.Marshal(eki)
if err = os.WriteFile(src.Name(), bz, 0600); err != nil {
return err
}

path, err := pr.lastEnclaveKeyInfoFilePath()
if err != nil {
return err
}
if _, err := f.Write(bz); err != nil {
dst, err := os.Create(path)
if err != nil {
return err
}
if err := os.Rename(f.Name(), path); err != nil {
defer dst.Close()

if _, err = io.Copy(dst, src); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit a789dae

Please sign in to comment.