|
17 | 17 | package core |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "fmt" |
20 | 21 | "math/big" |
21 | 22 | "math/rand" |
22 | 23 | "sync" |
@@ -1810,3 +1811,123 @@ func TestPrunedImportSide(t *testing.T) { |
1810 | 1811 | testSideImport(t, 1, 10) |
1811 | 1812 | testSideImport(t, 1, -10) |
1812 | 1813 | } |
| 1814 | + |
| 1815 | +// getLongAndShortChains returns two chains, |
| 1816 | +// A is longer, B is heavier |
| 1817 | +func getLongAndShortChains() (*BlockChain, []*types.Block, []*types.Block, error) { |
| 1818 | + // Generate a canonical chain to act as the main dataset |
| 1819 | + engine := ethash.NewFaker() |
| 1820 | + db := rawdb.NewMemoryDatabase() |
| 1821 | + genesis := new(Genesis).MustCommit(db) |
| 1822 | + |
| 1823 | + // Generate and import the canonical chain, |
| 1824 | + // Offset the time, to keep the difficulty low |
| 1825 | + longChain, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, 80, func(i int, b *BlockGen) { |
| 1826 | + b.SetCoinbase(common.Address{1}) |
| 1827 | + }) |
| 1828 | + diskdb := rawdb.NewMemoryDatabase() |
| 1829 | + new(Genesis).MustCommit(diskdb) |
| 1830 | + |
| 1831 | + chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil) |
| 1832 | + if err != nil { |
| 1833 | + return nil, nil, nil, fmt.Errorf("failed to create tester chain: %v", err) |
| 1834 | + } |
| 1835 | + |
| 1836 | + // Generate fork chain, make it shorter than canon, with common ancestor pretty early |
| 1837 | + parentIndex := 3 |
| 1838 | + parent := longChain[parentIndex] |
| 1839 | + heavyChain, _ := GenerateChain(params.TestChainConfig, parent, engine, db, 75, func(i int, b *BlockGen) { |
| 1840 | + b.SetCoinbase(common.Address{2}) |
| 1841 | + b.OffsetTime(-9) |
| 1842 | + }) |
| 1843 | + // Verify that the test is sane |
| 1844 | + var ( |
| 1845 | + longerTd = new(big.Int) |
| 1846 | + shorterTd = new(big.Int) |
| 1847 | + ) |
| 1848 | + for index, b := range longChain { |
| 1849 | + longerTd.Add(longerTd, b.Difficulty()) |
| 1850 | + if index <= parentIndex { |
| 1851 | + shorterTd.Add(shorterTd, b.Difficulty()) |
| 1852 | + } |
| 1853 | + } |
| 1854 | + for _, b := range heavyChain { |
| 1855 | + shorterTd.Add(shorterTd, b.Difficulty()) |
| 1856 | + } |
| 1857 | + if shorterTd.Cmp(longerTd) <= 0 { |
| 1858 | + return nil, nil, nil, fmt.Errorf("Test is moot, heavyChain td (%v) must be larger than canon td (%v)", shorterTd, longerTd) |
| 1859 | + } |
| 1860 | + longerNum := longChain[len(longChain)-1].NumberU64() |
| 1861 | + shorterNum := heavyChain[len(heavyChain)-1].NumberU64() |
| 1862 | + if shorterNum >= longerNum { |
| 1863 | + return nil, nil, nil, fmt.Errorf("Test is moot, heavyChain num (%v) must be lower than canon num (%v)", shorterNum, longerNum) |
| 1864 | + } |
| 1865 | + return chain, longChain, heavyChain, nil |
| 1866 | +} |
| 1867 | + |
| 1868 | +// TestReorgToShorterRemovesCanonMapping tests that if we |
| 1869 | +// 1. Have a chain [0 ... N .. X] |
| 1870 | +// 2. Reorg to shorter but heavier chain [0 ... N ... Y] |
| 1871 | +// 3. Then there should be no canon mapping for the block at height X |
| 1872 | +func TestReorgToShorterRemovesCanonMapping(t *testing.T) { |
| 1873 | + chain, canonblocks, sideblocks, err := getLongAndShortChains() |
| 1874 | + if err != nil { |
| 1875 | + t.Fatal(err) |
| 1876 | + } |
| 1877 | + if n, err := chain.InsertChain(canonblocks); err != nil { |
| 1878 | + t.Fatalf("block %d: failed to insert into chain: %v", n, err) |
| 1879 | + } |
| 1880 | + canonNum := chain.CurrentBlock().NumberU64() |
| 1881 | + _, err = chain.InsertChain(sideblocks) |
| 1882 | + if err != nil { |
| 1883 | + t.Errorf("Got error, %v", err) |
| 1884 | + } |
| 1885 | + head := chain.CurrentBlock() |
| 1886 | + if got := sideblocks[len(sideblocks)-1].Hash(); got != head.Hash() { |
| 1887 | + t.Fatalf("head wrong, expected %x got %x", head.Hash(), got) |
| 1888 | + } |
| 1889 | + // We have now inserted a sidechain. |
| 1890 | + if blockByNum := chain.GetBlockByNumber(canonNum); blockByNum != nil { |
| 1891 | + t.Errorf("expected block to be gone: %v", blockByNum.NumberU64()) |
| 1892 | + } |
| 1893 | + if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil { |
| 1894 | + t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64()) |
| 1895 | + } |
| 1896 | +} |
| 1897 | + |
| 1898 | +// TestReorgToShorterRemovesCanonMappingHeaderChain is the same scenario |
| 1899 | +// as TestReorgToShorterRemovesCanonMapping, but applied on headerchain |
| 1900 | +// imports -- that is, for fast sync |
| 1901 | +func TestReorgToShorterRemovesCanonMappingHeaderChain(t *testing.T) { |
| 1902 | + chain, canonblocks, sideblocks, err := getLongAndShortChains() |
| 1903 | + if err != nil { |
| 1904 | + t.Fatal(err) |
| 1905 | + } |
| 1906 | + // Convert into headers |
| 1907 | + canonHeaders := make([]*types.Header, len(canonblocks)) |
| 1908 | + for i, block := range canonblocks { |
| 1909 | + canonHeaders[i] = block.Header() |
| 1910 | + } |
| 1911 | + if n, err := chain.InsertHeaderChain(canonHeaders, 0); err != nil { |
| 1912 | + t.Fatalf("header %d: failed to insert into chain: %v", n, err) |
| 1913 | + } |
| 1914 | + canonNum := chain.CurrentHeader().Number.Uint64() |
| 1915 | + sideHeaders := make([]*types.Header, len(sideblocks)) |
| 1916 | + for i, block := range sideblocks { |
| 1917 | + sideHeaders[i] = block.Header() |
| 1918 | + } |
| 1919 | + if n, err := chain.InsertHeaderChain(sideHeaders, 0); err != nil { |
| 1920 | + t.Fatalf("header %d: failed to insert into chain: %v", n, err) |
| 1921 | + } |
| 1922 | + head := chain.CurrentHeader() |
| 1923 | + if got := sideblocks[len(sideblocks)-1].Hash(); got != head.Hash() { |
| 1924 | + t.Fatalf("head wrong, expected %x got %x", head.Hash(), got) |
| 1925 | + } |
| 1926 | + // We have now inserted a sidechain. |
| 1927 | + if blockByNum := chain.GetBlockByNumber(canonNum); blockByNum != nil { |
| 1928 | + t.Errorf("expected block to be gone: %v", blockByNum.NumberU64()) |
| 1929 | + } |
| 1930 | + if headerByNum := chain.GetHeaderByNumber(canonNum); headerByNum != nil { |
| 1931 | + t.Errorf("expected header to be gone: %v", headerByNum.Number.Uint64()) |
| 1932 | + } |
| 1933 | +} |
0 commit comments