lightwalletd/frontend/service.go

476 lines
13 KiB
Go
Raw Normal View History

package frontend
import (
"context"
"encoding/hex"
2019-01-22 11:05:49 -08:00
"encoding/json"
"errors"
2020-05-17 22:51:51 -07:00
"math"
"net"
2019-12-04 14:02:21 -08:00
"regexp"
2019-01-22 11:05:49 -08:00
"strconv"
"strings"
2020-05-17 22:51:51 -07:00
"sync"
"time"
2019-09-25 11:43:54 -07:00
"github.com/btcsuite/btcd/rpcclient"
2019-09-13 16:02:58 -07:00
"github.com/sirupsen/logrus"
2020-05-19 20:12:54 -07:00
"google.golang.org/grpc/metadata"
2020-05-17 22:51:51 -07:00
"google.golang.org/grpc/peer"
2019-01-22 11:05:49 -08:00
"github.com/adityapk00/lightwalletd/common"
2019-09-12 12:08:53 -07:00
"github.com/adityapk00/lightwalletd/walletrpc"
)
var (
ErrUnspecified = errors.New("request for unspecified identifier")
)
2020-05-17 22:51:51 -07:00
type latencyCacheEntry struct {
timeNanos int64
lastBlock uint64
totalBlocks uint64
}
// the service type
type SqlStreamer struct {
2020-05-17 22:51:51 -07:00
cache *common.BlockCache
client *rpcclient.Client
log *logrus.Entry
metrics *common.PrometheusMetrics
latencyCache map[string]*latencyCacheEntry
latencyMutex sync.RWMutex
}
2020-04-21 16:52:27 -07:00
func NewSQLiteStreamer(client *rpcclient.Client, cache *common.BlockCache, log *logrus.Entry, metrics *common.PrometheusMetrics) (walletrpc.CompactTxStreamerServer, error) {
2020-05-17 22:51:51 -07:00
return &SqlStreamer{cache, client, log, metrics, make(map[string]*latencyCacheEntry), sync.RWMutex{}}, nil
}
func (s *SqlStreamer) GracefulStop() error {
2019-09-25 13:28:55 -07:00
return nil
}
func (s *SqlStreamer) GetCache() *common.BlockCache {
return s.cache
}
func (s *SqlStreamer) GetLatestBlock(ctx context.Context, placeholder *walletrpc.ChainSpec) (*walletrpc.BlockID, error) {
latestBlock := s.cache.GetLatestBlock()
if latestBlock == -1 {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
return nil, errors.New("Cache is empty. Server is probably not yet ready.")
}
2020-04-21 16:52:27 -07:00
s.metrics.LatestBlockCounter.Inc()
2019-09-25 13:28:55 -07:00
// TODO: also return block hashes here
return &walletrpc.BlockID{Height: uint64(latestBlock)}, nil
}
2019-09-13 16:02:58 -07:00
func (s *SqlStreamer) GetAddressTxids(addressBlockFilter *walletrpc.TransparentAddressBlockFilter, resp walletrpc.CompactTxStreamer_GetAddressTxidsServer) error {
2019-12-04 14:02:21 -08:00
var err error
var errCode int64
2020-06-28 06:59:24 -07:00
if addressBlockFilter == nil || addressBlockFilter.Range == nil || addressBlockFilter.Range.Start == nil || addressBlockFilter.Range.End == nil {
s.log.Errorf("Bad Structure")
return nil
}
2020-05-13 19:02:09 -07:00
2019-12-04 14:02:21 -08:00
// Test to make sure Address is a single t address
match, err := regexp.Match("^t[a-zA-Z0-9]{34}$", []byte(addressBlockFilter.Address))
if err != nil || !match {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
2019-12-04 14:02:21 -08:00
s.log.Errorf("Unrecognized address: %s", addressBlockFilter.Address)
return nil
}
2019-09-13 16:02:58 -07:00
params := make([]json.RawMessage, 1)
st := "{\"addresses\": [\"" + addressBlockFilter.Address + "\"]," +
"\"start\": " + strconv.FormatUint(addressBlockFilter.Range.Start.Height, 10) +
", \"end\": " + strconv.FormatUint(addressBlockFilter.Range.End.Height, 10) + "}"
params[0] = json.RawMessage(st)
result, rpcErr := s.client.RawRequest("getaddresstxids", params)
// For some reason, the error responses are not JSON
if rpcErr != nil {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
2019-09-13 16:02:58 -07:00
s.log.Errorf("Got error: %s", rpcErr.Error())
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
//Check to see if we are requesting a height the zcashd doesn't have yet
if err == nil && errCode == -8 {
return nil
}
return nil
}
var txids []string
err = json.Unmarshal(result, &txids)
if err != nil {
s.log.Errorf("Got error: %s", err.Error())
return nil
}
timeout, cancel := context.WithTimeout(resp.Context(), 30*time.Second)
defer cancel()
for _, txidstr := range txids {
txid, _ := hex.DecodeString(txidstr)
// Txid is read as a string, which is in big-endian order. But when converting
// to bytes, it should be little-endian
for left, right := 0, len(txid)-1; left < right; left, right = left+1, right-1 {
txid[left], txid[right] = txid[right], txid[left]
}
tx, err := s.GetTransaction(timeout, &walletrpc.TxFilter{Hash: txid})
if err != nil {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
2019-09-13 16:02:58 -07:00
s.log.Errorf("Got error: %s", err.Error())
return nil
}
resp.Send(tx)
}
2020-06-24 10:47:24 -07:00
go func() {
s.log.WithFields(logrus.Fields{
"method": "GetAddressTxids",
"address": addressBlockFilter.Address,
"start": addressBlockFilter.Range.Start.Height,
"end": addressBlockFilter.Range.End.Height,
}).Info("Service")
}()
2019-09-13 16:02:58 -07:00
return nil
}
2020-05-19 15:39:06 -07:00
func (s *SqlStreamer) peerIPFromContext(ctx context.Context) string {
2020-05-19 20:12:54 -07:00
if xRealIP, ok := metadata.FromIncomingContext(ctx); ok {
realIP := xRealIP.Get("x-real-ip")
if len(realIP) > 0 {
return realIP[0]
}
}
2020-05-19 15:39:06 -07:00
if peerInfo, ok := peer.FromContext(ctx); ok {
ip, _, err := net.SplitHostPort(peerInfo.Addr.String())
if err == nil {
2020-05-19 20:12:54 -07:00
return ip
2020-05-19 15:39:06 -07:00
}
}
2020-05-19 20:12:54 -07:00
return "unknown"
2020-05-19 15:39:06 -07:00
}
func (s *SqlStreamer) dailyActiveBlock(height uint64, peerip string) {
if height%1152 == 0 {
s.log.WithFields(logrus.Fields{
"method": "DailyActiveBlock",
"peer_addr": peerip,
"block_height": height,
}).Info("Service")
}
}
2019-09-25 13:28:55 -07:00
func (s *SqlStreamer) GetBlock(ctx context.Context, id *walletrpc.BlockID) (*walletrpc.CompactBlock, error) {
2020-06-24 10:47:24 -07:00
2019-09-25 13:28:55 -07:00
if id.Height == 0 && id.Hash == nil {
return nil, ErrUnspecified
}
2020-05-13 19:02:09 -07:00
s.log.WithFields(logrus.Fields{
"method": "GetBlockRange",
"start": id.Height,
"end": id.Height,
}).Info("Service")
2020-05-19 15:39:06 -07:00
// Log a daily active user if the user requests the day's "key block"
go func() {
s.dailyActiveBlock(id.Height, s.peerIPFromContext(ctx))
}()
2019-09-25 13:28:55 -07:00
// Precedence: a hash is more specific than a height. If we have it, use it first.
if id.Hash != nil {
// TODO: Get block by hash
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
2019-09-25 13:28:55 -07:00
return nil, errors.New("GetBlock by Hash is not yet implemented")
} else {
2019-09-25 15:55:37 -07:00
cBlock, err := common.GetBlock(s.client, s.cache, int(id.Height))
2019-09-25 13:28:55 -07:00
if err != nil {
return nil, err
}
2020-04-21 20:58:17 -07:00
s.metrics.TotalBlocksServedConter.Inc()
2019-09-25 17:18:27 -07:00
return cBlock, err
2019-09-25 13:28:55 -07:00
}
}
func (s *SqlStreamer) GetBlockRange(span *walletrpc.BlockRange, resp walletrpc.CompactTxStreamer_GetBlockRangeServer) error {
2020-06-24 10:47:24 -07:00
2019-09-25 13:28:55 -07:00
blockChan := make(chan walletrpc.CompactBlock)
errChan := make(chan error)
2020-05-19 15:39:06 -07:00
peerip := s.peerIPFromContext(resp.Context())
2020-05-17 22:51:51 -07:00
2020-05-19 15:39:06 -07:00
// Latency logging
2020-05-17 22:51:51 -07:00
go func() {
// If there is no ip, ignore
if peerip == "unknown" {
return
}
// Log only if bulk requesting blocks
if span.End.Height-span.Start.Height < 100 {
return
}
now := time.Now().UnixNano()
s.latencyMutex.Lock()
defer s.latencyMutex.Unlock()
// remove all old entries
for ip, entry := range s.latencyCache {
if entry.timeNanos+int64(30*math.Pow10(9)) < now { // delete after 30 seconds
delete(s.latencyCache, ip)
}
}
// Look up if this ip address has a previous getblock range
if entry, ok := s.latencyCache[peerip]; ok {
// Log only continous blocks
if entry.lastBlock+1 == span.Start.Height {
s.log.WithFields(logrus.Fields{
"method": "GetBlockRangeLatency",
"peer_addr": peerip,
"num_blocks": entry.totalBlocks,
2020-05-19 16:00:01 -07:00
"end_height": entry.lastBlock,
2020-05-17 22:51:51 -07:00
"latency_millis": (now - entry.timeNanos) / int64(math.Pow10(6)),
}).Info("Service")
}
}
// Add or update the ip entry
s.latencyCache[peerip] = &latencyCacheEntry{
lastBlock: span.End.Height,
totalBlocks: span.End.Height - span.Start.Height + 1,
timeNanos: now,
}
}()
2020-05-19 15:39:06 -07:00
// Log a daily active user if the user requests the day's "key block"
go func() {
for height := span.Start.Height; height <= span.End.Height; height++ {
s.dailyActiveBlock(height, peerip)
}
}()
2020-05-13 19:02:09 -07:00
s.log.WithFields(logrus.Fields{
2020-05-17 22:51:51 -07:00
"method": "GetBlockRange",
"start": span.Start.Height,
"end": span.End.Height,
"peer_addr": peerip,
2020-05-13 19:02:09 -07:00
}).Info("Service")
2019-09-25 15:55:37 -07:00
go common.GetBlockRange(s.client, s.cache, blockChan, errChan, int(span.Start.Height), int(span.End.Height))
for {
select {
case err := <-errChan:
// this will also catch context.DeadlineExceeded from the timeout
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
return err
2019-09-25 13:28:55 -07:00
case cBlock := <-blockChan:
2020-04-21 20:58:17 -07:00
s.metrics.TotalBlocksServedConter.Inc()
2019-09-25 13:28:55 -07:00
err := resp.Send(&cBlock)
if err != nil {
return err
}
}
}
}
func (s *SqlStreamer) GetTransaction(ctx context.Context, txf *walletrpc.TxFilter) (*walletrpc.RawTransaction, error) {
2020-06-24 10:47:24 -07:00
var txBytes []byte
var txHeight float64
if txf.Hash != nil {
txid := txf.Hash
for left, right := 0, len(txid)-1; left < right; left, right = left+1, right-1 {
txid[left], txid[right] = txid[right], txid[left]
}
leHashString := hex.EncodeToString(txid)
// First call to get the raw transaction bytes
params := make([]json.RawMessage, 1)
params[0] = json.RawMessage("\"" + leHashString + "\"")
result, rpcErr := s.client.RawRequest("getrawtransaction", params)
var err error
var errCode int64
// For some reason, the error responses are not JSON
if rpcErr != nil {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
s.log.Errorf("Got error: %s", rpcErr.Error())
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
//Check to see if we are requesting a height the zcashd doesn't have yet
if err == nil && errCode == -8 {
return nil, err
}
return nil, err
}
var txhex string
err = json.Unmarshal(result, &txhex)
if err != nil {
return nil, err
}
txBytes, err = hex.DecodeString(txhex)
if err != nil {
return nil, err
}
// Second call to get height
params = make([]json.RawMessage, 2)
params[0] = json.RawMessage("\"" + leHashString + "\"")
params[1] = json.RawMessage("1")
result, rpcErr = s.client.RawRequest("getrawtransaction", params)
// For some reason, the error responses are not JSON
if rpcErr != nil {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
s.log.Errorf("Got error: %s", rpcErr.Error())
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
//Check to see if we are requesting a height the zcashd doesn't have yet
if err == nil && errCode == -8 {
return nil, err
}
return nil, err
}
2020-06-28 06:59:24 -07:00
var txinfo interface{}
err = json.Unmarshal(result, &txinfo)
if err != nil {
return nil, err
}
txHeight = txinfo.(map[string]interface{})["height"].(float64)
2020-06-28 06:59:24 -07:00
go func() {
peerip := s.peerIPFromContext(ctx)
s.log.WithFields(logrus.Fields{
"method": "GetTransaction",
"hash": leHashString,
"peer_addr": peerip,
}).Info("Service")
}()
2019-09-17 13:26:23 -07:00
return &walletrpc.RawTransaction{Data: txBytes, Height: uint64(txHeight)}, nil
}
if txf.Block.Hash != nil {
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
s.log.Error("Can't GetTransaction with a blockhash+num. Please call GetTransaction with txid")
return nil, errors.New("Can't GetTransaction with a blockhash+num. Please call GetTransaction with txid")
}
2019-09-17 13:26:23 -07:00
return &walletrpc.RawTransaction{Data: txBytes, Height: uint64(txHeight)}, nil
}
2019-09-05 11:48:32 -07:00
// GetLightdInfo gets the LightWalletD (this server) info
2019-09-10 16:34:01 -07:00
func (s *SqlStreamer) GetLightdInfo(ctx context.Context, in *walletrpc.Empty) (*walletrpc.LightdInfo, error) {
2020-06-24 10:47:24 -07:00
saplingHeight, blockHeight, chainName, consensusBranchId, err := common.GetSaplingInfo(s.client)
2019-09-25 11:35:52 -07:00
if err != nil {
s.log.WithFields(logrus.Fields{
"error": err,
}).Warn("Unable to get sapling activation height")
2020-04-21 20:58:17 -07:00
s.metrics.TotalErrors.Inc()
2019-09-20 14:49:47 -07:00
return nil, err
2019-09-25 11:35:52 -07:00
}
2019-09-05 11:48:32 -07:00
// TODO these are called Error but they aren't at the moment.
// A success will return code 0 and message txhash.
2019-09-10 16:34:01 -07:00
return &walletrpc.LightdInfo{
Version: "0.1-zeclightd",
Vendor: "ZecWallet LightWalletD",
TaddrSupport: true,
ChainName: chainName,
SaplingActivationHeight: uint64(saplingHeight),
2019-09-25 17:46:05 -07:00
ConsensusBranchId: consensusBranchId,
BlockHeight: uint64(blockHeight),
2019-09-05 11:48:32 -07:00
}, nil
}
2019-01-22 11:05:49 -08:00
// SendTransaction forwards raw transaction bytes to a zcashd instance over JSON-RPC
func (s *SqlStreamer) SendTransaction(ctx context.Context, rawtx *walletrpc.RawTransaction) (*walletrpc.SendResponse, error) {
2019-01-22 11:05:49 -08:00
// sendrawtransaction "hexstring" ( allowhighfees )
//
// Submits raw transaction (serialized, hex-encoded) to local node and network.
//
// Also see createrawtransaction and signrawtransaction calls.
//
// Arguments:
// 1. "hexstring" (string, required) The hex string of the raw transaction)
// 2. allowhighfees (boolean, optional, default=false) Allow high fees
//
// Result:
// "hex" (string) The transaction hash in hex
// Construct raw JSON-RPC params
params := make([]json.RawMessage, 1)
txHexString := hex.EncodeToString(rawtx.Data)
params[0] = json.RawMessage("\"" + txHexString + "\"")
result, rpcErr := s.client.RawRequest("sendrawtransaction", params)
var err error
var errCode int64
var errMsg string
// For some reason, the error responses are not JSON
if rpcErr != nil {
errParts := strings.SplitN(rpcErr.Error(), ":", 2)
errMsg = strings.TrimSpace(errParts[1])
errCode, err = strconv.ParseInt(errParts[0], 10, 32)
if err != nil {
// This should never happen. We can't panic here, but it's that class of error.
// This is why we need integration testing to work better than regtest currently does. TODO.
return nil, errors.New("SendTransaction couldn't parse error code")
}
} else {
errMsg = string(result)
}
// TODO these are called Error but they aren't at the moment.
// A success will return code 0 and message txhash.
2020-04-21 20:58:17 -07:00
resp := &walletrpc.SendResponse{
2019-01-22 11:05:49 -08:00
ErrorCode: int32(errCode),
ErrorMessage: errMsg,
2020-04-21 20:58:17 -07:00
}
s.metrics.SendTransactionsCounter.Inc()
return resp, nil
}