|
| 1 | +package XDPoS |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/XinFinOrg/XDPoSChain/common" |
| 8 | + "github.com/XinFinOrg/XDPoSChain/core/types" |
| 9 | + "github.com/XinFinOrg/XDPoSChain/params" |
| 10 | + "github.com/XinFinOrg/XDPoSChain/trie" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func makeTestBlock(number int64, time int64, txs ...*types.Transaction) *types.Block { |
| 15 | + header := &types.Header{ |
| 16 | + Number: big.NewInt(number), |
| 17 | + Time: big.NewInt(time), |
| 18 | + Difficulty: big.NewInt(1), |
| 19 | + GasLimit: 1_000_000, |
| 20 | + } |
| 21 | + return types.NewBlock(header, txs, nil, nil, trie.NewStackTrie(nil)) |
| 22 | +} |
| 23 | + |
| 24 | +type stubVerifyChainReader struct { |
| 25 | + config *params.ChainConfig |
| 26 | + currentHeader *types.Header |
| 27 | + headersByHash map[common.Hash]*types.Header |
| 28 | + headersByNumber map[uint64]*types.Header |
| 29 | + blocksByHashNo map[hashAndNumber]*types.Block |
| 30 | +} |
| 31 | + |
| 32 | +func (s *stubVerifyChainReader) Config() *params.ChainConfig { return s.config } |
| 33 | + |
| 34 | +func (s *stubVerifyChainReader) CurrentHeader() *types.Header { return s.currentHeader } |
| 35 | + |
| 36 | +func (s *stubVerifyChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { |
| 37 | + header := s.GetHeaderByHash(hash) |
| 38 | + if header == nil || header.Number == nil || header.Number.Uint64() != number { |
| 39 | + return nil |
| 40 | + } |
| 41 | + return header |
| 42 | +} |
| 43 | + |
| 44 | +func (s *stubVerifyChainReader) GetHeaderByNumber(number uint64) *types.Header { |
| 45 | + if s.headersByNumber == nil { |
| 46 | + return nil |
| 47 | + } |
| 48 | + return s.headersByNumber[number] |
| 49 | +} |
| 50 | + |
| 51 | +func (s *stubVerifyChainReader) GetHeaderByHash(hash common.Hash) *types.Header { |
| 52 | + if s.headersByHash == nil { |
| 53 | + return nil |
| 54 | + } |
| 55 | + return s.headersByHash[hash] |
| 56 | +} |
| 57 | + |
| 58 | +func (s *stubVerifyChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { |
| 59 | + if s.blocksByHashNo == nil { |
| 60 | + return nil |
| 61 | + } |
| 62 | + return s.blocksByHashNo[hashAndNumber{hash: hash, number: number}] |
| 63 | +} |
| 64 | + |
| 65 | +func TestVerifyChainReaderWithNilChainIsNilSafe(t *testing.T) { |
| 66 | + batchHeader := &types.Header{Number: big.NewInt(1)} |
| 67 | + reader := NewVerifyHeadersChainReader(nil, []*types.Header{batchHeader}, nil).(*verifyChainReader) |
| 68 | + |
| 69 | + assert.NotNil(t, reader) |
| 70 | + assert.Nil(t, reader.Config()) |
| 71 | + assert.Nil(t, reader.CurrentHeader()) |
| 72 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeaderByNumber(1).Hash()) |
| 73 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeaderByHash(batchHeader.Hash()).Hash()) |
| 74 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeader(batchHeader.Hash(), 1).Hash()) |
| 75 | + assert.Nil(t, reader.GetBlock(batchHeader.Hash(), 1)) |
| 76 | + assert.Nil(t, reader.GetHeader(common.Hash{}, 2)) |
| 77 | + assert.Nil(t, reader.GetBlock(common.Hash{}, 2)) |
| 78 | +} |
| 79 | + |
| 80 | +func TestVerifyChainReaderShadowsBatchEntries(t *testing.T) { |
| 81 | + baseHeader := &types.Header{Number: big.NewInt(100), Time: big.NewInt(1)} |
| 82 | + batchHeader := &types.Header{Number: big.NewInt(100), Time: big.NewInt(2)} |
| 83 | + batchTx := types.NewTransaction(1, common.Address{0x1}, big.NewInt(1), 21000, big.NewInt(1), []byte{0x1}) |
| 84 | + batchBlock := makeTestBlock(100, 2, batchTx) |
| 85 | + batchHeader = batchBlock.Header() |
| 86 | + currentHeader := &types.Header{Number: big.NewInt(99), Time: big.NewInt(3)} |
| 87 | + baseBlock := types.NewBlockWithHeader(baseHeader) |
| 88 | + |
| 89 | + base := &stubVerifyChainReader{ |
| 90 | + config: params.TestXDPoSMockChainConfig, |
| 91 | + currentHeader: currentHeader, |
| 92 | + headersByHash: map[common.Hash]*types.Header{baseHeader.Hash(): baseHeader}, |
| 93 | + headersByNumber: map[uint64]*types.Header{ |
| 94 | + 100: baseHeader, |
| 95 | + }, |
| 96 | + blocksByHashNo: map[hashAndNumber]*types.Block{ |
| 97 | + {hash: baseHeader.Hash(), number: 100}: baseBlock, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + reader := NewVerifyHeadersChainReader(base, []*types.Header{batchHeader}, []*types.Block{batchBlock}).(*verifyChainReader) |
| 102 | + |
| 103 | + assert.Equal(t, params.TestXDPoSMockChainConfig, reader.Config()) |
| 104 | + assert.Equal(t, currentHeader.Hash(), reader.CurrentHeader().Hash()) |
| 105 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeaderByNumber(100).Hash()) |
| 106 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeaderByHash(batchHeader.Hash()).Hash()) |
| 107 | + assert.Equal(t, batchHeader.Hash(), reader.GetHeader(batchHeader.Hash(), 100).Hash()) |
| 108 | + assert.Equal(t, batchHeader.Hash(), reader.GetBlock(batchHeader.Hash(), 100).Hash()) |
| 109 | + assert.Len(t, reader.GetBlock(batchHeader.Hash(), 100).Transactions(), 1) |
| 110 | + assert.Equal(t, baseHeader.Hash(), reader.GetBlock(baseHeader.Hash(), 100).Hash()) |
| 111 | +} |
| 112 | + |
| 113 | +func TestVerifyChainReaderReusesExistingWrapper(t *testing.T) { |
| 114 | + firstTx := types.NewTransaction(1, common.Address{0x1}, big.NewInt(1), 21000, big.NewInt(1), []byte{0x1}) |
| 115 | + secondTx := types.NewTransaction(2, common.Address{0x2}, big.NewInt(2), 21000, big.NewInt(1), []byte{0x2}) |
| 116 | + firstBlock := makeTestBlock(100, 1, firstTx) |
| 117 | + secondBlock := makeTestBlock(101, 2, secondTx) |
| 118 | + |
| 119 | + reader := NewVerifyHeadersChainReader(nil, []*types.Header{firstBlock.Header()}, []*types.Block{firstBlock}) |
| 120 | + reused := NewVerifyHeadersChainReader(reader, []*types.Header{secondBlock.Header()}, nil) |
| 121 | + |
| 122 | + assert.Same(t, reader, reused) |
| 123 | + wrapped := reused.(*verifyChainReader) |
| 124 | + assert.Len(t, wrapped.GetBlock(firstBlock.Hash(), firstBlock.NumberU64()).Transactions(), 1) |
| 125 | + assert.Nil(t, wrapped.GetHeaderByNumber(secondBlock.NumberU64())) |
| 126 | + assert.Nil(t, wrapped.GetBlock(secondBlock.Hash(), secondBlock.NumberU64())) |
| 127 | + |
| 128 | + reused = NewVerifyHeadersChainReader(reused, nil, []*types.Block{secondBlock}) |
| 129 | + wrapped = reused.(*verifyChainReader) |
| 130 | + assert.Nil(t, wrapped.GetBlock(secondBlock.Hash(), secondBlock.NumberU64())) |
| 131 | +} |
0 commit comments