|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rand" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + "github.com/scroll-tech/go-ethereum/export-headers-toolkit/types" |
| 11 | +) |
| 12 | + |
| 13 | +func TestMissingHeaderWriter(t *testing.T) { |
| 14 | + vanity1 := [32]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} |
| 15 | + vanity2 := [32]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02} |
| 16 | + vanity8 := [32]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08} |
| 17 | + |
| 18 | + var expectedBytes []byte |
| 19 | + expectedBytes = append(expectedBytes, 0x03) |
| 20 | + expectedBytes = append(expectedBytes, vanity1[:]...) |
| 21 | + expectedBytes = append(expectedBytes, vanity2[:]...) |
| 22 | + expectedBytes = append(expectedBytes, vanity8[:]...) |
| 23 | + |
| 24 | + seenVanity := map[[32]byte]bool{ |
| 25 | + vanity8: true, |
| 26 | + vanity1: true, |
| 27 | + vanity2: true, |
| 28 | + } |
| 29 | + var buf []byte |
| 30 | + bytesBuffer := bytes.NewBuffer(buf) |
| 31 | + mhw := newMissingHeaderWriter(bytesBuffer, seenVanity) |
| 32 | + |
| 33 | + mhw.writeVanities() |
| 34 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 35 | + |
| 36 | + // Header0 |
| 37 | + { |
| 38 | + seal := randomSeal(65) |
| 39 | + header := types.NewHeader(0, 2, append(vanity1[:], seal...)) |
| 40 | + mhw.write(header) |
| 41 | + |
| 42 | + expectedBytes = append(expectedBytes, 0x00) // index 0 |
| 43 | + expectedBytes = append(expectedBytes, 0x00) // difficulty 2, seal length 65 |
| 44 | + expectedBytes = append(expectedBytes, seal...) |
| 45 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 46 | + } |
| 47 | + |
| 48 | + // Header1 |
| 49 | + { |
| 50 | + seal := randomSeal(65) |
| 51 | + header := types.NewHeader(1, 1, append(vanity2[:], seal...)) |
| 52 | + mhw.write(header) |
| 53 | + |
| 54 | + expectedBytes = append(expectedBytes, 0x01) // index 1 |
| 55 | + expectedBytes = append(expectedBytes, 0x01) // difficulty 1, seal length 65 |
| 56 | + expectedBytes = append(expectedBytes, seal...) |
| 57 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 58 | + } |
| 59 | + |
| 60 | + // Header2 |
| 61 | + { |
| 62 | + seal := randomSeal(85) |
| 63 | + header := types.NewHeader(2, 2, append(vanity2[:], seal...)) |
| 64 | + mhw.write(header) |
| 65 | + |
| 66 | + expectedBytes = append(expectedBytes, 0x01) // index 1 |
| 67 | + expectedBytes = append(expectedBytes, 0x02) // difficulty 2, seal length 85 |
| 68 | + expectedBytes = append(expectedBytes, seal...) |
| 69 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 70 | + } |
| 71 | + |
| 72 | + // Header3 |
| 73 | + { |
| 74 | + seal := randomSeal(85) |
| 75 | + header := types.NewHeader(3, 1, append(vanity8[:], seal...)) |
| 76 | + mhw.write(header) |
| 77 | + |
| 78 | + expectedBytes = append(expectedBytes, 0x02) // index 2 |
| 79 | + expectedBytes = append(expectedBytes, 0x03) // difficulty 1, seal length 85 |
| 80 | + expectedBytes = append(expectedBytes, seal...) |
| 81 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 82 | + } |
| 83 | + |
| 84 | + // Header4 |
| 85 | + { |
| 86 | + seal := randomSeal(65) |
| 87 | + header := types.NewHeader(4, 2, append(vanity1[:], seal...)) |
| 88 | + mhw.write(header) |
| 89 | + |
| 90 | + expectedBytes = append(expectedBytes, 0x00) // index 0 |
| 91 | + expectedBytes = append(expectedBytes, 0x00) // difficulty 2, seal length 65 |
| 92 | + expectedBytes = append(expectedBytes, seal...) |
| 93 | + assert.Equal(t, expectedBytes, bytesBuffer.Bytes()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func randomSeal(length int) []byte { |
| 98 | + buf := make([]byte, length) |
| 99 | + _, _ = rand.Read(buf) |
| 100 | + return buf |
| 101 | +} |
0 commit comments