neoneo-go/pkg/core/blockchain_storer.go
Anthony De Meulemeester 628656483a
bug fixes (TCP + uint256) and started core part (#14)
* Fixed TCP read + Uint256 reversed array + started on some core pieces

* Disabled some debug output + muted test

* 0.5.0
2018-02-04 20:54:51 +01:00

69 lines
1.4 KiB
Go

package core
import (
"log"
"sync"
"github.com/CityOfZion/neo-go/pkg/util"
)
// BlockchainStorer is anything that can persist and retrieve the blockchain.
type BlockchainStorer interface {
HasBlock(util.Uint256) bool
GetBlockByHeight(uint32) (*Block, error)
GetBlockByHash(util.Uint256) (*Block, error)
Put(*Header) error
}
// MemoryStore is an in memory implementation of a BlockChainStorer.
type MemoryStore struct {
mtx sync.RWMutex
blocks map[util.Uint256]*Header
}
// NewMemoryStore returns a pointer to a MemoryStore object.
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
blocks: map[util.Uint256]*Header{},
}
}
// HasBlock implements the BlockchainStorer interface.
func (s *MemoryStore) HasBlock(hash util.Uint256) bool {
s.mtx.RLock()
defer s.mtx.RUnlock()
_, ok := s.blocks[hash]
return ok
}
// GetBlockByHash returns a block by its hash.
func (s *MemoryStore) GetBlockByHash(hash util.Uint256) (*Block, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
return nil, nil
}
// GetBlockByHeight returns a block by its height.
func (s *MemoryStore) GetBlockByHeight(i uint32) (*Block, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
return nil, nil
}
// Put persist a BlockHead in memory
func (s *MemoryStore) Put(header *Header) error {
s.mtx.Lock()
defer s.mtx.Unlock()
hash, err := header.Hash()
if err != nil {
s.blocks[hash] = header
}
log.Printf("persisted block %s\n", hash)
return err
}