Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #572 from vegaprotocol/release/v0.15.1
Browse files Browse the repository at this point in the history
Release/v0.15.1
  • Loading branch information
jeremyletang authored May 26, 2022
2 parents 4588476 + 4ae9c33 commit a72dd2f
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 208 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
### 🐛 Fixes
- [](https://github.com/vegaprotocol/vegawallet/pull/) -

## 0.15.1

### 🐛 Fixes
- [558](https://github.com/vegaprotocol/vegawallet/pull/558) - Use user-friendly RPC code as error message
- [569](https://github.com/vegaprotocol/vegawallet/pull/569) - Ensure the winfile scheme for logger is register only once

## 0.15.0

### 🛠 Improvements
Expand Down
10 changes: 6 additions & 4 deletions cmd/command_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
"code.vegaprotocol.io/vegawallet/network"
"code.vegaprotocol.io/vegawallet/node"
"code.vegaprotocol.io/vegawallet/wallets"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/golang/protobuf/jsonpb"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
Expand Down Expand Up @@ -244,6 +243,9 @@ func SendCommand(w io.Writer, rf *RootFlags, req *SendCommandRequest) error {
Hosts: hosts,
Retries: req.Retries,
})

log = log.Named("command")

if err != nil {
return fmt.Errorf("couldn't initialise the node forwarder: %w", err)
}
Expand Down Expand Up @@ -290,7 +292,7 @@ func SendCommand(w io.Writer, rf *RootFlags, req *SendCommandRequest) error {
Nonce: powNonce,
}

log.Info("calculated proof of work for transaction with signature", zap.String("signature", tx.Signature.Value))
log.Info("calculated proof of work for transaction", zap.String("signature", tx.Signature.Value))

txHash, err := forwarder.SendTx(ctx, tx, api.SubmitTransactionRequest_TYPE_ASYNC, cltIdx)
if err != nil {
Expand Down
21 changes: 1 addition & 20 deletions cmd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"fmt"
"net/url"
"os"
"runtime"
"time"

"code.vegaprotocol.io/shared/paths"
Expand Down Expand Up @@ -78,15 +76,7 @@ func BuildJSONLogger(level string, vegaPaths paths.Paths, logsDir paths.StatePat
return nil, "", fmt.Errorf("failed getting path for %s: %w", logFile, err)
}

zapLogPath := appLogPath
if runtime.GOOS == "windows" {
err := zap.RegisterSink("winfile", newWinFileSink)
if err != nil {
return nil, "", fmt.Errorf("couldn't register the windows file sink: %w", err)
}
zapLogPath = "winfile:///" + appLogPath
}

zapLogPath := toZapLogPath(appLogPath)
cfg.OutputPaths = []string{zapLogPath}
cfg.ErrorOutputPaths = []string{zapLogPath}

Expand Down Expand Up @@ -159,12 +149,3 @@ func isSupportedLogLevel(level string) bool {
}
return false
}

// newWinFileSink creates a log sink on Windows machines as zap, by default,
// doesn't support Windows paths. A workaround is to create a fake winfile
// scheme and register it with zap instead. This workaround is taken from
// the GitHub issue at https://github.com/uber-go/zap/issues/621.
func newWinFileSink(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse().
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644) // nolint:gomnd
}
8 changes: 8 additions & 0 deletions cmd/logger_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows
// +build !windows

package cmd

func toZapLogPath(p string) string {
return p
}
29 changes: 29 additions & 0 deletions cmd/logger_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"fmt"
"net/url"
"os"

"go.uber.org/zap"
)

func init() {
err := zap.RegisterSink("winfile", newWinFileSink)
if err != nil {
panic(fmt.Errorf("couldn't register the windows file sink: %w", err))
}
}

func toZapLogPath(logPath string) string {
return "winfile:///" + logPath
}

// newWinFileSink creates a log sink on Windows machines as zap, by default,
// doesn't support Windows paths. A workaround is to create a fake winfile
// scheme and register it with zap instead. This workaround is taken from
// the GitHub issue at https://github.com/uber-go/zap/issues/621.
func newWinFileSink(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse().
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644) // nolint:gomnd
}
75 changes: 51 additions & 24 deletions cmd/service_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"

vgterm "code.vegaprotocol.io/shared/libs/term"
Expand All @@ -24,12 +23,15 @@ import (
"code.vegaprotocol.io/vegawallet/service"
svcstore "code.vegaprotocol.io/vegawallet/service/store/v1"
"code.vegaprotocol.io/vegawallet/wallets"
"github.com/golang/protobuf/jsonpb"

"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

const MaxConsentRequests = 100

var ErrEnableAutomaticConsentFlagIsRequiredWithoutTTY = errors.New("--automatic-consent flag is required without TTY")

var (
Expand Down Expand Up @@ -209,9 +211,6 @@ func RunService(w io.Writer, rf *RootFlags, f *RunServiceFlags) error {
return fmt.Errorf("couldn't initialise the node forwarder: %w", err)
}

pendingConsents := make(chan service.ConsentRequest, 1)
sentTxs := make(chan service.SentTransaction)

cliLog, cliLogPath, err := BuildJSONLogger(cfg.Level.String(), vegaPaths, paths.WalletCLILogsHome)
if err != nil {
return err
Expand All @@ -220,6 +219,11 @@ func RunService(w io.Writer, rf *RootFlags, f *RunServiceFlags) error {

cliLog = cliLog.Named("command")

consentRequests := make(chan service.ConsentRequest, MaxConsentRequests)
defer close(consentRequests)
sentTransactions := make(chan service.SentTransaction)
defer close(sentTransactions)

var policy service.Policy
if vgterm.HasTTY() {
cliLog.Info("TTY detected")
Expand All @@ -228,7 +232,7 @@ func RunService(w io.Writer, rf *RootFlags, f *RunServiceFlags) error {
policy = service.NewAutomaticConsentPolicy()
} else {
cliLog.Info("Explicit consent enabled")
policy = service.NewExplicitConsentPolicy(pendingConsents, sentTxs)
policy = service.NewExplicitConsentPolicy(ctx, consentRequests, sentTransactions)
}
} else {
cliLog.Info("No TTY detected")
Expand Down Expand Up @@ -296,7 +300,7 @@ func RunService(w io.Writer, rf *RootFlags, f *RunServiceFlags) error {
p.NextLine()
}

waitSig(ctx, cancel, cliLog, pendingConsents, sentTxs, p)
waitSig(ctx, cancel, cliLog, consentRequests, sentTransactions, p)

return nil
}
Expand Down Expand Up @@ -366,54 +370,77 @@ func startTokenDApp(log *zap.Logger, f *RunServiceFlags, cfg *network.Network, c
}

// waitSig will wait for a sigterm or sigint interrupt.
func waitSig(ctx context.Context, cfunc func(), log *zap.Logger, pendingSigRequests chan service.ConsentRequest, sentTxs chan service.SentTransaction, p *printer.InteractivePrinter) {
func waitSig(
ctx context.Context,
cancelFunc context.CancelFunc,
log *zap.Logger,
consentRequests chan service.ConsentRequest,
sentTransactions chan service.SentTransaction,
p *printer.InteractivePrinter,
) {
gracefulStop := make(chan os.Signal, 1)
defer close(gracefulStop)

signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
signal.Notify(gracefulStop, syscall.SIGQUIT)

go func() {
if err := handleConsentRequests(ctx, log, consentRequests, sentTransactions, p); err != nil {
cancelFunc()
}
}()

for {
select {
case sig := <-gracefulStop:
log.Info("caught signal", zap.String("signal", fmt.Sprintf("%+v", sig)))
cfunc()
cancelFunc()
return
case <-ctx.Done():
// nothing to do
return
case signRequest := <-pendingSigRequests:
txStr, err := signRequest.String()
}
}
}

func handleConsentRequests(ctx context.Context, log *zap.Logger, consentRequests chan service.ConsentRequest, sentTransactions chan service.SentTransaction, p *printer.InteractivePrinter) error {
for {
select {
case <-ctx.Done():
// nothing to do
return nil
case consentRequest := <-consentRequests:
m := jsonpb.Marshaler{Indent: " "}
marshalledTx, err := m.MarshalToString(consentRequest.Tx)
if err != nil {
log.Info("failed to marshall sign request content")
cfunc()
return
log.Error("couldn't marshal transaction from consent request", zap.Error(err))
return err
}

p.BlueArrow().Text("New transaction received: ").NextLine()
p.InfoText(txStr).NextLine()
p.InfoText(marshalledTx).NextLine()

if flags.DoYouApproveTx() {
log.Info("user approved the signing of the transaction", zap.Any("transaction", txStr))
signRequest.Confirmations <- service.ConsentConfirmation{Decision: true, TxStr: txStr}
log.Info("user approved the signing of the transaction", zap.Any("transaction", marshalledTx))
consentRequest.Confirmation <- service.ConsentConfirmation{Decision: true}
p.CheckMark().SuccessText("Transaction approved").NextLine()

sentTx := <-sentTxs
sentTx := <-sentTransactions
log.Info("transaction sent", zap.Any("ID", sentTx.TxID), zap.Any("hash", sentTx.TxHash))
if sentTx.Error != nil {
log.Error("transaction failed", zap.Any("transaction", txStr))
p.BangMark().DangerText("Transaction failed! ").NextLine()
p.BangMark().DangerText("Error: ").DangerText(sentTx.Error.Error()).NextLine()
p.BangMark().DangerText("Details: ").DangerText(strings.Join(sentTx.ErrorDetails, " ,")).NextSection()
log.Error("transaction failed", zap.Any("transaction", marshalledTx))
p.BangMark().DangerText("Transaction failed").NextLine()
p.BangMark().DangerText("Error: ").DangerText(sentTx.Error.Error()).NextSection()
} else {
log.Info("transaction sent", zap.Any("hash", sentTx.TxHash))
p.CheckMark().Text("Transaction with hash ").SuccessText(sentTx.TxHash).Text(" sent!").NextSection()
}
} else {
log.Info("user rejected the signing of the transaction", zap.Any("transaction", txStr))
signRequest.Confirmations <- service.ConsentConfirmation{Decision: false, TxStr: txStr}
log.Info("user rejected the signing of the transaction", zap.Any("transaction", marshalledTx))
consentRequest.Confirmation <- service.ConsentConfirmation{Decision: false}
p.BangMark().DangerText("Transaction rejected").NextSection()
}
close(signRequest.Confirmations)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module code.vegaprotocol.io/vegawallet
go 1.17

require (
code.vegaprotocol.io/protos v0.51.0
code.vegaprotocol.io/protos v0.51.1-0.20220517145005-fc5e0192af7c
code.vegaprotocol.io/shared v0.0.0-20220321185018-3b5684b00533
github.com/blang/semver/v4 v4.0.0
github.com/cenkalti/backoff/v4 v4.1.2
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
code.vegaprotocol.io/protos v0.50.4-0.20220503163151-7fc5916bf54e h1:8r9buW3Cl2aBkZj37Yuboq2Pj+pxcmXSRXI8lc3Z7Sk=
code.vegaprotocol.io/protos v0.50.4-0.20220503163151-7fc5916bf54e/go.mod h1:4BqwDw6jhc/mnwbXq8ZFUtYBFCnk8tBW6zuPsBt8OrQ=
code.vegaprotocol.io/protos v0.51.0 h1:HscwExZenysYze9N43JTqR1mStpUB82Hb48oY+lHX8M=
code.vegaprotocol.io/protos v0.51.0/go.mod h1:4BqwDw6jhc/mnwbXq8ZFUtYBFCnk8tBW6zuPsBt8OrQ=
code.vegaprotocol.io/protos v0.51.1-0.20220517145005-fc5e0192af7c h1:UtDLlSEiMHY8gNb4nHtJWhN59mu5GPXVymdAJ7UrQJ0=
code.vegaprotocol.io/protos v0.51.1-0.20220517145005-fc5e0192af7c/go.mod h1:4BqwDw6jhc/mnwbXq8ZFUtYBFCnk8tBW6zuPsBt8OrQ=
code.vegaprotocol.io/shared v0.0.0-20220321185018-3b5684b00533 h1:IxTWvyF0i0AgUAS+FTgQKIpbsS6Ki/Y9Ug0GSlgChJw=
code.vegaprotocol.io/shared v0.0.0-20220321185018-3b5684b00533/go.mod h1:pNHKwqRDkotUN0s6jErl7Eb2EpZa2HAf03lyPMTCUT0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down
42 changes: 41 additions & 1 deletion node/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package node

import "errors"
import (
"errors"
"fmt"
"strings"

typespb "code.vegaprotocol.io/protos/vega"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var ErrNoHostSpecified = errors.New("no host specified")

type ErrorCode codes.Code

type StatusError struct {
Code codes.Code
Details []string
}

func (e *StatusError) Error() string {
return fmt.Sprintf("%s - %v", e.Code.String(), strings.Join(e.Details, ", "))
}

// intoStatusError extract useful information from a gRPC status error.
// Returns nil if the underlying error is not a gRPC status error.
func intoStatusError(err error) *StatusError {
st, ok := status.FromError(err)
if !ok {
return nil
}
statusErr := &StatusError{
Code: st.Code(),
Details: []string{},
}
for _, v := range st.Details() {
v, ok := v.(*typespb.ErrorDetail)
if !ok {
continue
}
statusErr.Details = append(statusErr.Details, v.GetMessage())
}
return statusErr
}
Loading

0 comments on commit a72dd2f

Please sign in to comment.