Skip to content

Commit

Permalink
Merge pull request #24 from datachainlab/logger-add-more-context
Browse files Browse the repository at this point in the history
Add more context to logging and errors

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Jun 2, 2024
2 parents f8ee89f + 974f9c9 commit a46b91d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 113 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ lcp-go includes the followings:

## Dependencies

- [lcp v0.2.6](https://github.com/datachainlab/lcp/releases/tag/v0.2.6)
- [lcp v0.2.8](https://github.com/datachainlab/lcp/releases/tag/v0.2.8)
- [ibc-go v8.2](https://github.com/cosmos/ibc-go/releases/tag/v8.2.0)
- [yui-relayer v0.5.1](https://github.com/hyperledger-labs/yui-relayer/releases/tag/v0.5.1)
- [yui-relayer v0.5.3](https://github.com/hyperledger-labs/yui-relayer/releases/tag/v0.5.3)

## How to run tests

Expand Down
16 changes: 8 additions & 8 deletions light-clients/lcp/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func EthABIDecodeCommitmentProof(bz []byte) (*CommitmentProof, error) {
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack commitment proof: bz=%x %w", bz, err)
}
p := CommitmentProof(v[0].(struct {
Message []byte `json:"message"`
Expand All @@ -346,7 +346,7 @@ func EthABIDecodeHeaderedProxyMessage(bz []byte) (*HeaderedProxyMessage, error)
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack headered message: bz=%x %w", bz, err)
}
p := v[0].(struct {
Header [32]byte `json:"header"`
Expand Down Expand Up @@ -431,7 +431,7 @@ func EthABIDecodeMisbehaviourProxyMessage(bz []byte) (*MisbehaviourProxyMessage,
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack misbehaviourProxyMessage: bz=%x %w", bz, err)
}
p := v[0].(struct {
PrevStates []struct {
Expand All @@ -446,7 +446,7 @@ func EthABIDecodeMisbehaviourProxyMessage(bz []byte) (*MisbehaviourProxyMessage,
})
cctx, err := EthABIDecodeValidationContext(p.Context)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to decode validation context: bz=%x %w", p.Context, err)
}
var prevStates []struct {
Height clienttypes.Height
Expand Down Expand Up @@ -474,7 +474,7 @@ func EthABIDecodeValidationContext(bz []byte) (ValidationContext, error) {
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack headered message context: bz=%x %w", bz, err)
}
p := v[0].(struct {
Header [32]byte `json:"header"`
Expand All @@ -500,14 +500,14 @@ func EthABIDecodeValidationContext(bz []byte) (ValidationContext, error) {

func EthABIDecodeTrustingPeriodValidationContext(bz []byte) (*TrustingPeriodValidationContext, error) {
if len(bz) != 64 {
return nil, fmt.Errorf("unexpected length of trusting period commitment context: %d", len(bz))
return nil, fmt.Errorf("unexpected length of trusting period commitment context: bz=%x", bz)
}
unpacker := abi.Arguments{
{Type: trustingPeriodContextABI},
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack trusting period context: bz=%x %w", bz, err)
}
p := v[0].(struct {
Timestamps [32]byte `json:"timestamps"`
Expand All @@ -522,7 +522,7 @@ func EthABIDecodeVerifyMembershipProxyMessage(bz []byte) (*ELCVerifyMembershipMe
}
v, err := unpacker.Unpack(bz)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unpack verify membership message: bz=%x %w", bz, err)
}
p := v[0].(struct {
Prefix []byte `json:"prefix"`
Expand Down
8 changes: 6 additions & 2 deletions relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (pc ProverConfig) Validate() error {
}

func decodeMrenclaveHex(s string) ([]byte, error) {
s = strings.ToLower(strings.TrimPrefix(s, "0x"))
return hex.DecodeString(s)
trimmed := strings.ToLower(strings.TrimPrefix(s, "0x"))
bz, err := hex.DecodeString(trimmed)
if err != nil {
return nil, fmt.Errorf("failed to decode MRENCLAVE: value=%v %w", s, err)
}
return bz, nil
}
49 changes: 30 additions & 19 deletions relay/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/datachainlab/lcp-go/relay/enclave"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/hyperledger-labs/yui-relayer/log"
)

const (
Expand Down Expand Up @@ -44,11 +43,11 @@ func (pr *Prover) loadLastFinalizedEnclaveKey(context.Context) (*enclave.Enclave
if os.IsNotExist(err) {
return nil, fmt.Errorf("%v not found: %w", path, ErrEnclaveKeyInfoNotFound)
}
return nil, err
return nil, fmt.Errorf("failed to stat file: path=%v %w", path, err)
}
var eki enclave.EnclaveKeyInfo
if err := json.Unmarshal(bz, &eki); err != nil {
return nil, err
return nil, fmt.Errorf("failed to unmarshal enclave key info: path=%v %w", path, err)
}
return &eki, nil
}
Expand All @@ -60,66 +59,78 @@ func (pr *Prover) loadLastUnfinalizedEnclaveKey(context.Context) (*enclave.Encla
if os.IsNotExist(err) {
return nil, nil, fmt.Errorf("%v not found: %w", path, ErrEnclaveKeyInfoNotFound)
}
return nil, nil, err
return nil, nil, fmt.Errorf("failed to stat file: path=%v %w", path, err)
}
var ueki unfinalizedEKI
if err := json.Unmarshal(bz, &ueki); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to unmarshal unfinalized enclave key info: %w", err)
}
var unfinalizedMsgID core.MsgID
if err := pr.codec.UnmarshalInterface(ueki.MsgIDBytes, &unfinalizedMsgID); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to unmarshal msg id: value=%x %w", ueki.MsgIDBytes, err)
}
return ueki.Info, unfinalizedMsgID, nil
}

func (pr *Prover) saveFinalizedEnclaveKeyInfo(_ context.Context, eki *enclave.EnclaveKeyInfo) error {
log.GetLogger().Info("save finalized enclave key info")
pr.getLogger().Info("save finalized enclave key info")
bz, err := json.Marshal(eki)
if err != nil {
return err
return fmt.Errorf("failed to marshal enclave key info: %w", err)
}
if err := os.WriteFile(pr.lastEnclaveKeyInfoFilePath(true), bz, 0600); err != nil {
return fmt.Errorf("failed to write enclave key info: %w", err)
}
return os.WriteFile(pr.lastEnclaveKeyInfoFilePath(true), bz, 0600)
return nil
}

func (pr *Prover) saveUnfinalizedEnclaveKeyInfo(_ context.Context, eki *enclave.EnclaveKeyInfo, msgID core.MsgID) error {
log.GetLogger().Info("save unfinalized enclave key info")
pr.getLogger().Info("save unfinalized enclave key info")
msgIDBytes, err := pr.codec.MarshalInterface(msgID)
if err != nil {
return err
return fmt.Errorf("failed to marshal msg id: %w", err)
}
bz, err := json.Marshal(unfinalizedEKI{
Info: eki,
MsgIDBytes: msgIDBytes,
})
if err != nil {
return err
return fmt.Errorf("failed to marshal enclave key info: %w", err)
}
if err := os.WriteFile(pr.lastEnclaveKeyInfoFilePath(false), bz, 0600); err != nil {
return fmt.Errorf("failed to write enclave key info: %w", err)
}
return os.WriteFile(pr.lastEnclaveKeyInfoFilePath(false), bz, 0600)
return nil
}

func (pr *Prover) removeFinalizedEnclaveKeyInfo(context.Context) error {
path := pr.lastEnclaveKeyInfoFilePath(true)
log.GetLogger().Info("remove finalized enclave key info", "path", path)
pr.getLogger().Info("remove finalized enclave key info", "path", path)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
return fmt.Errorf("failed to stat file: path=%v %w", path, err)
}
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to remove file: path=%v %w", path, err)
}
return os.Remove(path)
return nil
}

func (pr *Prover) removeUnfinalizedEnclaveKeyInfo(context.Context) error {
path := pr.lastEnclaveKeyInfoFilePath(false)
log.GetLogger().Info("remove unfinalized enclave key info", "path", path)
pr.getLogger().Info("remove unfinalized enclave key info", "path", path)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
return fmt.Errorf("failed to stat file: path=%v %w", path, err)
}
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to remove file: path=%v %w", path, err)
}
return os.Remove(path)
return nil
}

func (pr *Prover) removeEnclaveKeyInfos(ctx context.Context) error {
Expand Down
Loading

0 comments on commit a46b91d

Please sign in to comment.