|
17 | 17 | package clique |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "math/big" |
| 22 | + "strings" |
21 | 23 | "testing" |
22 | 24 |
|
23 | 25 | "github.com/scroll-tech/go-ethereum/common" |
@@ -123,3 +125,90 @@ func TestSealHash(t *testing.T) { |
123 | 125 | t.Errorf("have %x, want %x", have, want) |
124 | 126 | } |
125 | 127 | } |
| 128 | + |
| 129 | +func TestShadowFork(t *testing.T) { |
| 130 | + engineConf := *params.AllCliqueProtocolChanges.Clique |
| 131 | + engineConf.Epoch = 2 |
| 132 | + forkedEngineConf := engineConf |
| 133 | + forkedEngineConf.ShadowForkHeight = 3 |
| 134 | + shadowForkKey, _ := crypto.HexToECDSA(strings.Repeat("11", 32)) |
| 135 | + shadowForkAddr := crypto.PubkeyToAddress(shadowForkKey.PublicKey) |
| 136 | + forkedEngineConf.ShadowForkSigner = shadowForkAddr |
| 137 | + |
| 138 | + // Initialize a Clique chain with a single signer |
| 139 | + var ( |
| 140 | + db = rawdb.NewMemoryDatabase() |
| 141 | + key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 142 | + addr = crypto.PubkeyToAddress(key.PublicKey) |
| 143 | + engine = New(&engineConf, db) |
| 144 | + signer = new(types.HomesteadSigner) |
| 145 | + forkedEngine = New(&forkedEngineConf, db) |
| 146 | + ) |
| 147 | + genspec := &core.Genesis{ |
| 148 | + ExtraData: make([]byte, extraVanity+common.AddressLength+extraSeal), |
| 149 | + Alloc: map[common.Address]core.GenesisAccount{ |
| 150 | + addr: {Balance: big.NewInt(10000000000000000)}, |
| 151 | + }, |
| 152 | + BaseFee: big.NewInt(params.InitialBaseFee), |
| 153 | + } |
| 154 | + copy(genspec.ExtraData[extraVanity:], addr[:]) |
| 155 | + genesis := genspec.MustCommit(db) |
| 156 | + |
| 157 | + // Generate a batch of blocks, each properly signed |
| 158 | + chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) |
| 159 | + defer chain.Stop() |
| 160 | + |
| 161 | + forkedChain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, forkedEngine, vm.Config{}, nil, nil) |
| 162 | + defer forkedChain.Stop() |
| 163 | + |
| 164 | + blocks, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, forkedEngine, db, 16, func(i int, block *core.BlockGen) { |
| 165 | + // The chain maker doesn't have access to a chain, so the difficulty will be |
| 166 | + // lets unset (nil). Set it here to the correct value. |
| 167 | + if block.Number().Uint64() > forkedEngineConf.ShadowForkHeight { |
| 168 | + block.SetDifficulty(diffShadowFork) |
| 169 | + } else { |
| 170 | + block.SetDifficulty(diffInTurn) |
| 171 | + } |
| 172 | + |
| 173 | + tx, err := types.SignTx(types.NewTransaction(block.TxNonce(addr), common.Address{0x00}, new(big.Int), params.TxGas, block.BaseFee(), nil), signer, key) |
| 174 | + if err != nil { |
| 175 | + panic(err) |
| 176 | + } |
| 177 | + block.AddTxWithChain(chain, tx) |
| 178 | + }) |
| 179 | + for i, block := range blocks { |
| 180 | + header := block.Header() |
| 181 | + if i > 0 { |
| 182 | + header.ParentHash = blocks[i-1].Hash() |
| 183 | + } |
| 184 | + |
| 185 | + signingAddr, signingKey := addr, key |
| 186 | + if header.Number.Uint64() > forkedEngineConf.ShadowForkHeight { |
| 187 | + // start signing with shadow fork authority key |
| 188 | + signingAddr, signingKey = shadowForkAddr, shadowForkKey |
| 189 | + } |
| 190 | + |
| 191 | + header.Extra = make([]byte, extraVanity) |
| 192 | + if header.Number.Uint64()%engineConf.Epoch == 0 { |
| 193 | + header.Extra = append(header.Extra, signingAddr.Bytes()...) |
| 194 | + } |
| 195 | + header.Extra = append(header.Extra, bytes.Repeat([]byte{0}, extraSeal)...) |
| 196 | + |
| 197 | + sig, _ := crypto.Sign(SealHash(header).Bytes(), signingKey) |
| 198 | + copy(header.Extra[len(header.Extra)-extraSeal:], sig) |
| 199 | + blocks[i] = block.WithSeal(header) |
| 200 | + } |
| 201 | + |
| 202 | + if _, err := chain.InsertChain(blocks); err == nil { |
| 203 | + t.Fatalf("should've failed to insert some blocks to canonical chain") |
| 204 | + } |
| 205 | + if chain.CurrentHeader().Number.Uint64() != forkedEngineConf.ShadowForkHeight { |
| 206 | + t.Fatalf("unexpected canonical chain height") |
| 207 | + } |
| 208 | + if _, err := forkedChain.InsertChain(blocks); err != nil { |
| 209 | + t.Fatalf("failed to insert blocks to forked chain: %v %d", err, forkedChain.CurrentHeader().Number) |
| 210 | + } |
| 211 | + if forkedChain.CurrentHeader().Number.Uint64() != uint64(len(blocks)) { |
| 212 | + t.Fatalf("unexpected forked chain height") |
| 213 | + } |
| 214 | +} |
0 commit comments