ipld-eth-server/pkg/repositories/in_memory.go

76 lines
1.9 KiB
Go
Raw Normal View History

2017-11-06 16:42:33 +00:00
package repositories
import (
2017-11-06 18:53:43 +00:00
"github.com/8thlight/vulcanizedb/pkg/core"
2017-11-06 16:42:33 +00:00
)
type InMemory struct {
2017-12-04 22:54:35 +00:00
blocks map[int64]*core.Block
contracts map[string]*core.Contract
}
2017-12-04 22:54:35 +00:00
func (repository *InMemory) CreateContract(contract core.Contract) error {
repository.contracts[contract.Hash] = &contract
return nil
}
2017-12-04 22:54:35 +00:00
func (repository *InMemory) ContractExists(contractHash string) bool {
_, present := repository.contracts[contractHash]
return present
2017-11-06 16:42:33 +00:00
}
2017-12-04 22:54:35 +00:00
func (repository *InMemory) FindContract(contractHash string) *core.Contract {
contract, ok := repository.contracts[contractHash]
if !ok {
return nil
}
for _, block := range repository.blocks {
for _, transaction := range block.Transactions {
if transaction.To == contractHash {
2017-12-04 22:54:35 +00:00
contract.Transactions = append(contract.Transactions, transaction)
}
}
}
2017-12-04 22:54:35 +00:00
return contract
}
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
}
2017-11-06 16:42:33 +00:00
func NewInMemory() *InMemory {
return &InMemory{
2017-12-04 22:54:35 +00:00
blocks: make(map[int64]*core.Block),
contracts: make(map[string]*core.Contract),
2017-11-06 16:42:33 +00:00
}
}
func (repository *InMemory) CreateBlock(block core.Block) error {
2017-11-06 16:42:33 +00:00
repository.blocks[block.Number] = &block
return nil
2017-11-06 16:42:33 +00:00
}
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
}