herald.go/main.go
2021-12-12 18:35:16 -05:00

108 lines
2.1 KiB
Go

package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"time"
"github.com/lbryio/hub/db"
"github.com/lbryio/hub/db/prefixes"
pb "github.com/lbryio/hub/protobuf/go"
"github.com/lbryio/hub/server"
"github.com/lbryio/lbry.go/v2/extras/util"
"google.golang.org/grpc"
)
func main() {
ctx := context.Background()
searchRequest := &pb.SearchRequest{}
args := server.ParseArgs(searchRequest)
if args.CmdType == server.ServeCmd {
// This will cancel goroutines with the server finishes.
ctxWCancel, cancel := context.WithCancel(ctx)
defer cancel()
s := server.MakeHubServer(ctxWCancel, args)
s.Run()
return
} else if args.CmdType == server.DBCmd {
dbVal, err := db.GetDB("/mnt/d/data/wallet/lbry-rocksdb/")
if err != nil {
log.Fatalln(err)
}
pr := &db.PrefixRow{
Prefix: prefixes.UTXO,
KeyPackFunc: nil,
ValuePackFunc: nil,
KeyUnpackFunc: db.UTXOKeyUnpack,
ValueUnpackFunc: db.UTXOValueUnpack,
DB: dbVal,
}
b, err := hex.DecodeString("000013")
if err != nil {
log.Println(err)
}
stopKey := &db.UTXOKey{
Prefix: prefixes.UTXO,
HashX: b,
TxNum: 0,
Nout: 0,
}
stop := db.UTXOKeyPackPartial(stopKey, 1)
log.Print(hex.EncodeToString(stop))
options := &db.IterOptions{
FillCache: false,
Start: nil,
Stop: stop,
IncludeStart: true,
IncludeStop: false,
IncludeKey: true,
IncludeValue: true,
}
db.OpenAndWriteDB(pr, options, "./resources/asdf.db")
return
}
conn, err := grpc.Dial("localhost:"+args.Port,
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewHubClient(conn)
ctxWTimeout, cancelQuery := context.WithTimeout(ctx, time.Second)
defer cancelQuery()
log.Println(args)
switch args.CmdType {
case server.SearchCmd:
r, err := c.Search(ctxWTimeout, searchRequest)
if err != nil {
log.Fatal(err)
}
log.Printf("found %d results\n", r.GetTotal())
for _, t := range r.Txos {
fmt.Printf("%s:%d\n", util.TxHashToTxId(t.TxHash), t.Nout)
}
default:
log.Fatalln("Unknown Command Type!")
}
}