mirror of https://github.com/ethereum/go-ethereum
parent
c51e153b5c
commit
42f44dda54
@ -0,0 +1,525 @@ |
|||||||
|
package eth |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"math/big" |
||||||
|
"math/rand" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core" |
||||||
|
"github.com/ethereum/go-ethereum/core/state" |
||||||
|
"github.com/ethereum/go-ethereum/core/types" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/ethereum/go-ethereum/eth/downloader" |
||||||
|
"github.com/ethereum/go-ethereum/ethdb" |
||||||
|
"github.com/ethereum/go-ethereum/p2p" |
||||||
|
"github.com/ethereum/go-ethereum/params" |
||||||
|
) |
||||||
|
|
||||||
|
// Tests that hashes can be retrieved from a remote chain by hashes in reverse
|
||||||
|
// order.
|
||||||
|
func TestGetBlockHashes60(t *testing.T) { testGetBlockHashes(t, 60) } |
||||||
|
func TestGetBlockHashes61(t *testing.T) { testGetBlockHashes(t, 61) } |
||||||
|
|
||||||
|
func testGetBlockHashes(t *testing.T, protocol int) { |
||||||
|
pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Create a batch of tests for various scenarios
|
||||||
|
limit := downloader.MaxHashFetch |
||||||
|
tests := []struct { |
||||||
|
origin common.Hash |
||||||
|
number int |
||||||
|
result int |
||||||
|
}{ |
||||||
|
{common.Hash{}, 1, 0}, // Make sure non existent hashes don't return results
|
||||||
|
{pm.chainman.Genesis().Hash(), 1, 0}, // There are no hashes to retrieve up from the genesis
|
||||||
|
{pm.chainman.GetBlockByNumber(5).Hash(), 5, 5}, // All the hashes including the genesis requested
|
||||||
|
{pm.chainman.GetBlockByNumber(5).Hash(), 10, 5}, // More hashes than available till the genesis requested
|
||||||
|
{pm.chainman.GetBlockByNumber(100).Hash(), 10, 10}, // All hashes available from the middle of the chain
|
||||||
|
{pm.chainman.CurrentBlock().Hash(), 10, 10}, // All hashes available from the head of the chain
|
||||||
|
{pm.chainman.CurrentBlock().Hash(), limit, limit}, // Request the maximum allowed hash count
|
||||||
|
{pm.chainman.CurrentBlock().Hash(), limit + 1, limit}, // Request more than the maximum allowed hash count
|
||||||
|
} |
||||||
|
// Run each of the tests and verify the results against the chain
|
||||||
|
for i, tt := range tests { |
||||||
|
// Assemble the hash response we would like to receive
|
||||||
|
resp := make([]common.Hash, tt.result) |
||||||
|
if len(resp) > 0 { |
||||||
|
from := pm.chainman.GetBlock(tt.origin).NumberU64() - 1 |
||||||
|
for j := 0; j < len(resp); j++ { |
||||||
|
resp[j] = pm.chainman.GetBlockByNumber(uint64(int(from) - j)).Hash() |
||||||
|
} |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x03, getBlockHashesData{tt.origin, uint64(tt.number)}) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x04, resp); err != nil { |
||||||
|
t.Errorf("test %d: block hashes mismatch: %v", i, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that hashes can be retrieved from a remote chain by numbers in forward
|
||||||
|
// order.
|
||||||
|
func TestGetBlockHashesFromNumber60(t *testing.T) { testGetBlockHashesFromNumber(t, 60) } |
||||||
|
func TestGetBlockHashesFromNumber61(t *testing.T) { testGetBlockHashesFromNumber(t, 61) } |
||||||
|
|
||||||
|
func testGetBlockHashesFromNumber(t *testing.T, protocol int) { |
||||||
|
pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Create a batch of tests for various scenarios
|
||||||
|
limit := downloader.MaxHashFetch |
||||||
|
tests := []struct { |
||||||
|
origin uint64 |
||||||
|
number int |
||||||
|
result int |
||||||
|
}{ |
||||||
|
{pm.chainman.CurrentBlock().NumberU64() + 1, 1, 0}, // Out of bounds requests should return empty
|
||||||
|
{pm.chainman.CurrentBlock().NumberU64(), 1, 1}, // Make sure the head hash can be retrieved
|
||||||
|
{pm.chainman.CurrentBlock().NumberU64() - 4, 5, 5}, // All hashes, including the head hash requested
|
||||||
|
{pm.chainman.CurrentBlock().NumberU64() - 4, 10, 5}, // More hashes requested than available till the head
|
||||||
|
{pm.chainman.CurrentBlock().NumberU64() - 100, 10, 10}, // All hashes available from the middle of the chain
|
||||||
|
{0, 10, 10}, // All hashes available from the root of the chain
|
||||||
|
{0, limit, limit}, // Request the maximum allowed hash count
|
||||||
|
{0, limit + 1, limit}, // Request more than the maximum allowed hash count
|
||||||
|
{0, 1, 1}, // Make sure the genesis hash can be retrieved
|
||||||
|
} |
||||||
|
// Run each of the tests and verify the results against the chain
|
||||||
|
for i, tt := range tests { |
||||||
|
// Assemble the hash response we would like to receive
|
||||||
|
resp := make([]common.Hash, tt.result) |
||||||
|
for j := 0; j < len(resp); j++ { |
||||||
|
resp[j] = pm.chainman.GetBlockByNumber(tt.origin + uint64(j)).Hash() |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x08, getBlockHashesFromNumberData{tt.origin, uint64(tt.number)}) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x04, resp); err != nil { |
||||||
|
t.Errorf("test %d: block hashes mismatch: %v", i, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that blocks can be retrieved from a remote chain based on their hashes.
|
||||||
|
func TestGetBlocks60(t *testing.T) { testGetBlocks(t, 60) } |
||||||
|
func TestGetBlocks61(t *testing.T) { testGetBlocks(t, 61) } |
||||||
|
|
||||||
|
func testGetBlocks(t *testing.T, protocol int) { |
||||||
|
pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Create a batch of tests for various scenarios
|
||||||
|
limit := downloader.MaxBlockFetch |
||||||
|
tests := []struct { |
||||||
|
random int // Number of blocks to fetch randomly from the chain
|
||||||
|
explicit []common.Hash // Explicitly requested blocks
|
||||||
|
available []bool // Availability of explicitly requested blocks
|
||||||
|
expected int // Total number of existing blocks to expect
|
||||||
|
}{ |
||||||
|
{1, nil, nil, 1}, // A single random block should be retrievable
|
||||||
|
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
|
||||||
|
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
|
||||||
|
{limit + 1, nil, nil, limit}, // No more that the possible block count should be returned
|
||||||
|
{0, []common.Hash{pm.chainman.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
|
||||||
|
{0, []common.Hash{pm.chainman.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
|
||||||
|
{0, []common.Hash{common.Hash{}}, []bool{false}, 0}, // A non existent block should not be returned
|
||||||
|
|
||||||
|
// Existing and non-existing blocks interleaved should not cause problems
|
||||||
|
{0, []common.Hash{ |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(1).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(10).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(100).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
}, []bool{false, true, false, true, false, true, false}, 3}, |
||||||
|
} |
||||||
|
// Run each of the tests and verify the results against the chain
|
||||||
|
for i, tt := range tests { |
||||||
|
// Collect the hashes to request, and the response to expect
|
||||||
|
hashes, seen := []common.Hash{}, make(map[int64]bool) |
||||||
|
blocks := []*types.Block{} |
||||||
|
|
||||||
|
for j := 0; j < tt.random; j++ { |
||||||
|
for { |
||||||
|
num := rand.Int63n(int64(pm.chainman.CurrentBlock().NumberU64())) |
||||||
|
if !seen[num] { |
||||||
|
seen[num] = true |
||||||
|
|
||||||
|
block := pm.chainman.GetBlockByNumber(uint64(num)) |
||||||
|
hashes = append(hashes, block.Hash()) |
||||||
|
if len(blocks) < tt.expected { |
||||||
|
blocks = append(blocks, block) |
||||||
|
} |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
for j, hash := range tt.explicit { |
||||||
|
hashes = append(hashes, hash) |
||||||
|
if tt.available[j] && len(blocks) < tt.expected { |
||||||
|
blocks = append(blocks, pm.chainman.GetBlock(hash)) |
||||||
|
} |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x05, hashes) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x06, blocks); err != nil { |
||||||
|
t.Errorf("test %d: blocks mismatch: %v", i, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that block headers can be retrieved from a remote chain based on user queries.
|
||||||
|
func TestGetBlockHeaders62(t *testing.T) { testGetBlockHeaders(t, 62) } |
||||||
|
func TestGetBlockHeaders63(t *testing.T) { testGetBlockHeaders(t, 63) } |
||||||
|
func TestGetBlockHeaders64(t *testing.T) { testGetBlockHeaders(t, 64) } |
||||||
|
|
||||||
|
func testGetBlockHeaders(t *testing.T, protocol int) { |
||||||
|
pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Create a "random" unknown hash for testing
|
||||||
|
var unknown common.Hash |
||||||
|
for i, _ := range unknown { |
||||||
|
unknown[i] = byte(i) |
||||||
|
} |
||||||
|
// Create a batch of tests for various scenarios
|
||||||
|
limit := uint64(downloader.MaxHeaderFetch) |
||||||
|
tests := []struct { |
||||||
|
query *getBlockHeadersData // The query to execute for header retrieval
|
||||||
|
expect []common.Hash // The hashes of the block whose headers are expected
|
||||||
|
}{ |
||||||
|
// A single random block should be retrievable by hash and number too
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Hash: pm.chainman.GetBlockByNumber(limit / 2).Hash()}, Amount: 1}, |
||||||
|
[]common.Hash{pm.chainman.GetBlockByNumber(limit / 2).Hash()}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1}, |
||||||
|
[]common.Hash{pm.chainman.GetBlockByNumber(limit / 2).Hash()}, |
||||||
|
}, |
||||||
|
// Multiple headers should be retrievable in both directions
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(limit / 2).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 + 1).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 + 2).Hash(), |
||||||
|
}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(limit / 2).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 - 1).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 - 2).Hash(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// Multiple headers with skip lists should be retrievable
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(limit / 2).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 + 4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 + 8).Hash(), |
||||||
|
}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(limit / 2).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 - 4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(limit/2 - 8).Hash(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// The chain endpoints should be retrievable
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: 0}, Amount: 1}, |
||||||
|
[]common.Hash{pm.chainman.GetBlockByNumber(0).Hash()}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: pm.chainman.CurrentBlock().NumberU64()}, Amount: 1}, |
||||||
|
[]common.Hash{pm.chainman.CurrentBlock().Hash()}, |
||||||
|
}, |
||||||
|
// Ensure protocol limits are honored
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: pm.chainman.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true}, |
||||||
|
pm.chainman.GetBlockHashesFromHash(pm.chainman.CurrentBlock().Hash(), limit), |
||||||
|
}, |
||||||
|
// Check that requesting more than available is handled gracefully
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: pm.chainman.CurrentBlock().NumberU64() - 4}, Skip: 3, Amount: 3}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(pm.chainman.CurrentBlock().NumberU64() - 4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(pm.chainman.CurrentBlock().NumberU64()).Hash(), |
||||||
|
}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(0).Hash(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// Check that requesting more than available is handled gracefully, even if mid skip
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: pm.chainman.CurrentBlock().NumberU64() - 4}, Skip: 2, Amount: 3}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(pm.chainman.CurrentBlock().NumberU64() - 4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(pm.chainman.CurrentBlock().NumberU64() - 1).Hash(), |
||||||
|
}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true}, |
||||||
|
[]common.Hash{ |
||||||
|
pm.chainman.GetBlockByNumber(4).Hash(), |
||||||
|
pm.chainman.GetBlockByNumber(1).Hash(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
// Check that non existing headers aren't returned
|
||||||
|
{ |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Hash: unknown}, Amount: 1}, |
||||||
|
[]common.Hash{}, |
||||||
|
}, { |
||||||
|
&getBlockHeadersData{Origin: hashOrNumber{Number: pm.chainman.CurrentBlock().NumberU64() + 1}, Amount: 1}, |
||||||
|
[]common.Hash{}, |
||||||
|
}, |
||||||
|
} |
||||||
|
// Run each of the tests and verify the results against the chain
|
||||||
|
for i, tt := range tests { |
||||||
|
// Collect the headers to expect in the response
|
||||||
|
headers := []*types.Header{} |
||||||
|
for _, hash := range tt.expect { |
||||||
|
headers = append(headers, pm.chainman.GetBlock(hash).Header()) |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x03, tt.query) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x04, headers); err != nil { |
||||||
|
t.Errorf("test %d: headers mismatch: %v", i, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that block contents can be retrieved from a remote chain based on their hashes.
|
||||||
|
func TestGetBlockBodies62(t *testing.T) { testGetBlockBodies(t, 62) } |
||||||
|
func TestGetBlockBodies63(t *testing.T) { testGetBlockBodies(t, 63) } |
||||||
|
func TestGetBlockBodies64(t *testing.T) { testGetBlockBodies(t, 64) } |
||||||
|
|
||||||
|
func testGetBlockBodies(t *testing.T, protocol int) { |
||||||
|
pm := newTestProtocolManager(downloader.MaxBlockFetch+15, nil, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Create a batch of tests for various scenarios
|
||||||
|
limit := downloader.MaxBlockFetch |
||||||
|
tests := []struct { |
||||||
|
random int // Number of blocks to fetch randomly from the chain
|
||||||
|
explicit []common.Hash // Explicitly requested blocks
|
||||||
|
available []bool // Availability of explicitly requested blocks
|
||||||
|
expected int // Total number of existing blocks to expect
|
||||||
|
}{ |
||||||
|
{1, nil, nil, 1}, // A single random block should be retrievable
|
||||||
|
{10, nil, nil, 10}, // Multiple random blocks should be retrievable
|
||||||
|
{limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
|
||||||
|
{limit + 1, nil, nil, limit}, // No more that the possible block count should be returned
|
||||||
|
{0, []common.Hash{pm.chainman.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
|
||||||
|
{0, []common.Hash{pm.chainman.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
|
||||||
|
{0, []common.Hash{common.Hash{}}, []bool{false}, 0}, // A non existent block should not be returned
|
||||||
|
|
||||||
|
// Existing and non-existing blocks interleaved should not cause problems
|
||||||
|
{0, []common.Hash{ |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(1).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(10).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
pm.chainman.GetBlockByNumber(100).Hash(), |
||||||
|
common.Hash{}, |
||||||
|
}, []bool{false, true, false, true, false, true, false}, 3}, |
||||||
|
} |
||||||
|
// Run each of the tests and verify the results against the chain
|
||||||
|
for i, tt := range tests { |
||||||
|
// Collect the hashes to request, and the response to expect
|
||||||
|
hashes, seen := []common.Hash{}, make(map[int64]bool) |
||||||
|
bodies := []*blockBody{} |
||||||
|
|
||||||
|
for j := 0; j < tt.random; j++ { |
||||||
|
for { |
||||||
|
num := rand.Int63n(int64(pm.chainman.CurrentBlock().NumberU64())) |
||||||
|
if !seen[num] { |
||||||
|
seen[num] = true |
||||||
|
|
||||||
|
block := pm.chainman.GetBlockByNumber(uint64(num)) |
||||||
|
hashes = append(hashes, block.Hash()) |
||||||
|
if len(bodies) < tt.expected { |
||||||
|
bodies = append(bodies, &blockBody{Transactions: block.Transactions(), Uncles: block.Uncles()}) |
||||||
|
} |
||||||
|
break |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
for j, hash := range tt.explicit { |
||||||
|
hashes = append(hashes, hash) |
||||||
|
if tt.available[j] && len(bodies) < tt.expected { |
||||||
|
block := pm.chainman.GetBlock(hash) |
||||||
|
bodies = append(bodies, &blockBody{Transactions: block.Transactions(), Uncles: block.Uncles()}) |
||||||
|
} |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x05, hashes) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x06, bodies); err != nil { |
||||||
|
t.Errorf("test %d: bodies mismatch: %v", i, err) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that the node state database can be retrieved based on hashes.
|
||||||
|
func TestGetNodeData63(t *testing.T) { testGetNodeData(t, 63) } |
||||||
|
func TestGetNodeData64(t *testing.T) { testGetNodeData(t, 64) } |
||||||
|
|
||||||
|
func testGetNodeData(t *testing.T, protocol int) { |
||||||
|
// Define three accounts to simulate transactions with
|
||||||
|
acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") |
||||||
|
acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") |
||||||
|
acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey) |
||||||
|
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey) |
||||||
|
|
||||||
|
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makerts_test)
|
||||||
|
generator := func(i int, block *core.BlockGen) { |
||||||
|
switch i { |
||||||
|
case 0: |
||||||
|
// In block 1, the test bank sends account #1 some ether.
|
||||||
|
tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) |
||||||
|
block.AddTx(tx) |
||||||
|
case 1: |
||||||
|
// In block 2, the test bank sends some more ether to account #1.
|
||||||
|
// acc1Addr passes it on to account #2.
|
||||||
|
tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) |
||||||
|
tx2, _ := types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) |
||||||
|
block.AddTx(tx1) |
||||||
|
block.AddTx(tx2) |
||||||
|
case 2: |
||||||
|
// Block 3 is empty but was mined by account #2.
|
||||||
|
block.SetCoinbase(acc2Addr) |
||||||
|
block.SetExtra([]byte("yeehaw")) |
||||||
|
case 3: |
||||||
|
// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
|
||||||
|
b2 := block.PrevBlock(1).Header() |
||||||
|
b2.Extra = []byte("foo") |
||||||
|
block.AddUncle(b2) |
||||||
|
b3 := block.PrevBlock(2).Header() |
||||||
|
b3.Extra = []byte("foo") |
||||||
|
block.AddUncle(b3) |
||||||
|
} |
||||||
|
} |
||||||
|
// Assemble the test environment
|
||||||
|
pm := newTestProtocolManager(4, generator, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Fetch for now the entire chain db
|
||||||
|
hashes := []common.Hash{} |
||||||
|
for _, key := range pm.chaindb.(*ethdb.MemDatabase).Keys() { |
||||||
|
hashes = append(hashes, common.BytesToHash(key)) |
||||||
|
} |
||||||
|
p2p.Send(peer.app, 0x0d, hashes) |
||||||
|
msg, err := peer.app.ReadMsg() |
||||||
|
if err != nil { |
||||||
|
t.Fatalf("failed to read node data response: %v", err) |
||||||
|
} |
||||||
|
if msg.Code != 0x0e { |
||||||
|
t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, 0x0c) |
||||||
|
} |
||||||
|
var data [][]byte |
||||||
|
if err := msg.Decode(&data); err != nil { |
||||||
|
t.Fatalf("failed to decode response node data: %v", err) |
||||||
|
} |
||||||
|
// Verify that all hashes correspond to the requested data, and reconstruct a state tree
|
||||||
|
for i, want := range hashes { |
||||||
|
if hash := crypto.Sha3Hash(data[i]); hash != want { |
||||||
|
fmt.Errorf("data hash mismatch: have %x, want %x", hash, want) |
||||||
|
} |
||||||
|
} |
||||||
|
statedb, _ := ethdb.NewMemDatabase() |
||||||
|
for i := 0; i < len(data); i++ { |
||||||
|
statedb.Put(hashes[i].Bytes(), data[i]) |
||||||
|
} |
||||||
|
accounts := []common.Address{testBankAddress, acc1Addr, acc2Addr} |
||||||
|
for i := uint64(0); i <= pm.chainman.CurrentBlock().NumberU64(); i++ { |
||||||
|
trie := state.New(pm.chainman.GetBlockByNumber(i).Root(), statedb) |
||||||
|
|
||||||
|
for j, acc := range accounts { |
||||||
|
bw := pm.chainman.State().GetBalance(acc) |
||||||
|
bh := trie.GetBalance(acc) |
||||||
|
|
||||||
|
if (bw != nil && bh == nil) || (bw == nil && bh != nil) { |
||||||
|
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw) |
||||||
|
} |
||||||
|
if bw != nil && bh != nil && bw.Cmp(bw) != 0 { |
||||||
|
t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Tests that the transaction receipts can be retrieved based on hashes.
|
||||||
|
func TestGetReceipt63(t *testing.T) { testGetReceipt(t, 63) } |
||||||
|
func TestGetReceipt64(t *testing.T) { testGetReceipt(t, 64) } |
||||||
|
|
||||||
|
func testGetReceipt(t *testing.T, protocol int) { |
||||||
|
// Define three accounts to simulate transactions with
|
||||||
|
acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") |
||||||
|
acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") |
||||||
|
acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey) |
||||||
|
acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey) |
||||||
|
|
||||||
|
// Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makerts_test)
|
||||||
|
generator := func(i int, block *core.BlockGen) { |
||||||
|
switch i { |
||||||
|
case 0: |
||||||
|
// In block 1, the test bank sends account #1 some ether.
|
||||||
|
tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey) |
||||||
|
block.AddTx(tx) |
||||||
|
case 1: |
||||||
|
// In block 2, the test bank sends some more ether to account #1.
|
||||||
|
// acc1Addr passes it on to account #2.
|
||||||
|
tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey) |
||||||
|
tx2, _ := types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key) |
||||||
|
block.AddTx(tx1) |
||||||
|
block.AddTx(tx2) |
||||||
|
case 2: |
||||||
|
// Block 3 is empty but was mined by account #2.
|
||||||
|
block.SetCoinbase(acc2Addr) |
||||||
|
block.SetExtra([]byte("yeehaw")) |
||||||
|
case 3: |
||||||
|
// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
|
||||||
|
b2 := block.PrevBlock(1).Header() |
||||||
|
b2.Extra = []byte("foo") |
||||||
|
block.AddUncle(b2) |
||||||
|
b3 := block.PrevBlock(2).Header() |
||||||
|
b3.Extra = []byte("foo") |
||||||
|
block.AddUncle(b3) |
||||||
|
} |
||||||
|
} |
||||||
|
// Assemble the test environment
|
||||||
|
pm := newTestProtocolManager(4, generator, nil) |
||||||
|
peer, _ := newTestPeer("peer", protocol, pm, true) |
||||||
|
defer peer.close() |
||||||
|
|
||||||
|
// Collect the hashes to request, and the response to expect
|
||||||
|
hashes := []common.Hash{} |
||||||
|
for i := uint64(0); i <= pm.chainman.CurrentBlock().NumberU64(); i++ { |
||||||
|
for _, tx := range pm.chainman.GetBlockByNumber(i).Transactions() { |
||||||
|
hashes = append(hashes, tx.Hash()) |
||||||
|
} |
||||||
|
} |
||||||
|
receipts := make([]*types.Receipt, len(hashes)) |
||||||
|
for i, hash := range hashes { |
||||||
|
receipts[i] = core.GetReceipt(pm.chaindb, hash) |
||||||
|
} |
||||||
|
// Send the hash request and verify the response
|
||||||
|
p2p.Send(peer.app, 0x0f, hashes) |
||||||
|
if err := p2p.ExpectMsg(peer.app, 0x10, receipts); err != nil { |
||||||
|
t.Errorf("receipts mismatch: %v", err) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,147 @@ |
|||||||
|
// This file contains some shares testing functionality, common to multiple
|
||||||
|
// different files and modules being tested.
|
||||||
|
|
||||||
|
package eth |
||||||
|
|
||||||
|
import ( |
||||||
|
"crypto/rand" |
||||||
|
"math/big" |
||||||
|
"sync" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common" |
||||||
|
"github.com/ethereum/go-ethereum/core" |
||||||
|
"github.com/ethereum/go-ethereum/core/types" |
||||||
|
"github.com/ethereum/go-ethereum/crypto" |
||||||
|
"github.com/ethereum/go-ethereum/ethdb" |
||||||
|
"github.com/ethereum/go-ethereum/event" |
||||||
|
"github.com/ethereum/go-ethereum/p2p" |
||||||
|
"github.com/ethereum/go-ethereum/p2p/discover" |
||||||
|
) |
||||||
|
|
||||||
|
var ( |
||||||
|
testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
||||||
|
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) |
||||||
|
testBankFunds = big.NewInt(1000000) |
||||||
|
) |
||||||
|
|
||||||
|
// newTestProtocolManager creates a new protocol manager for testing purposes,
|
||||||
|
// with the given number of blocks already known, and potential notification
|
||||||
|
// channels for different events.
|
||||||
|
func newTestProtocolManager(blocks int, generator func(int, *core.BlockGen), newtx chan<- []*types.Transaction) *ProtocolManager { |
||||||
|
var ( |
||||||
|
evmux = new(event.TypeMux) |
||||||
|
pow = new(core.FakePow) |
||||||
|
db, _ = ethdb.NewMemDatabase() |
||||||
|
genesis = core.WriteGenesisBlockForTesting(db, testBankAddress, testBankFunds) |
||||||
|
chainman, _ = core.NewChainManager(db, pow, evmux) |
||||||
|
blockproc = core.NewBlockProcessor(db, pow, chainman, evmux) |
||||||
|
) |
||||||
|
chainman.SetProcessor(blockproc) |
||||||
|
if _, err := chainman.InsertChain(core.GenerateChain(genesis, db, blocks, generator)); err != nil { |
||||||
|
panic(err) |
||||||
|
} |
||||||
|
pm := NewProtocolManager(NetworkId, evmux, &testTxPool{added: newtx}, pow, chainman, db) |
||||||
|
pm.Start() |
||||||
|
return pm |
||||||
|
} |
||||||
|
|
||||||
|
// testTxPool is a fake, helper transaction pool for testing purposes
|
||||||
|
type testTxPool struct { |
||||||
|
pool []*types.Transaction // Collection of all transactions
|
||||||
|
added chan<- []*types.Transaction // Notification channel for new transactions
|
||||||
|
|
||||||
|
lock sync.RWMutex // Protects the transaction pool
|
||||||
|
} |
||||||
|
|
||||||
|
// AddTransactions appends a batch of transactions to the pool, and notifies any
|
||||||
|
// listeners if the addition channel is non nil
|
||||||
|
func (p *testTxPool) AddTransactions(txs []*types.Transaction) { |
||||||
|
p.lock.Lock() |
||||||
|
defer p.lock.Unlock() |
||||||
|
|
||||||
|
p.pool = append(p.pool, txs...) |
||||||
|
if p.added != nil { |
||||||
|
p.added <- txs |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// GetTransactions returns all the transactions known to the pool
|
||||||
|
func (p *testTxPool) GetTransactions() types.Transactions { |
||||||
|
p.lock.RLock() |
||||||
|
defer p.lock.RUnlock() |
||||||
|
|
||||||
|
txs := make([]*types.Transaction, len(p.pool)) |
||||||
|
copy(txs, p.pool) |
||||||
|
|
||||||
|
return txs |
||||||
|
} |
||||||
|
|
||||||
|
// newTestTransaction create a new dummy transaction.
|
||||||
|
func newTestTransaction(from *crypto.Key, nonce uint64, datasize int) *types.Transaction { |
||||||
|
tx := types.NewTransaction(nonce, common.Address{}, big.NewInt(0), big.NewInt(100000), big.NewInt(0), make([]byte, datasize)) |
||||||
|
tx, _ = tx.SignECDSA(from.PrivateKey) |
||||||
|
|
||||||
|
return tx |
||||||
|
} |
||||||
|
|
||||||
|
// testPeer is a simulated peer to allow testing direct network calls.
|
||||||
|
type testPeer struct { |
||||||
|
net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging
|
||||||
|
app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side
|
||||||
|
*peer |
||||||
|
} |
||||||
|
|
||||||
|
// newTestPeer creates a new peer registered at the given protocol manager.
|
||||||
|
func newTestPeer(name string, version int, pm *ProtocolManager, shake bool) (*testPeer, <-chan error) { |
||||||
|
// Create a message pipe to communicate through
|
||||||
|
app, net := p2p.MsgPipe() |
||||||
|
|
||||||
|
// Generate a random id and create the peer
|
||||||
|
var id discover.NodeID |
||||||
|
rand.Read(id[:]) |
||||||
|
|
||||||
|
peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net) |
||||||
|
|
||||||
|
// Start the peer on a new thread
|
||||||
|
errc := make(chan error, 1) |
||||||
|
go func() { |
||||||
|
pm.newPeerCh <- peer |
||||||
|
errc <- pm.handle(peer) |
||||||
|
}() |
||||||
|
tp := &testPeer{ |
||||||
|
app: app, |
||||||
|
net: net, |
||||||
|
peer: peer, |
||||||
|
} |
||||||
|
// Execute any implicitly requested handshakes and return
|
||||||
|
if shake { |
||||||
|
td, head, genesis := pm.chainman.Status() |
||||||
|
tp.handshake(nil, td, head, genesis) |
||||||
|
} |
||||||
|
return tp, errc |
||||||
|
} |
||||||
|
|
||||||
|
// handshake simulates a trivial handshake that expects the same state from the
|
||||||
|
// remote side as we are simulating locally.
|
||||||
|
func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, genesis common.Hash) { |
||||||
|
msg := &statusData{ |
||||||
|
ProtocolVersion: uint32(p.version), |
||||||
|
NetworkId: uint32(NetworkId), |
||||||
|
TD: td, |
||||||
|
CurrentBlock: head, |
||||||
|
GenesisBlock: genesis, |
||||||
|
} |
||||||
|
if err := p2p.ExpectMsg(p.app, StatusMsg, msg); err != nil { |
||||||
|
t.Fatalf("status recv: %v", err) |
||||||
|
} |
||||||
|
if err := p2p.Send(p.app, StatusMsg, msg); err != nil { |
||||||
|
t.Fatalf("status send: %v", err) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// close terminates the local side of the peer, notifying the remote protocol
|
||||||
|
// manager of termination.
|
||||||
|
func (p *testPeer) close() { |
||||||
|
p.app.Close() |
||||||
|
} |
Loading…
Reference in new issue