From 6c071ca9bb3a96d1dcbb98a1ac0b80c027103d52 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Fri, 13 Nov 2020 20:57:14 -0500 Subject: [PATCH] simplify resolve call on wallet node --- cmd/resolve.go | 14 +------------- wallet/client.go | 34 ++++++++++++++++++++++++++++++++++ wallet/network.go | 23 ++++++++++++++--------- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/cmd/resolve.go b/cmd/resolve.go index cad9698..631957e 100644 --- a/cmd/resolve.go +++ b/cmd/resolve.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/hex" "encoding/json" "fmt" @@ -29,10 +28,7 @@ func resolveCmd(cmd *cobra.Command, args []string) { err := node.Connect([]string{addr}, nil) checkErr(err) - output, err := node.Resolve(url) - checkErr(err) - - claim, err := node.GetClaimInTx(hex.EncodeToString(rev(output.GetTxHash())), int(output.GetNout())) + claim, _, err := node.ResolveToClaim(url) checkErr(err) jsonClaim, err := json.MarshalIndent(claim, "", " ") @@ -40,11 +36,3 @@ func resolveCmd(cmd *cobra.Command, args []string) { fmt.Println(string(jsonClaim)) } - -func rev(b []byte) []byte { - r := make([]byte, len(b)) - for left, right := 0, len(b)-1; left < right; left, right = left+1, right-1 { - r[left], r[right] = b[right], b[left] - } - return r -} diff --git a/wallet/client.go b/wallet/client.go index a4cb165..78ecb1c 100644 --- a/wallet/client.go +++ b/wallet/client.go @@ -3,6 +3,7 @@ package wallet import ( "encoding/base64" "encoding/hex" + "strings" "github.com/lbryio/chainquery/lbrycrd" "github.com/lbryio/lbry.go/v2/extras/errors" @@ -14,6 +15,8 @@ import ( "github.com/spf13/cast" ) +var ErrClaimNotFound = errors.Base("claim not found") + // Raw makes a raw wallet server request func (n *Node) Raw(method string, params []string, v interface{}) error { return n.request(method, params, v) @@ -61,6 +64,12 @@ func (n *Node) Resolve(url string) (*types.Output, error) { } if e := outputs.GetTxos()[0].GetError(); e != nil { + // TODO: return these errors as real error values so callers don't have to string-match + // https://github.com/lbryio/types/blob/master/v2/proto/result.proto#L45 + // UNKNOWN_CODE = 0; + // NOT_FOUND = 1; + // INVALID = 2; + // BLOCKED = 3; return nil, errors.Err("%s: %s", e.GetCode(), e.GetText()) } @@ -147,3 +156,28 @@ func (n *Node) GetClaimInTx(txid string, nout int) (*types.Claim, error) { return ch.Claim, nil } + +func (n *Node) ResolveToClaim(url string) (*types.Claim, *types.Output, error) { + output, err := n.Resolve(url) + if err != nil { + if strings.Contains(err.Error(), "NOT_FOUND") { + return nil, nil, errors.Err(ErrClaimNotFound) + } + return nil, nil, err + } + + claim, err := n.GetClaimInTx(hex.EncodeToString(rev(output.GetTxHash())), int(output.GetNout())) + if err != nil { + return nil, nil, err + } + + return claim, output, nil +} + +func rev(b []byte) []byte { + r := make([]byte, len(b)) + for left, right := 0, len(b)-1; left < right; left, right = left+1, right-1 { + r[left], r[right] = b[right], b[left] + } + return r +} diff --git a/wallet/network.go b/wallet/network.go index 10e4edf..83f5505 100644 --- a/wallet/network.go +++ b/wallet/network.go @@ -23,10 +23,10 @@ const ( ) var ( - ErrNotImplemented = errors.Base("not implemented") - ErrNodeConnected = errors.Base("node already connected") - ErrConnectFailed = errors.Base("failed to connect") - ErrTimeout = errors.Base("timeout") + ErrNotImplemented = errors.Base("not implemented") + ErrAlreadyConnected = errors.Base("node already connected") + ErrConnectFailed = errors.Base("failed to connect") + ErrTimeout = errors.Base("timeout") ) type response struct { @@ -35,6 +35,8 @@ type response struct { } type Node struct { + ErrFn func(error) + transport *TCPTransport nextId atomic.Uint32 grp *stop.Group @@ -63,7 +65,7 @@ func NewNode() *Node { // Connect creates a new connection to the specified address. func (n *Node) Connect(addrs []string, config *tls.Config) error { if n.transport != nil { - return errors.Err(ErrNodeConnected) + return errors.Err(ErrAlreadyConnected) } // shuffle addresses for load balancing @@ -135,10 +137,13 @@ func (n *Node) handleErrors() { } } -// err handles errors produced by the foreign node. -func (n *Node) err(err error) { - // TODO: Better error handling. - log.Error(errors.FullTrace(err)) +// err handles errors produced by the server +func (n *Node) err(e error) { + if n.ErrFn != nil { + n.ErrFn(e) + } else { + log.Error(errors.FullTrace(e)) + } } // listen processes messages from the server.