|
| 1 | +package main |
| 2 | + |
| 3 | +// Checks if a db instance is equivalent to some prefix of an input script. |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + |
| 10 | + ds "github.com/ipfs/go-datastore" |
| 11 | + fuzzer "github.com/ipfs/go-datastore/fuzz" |
| 12 | + dsq "github.com/ipfs/go-datastore/query" |
| 13 | + |
| 14 | + "github.com/spf13/pflag" |
| 15 | +) |
| 16 | + |
| 17 | +var input *string = pflag.StringP("input", "i", "", "file to read input from (stdin used if not specified)") |
| 18 | +var db *string = pflag.StringP("db", "d", "go-ds-badger", "database driver") |
| 19 | +var dbPrev *string = pflag.StringP("exist", "e", "tmp1", "database instance already made") |
| 20 | +var dbFile *string = pflag.StringP("file", "f", "tmp2", "where the replay should live") |
| 21 | +var threads *int = pflag.IntP("threads", "t", 1, "concurrent threads") |
| 22 | + |
| 23 | +type validatingReader struct { |
| 24 | + b []byte |
| 25 | + i int |
| 26 | + validator func(bool) bool |
| 27 | + validI int |
| 28 | +} |
| 29 | + |
| 30 | +func (v *validatingReader) Read(buf []byte) (n int, err error) { |
| 31 | + if v.i == len(v.b) { |
| 32 | + return 0, nil |
| 33 | + } |
| 34 | + if v.validator(false) { |
| 35 | + v.validI = v.i |
| 36 | + } |
| 37 | + buf[0] = v.b[v.i] |
| 38 | + v.i++ |
| 39 | + return 1, nil |
| 40 | +} |
| 41 | + |
| 42 | +func main() { |
| 43 | + pflag.Parse() |
| 44 | + |
| 45 | + fuzzer.Threads = *threads |
| 46 | + |
| 47 | + var dat []byte |
| 48 | + var err error |
| 49 | + if *input == "" { |
| 50 | + dat, err = ioutil.ReadAll(os.Stdin) |
| 51 | + } else { |
| 52 | + dat, err = ioutil.ReadFile(*input) |
| 53 | + } |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(os.Stderr, "could not read %s: %v\n", *input, err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + previousDB, err := fuzzer.Open(*db, *dbPrev, false) |
| 60 | + if err != nil { |
| 61 | + fmt.Fprintf(os.Stderr, "could not open: %v\n", err) |
| 62 | + return |
| 63 | + } |
| 64 | + defer previousDB.Cancel() |
| 65 | + |
| 66 | + replayDB, err := fuzzer.Open(*db, *dbFile, true) |
| 67 | + if err != nil { |
| 68 | + fmt.Fprintf(os.Stderr, "could not open: %v\n", err) |
| 69 | + return |
| 70 | + } |
| 71 | + defer replayDB.Cancel() |
| 72 | + |
| 73 | + reader := validatingReader{dat, 0, func(verbose bool) bool { |
| 74 | + res, _ := replayDB.DB().Query(dsq.Query{}) |
| 75 | + for e := range res.Next() { |
| 76 | + if e.Entry.Key == "/" { |
| 77 | + continue |
| 78 | + } |
| 79 | + if h, _ := previousDB.DB().Has(ds.NewKey(e.Entry.Key)); !h { |
| 80 | + if verbose { |
| 81 | + fmt.Printf("failed - script run db has %s not in existing.\n", e.Entry.Key) |
| 82 | + } |
| 83 | + return false // not yet complete |
| 84 | + } |
| 85 | + } |
| 86 | + // next; make sure the other way is equal. |
| 87 | + res, _ = previousDB.DB().Query(dsq.Query{}) |
| 88 | + for e := range res.Next() { |
| 89 | + if e.Entry.Key == "/" { |
| 90 | + continue |
| 91 | + } |
| 92 | + if h, _ := replayDB.DB().Has(ds.NewKey(e.Entry.Key)); !h { |
| 93 | + if verbose { |
| 94 | + fmt.Printf("failed - existing db has %s not in replay.\n", e.Entry.Key) |
| 95 | + } |
| 96 | + return false |
| 97 | + } |
| 98 | + } |
| 99 | + // db images are the same. |
| 100 | + return true |
| 101 | + }, -1} |
| 102 | + |
| 103 | + replayDB.FuzzStream(&reader) |
| 104 | + if reader.validator(true) { |
| 105 | + reader.validI = reader.i |
| 106 | + } |
| 107 | + |
| 108 | + if reader.validI > -1 { |
| 109 | + fmt.Printf("Matched at stream position %d.\n", reader.validI) |
| 110 | + os.Exit(0) |
| 111 | + } else { |
| 112 | + fmt.Printf("Failed to match\n") |
| 113 | + os.Exit(1) |
| 114 | + } |
| 115 | +} |
0 commit comments