// Command ftool fills an instance from another instance's service, or makes // the key a service signs its manifest with. // // ftool keygen -out instance.key // ftool sync -from http://host:port -chain btc -from-height 0 -to-height 250000 -data ./data // // sync fetches chunks, checks each one against its name, and files blocks in // the body store under the hash their own header gives. It does not fill the // header store: headers are authenticated by the chain's rules, not by content // names, and the header sync is the chain client's job. package main import ( "context" "crypto/ed25519" "crypto/rand" "encoding/hex" "flag" "fmt" "os" "path/filepath" "strconv" "strings" "time" "git.smesh.lol/btcd-lite/internal/chains" "git.smesh.lol/btcd-lite/internal/fetch" "git.smesh.lol/btcd-lite/internal/store" ) func main() { if len(os.Args) < 2 { usage() } switch os.Args[1] { case "keygen": keygen(os.Args[2:]) case "sync": sync(os.Args[2:]) case "utxo": utxo(os.Args[2:]) default: usage() } } func usage() { fmt.Fprint(os.Stderr, `ftool: fill an instance from a btcd-lite service ftool keygen -out instance.key ftool sync -from URL -chain btc -from-height N -to-height M [-key FILE] -data DIR ftool utxo -from URL -chain btc -data DIR -from is the service's base URL, no trailing slash. -chain is btc or xbt. -from-height and -to-height are a half-open range, in heights. -key, when given, is an Ed25519 seed in hex: a manifest not signed by it is refused before a chunk is fetched. `) os.Exit(2) } func keygen(args []string) { fs := flag.NewFlagSet("keygen", flag.ExitOnError) out := fs.String("out", "instance.key", "file to write the seed to") _ = fs.Parse(args) seed := make([]byte, ed25519.SeedSize) if _, err := rand.Read(seed); err != nil { fail("keygen: %v", err) } if err := os.WriteFile(*out, []byte(hex.EncodeToString(seed)+"\n"), 0o600); err != nil { fail("keygen: %v", err) } pub := ed25519.NewKeyFromSeed(seed).Public().(ed25519.PublicKey) fmt.Printf("seed written to %s\npublic key: %x\n", *out, pub) } func sync(args []string) { fs := flag.NewFlagSet("sync", flag.ExitOnError) from := fs.String("from", "", "service base URL") chainName := fs.String("chain", chains.Parent, "btc or xbt") fromHeight := fs.String("from-height", "0", "first height") toHeight := fs.String("to-height", "", "height past the last (required)") keyFile := fs.String("key", "", "Ed25519 seed file the manifest must be signed with") data := fs.String("data", "", "instance data directory") _ = fs.Parse(args) if *from == "" || *data == "" || *toHeight == "" { usage() } if _, err := chains.ByName(*chainName); err != nil { fail("%v", err) } lo, err := strconv.ParseUint(*fromHeight, 10, 32) if err != nil { fail("from-height: %v", err) } hi, err := strconv.ParseUint(*toHeight, 10, 32) if err != nil || hi <= lo { fail("to-height must be above from-height") } var pub ed25519.PublicKey if *keyFile != "" { seed, err := os.ReadFile(*keyFile) if err != nil { fail("key: %v", err) } raw, err := hex.DecodeString(strings.TrimSpace(string(seed))) if err != nil || len(raw) != ed25519.SeedSize { fail("key: %s is not a 32-byte hex seed", *keyFile) } pub = ed25519.NewKeyFromSeed(raw).Public().(ed25519.PublicKey) } bodies, err := store.NewBlockStore(filepath.Join(*data, "blocks")) if err != nil { fail("blocks: %v", err) } defer bodies.Close() ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour) defer cancel() c := &fetch.Client{Base: strings.TrimRight(*from, "/")} if _, err := c.Manifest(ctx, pub); err != nil { fail("manifest: %v", err) } start := time.Now() n, err := c.Blocks(ctx, *chainName, uint32(lo), uint32(hi), bodies) if err != nil { fail("sync: %v", err) } fmt.Printf("filed %d blocks for %s %d..%d in %s; the store holds %d\n", n, *chainName, lo, hi-1, time.Since(start).Round(time.Second), bodies.Len()) } // utxo fills a data directory's checkpoint chain from a service: the base // set, then each diff, verified at every boundary against the name the // service published. func utxo(args []string) { fs := flag.NewFlagSet("utxo", flag.ExitOnError) from := fs.String("from", "", "service base URL") chainName := fs.String("chain", chains.Parent, "btc or xbt") data := fs.String("data", "", "instance data directory") _ = fs.Parse(args) if *from == "" || *data == "" { usage() } ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour) defer cancel() c := &fetch.Client{Base: strings.TrimRight(*from, "/")} if _, err := chains.ByName(*chainName); err != nil { fail("%v", err) } dir := filepath.Join(*data, "utxo", *chainName) start := time.Now() n, err := c.UTXO(ctx, *chainName, dir) if err != nil { fail("utxo: %v (reached %d checkpoints)", err, n) } fmt.Printf("reached %d checkpoints in %s, in %s\n", n, dir, time.Since(start).Round(time.Second)) } func fail(format string, args ...any) { fmt.Fprintf(os.Stderr, "ftool: "+format+"\n", args...) os.Exit(1) }