• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisioneed308370dd59342dd354707cf5bd213c9febc3b (tree)
Time2019-06-18 10:44:33
AuthorChengcheng Zhang <943420582@qq.c...>
CommiterChengcheng Zhang

Log Message

fix conflicts

Change Summary

Incremental Difference

--- a/database/cache.go
+++ b/database/cache.go
@@ -1,7 +1,6 @@
11 package database
22
33 import (
4- "fmt"
54 "strconv"
65
76 "github.com/golang/groupcache/singleflight"
@@ -13,17 +12,17 @@ import (
1312 )
1413
1514 const (
16- maxCachedBlockHeaders = 1000
17- maxCachedBlockTransactions = 1000
18- maxCachedVoteResults = 144 // int(60 * 60 * 24 * 1000 / consensus.BlockTimeInterval / consensus.RoundVoteBlockNums)
15+ maxCachedBlockHeaders = 4096
16+ maxCachedBlockTransactions = 1024
17+ maxCachedVoteResults = 128
1918 )
2019
2120 type fillBlockHeaderFn func(hash *bc.Hash, height uint64) (*types.BlockHeader, error)
2221 type fillBlockTransactionsFn func(hash *bc.Hash) ([]*types.Tx, error)
2322 type fillVoteResultFn func(seq uint64) (*state.VoteResult, error)
2423
25-func newBlockCache(fillBlockHeader fillBlockHeaderFn, fillBlockTxs fillBlockTransactionsFn, fillVoteResult fillVoteResultFn) blockCache {
26- return blockCache{
24+func newCache(fillBlockHeader fillBlockHeaderFn, fillBlockTxs fillBlockTransactionsFn, fillVoteResult fillVoteResultFn) cache {
25+ return cache{
2726 lruBlockHeaders: common.NewCache(maxCachedBlockHeaders),
2827 lruBlockTxs: common.NewCache(maxCachedBlockTransactions),
2928 lruVoteResults: common.NewCache(maxCachedVoteResults),
@@ -34,7 +33,7 @@ func newBlockCache(fillBlockHeader fillBlockHeaderFn, fillBlockTxs fillBlockTran
3433 }
3534 }
3635
37-type blockCache struct {
36+type cache struct {
3837 lruBlockHeaders *common.Cache
3938 lruBlockTxs *common.Cache
4039 lruVoteResults *common.Cache
@@ -43,28 +42,22 @@ type blockCache struct {
4342 fillBlockTransactionFn func(hash *bc.Hash) ([]*types.Tx, error)
4443 fillVoteResultFn func(seq uint64) (*state.VoteResult, error)
4544
46- singleBlockHeader singleflight.Group
47- singleBlockTxs singleflight.Group
48- singleVoteResult singleflight.Group
45+ sf singleflight.Group
4946 }
5047
51-func (c *blockCache) lookupBlockHeader(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
52- if bH, ok := c.getBlockHeader(hash); ok {
53- return bH, nil
48+func (c *cache) lookupBlockHeader(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
49+ if data, ok := c.lruBlockHeaders.Get(*hash); ok {
50+ return data.(*types.BlockHeader), nil
5451 }
5552
56- blockHeader, err := c.singleBlockHeader.Do(hash.String(), func() (interface{}, error) {
57- bH, err := c.fillBlockHeaderFn(hash, height)
53+ blockHeader, err := c.sf.Do("BlockHeader:"+hash.String(), func() (interface{}, error) {
54+ blockHeader, err := c.fillBlockHeaderFn(hash, height)
5855 if err != nil {
5956 return nil, err
6057 }
6158
62- if bH == nil {
63- return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
64- }
65-
66- c.addBlockHeader(bH)
67- return bH, nil
59+ c.lruBlockHeaders.Add(blockHeader.Hash(), blockHeader)
60+ return blockHeader, nil
6861 })
6962 if err != nil {
7063 return nil, err
@@ -72,87 +65,51 @@ func (c *blockCache) lookupBlockHeader(hash *bc.Hash, height uint64) (*types.Blo
7265 return blockHeader.(*types.BlockHeader), nil
7366 }
7467
75-func (c *blockCache) lookupBlockTxs(hash *bc.Hash) ([]*types.Tx, error) {
76- if bTxs, ok := c.getBlockTransactions(hash); ok {
77- return bTxs, nil
68+func (c *cache) lookupBlockTxs(hash *bc.Hash) ([]*types.Tx, error) {
69+ if data, ok := c.lruBlockTxs.Get(*hash); ok {
70+ return data.([]*types.Tx), nil
7871 }
7972
80- blockTransactions, err := c.singleBlockTxs.Do(hash.String(), func() (interface{}, error) {
81- bTxs, err := c.fillBlockTransactionFn(hash)
73+ blockTxs, err := c.sf.Do("BlockTxs:"+hash.String(), func() (interface{}, error) {
74+ blockTxs, err := c.fillBlockTransactionFn(hash)
8275 if err != nil {
8376 return nil, err
8477 }
8578
86- if bTxs == nil {
87- return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
88- }
89-
90- c.addBlockTxs(*hash, bTxs)
91- return bTxs, nil
79+ c.lruBlockTxs.Add(hash, blockTxs)
80+ return blockTxs, nil
9281 })
9382 if err != nil {
9483 return nil, err
9584 }
96- return blockTransactions.([]*types.Tx), nil
85+ return blockTxs.([]*types.Tx), nil
9786 }
9887
99-func (c *blockCache) lookupVoteResult(seq uint64) (*state.VoteResult, error) {
100- if vr, ok := c.getVoteResult(seq); ok {
101- return vr, nil
88+func (c *cache) lookupVoteResult(seq uint64) (*state.VoteResult, error) {
89+ if data, ok := c.lruVoteResults.Get(seq); ok {
90+ return data.(*state.VoteResult).Fork(), nil
10291 }
10392
10493 seqStr := strconv.FormatUint(seq, 10)
105- voteResult, err := c.singleVoteResult.Do(seqStr, func() (interface{}, error) {
106- v, err := c.fillVoteResultFn(seq)
94+ voteResult, err := c.sf.Do("VoteResult:"+seqStr, func() (interface{}, error) {
95+ voteResult, err := c.fillVoteResultFn(seq)
10796 if err != nil {
10897 return nil, err
10998 }
11099
111- if v == nil {
112- return nil, fmt.Errorf("There are no vote result with given seq %s", seqStr)
113- }
114-
115- c.addVoteResult(v)
116- return v, nil
100+ c.lruVoteResults.Add(voteResult.Seq, voteResult)
101+ return voteResult, nil
117102 })
118103 if err != nil {
119104 return nil, err
120105 }
121- return voteResult.(*state.VoteResult), nil
122-}
123-
124-func (c *blockCache) getBlockHeader(hash *bc.Hash) (*types.BlockHeader, bool) {
125- blockHeader, ok := c.lruBlockHeaders.Get(*hash)
126- if blockHeader == nil {
127- return nil, ok
128- }
129- return blockHeader.(*types.BlockHeader), ok
130-}
131-
132-func (c *blockCache) getBlockTransactions(hash *bc.Hash) ([]*types.Tx, bool) {
133- txs, ok := c.lruBlockTxs.Get(*hash)
134- if txs == nil {
135- return nil, ok
136- }
137- return txs.([]*types.Tx), ok
138-}
139-
140-func (c *blockCache) getVoteResult(seq uint64) (*state.VoteResult, bool) {
141- voteResult, ok := c.lruVoteResults.Get(seq)
142- if voteResult == nil {
143- return nil, ok
144- }
145- return voteResult.(*state.VoteResult), ok
146-}
147-
148-func (c *blockCache) addBlockHeader(blockHeader *types.BlockHeader) {
149- c.lruBlockHeaders.Add(blockHeader.Hash(), blockHeader)
106+ return voteResult.(*state.VoteResult).Fork(), nil
150107 }
151108
152-func (c *blockCache) addBlockTxs(hash bc.Hash, txs []*types.Tx) {
153- c.lruBlockTxs.Add(hash, txs)
109+func (c *cache) removeBlockHeader(blockHeader *types.BlockHeader) {
110+ c.lruBlockHeaders.Remove(blockHeader.Hash())
154111 }
155112
156-func (c *blockCache) addVoteResult(voteResult *state.VoteResult) {
157- c.lruVoteResults.Add(voteResult.Seq, voteResult)
113+func (c *cache) removeVoteResult(voteResult *state.VoteResult) {
114+ c.lruVoteResults.Remove(voteResult.Seq)
158115 }
--- a/database/cache_test.go
+++ b/database/cache_test.go
@@ -44,7 +44,7 @@ func TestBlockCache(t *testing.T) {
4444 return voteResults[seq], nil
4545 }
4646
47- cache := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
47+ cache := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
4848
4949 for i := 0; i < maxCachedBlockHeaders+10; i++ {
5050 block := newBlock(uint64(i))
@@ -55,7 +55,7 @@ func TestBlockCache(t *testing.T) {
5555 for i := 0; i < 10; i++ {
5656 block := newBlock(uint64(i))
5757 hash := block.Hash()
58- if b, _ := cache.getBlockHeader(&hash); b != nil {
58+ if _, ok := cache.lruBlockHeaders.Get(hash); ok {
5959 t.Fatalf("find old block")
6060 }
6161 }
@@ -63,7 +63,7 @@ func TestBlockCache(t *testing.T) {
6363 for i := 10; i < maxCachedBlockHeaders+10; i++ {
6464 block := newBlock(uint64(i))
6565 hash := block.Hash()
66- if b, _ := cache.getBlockHeader(&hash); b == nil {
66+ if _, ok := cache.lruBlockHeaders.Get(hash); !ok {
6767 t.Fatalf("can't find new block")
6868 }
6969 }
@@ -75,14 +75,14 @@ func TestBlockCache(t *testing.T) {
7575
7676 for i := 0; i < 10; i++ {
7777 voteResult := newVoteResult(uint64(i))
78- if v, _ := cache.getVoteResult(voteResult.Seq); v != nil {
78+ if _, ok := cache.lruVoteResults.Get(voteResult.Seq); ok {
7979 t.Fatalf("find old vote result")
8080 }
8181 }
8282
8383 for i := 10; i < maxCachedVoteResults+10; i++ {
8484 voteResult := newVoteResult(uint64(i))
85- if v, _ := cache.getVoteResult(voteResult.Seq); v == nil {
85+ if _, ok := cache.lruVoteResults.Get(voteResult.Seq); !ok {
8686 t.Fatalf("can't find new vote result")
8787 }
8888 }
--- a/database/store.go
+++ b/database/store.go
@@ -3,11 +3,11 @@ package database
33 import (
44 "encoding/binary"
55 "encoding/json"
6+ "fmt"
67 "time"
78
89 "github.com/golang/protobuf/proto"
910 log "github.com/sirupsen/logrus"
10- "github.com/tendermint/tmlibs/common"
1111
1212 dbm "github.com/vapor/database/leveldb"
1313 "github.com/vapor/database/storage"
@@ -33,9 +33,10 @@ func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState {
3333 if bytes == nil {
3434 return nil
3535 }
36+
3637 bsj := &protocol.BlockStoreState{}
3738 if err := json.Unmarshal(bytes, bsj); err != nil {
38- common.PanicCrisis(common.Fmt("Could not unmarshal bytes: %X", bytes))
39+ log.WithField("err", err).Panic("fail on unmarshal BlockStoreStateJSON")
3940 }
4041 return bsj
4142 }
@@ -45,7 +46,7 @@ func loadBlockStoreStateJSON(db dbm.DB) *protocol.BlockStoreState {
4546 // methods for querying current data.
4647 type Store struct {
4748 db dbm.DB
48- cache blockCache
49+ cache cache
4950 }
5051
5152 func calcBlockHeaderKey(height uint64, hash *bc.Hash) []byte {
@@ -71,26 +72,26 @@ func calcVoteResultKey(seq uint64) []byte {
7172
7273 // GetBlockHeader return the block header by given hash and height
7374 func GetBlockHeader(db dbm.DB, hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
74- block := &types.Block{}
7575 binaryBlockHeader := db.Get(calcBlockHeaderKey(height, hash))
7676 if binaryBlockHeader == nil {
77- return nil, nil
77+ return nil, fmt.Errorf("There are no blockHeader with given hash %s", hash.String())
7878 }
79+
80+ block := &types.Block{}
7981 if err := block.UnmarshalText(binaryBlockHeader); err != nil {
8082 return nil, err
8183 }
82-
8384 return &block.BlockHeader, nil
8485 }
8586
8687 // GetBlockTransactions return the block transactions by given hash
8788 func GetBlockTransactions(db dbm.DB, hash *bc.Hash) ([]*types.Tx, error) {
88- block := &types.Block{}
8989 binaryBlockTxs := db.Get(calcBlockTransactionsKey(hash))
9090 if binaryBlockTxs == nil {
91- return nil, errors.New("The transactions in the block is empty")
91+ return nil, fmt.Errorf("There are no block transactions with given hash %s", hash.String())
9292 }
9393
94+ block := &types.Block{}
9495 if err := block.UnmarshalText(binaryBlockTxs); err != nil {
9596 return nil, err
9697 }
@@ -122,22 +123,17 @@ func NewStore(db dbm.DB) *Store {
122123 fillVoteResultFn := func(seq uint64) (*state.VoteResult, error) {
123124 return GetVoteResult(db, seq)
124125 }
125- bc := newBlockCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
126+ bc := newCache(fillBlockHeaderFn, fillBlockTxsFn, fillVoteResultFn)
126127 return &Store{
127128 db: db,
128129 cache: bc,
129130 }
130131 }
131132
132-// GetUtxo will search the utxo in db
133-func (s *Store) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
134- return getUtxo(s.db, hash)
135-}
136-
137133 // BlockExist check if the block is stored in disk
138134 func (s *Store) BlockExist(hash *bc.Hash, height uint64) bool {
139- blockHeader, err := s.cache.lookupBlockHeader(hash, height)
140- return err == nil && blockHeader != nil
135+ _, err := s.cache.lookupBlockHeader(hash, height)
136+ return err == nil
141137 }
142138
143139 // GetBlock return the block by given hash
@@ -160,20 +156,17 @@ func (s *Store) GetBlock(hash *bc.Hash, height uint64) (*types.Block, error) {
160156
161157 // GetBlockHeader return the BlockHeader by given hash
162158 func (s *Store) GetBlockHeader(hash *bc.Hash, height uint64) (*types.BlockHeader, error) {
163- blockHeader, err := s.cache.lookupBlockHeader(hash, height)
164- if err != nil {
165- return nil, err
166- }
167- return blockHeader, nil
159+ return s.cache.lookupBlockHeader(hash, height)
168160 }
169161
170162 // GetBlockTransactions return the Block transactions by given hash
171163 func (s *Store) GetBlockTransactions(hash *bc.Hash) ([]*types.Tx, error) {
172- txs, err := s.cache.lookupBlockTxs(hash)
173- if err != nil {
174- return nil, err
175- }
176- return txs, nil
164+ return s.cache.lookupBlockTxs(hash)
165+}
166+
167+// GetStoreStatus return the BlockStoreStateJSON
168+func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
169+ return loadBlockStoreStateJSON(s.db)
177170 }
178171
179172 // GetTransactionsUtxo will return all the utxo that related to the input txs
@@ -195,9 +188,9 @@ func (s *Store) GetTransactionStatus(hash *bc.Hash) (*bc.TransactionStatus, erro
195188 return ts, nil
196189 }
197190
198-// GetStoreStatus return the BlockStoreStateJSON
199-func (s *Store) GetStoreStatus() *protocol.BlockStoreState {
200- return loadBlockStoreStateJSON(s.db)
191+// GetUtxo will search the utxo in db
192+func (s *Store) GetUtxo(hash *bc.Hash) (*storage.UtxoEntry, error) {
193+ return getUtxo(s.db, hash)
201194 }
202195
203196 // GetVoteResult retrive the voting result in specified vote sequence
@@ -251,7 +244,6 @@ func (s *Store) LoadBlockIndex(stateBestHeight uint64) (*state.BlockIndex, error
251244 // SaveBlock persists a new block in the protocol.
252245 func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
253246 startTime := time.Now()
254-
255247 binaryBlockHeader, err := block.MarshalTextForBlockHeader()
256248 if err != nil {
257249 return errors.Wrap(err, "Marshal block header")
@@ -285,8 +277,6 @@ func (s *Store) SaveBlock(block *types.Block, ts *bc.TransactionStatus) error {
285277
286278 // SaveBlockHeader persists a new block header in the protocol.
287279 func (s *Store) SaveBlockHeader(blockHeader *types.BlockHeader) error {
288- startTime := time.Now()
289-
290280 binaryBlockHeader, err := blockHeader.MarshalText()
291281 if err != nil {
292282 return errors.Wrap(err, "Marshal block header")
@@ -294,18 +284,7 @@ func (s *Store) SaveBlockHeader(blockHeader *types.BlockHeader) error {
294284
295285 blockHash := blockHeader.Hash()
296286 s.db.Set(calcBlockHeaderKey(blockHeader.Height, &blockHash), binaryBlockHeader)
297-
298- // updata blockheader cache
299- if _, ok := s.cache.getBlockHeader(&blockHash); ok {
300- s.cache.addBlockHeader(blockHeader)
301- }
302-
303- log.WithFields(log.Fields{
304- "module": logModule,
305- "height": blockHeader.Height,
306- "hash": blockHash.String(),
307- "duration": time.Since(startTime),
308- }).Info("blockHeader saved on disk")
287+ s.cache.removeBlockHeader(blockHeader)
309288 return nil
310289 }
311290
@@ -323,9 +302,7 @@ func (s *Store) SaveChainStatus(node, irreversibleNode *state.BlockNode, view *s
323302 }
324303
325304 batch.Set(calcVoteResultKey(vote.Seq), bytes)
326- if _, ok := s.cache.getVoteResult(vote.Seq); ok {
327- s.cache.addVoteResult(vote)
328- }
305+ s.cache.removeVoteResult(vote)
329306 }
330307
331308 bytes, err := json.Marshal(protocol.BlockStoreState{
--- a/wallet/utxo.go
+++ b/wallet/utxo.go
@@ -213,7 +213,6 @@ func txInToUtxos(tx *types.Tx, statusFail bool) []*account.UTXO {
213213 Vote: resOut.Vote,
214214 }
215215 default:
216- log.WithFields(log.Fields{"module": logModule, "err": errors.Wrapf(bc.ErrEntryType, "entry %x has unexpected type %T", inpID.Bytes(), e)}).Error("txInToUtxos fail on get resOut")
217216 continue
218217 }
219218 utxos = append(utxos, utxo)
Show on old repository browser