@@ -32,6 +32,7 @@ import (
3232 "github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
3333 "github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
3434 "github.com/XinFinOrg/XDPoSChain/common"
35+ "github.com/XinFinOrg/XDPoSChain/common/lru"
3536 "github.com/XinFinOrg/XDPoSChain/common/mclock"
3637 "github.com/XinFinOrg/XDPoSChain/common/prque"
3738 "github.com/XinFinOrg/XDPoSChain/common/sort"
@@ -52,7 +53,6 @@ import (
5253 "github.com/XinFinOrg/XDPoSChain/params"
5354 "github.com/XinFinOrg/XDPoSChain/rlp"
5455 "github.com/XinFinOrg/XDPoSChain/trie"
55- lru "github.com/hashicorp/golang-lru"
5656)
5757
5858var (
@@ -140,37 +140,40 @@ type BlockChain struct {
140140
141141 stateCache state.Database // State database to reuse between imports (contains state cache)
142142
143- bodyCache * lru.Cache // Cache for the most recent block bodies
144- bodyRLPCache * lru.Cache // Cache for the most recent block bodies in RLP encoded format
145- blockCache * lru.Cache // Cache for the most recent entire blocks
146- futureBlocks * lru.Cache // future blocks are blocks added for later processing
147- resultProcess * lru.Cache // Cache for processed blocks
148- calculatingBlock * lru.Cache // Cache for processing blocks
149- downloadingBlock * lru.Cache // Cache for downloading blocks (avoid duplication from fetcher)
150- quit chan struct {} // blockchain quit channel
151- running int32 // running must be called atomically
152- // procInterrupt must be atomically called
153- procInterrupt int32 // interrupt signaler for block processing
143+ bodyCache * lru.Cache [common. Hash , * types. Body ] // Cache for the most recent block bodies
144+ bodyRLPCache * lru.Cache [common. Hash , rlp. RawValue ] // Cache for the most recent block bodies in RLP encoded format
145+ blockCache * lru.Cache [common. Hash , * types. Block ] // Cache for the most recent entire blocks
146+ resultProcess * lru.Cache [common. Hash , * ResultProcessBlock ] // Cache for processed blocks
147+ calculatingBlock * lru.Cache [common. Hash , * CalculatedBlock ] // Cache for processing blocks
148+ downloadingBlock * lru.Cache [common. Hash , struct {}] // Cache for downloading blocks (avoid duplication from fetcher)
149+ badBlocks * lru.Cache [common. Hash , * types. Header ] // Bad block cache
150+
151+ // future blocks are blocks added for later processing
152+ futureBlocks * lru. Cache [common. Hash , * types. Block ]
153+
154154 wg sync.WaitGroup // chain processing wait group for shutting down
155+ quit chan struct {} // shutdown signal, closed in Stop.
156+ running int32 // 0 if chain is running, 1 when stopped
157+ procInterrupt int32 // interrupt signaler for block processing
155158
156159 engine consensus.Engine
157160 processor Processor // block processor interface
158161 validator Validator // block and state validator interface
159162 vmConfig vm.Config
160163
161- badBlocks * lru.Cache // Bad block cache
162164 shouldPreserve func (* types.Block ) bool // Function used to determine whether should preserve the given block.
163165 IPCEndpoint string
164166 Client bind.ContractBackend // Global ipc client instance.
167+
165168 // Blocks hash array by block number
166169 // cache field for tracking finality purpose, can't use for tracking block vs block relationship
167- blocksHashCache * lru.Cache
170+ blocksHashCache * lru.Cache [ uint64 , []common. Hash ]
168171
169- resultTrade * lru.Cache // trades result: key - takerOrderHash, value: trades corresponding to takerOrder
170- rejectedOrders * lru.Cache // rejected orders: key - takerOrderHash, value: rejected orders corresponding to takerOrder
171- resultLendingTrade * lru.Cache
172- rejectedLendingItem * lru.Cache
173- finalizedTrade * lru.Cache // include both trades which force update to closed/liquidated by the protocol
172+ resultTrade * lru.Cache [common. Hash , interface {}] // trades result: key - takerOrderHash, value: trades corresponding to takerOrder
173+ rejectedOrders * lru.Cache [common. Hash , interface {}] // rejected orders: key - takerOrderHash, value: rejected orders corresponding to takerOrder
174+ resultLendingTrade * lru.Cache [common. Hash , interface {}]
175+ rejectedLendingItem * lru.Cache [common. Hash , interface {}]
176+ finalizedTrade * lru.Cache [common. Hash , interface {}] // include both trades which force update to closed/liquidated by the protocol
174177}
175178
176179// NewBlockChain returns a fully initialised block chain using information
@@ -183,47 +186,30 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
183186 TrieTimeLimit : 5 * time .Minute ,
184187 }
185188 }
186- bodyCache , _ := lru .New (bodyCacheLimit )
187- bodyRLPCache , _ := lru .New (bodyCacheLimit )
188- blockCache , _ := lru .New (blockCacheLimit )
189- blocksHashCache , _ := lru .New (blocksHashCacheLimit )
190- futureBlocks , _ := lru .New (maxFutureBlocks )
191- badBlocks , _ := lru .New (badBlockLimit )
192- resultProcess , _ := lru .New (blockCacheLimit )
193- preparingBlock , _ := lru .New (blockCacheLimit )
194- downloadingBlock , _ := lru .New (blockCacheLimit )
195-
196- // for XDCx
197- resultTrade , _ := lru .New (tradingstate .OrderCacheLimit )
198- rejectedOrders , _ := lru .New (tradingstate .OrderCacheLimit )
199-
200- // XDCxlending
201- resultLendingTrade , _ := lru .New (tradingstate .OrderCacheLimit )
202- rejectedLendingItem , _ := lru .New (tradingstate .OrderCacheLimit )
203- finalizedTrade , _ := lru .New (tradingstate .OrderCacheLimit )
189+
204190 bc := & BlockChain {
205191 chainConfig : chainConfig ,
206192 cacheConfig : cacheConfig ,
207193 db : db ,
208194 triegc : prque .New (nil ),
209195 stateCache : state .NewDatabase (db ),
210196 quit : make (chan struct {}),
211- bodyCache : bodyCache ,
212- bodyRLPCache : bodyRLPCache ,
213- blockCache : blockCache ,
214- futureBlocks : futureBlocks ,
215- resultProcess : resultProcess ,
216- calculatingBlock : preparingBlock ,
217- downloadingBlock : downloadingBlock ,
197+ bodyCache : lru. NewCache [common. Hash , * types. Body ]( bodyCacheLimit ) ,
198+ bodyRLPCache : lru. NewCache [common. Hash , rlp. RawValue ]( bodyCacheLimit ) ,
199+ blockCache : lru. NewCache [common. Hash , * types. Block ]( blockCacheLimit ) ,
200+ futureBlocks : lru. NewCache [common. Hash , * types. Block ]( maxFutureBlocks ) ,
201+ resultProcess : lru. NewCache [common. Hash , * ResultProcessBlock ]( blockCacheLimit ) ,
202+ calculatingBlock : lru. NewCache [common. Hash , * CalculatedBlock ]( blockCacheLimit ) ,
203+ downloadingBlock : lru. NewCache [common. Hash , struct {}]( blockCacheLimit ) ,
218204 engine : engine ,
219205 vmConfig : vmConfig ,
220- badBlocks : badBlocks ,
221- blocksHashCache : blocksHashCache ,
222- resultTrade : resultTrade ,
223- rejectedOrders : rejectedOrders ,
224- resultLendingTrade : resultLendingTrade ,
225- rejectedLendingItem : rejectedLendingItem ,
226- finalizedTrade : finalizedTrade ,
206+ badBlocks : lru. NewCache [common. Hash , * types. Header ]( badBlockLimit ) ,
207+ blocksHashCache : lru. NewCache [ uint64 , []common. Hash ]( blocksHashCacheLimit ) ,
208+ resultTrade : lru. NewCache [common. Hash , interface {}]( tradingstate . OrderCacheLimit ) ,
209+ rejectedOrders : lru. NewCache [common. Hash , interface {}]( tradingstate . OrderCacheLimit ) ,
210+ resultLendingTrade : lru. NewCache [common. Hash , interface {}]( tradingstate . OrderCacheLimit ) ,
211+ rejectedLendingItem : lru. NewCache [common. Hash , interface {}]( tradingstate . OrderCacheLimit ) ,
212+ finalizedTrade : lru. NewCache [common. Hash , interface {}]( tradingstate . OrderCacheLimit ) ,
227213 }
228214 bc .SetValidator (NewBlockValidator (chainConfig , bc , engine ))
229215 bc .SetProcessor (NewStateProcessor (chainConfig , bc , engine ))
@@ -720,8 +706,7 @@ func (bc *BlockChain) Genesis() *types.Block {
720706func (bc * BlockChain ) GetBody (hash common.Hash ) * types.Body {
721707 // Short circuit if the body's already in the cache, retrieve otherwise
722708 if cached , ok := bc .bodyCache .Get (hash ); ok {
723- body := cached .(* types.Body )
724- return body
709+ return cached
725710 }
726711 body := GetBody (bc .db , hash , bc .hc .GetBlockNumber (hash ))
727712 if body == nil {
@@ -737,7 +722,7 @@ func (bc *BlockChain) GetBody(hash common.Hash) *types.Body {
737722func (bc * BlockChain ) GetBodyRLP (hash common.Hash ) rlp.RawValue {
738723 // Short circuit if the body's already in the cache, retrieve otherwise
739724 if cached , ok := bc .bodyRLPCache .Get (hash ); ok {
740- return cached .(rlp. RawValue )
725+ return cached
741726 }
742727 body := GetBodyRLP (bc .db , hash , bc .hc .GetBlockNumber (hash ))
743728 if len (body ) == 0 {
@@ -794,7 +779,7 @@ func (bc *BlockChain) HasBlockAndFullState(hash common.Hash, number uint64) bool
794779func (bc * BlockChain ) GetBlock (hash common.Hash , number uint64 ) * types.Block {
795780 // Short circuit if the block's already in the cache, retrieve otherwise
796781 if block , ok := bc .blockCache .Get (hash ); ok {
797- return block .( * types. Block )
782+ return block
798783 }
799784 block := GetBlock (bc .db , hash , number )
800785 if block == nil {
@@ -847,7 +832,7 @@ func (bc *BlockChain) GetBlocksHashCache(number uint64) []common.Hash {
847832 cached , ok := bc .blocksHashCache .Get (number )
848833
849834 if ok {
850- return cached .([]common. Hash )
835+ return cached
851836 }
852837 return nil
853838}
@@ -980,7 +965,7 @@ func (bc *BlockChain) procFutureBlocks() {
980965 blocks := make ([]* types.Block , 0 , bc .futureBlocks .Len ())
981966 for _ , hash := range bc .futureBlocks .Keys () {
982967 if block , exist := bc .futureBlocks .Peek (hash ); exist {
983- blocks = append (blocks , block .( * types. Block ) )
968+ blocks = append (blocks , block )
984969 }
985970 }
986971 if len (blocks ) > 0 {
@@ -1491,7 +1476,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
14911476 for i , block := range chain {
14921477 headers [i ] = block .Header ()
14931478 seals [i ] = verifySeals
1494- bc .downloadingBlock .Add (block .Hash (), true )
1479+ bc .downloadingBlock .Add (block .Hash (), struct {}{} )
14951480 }
14961481 abort , results := bc .engine .VerifyHeaders (bc , headers , seals )
14971482 defer close (abort )
@@ -1805,11 +1790,11 @@ func (bc *BlockChain) getResultBlock(block *types.Block, verifiedM2 bool) (*Resu
18051790 if verifiedM2 {
18061791 if result , check := bc .resultProcess .Get (block .HashNoValidator ()); check {
18071792 log .Debug ("Get result block from cache " , "number" , block .NumberU64 (), "hash" , block .Hash (), "hash no validator" , block .HashNoValidator ())
1808- return result .( * ResultProcessBlock ) , nil
1793+ return result , nil
18091794 }
18101795 log .Debug ("Not found cache prepare block " , "number" , block .NumberU64 (), "hash" , block .Hash (), "validator" , block .HashNoValidator ())
18111796 if calculatedBlock , _ := bc .calculatingBlock .Get (block .HashNoValidator ()); calculatedBlock != nil {
1812- calculatedBlock .( * CalculatedBlock ). stop = true
1797+ calculatedBlock .stop = true
18131798 }
18141799 }
18151800 calculatedBlock = & CalculatedBlock {block , false }
@@ -2007,7 +1992,7 @@ func (bc *BlockChain) UpdateBlocksHashCache(block *types.Block) []common.Hash {
20071992 cached , ok := bc .blocksHashCache .Get (blockNumber )
20081993
20091994 if ok {
2010- hashArr := cached .([]common. Hash )
1995+ hashArr := cached
20111996 hashArr = append (hashArr , block .Hash ())
20121997 bc .blocksHashCache .Remove (blockNumber )
20131998 bc .blocksHashCache .Add (blockNumber , hashArr )
@@ -2340,8 +2325,7 @@ type BadBlockArgs struct {
23402325func (bc * BlockChain ) BadBlocks () ([]BadBlockArgs , error ) {
23412326 headers := make ([]BadBlockArgs , 0 , bc .badBlocks .Len ())
23422327 for _ , hash := range bc .badBlocks .Keys () {
2343- if hdr , exist := bc .badBlocks .Peek (hash ); exist {
2344- header := hdr .(* types.Header )
2328+ if header , exist := bc .badBlocks .Peek (hash ); exist {
23452329 headers = append (headers , BadBlockArgs {header .Hash (), header })
23462330 }
23472331 }
0 commit comments