|
| 1 | +package trust |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/ethereum/go-ethereum/consensus/clique" |
| 9 | + "github.com/ethereum/go-ethereum/core" |
| 10 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/ethereum/go-ethereum/core/vm" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/ethdb" |
| 15 | + "github.com/ethereum/go-ethereum/p2p" |
| 16 | + "github.com/ethereum/go-ethereum/p2p/enode" |
| 17 | + "github.com/ethereum/go-ethereum/params" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + // testKey is a private key to use for funding a tester account. |
| 22 | + testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 23 | + |
| 24 | + // testAddr is the Ethereum address of the tester account. |
| 25 | + testAddr = crypto.PubkeyToAddress(testKey.PublicKey) |
| 26 | +) |
| 27 | + |
| 28 | +// testBackend is a mock implementation of the live Ethereum message handler. Its |
| 29 | +// purpose is to allow testing the request/reply workflows and wire serialization |
| 30 | +// in the `eth` protocol without actually doing any data processing. |
| 31 | +type testBackend struct { |
| 32 | + db ethdb.Database |
| 33 | + chain *core.BlockChain |
| 34 | + txpool *core.TxPool |
| 35 | +} |
| 36 | + |
| 37 | +// newTestBackend creates an empty chain and wraps it into a mock backend. |
| 38 | +func newTestBackend(blocks int) *testBackend { |
| 39 | + return newTestBackendWithGenerator(blocks) |
| 40 | +} |
| 41 | + |
| 42 | +// newTestBackend creates a chain with a number of explicitly defined blocks and |
| 43 | +// wraps it into a mock backend. |
| 44 | +func newTestBackendWithGenerator(blocks int) *testBackend { |
| 45 | + signer := types.HomesteadSigner{} |
| 46 | + db := rawdb.NewMemoryDatabase() |
| 47 | + engine := clique.New(params.AllCliqueProtocolChanges.Clique, db) |
| 48 | + genspec := &core.Genesis{ |
| 49 | + //Config: params.TestChainConfig, |
| 50 | + ExtraData: make([]byte, 32+common.AddressLength+65), |
| 51 | + Alloc: core.GenesisAlloc{testAddr: {Balance: big.NewInt(100000000000000000)}}, |
| 52 | + } |
| 53 | + copy(genspec.ExtraData[32:], testAddr[:]) |
| 54 | + genesis := genspec.MustCommit(db) |
| 55 | + |
| 56 | + chain, _ := core.NewBlockChain(db, nil, params.AllCliqueProtocolChanges, engine, vm.Config{}, nil, nil) |
| 57 | + generator := func(i int, block *core.BlockGen) { |
| 58 | + // The chain maker doesn't have access to a chain, so the difficulty will be |
| 59 | + // lets unset (nil). Set it here to the correct value. |
| 60 | + // block.SetCoinbase(testAddr) |
| 61 | + block.SetDifficulty(big.NewInt(2)) |
| 62 | + |
| 63 | + // We want to simulate an empty middle block, having the same state as the |
| 64 | + // first one. The last is needs a state change again to force a reorg. |
| 65 | + tx, err := types.SignTx(types.NewTransaction(block.TxNonce(testAddr), common.Address{0x01}, big.NewInt(1), params.TxGas, nil, nil), signer, testKey) |
| 66 | + if err != nil { |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + block.AddTxWithChain(chain, tx) |
| 70 | + } |
| 71 | + |
| 72 | + bs, _ := core.GenerateChain(params.AllCliqueProtocolChanges, genesis, engine, db, blocks, generator) |
| 73 | + for i, block := range bs { |
| 74 | + header := block.Header() |
| 75 | + if i > 0 { |
| 76 | + header.ParentHash = bs[i-1].Hash() |
| 77 | + } |
| 78 | + header.Extra = make([]byte, 32+65) |
| 79 | + header.Difficulty = big.NewInt(2) |
| 80 | + |
| 81 | + sig, _ := crypto.Sign(clique.SealHash(header).Bytes(), testKey) |
| 82 | + copy(header.Extra[len(header.Extra)-65:], sig) |
| 83 | + bs[i] = block.WithSeal(header) |
| 84 | + } |
| 85 | + |
| 86 | + if _, err := chain.InsertChain(bs); err != nil { |
| 87 | + panic(err) |
| 88 | + } |
| 89 | + |
| 90 | + txconfig := core.DefaultTxPoolConfig |
| 91 | + txconfig.Journal = "" // Don't litter the disk with test journals |
| 92 | + |
| 93 | + return &testBackend{ |
| 94 | + db: db, |
| 95 | + chain: chain, |
| 96 | + txpool: core.NewTxPool(txconfig, params.AllCliqueProtocolChanges, chain), |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// close tears down the transaction pool and chain behind the mock backend. |
| 101 | +func (b *testBackend) close() { |
| 102 | + b.txpool.Stop() |
| 103 | + b.chain.Stop() |
| 104 | +} |
| 105 | + |
| 106 | +func (b *testBackend) Chain() *core.BlockChain { return b.chain } |
| 107 | + |
| 108 | +func (b *testBackend) RunPeer(peer *Peer, handler Handler) error { |
| 109 | + // Normally the backend would do peer mainentance and handshakes. All that |
| 110 | + // is omitted and we will just give control back to the handler. |
| 111 | + return handler(peer) |
| 112 | +} |
| 113 | +func (b *testBackend) PeerInfo(enode.ID) interface{} { panic("not implemented") } |
| 114 | + |
| 115 | +func (b *testBackend) Handle(*Peer, Packet) error { |
| 116 | + panic("data processing tests should be done in the handler package") |
| 117 | +} |
| 118 | + |
| 119 | +func TestRequestRoot(t *testing.T) { testRequestRoot(t, Trust1) } |
| 120 | + |
| 121 | +func testRequestRoot(t *testing.T, protocol uint) { |
| 122 | + t.Parallel() |
| 123 | + |
| 124 | + blockNum := 1032 // The latest 1024 blocks' DiffLayer will be cached. |
| 125 | + backend := newTestBackend(blockNum) |
| 126 | + defer backend.close() |
| 127 | + |
| 128 | + peer, _ := newTestPeer("peer", protocol, backend) |
| 129 | + defer peer.close() |
| 130 | + |
| 131 | + pairs := []struct { |
| 132 | + req RootRequestPacket |
| 133 | + res RootResponsePacket |
| 134 | + }{ |
| 135 | + { |
| 136 | + req: RootRequestPacket{ |
| 137 | + RequestId: 1, |
| 138 | + BlockNumber: 1, |
| 139 | + }, |
| 140 | + res: RootResponsePacket{ |
| 141 | + RequestId: 1, |
| 142 | + Status: types.StatusPartiallyVerified, |
| 143 | + BlockNumber: 1, |
| 144 | + Extra: defaultExtra, |
| 145 | + }, |
| 146 | + }, |
| 147 | + { |
| 148 | + req: RootRequestPacket{ |
| 149 | + RequestId: 2, |
| 150 | + BlockNumber: 128, |
| 151 | + }, |
| 152 | + res: RootResponsePacket{ |
| 153 | + RequestId: 2, |
| 154 | + Status: types.StatusFullVerified, |
| 155 | + BlockNumber: 128, |
| 156 | + Extra: defaultExtra, |
| 157 | + }, |
| 158 | + }, |
| 159 | + { |
| 160 | + req: RootRequestPacket{ |
| 161 | + RequestId: 3, |
| 162 | + BlockNumber: 128, |
| 163 | + BlockHash: types.EmptyRootHash, |
| 164 | + DiffHash: types.EmptyRootHash, |
| 165 | + }, |
| 166 | + res: RootResponsePacket{ |
| 167 | + RequestId: 3, |
| 168 | + Status: types.StatusImpossibleFork, |
| 169 | + BlockNumber: 128, |
| 170 | + BlockHash: types.EmptyRootHash, |
| 171 | + Root: common.Hash{}, |
| 172 | + Extra: defaultExtra, |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + req: RootRequestPacket{ |
| 177 | + RequestId: 4, |
| 178 | + BlockNumber: 128, |
| 179 | + DiffHash: types.EmptyRootHash, |
| 180 | + }, |
| 181 | + res: RootResponsePacket{ |
| 182 | + RequestId: 4, |
| 183 | + Status: types.StatusDiffHashMismatch, |
| 184 | + BlockNumber: 128, |
| 185 | + Root: common.Hash{}, |
| 186 | + Extra: defaultExtra, |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + req: RootRequestPacket{ |
| 191 | + RequestId: 5, |
| 192 | + BlockNumber: 1024, |
| 193 | + }, |
| 194 | + res: RootResponsePacket{ |
| 195 | + RequestId: 5, |
| 196 | + Status: types.StatusFullVerified, |
| 197 | + BlockNumber: 1024, |
| 198 | + Extra: defaultExtra, |
| 199 | + }, |
| 200 | + }, |
| 201 | + { |
| 202 | + req: RootRequestPacket{ |
| 203 | + RequestId: 6, |
| 204 | + BlockNumber: 1024, |
| 205 | + BlockHash: types.EmptyRootHash, |
| 206 | + DiffHash: types.EmptyRootHash, |
| 207 | + }, |
| 208 | + res: RootResponsePacket{ |
| 209 | + RequestId: 6, |
| 210 | + Status: types.StatusPossibleFork, |
| 211 | + BlockNumber: 1024, |
| 212 | + BlockHash: types.EmptyRootHash, |
| 213 | + Root: common.Hash{}, |
| 214 | + Extra: defaultExtra, |
| 215 | + }, |
| 216 | + }, |
| 217 | + { |
| 218 | + req: RootRequestPacket{ |
| 219 | + RequestId: 7, |
| 220 | + BlockNumber: 1033, |
| 221 | + BlockHash: types.EmptyRootHash, |
| 222 | + DiffHash: types.EmptyRootHash, |
| 223 | + }, |
| 224 | + res: RootResponsePacket{ |
| 225 | + RequestId: 7, |
| 226 | + Status: types.StatusBlockNewer, |
| 227 | + BlockNumber: 1033, |
| 228 | + BlockHash: types.EmptyRootHash, |
| 229 | + Root: common.Hash{}, |
| 230 | + Extra: defaultExtra, |
| 231 | + }, |
| 232 | + }, |
| 233 | + { |
| 234 | + req: RootRequestPacket{ |
| 235 | + RequestId: 8, |
| 236 | + BlockNumber: 1044, |
| 237 | + BlockHash: types.EmptyRootHash, |
| 238 | + DiffHash: types.EmptyRootHash, |
| 239 | + }, |
| 240 | + res: RootResponsePacket{ |
| 241 | + RequestId: 8, |
| 242 | + Status: types.StatusBlockTooNew, |
| 243 | + BlockNumber: 1044, |
| 244 | + BlockHash: types.EmptyRootHash, |
| 245 | + Root: common.Hash{}, |
| 246 | + Extra: defaultExtra, |
| 247 | + }, |
| 248 | + }, |
| 249 | + } |
| 250 | + |
| 251 | + for idx, pair := range pairs { |
| 252 | + header := backend.Chain().GetHeaderByNumber(pair.req.BlockNumber) |
| 253 | + if header != nil { |
| 254 | + if pair.res.Status.Code&0xFF00 == types.StatusVerified.Code { |
| 255 | + pair.req.BlockHash = header.Hash() |
| 256 | + pair.req.DiffHash, _ = core.CalculateDiffHash(backend.Chain().GetTrustedDiffLayer(header.Hash())) |
| 257 | + pair.res.BlockHash = pair.req.BlockHash |
| 258 | + pair.res.Root = header.Root |
| 259 | + } else if pair.res.Status.Code == types.StatusDiffHashMismatch.Code { |
| 260 | + pair.req.BlockHash = header.Hash() |
| 261 | + pair.res.BlockHash = pair.req.BlockHash |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + p2p.Send(peer.app, RequestRootMsg, pair.req) |
| 266 | + if err := p2p.ExpectMsg(peer.app, RespondRootMsg, pair.res); err != nil { |
| 267 | + t.Errorf("test %d: root response not expected: %v", idx, err) |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments