• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版532549b70b01ac8e74ecc9b61b957728397ef8a7 (tree)
时间2020-02-26 11:23:35
作者ipqhjjybj <250657661@qq.c...>
Commiteripqhjjybj

Log Message

add all

更改概述

差异

--- a/protocol/consensus_node_manager_test.go
+++ b/protocol/consensus_node_manager_test.go
@@ -678,16 +678,20 @@ func mustDecodeXPub(xpub string) chainkd.XPub {
678678 }
679679
680680 type dummyStore struct {
681- blockHeaders map[string]*types.BlockHeader
682- blocks map[string]*types.Block
683- consensusResults map[uint64]*state.ConsensusResult
681+ blockHeaders map[string]*types.BlockHeader
682+ blocks map[string]*types.Block
683+ consensusResults map[uint64]*state.ConsensusResult
684+ mainChainHash map[uint64]*bc.Hash
685+ transactionStatus map[string]*bc.TransactionStatus
684686 }
685687
686688 func newDummyStore() *dummyStore {
687689 return &dummyStore{
688- blockHeaders: make(map[string]*types.BlockHeader),
689- consensusResults: make(map[uint64]*state.ConsensusResult),
690- blocks: make(map[string]*types.Block),
690+ blockHeaders: make(map[string]*types.BlockHeader),
691+ consensusResults: make(map[uint64]*state.ConsensusResult),
692+ blocks: make(map[string]*types.Block),
693+ mainChainHash: make(map[uint64]*bc.Hash),
694+ transactionStatus: make(map[string]*bc.TransactionStatus),
691695 }
692696 }
693697
@@ -707,8 +711,8 @@ func (s *dummyStore) GetStoreStatus() *BlockStoreState {
707711 return nil
708712 }
709713
710-func (s *dummyStore) GetTransactionStatus(*bc.Hash) (*bc.TransactionStatus, error) {
711- return nil, nil
714+func (s *dummyStore) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, error) {
715+ return s.transactionStatus[hash.String()], nil
712716 }
713717
714718 func (s *dummyStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error {
@@ -727,8 +731,8 @@ func (s *dummyStore) SetConsensusResult(consensusResult *state.ConsensusResult)
727731 s.consensusResults[consensusResult.Seq] = consensusResult
728732 }
729733
730-func (s *dummyStore) GetMainChainHash(uint64) (*bc.Hash, error) {
731- return nil, nil
734+func (s *dummyStore) GetMainChainHash(height uint64) (*bc.Hash, error) {
735+ return s.mainChainHash[height], nil
732736 }
733737
734738 func (s *dummyStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) {
@@ -736,11 +740,15 @@ func (s *dummyStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) {
736740 }
737741
738742 func (s *dummyStore) DeleteConsensusResult(seq uint64) error {
743+ delete(s.consensusResults, seq)
739744 return nil
740745 }
741746
742-func (s *dummyStore) SaveBlock(block *types.Block, _ *bc.TransactionStatus) error {
747+func (s *dummyStore) SaveBlock(block *types.Block, status *bc.TransactionStatus) error {
743748 hash := block.Hash()
749+ s.transactionStatus[hash.String()] = status
750+ s.mainChainHash[block.Height] = &hash
751+ s.blockHeaders[hash.String()] = &block.BlockHeader
744752 s.blocks[hash.String()] = block
745753 return nil
746754 }
--- a/protocol/rollback_mock_test.go
+++ b/protocol/rollback_mock_test.go
@@ -1,12 +1,10 @@
11 package protocol
22
33 import (
4- "fmt"
54 "sync"
65 "testing"
76
87 "github.com/bytom/vapor/consensus"
9- "github.com/bytom/vapor/database/storage"
108 "github.com/bytom/vapor/protocol/bc"
119 "github.com/bytom/vapor/protocol/bc/types"
1210 "github.com/bytom/vapor/protocol/state"
@@ -36,83 +34,17 @@ func (p *rProtocoler) ValidateTx(tx *types.Tx, verifyResult *bc.TxVerifyResult)
3634 func (p *rProtocoler) ApplyBlock(block *types.Block) error { return nil }
3735 func (p *rProtocoler) DetachBlock(block *types.Block) error { return nil }
3836
39-type rStore struct {
40- blockHeaders map[string]*types.BlockHeader
41- blocks map[string]*types.Block
42- consensusResults map[uint64]*state.ConsensusResult
43- mainChainHash map[uint64]*bc.Hash
44- transactionStatus map[string]*bc.TransactionStatus
45-}
46-
47-// newRStore create new AccountStore.
48-func newRStore() *rStore {
49- return &rStore{
50- blockHeaders: make(map[string]*types.BlockHeader),
51- blocks: make(map[string]*types.Block),
52- consensusResults: make(map[uint64]*state.ConsensusResult),
53- mainChainHash: make(map[uint64]*bc.Hash),
54- transactionStatus: make(map[string]*bc.TransactionStatus),
55- }
56-}
57-
58-func (s *rStore) BlockExist(hash *bc.Hash) bool { return false }
59-func (s *rStore) GetBlock(hash *bc.Hash) (*types.Block, error) {
60- return s.blocks[hash.String()], nil
61-}
62-func (s *rStore) GetBlockHeader(hash *bc.Hash) (*types.BlockHeader, error) {
63- return s.blockHeaders[hash.String()], nil
64-}
65-func (s *rStore) GetStoreStatus() *BlockStoreState { return nil }
66-func (s *rStore) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, error) {
67- return s.transactionStatus[hash.String()], nil
68-}
69-func (s *rStore) GetTransactionsUtxo(*state.UtxoViewpoint, []*bc.Tx) error { return nil }
70-func (s *rStore) GetUtxo(*bc.Hash) (*storage.UtxoEntry, error) { return nil, nil }
71-func (s *rStore) GetMainChainHash(height uint64) (*bc.Hash, error) {
72- return s.mainChainHash[height], nil
73-}
74-func (s *rStore) GetBlockHashesByHeight(uint64) ([]*bc.Hash, error) { return nil, nil }
75-func (s *rStore) DeleteConsensusResult(seq uint64) error {
76- delete(s.consensusResults, seq)
77- return nil
78-}
79-func (s *rStore) DeleteBlock(*types.Block) error { return nil }
80-
81-func (s *rStore) GetConsensusResult(seq uint64) (*state.ConsensusResult, error) {
82- return s.consensusResults[seq], nil
83-}
84-func (s *rStore) SetConsensusResult(consensusResult *state.ConsensusResult) {
85- s.consensusResults[consensusResult.Seq] = consensusResult
86-}
87-
88-func (s *rStore) SaveBlock(block *types.Block, status *bc.TransactionStatus) error {
89- hash := block.Hash()
90- fmt.Println("has save block hash:", hash.String(), block.Height)
91- s.transactionStatus[hash.String()] = status
92- s.mainChainHash[block.Height] = &hash
93- s.blockHeaders[hash.String()] = &block.BlockHeader
94- s.blocks[hash.String()] = block
95- return nil
96-}
97-func (s *rStore) SaveBlockHeader(header *types.BlockHeader) error {
98- hash := header.Hash()
99- fmt.Println("has save hash:", hash.String(), header.Height)
100- s.blockHeaders[hash.String()] = header
101- return nil
102-}
103-func (s *rStore) SaveChainStatus(*types.BlockHeader, *types.BlockHeader, []*types.BlockHeader, *state.UtxoViewpoint, []*state.ConsensusResult) error {
104- return nil
105-}
106-
10737 func TestRollbackMock(t *testing.T) {
108- fmt.Println("\n\n\nTestRollbackMock")
10938 cases := []struct {
11039 desc string
11140 bestBlockHeader *types.BlockHeader
11241 lastIrrBlockHeader *types.BlockHeader
11342 storedBlocks []*types.Block
43+ storedConsensusResult []*state.ConsensusResult
11444 beforeBestConsensusResult *state.ConsensusResult
115- afterBestConsensusResult *state.ConsensusResult
45+ expectBestBlockHeader *types.BlockHeader
46+ expectLastIrrBlockHeader *types.BlockHeader
47+ expectBestConsensusResult *state.ConsensusResult
11648 targetHeight uint64
11749 }{
11850 {
@@ -121,10 +53,16 @@ func TestRollbackMock(t *testing.T) {
12153 Height: 1,
12254 PreviousBlockHash: testutil.MustDecodeHash("39dee75363127a2857f554d2ad2706eb876407a2e09fbe0338683ca4ad4c2f90"),
12355 },
56+ expectBestBlockHeader: &types.BlockHeader{
57+ Height: 0,
58+ },
12459 lastIrrBlockHeader: &types.BlockHeader{
12560 Height: 1,
12661 PreviousBlockHash: testutil.MustDecodeHash("39dee75363127a2857f554d2ad2706eb876407a2e09fbe0338683ca4ad4c2f90"),
12762 },
63+ expectLastIrrBlockHeader: &types.BlockHeader{
64+ Height: 0,
65+ },
12866 targetHeight: 0,
12967
13068 storedBlocks: []*types.Block{
@@ -164,6 +102,28 @@ func TestRollbackMock(t *testing.T) {
164102 },
165103 },
166104 },
105+ storedConsensusResult: []*state.ConsensusResult{
106+ {
107+ Seq: 1,
108+ NumOfVote: map[string]uint64{
109+ "b7f463446a31b3792cd168d52b7a89b3657bca3e25d6854db1488c389ab6fc8d538155c25c1ee6975cc7def19710908c7d9b7463ca34a22058b456b45e498db9": 100002000,
110+ "36695997983028c279c3360ca345a90e3af1f9e3df2506119fca31cdc844be31630f9a421f4d1658e15d67a15ce29c36332dd45020d2a0147fcce4949ccd9a67": 100002000,
111+ },
112+ BlockHash: testutil.MustDecodeHash("52463075c66259098f2a1fa711288cf3b866d7c57b4a7a78cd22a1dcd69a0514"),
113+ BlockHeight: 1,
114+ CoinbaseReward: map[string]uint64{"0001": consensus.BlockSubsidy(1)},
115+ },
116+ {
117+ Seq: 0,
118+ NumOfVote: map[string]uint64{
119+ "b7f463446a31b3792cd168d52b7a89b3657bca3e25d6854db1488c389ab6fc8d538155c25c1ee6975cc7def19710908c7d9b7463ca34a22058b456b45e498db9": 100000000,
120+ "36695997983028c279c3360ca345a90e3af1f9e3df2506119fca31cdc844be31630f9a421f4d1658e15d67a15ce29c36332dd45020d2a0147fcce4949ccd9a67": 100002000,
121+ },
122+ BlockHash: testutil.MustDecodeHash("39dee75363127a2857f554d2ad2706eb876407a2e09fbe0338683ca4ad4c2f90"),
123+ BlockHeight: 0,
124+ CoinbaseReward: map[string]uint64{"0001": 0},
125+ },
126+ },
167127 beforeBestConsensusResult: &state.ConsensusResult{
168128 Seq: 1,
169129 NumOfVote: map[string]uint64{
@@ -174,7 +134,7 @@ func TestRollbackMock(t *testing.T) {
174134 BlockHeight: 1,
175135 CoinbaseReward: map[string]uint64{"0001": consensus.BlockSubsidy(1)},
176136 },
177- afterBestConsensusResult: &state.ConsensusResult{
137+ expectBestConsensusResult: &state.ConsensusResult{
178138 Seq: 0,
179139 NumOfVote: map[string]uint64{
180140 "b7f463446a31b3792cd168d52b7a89b3657bca3e25d6854db1488c389ab6fc8d538155c25c1ee6975cc7def19710908c7d9b7463ca34a22058b456b45e498db9": 100000000,
@@ -188,13 +148,9 @@ func TestRollbackMock(t *testing.T) {
188148 }
189149
190150 for _, c := range cases {
191- mockStore := newRStore()
151+ mockStore := newDummyStore()
192152 mockProtocoler := newRProtocoler()
193153
194- // for _, header := range c.storedBlockHeaders {
195- // mockStore.SaveBlockHeader(header)
196- // }
197-
198154 for _, block := range c.storedBlocks {
199155 newTrans := []*types.Tx{}
200156 status := bc.NewTransactionStatus()
@@ -204,12 +160,12 @@ func TestRollbackMock(t *testing.T) {
204160 newTrans = append(newTrans, tx)
205161 }
206162 block.Transactions = newTrans
207- fmt.Println("block", block)
208- fmt.Println("what start:", block.Transactions, block.Transactions[0])
209163 mockStore.SaveBlock(block, status)
210164 }
211165
212- mockStore.SetConsensusResult(c.beforeBestConsensusResult)
166+ for _, consensusResult := range c.storedConsensusResult {
167+ mockStore.SetConsensusResult(consensusResult)
168+ }
213169
214170 chain := &Chain{
215171 store: mockStore,
@@ -224,5 +180,21 @@ func TestRollbackMock(t *testing.T) {
224180 t.Fatal(err)
225181 }
226182
183+ if !testutil.DeepEqual(chain.lastIrrBlockHeader, c.expectLastIrrBlockHeader) {
184+ t.Fatalf("lastIrrBlockHeader is not right!")
185+ }
186+
187+ if !testutil.DeepEqual(chain.bestBlockHeader, c.expectBestBlockHeader) {
188+ t.Fatalf("expectBestBlockHeader is not right!")
189+ }
190+
191+ nowConsensusResult, err := chain.getBestConsensusResult()
192+ if err != nil {
193+ t.Fatal(err)
194+ }
195+
196+ if !testutil.DeepEqual(nowConsensusResult, c.expectBestConsensusResult) {
197+ t.Fatalf("expectBestConsensusResult is not right!")
198+ }
227199 }
228200 }
Show on old repository browser