mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 23:31:35 +00:00
1360e1de68
* Fixed small incosistencies related to comments and variable name * For testing purposes we have to move the method newBlockchain from the cli/server/server.go to pkg/core/blockchain.go. In addition we have to rename it to NewBlockchainLevelDB in order to be able to export it and avoid naming conflicts. In future we still need to think how to switch between different blockchain implementation easily but for the time being this is not possible. * Added unit tests for the rpc server * Added unit_testnet chain fixture * fixed port number * Added errors handling * move unit_testnet chain from 'cli/chains' to 'pkg/rpc/chains'
73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package core
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
)
|
|
|
|
// Cache is data structure with fixed type key of Uint256, but has a
|
|
// generic value. Used for block, tx and header cache types.
|
|
type Cache struct {
|
|
lock sync.RWMutex
|
|
m map[util.Uint256]interface{}
|
|
}
|
|
|
|
// NewCache returns a ready to use Cache object.
|
|
func NewCache() *Cache {
|
|
return &Cache{
|
|
m: make(map[util.Uint256]interface{}),
|
|
}
|
|
}
|
|
|
|
// GetBlock will return a Block type from the cache.
|
|
func (c *Cache) GetBlock(h util.Uint256) (block *Block, ok bool) {
|
|
c.lock.RLock()
|
|
defer c.lock.RUnlock()
|
|
return c.getBlock(h)
|
|
}
|
|
|
|
func (c *Cache) getBlock(h util.Uint256) (block *Block, ok bool) {
|
|
if v, b := c.m[h]; b {
|
|
block, ok = v.(*Block)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Add adds the given hash along with its value to the cache.
|
|
func (c *Cache) Add(h util.Uint256, v interface{}) {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
c.add(h, v)
|
|
}
|
|
|
|
func (c *Cache) add(h util.Uint256, v interface{}) {
|
|
c.m[h] = v
|
|
}
|
|
|
|
func (c *Cache) has(h util.Uint256) bool {
|
|
_, ok := c.m[h]
|
|
return ok
|
|
}
|
|
|
|
// Has returns whether the cach contains the given hash.
|
|
func (c *Cache) Has(h util.Uint256) bool {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
return c.has(h)
|
|
}
|
|
|
|
// Len return the number of items present in the cache.
|
|
func (c *Cache) Len() int {
|
|
c.lock.RLock()
|
|
defer c.lock.RUnlock()
|
|
return len(c.m)
|
|
}
|
|
|
|
// Delete removes the item out of the cache.
|
|
func (c *Cache) Delete(h util.Uint256) {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
delete(c.m, h)
|
|
}
|