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-11-13 16:11:27 +00:00
|
|
|
blocks map[int64]*core.Block
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContracts map[string]*WatchedContract
|
2017-11-09 19:59:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:33:07 +00:00
|
|
|
func (repository *InMemory) CreateWatchedContract(watchedContract WatchedContract) error {
|
2017-11-13 16:11:27 +00:00
|
|
|
repository.watchedContracts[watchedContract.Hash] = &watchedContract
|
2017-11-09 19:59:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
2017-11-13 16:11:27 +00:00
|
|
|
_, present := repository.watchedContracts[contractHash]
|
2017-11-09 19:59:12 +00:00
|
|
|
return present
|
2017-11-06 16:42:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:33:07 +00:00
|
|
|
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
|
2017-12-04 20:13:15 +00:00
|
|
|
watchedContract, ok := repository.watchedContracts[contractHash]
|
|
|
|
if !ok {
|
2017-11-13 21:41:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, block := range repository.blocks {
|
|
|
|
for _, transaction := range block.Transactions {
|
|
|
|
if transaction.To == contractHash {
|
2017-12-04 20:13:15 +00:00
|
|
|
watchedContract.Transactions = append(watchedContract.Transactions, transaction)
|
2017-11-13 21:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-04 20:13:15 +00:00
|
|
|
return watchedContract
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:36:12 +00:00
|
|
|
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-11-13 16:11:27 +00:00
|
|
|
blocks: make(map[int64]*core.Block),
|
2017-12-04 19:33:07 +00:00
|
|
|
watchedContracts: make(map[string]*WatchedContract),
|
2017-11-06 16:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:56:49 +00:00
|
|
|
func (repository *InMemory) CreateBlock(block core.Block) error {
|
2017-11-06 16:42:33 +00:00
|
|
|
repository.blocks[block.Number] = &block
|
2017-11-07 17:56:49 +00:00
|
|
|
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]
|
|
|
|
}
|
2017-11-06 20:36:12 +00:00
|
|
|
|
|
|
|
func (repository *InMemory) MaxBlockNumber() int64 {
|
|
|
|
highestBlockNumber := int64(-1)
|
|
|
|
for key := range repository.blocks {
|
|
|
|
if key > highestBlockNumber {
|
|
|
|
highestBlockNumber = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return highestBlockNumber
|
|
|
|
}
|