|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/binary" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/scroll-tech/go-ethereum/export-headers-toolkit/types" |
| 14 | +) |
| 15 | + |
| 16 | +// dedupCmd represents the dedup command |
| 17 | +var dedupCmd = &cobra.Command{ |
| 18 | + Use: "dedup", |
| 19 | + Short: "Deduplicate the headers file, print unique values and create a new file with the deduplicated headers", |
| 20 | + Long: `Deduplicate the headers file, print unique values and create a new file with the deduplicated headers. |
| 21 | +
|
| 22 | +The binary layout of the deduplicated file is as follows: |
| 23 | +- 1 byte for the count of unique vanity |
| 24 | +- 32 bytes for each unique vanity |
| 25 | +- for each header: |
| 26 | + - 1 byte for the index of the vanity in the unique vanity list |
| 27 | + - 1 byte (bitmask, lsb first): |
| 28 | + - bit 0: 0 if difficulty is 2, 1 if difficulty is 1 |
| 29 | + - bit 1: 0 if seal length is 65, 1 if seal length is 85 |
| 30 | + - rest: 0 |
| 31 | + - 65 or 85 bytes for the seal`, |
| 32 | + Run: func(cmd *cobra.Command, args []string) { |
| 33 | + inputFile, err := cmd.Flags().GetString("input") |
| 34 | + if err != nil { |
| 35 | + log.Fatalf("Error reading output flag: %v", err) |
| 36 | + } |
| 37 | + outputFile, err := cmd.Flags().GetString("output") |
| 38 | + if err != nil { |
| 39 | + log.Fatalf("Error reading output flag: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + runDedup(inputFile, outputFile) |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +func init() { |
| 47 | + rootCmd.AddCommand(dedupCmd) |
| 48 | + |
| 49 | + dedupCmd.Flags().String("input", "headers.bin", "headers file") |
| 50 | + dedupCmd.Flags().String("output", "headers-dedup.bin", "deduplicated, binary formatted file") |
| 51 | +} |
| 52 | + |
| 53 | +func runDedup(inputFile, outputFile string) { |
| 54 | + reader := newHeaderReader(inputFile) |
| 55 | + defer reader.close() |
| 56 | + |
| 57 | + // track header fields we've seen |
| 58 | + seenDifficulty := make(map[uint64]bool) |
| 59 | + seenVanity := make(map[[32]byte]bool) |
| 60 | + seenSealLen := make(map[int]bool) |
| 61 | + |
| 62 | + reader.read(func(header *types.Header) { |
| 63 | + seenDifficulty[header.Difficulty] = true |
| 64 | + seenVanity[header.Vanity()] = true |
| 65 | + seenSealLen[header.SealLen()] = true |
| 66 | + }) |
| 67 | + |
| 68 | + // Print report |
| 69 | + fmt.Println("--------------------------------------------------") |
| 70 | + fmt.Printf("Unique values seen in the headers file (last seen block: %d):\n", reader.lastHeader.Number) |
| 71 | + fmt.Printf("Distinct count: Difficulty:%d, Vanity:%d, SealLen:%d\n", len(seenDifficulty), len(seenVanity), len(seenSealLen)) |
| 72 | + fmt.Printf("--------------------------------------------------\n\n") |
| 73 | + |
| 74 | + for diff := range seenDifficulty { |
| 75 | + fmt.Printf("Difficulty: %d\n", diff) |
| 76 | + } |
| 77 | + |
| 78 | + for vanity := range seenVanity { |
| 79 | + fmt.Printf("Vanity: %x\n", vanity) |
| 80 | + } |
| 81 | + |
| 82 | + for sealLen := range seenSealLen { |
| 83 | + fmt.Printf("SealLen: %d\n", sealLen) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +type headerReader struct { |
| 88 | + file *os.File |
| 89 | + reader *bufio.Reader |
| 90 | + lastHeader *types.Header |
| 91 | +} |
| 92 | + |
| 93 | +func newHeaderReader(inputFile string) *headerReader { |
| 94 | + f, err := os.Open(inputFile) |
| 95 | + if err != nil { |
| 96 | + log.Fatalf("Error opening input file: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + h := &headerReader{ |
| 100 | + file: f, |
| 101 | + reader: bufio.NewReader(f), |
| 102 | + } |
| 103 | + |
| 104 | + return h |
| 105 | +} |
| 106 | + |
| 107 | +func (h *headerReader) read(callback func(header *types.Header)) { |
| 108 | + headerSizeBytes := make([]byte, types.HeaderSizeSerialized) |
| 109 | + |
| 110 | + for { |
| 111 | + _, err := io.ReadFull(h.reader, headerSizeBytes) |
| 112 | + if err != nil { |
| 113 | + if err == io.EOF { |
| 114 | + break |
| 115 | + } |
| 116 | + log.Fatalf("Error reading headerSizeBytes: %v", err) |
| 117 | + } |
| 118 | + headerSize := binary.BigEndian.Uint16(headerSizeBytes) |
| 119 | + |
| 120 | + headerBytes := make([]byte, headerSize) |
| 121 | + _, err = io.ReadFull(h.reader, headerBytes) |
| 122 | + if err != nil { |
| 123 | + if err == io.EOF { |
| 124 | + break |
| 125 | + } |
| 126 | + log.Fatalf("Error reading headerBytes: %v", err) |
| 127 | + } |
| 128 | + header := new(types.Header).FromBytes(headerBytes) |
| 129 | + |
| 130 | + // sanity check: make sure headers are in order |
| 131 | + if h.lastHeader != nil && header.Number != h.lastHeader.Number+1 { |
| 132 | + fmt.Println("lastHeader:", h.lastHeader.String()) |
| 133 | + log.Fatalf("Missing block: %d, got %d instead", h.lastHeader.Number+1, header.Number) |
| 134 | + } |
| 135 | + h.lastHeader = header |
| 136 | + |
| 137 | + callback(header) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func (h *headerReader) close() { |
| 142 | + h.file.Close() |
| 143 | +} |
0 commit comments