simplify resolve call on wallet node

This commit is contained in:
Alex Grintsvayg 2020-11-13 20:57:14 -05:00
parent 7bddcf01b8
commit 6c071ca9bb
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
3 changed files with 49 additions and 22 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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.