ipld-eth-server/pkg/repositories/in_memory.go
Eric Meyer 4c84173bc0 Add ability to populate missing blocks
* The command populates up to the highest known block number
 * The anticipated use case is that the listener will be running
   in parallel to the populateBlocks command
    * This will mean that the listener is responsible for picking up
      new blocks, and the populateBlocks command is reposible for
      historical blocks
 * Reformat SQL statements
2017-11-08 14:52:38 -06:00

48 lines
1.1 KiB
Go

package repositories
import (
"github.com/8thlight/vulcanizedb/pkg/core"
)
type InMemory struct {
blocks map[int64]*core.Block
}
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
missingNumbers := []int64{}
for blockNumber := int64(startingBlockNumber); blockNumber <= endingBlockNumber; blockNumber++ {
if repository.blocks[blockNumber] == nil {
missingNumbers = append(missingNumbers, blockNumber)
}
}
return missingNumbers
}
func NewInMemory() *InMemory {
return &InMemory{
blocks: make(map[int64]*core.Block),
}
}
func (repository *InMemory) CreateBlock(block core.Block) {
repository.blocks[block.Number] = &block
}
func (repository *InMemory) BlockCount() int {
return len(repository.blocks)
}
func (repository *InMemory) FindBlockByNumber(blockNumber int64) *core.Block {
return repository.blocks[blockNumber]
}
func (repository *InMemory) MaxBlockNumber() int64 {
highestBlockNumber := int64(-1)
for key := range repository.blocks {
if key > highestBlockNumber {
highestBlockNumber = key
}
}
return highestBlockNumber
}