2018-02-02 21:53:16 +00:00
|
|
|
package inmemory
|
2017-11-06 16:42:33 +00:00
|
|
|
|
|
|
|
import (
|
2017-12-12 21:55:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-01-23 18:43:35 +00:00
|
|
|
"errors"
|
|
|
|
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
2018-02-02 21:53:16 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/repositories"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
blocksFromHeadBeforeFinal = 20
|
2017-11-06 16:42:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InMemory struct {
|
2018-01-05 17:55:00 +00:00
|
|
|
blocks map[int64]core.Block
|
|
|
|
receipts map[string]core.Receipt
|
|
|
|
contracts map[string]core.Contract
|
|
|
|
logs map[string][]core.Log
|
2018-01-23 18:43:35 +00:00
|
|
|
logFilters map[string]filters.LogFilter
|
2018-01-05 17:55:00 +00:00
|
|
|
CreateOrUpdateBlockCallCount int
|
2017-12-12 21:55:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 18:43:35 +00:00
|
|
|
func (repository *InMemory) AddFilter(filter filters.LogFilter) error {
|
|
|
|
key := filter.Name
|
|
|
|
if _, ok := repository.logFilters[key]; ok || key == "" {
|
|
|
|
return errors.New("filter name not unique")
|
|
|
|
}
|
|
|
|
repository.logFilters[key] = filter
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 17:23:43 +00:00
|
|
|
func NewInMemory() *InMemory {
|
|
|
|
return &InMemory{
|
2018-01-05 17:55:00 +00:00
|
|
|
CreateOrUpdateBlockCallCount: 0,
|
2018-01-23 18:43:35 +00:00
|
|
|
blocks: make(map[int64]core.Block),
|
|
|
|
receipts: make(map[string]core.Receipt),
|
|
|
|
contracts: make(map[string]core.Contract),
|
|
|
|
logs: make(map[string][]core.Log),
|
|
|
|
logFilters: make(map[string]filters.LogFilter),
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository *InMemory) FindReceipt(txHash string) (core.Receipt, error) {
|
|
|
|
if receipt, ok := repository.receipts[txHash]; ok {
|
|
|
|
return receipt, nil
|
|
|
|
}
|
2018-02-02 21:53:16 +00:00
|
|
|
return core.Receipt{}, repositories.ErrReceiptDoesNotExist(txHash)
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:06:22 +00:00
|
|
|
func (repository *InMemory) SetBlocksStatus(chainHead int64) {
|
|
|
|
for key, block := range repository.blocks {
|
|
|
|
if key < (chainHead - blocksFromHeadBeforeFinal) {
|
|
|
|
tmp := block
|
|
|
|
tmp.IsFinal = true
|
|
|
|
repository.blocks[key] = tmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:55:26 +00:00
|
|
|
func (repository *InMemory) CreateLogs(logs []core.Log) error {
|
|
|
|
for _, log := range logs {
|
|
|
|
key := fmt.Sprintf("%s%s", log.BlockNumber, log.Index)
|
|
|
|
var logs []core.Log
|
|
|
|
repository.logs[key] = append(logs, log)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repository *InMemory) FindLogs(address string, blockNumber int64) []core.Log {
|
|
|
|
var matchingLogs []core.Log
|
|
|
|
for _, logs := range repository.logs {
|
|
|
|
for _, log := range logs {
|
|
|
|
if log.Address == address && log.BlockNumber == blockNumber {
|
|
|
|
matchingLogs = append(matchingLogs, log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matchingLogs
|
2017-11-09 19:59:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
func (repository *InMemory) CreateContract(contract core.Contract) error {
|
2017-12-13 16:51:11 +00:00
|
|
|
repository.contracts[contract.Hash] = contract
|
2017-11-09 19:59:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
func (repository *InMemory) ContractExists(contractHash string) bool {
|
|
|
|
_, present := repository.contracts[contractHash]
|
2017-11-09 19:59:12 +00:00
|
|
|
return present
|
2017-11-06 16:42:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 16:51:11 +00:00
|
|
|
func (repository *InMemory) FindContract(contractHash string) (core.Contract, error) {
|
2017-12-04 22:54:35 +00:00
|
|
|
contract, ok := repository.contracts[contractHash]
|
2017-12-04 20:13:15 +00:00
|
|
|
if !ok {
|
2018-02-02 21:53:16 +00:00
|
|
|
return core.Contract{}, repositories.ErrContractDoesNotExist(contractHash)
|
2017-11-13 21:41:32 +00:00
|
|
|
}
|
|
|
|
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-11-13 21:41:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 16:51:11 +00:00
|
|
|
return contract, nil
|
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++ {
|
2017-12-13 16:51:11 +00:00
|
|
|
if _, ok := repository.blocks[blockNumber]; !ok {
|
2017-11-06 20:36:12 +00:00
|
|
|
missingNumbers = append(missingNumbers, blockNumber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingNumbers
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:14:41 +00:00
|
|
|
func (repository *InMemory) CreateOrUpdateBlock(block core.Block) error {
|
2018-01-05 17:55:00 +00:00
|
|
|
repository.CreateOrUpdateBlockCallCount++
|
2017-12-13 16:51:11 +00:00
|
|
|
repository.blocks[block.Number] = block
|
2018-01-03 17:23:43 +00:00
|
|
|
for _, transaction := range block.Transactions {
|
|
|
|
repository.receipts[transaction.Hash] = transaction.Receipt
|
2018-01-15 16:52:50 +00:00
|
|
|
repository.logs[transaction.TxHash] = transaction.Logs
|
2018-01-03 17:23:43 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:51:11 +00:00
|
|
|
func (repository *InMemory) FindBlockByNumber(blockNumber int64) (core.Block, error) {
|
|
|
|
if block, ok := repository.blocks[blockNumber]; ok {
|
|
|
|
return block, nil
|
|
|
|
}
|
2018-02-02 21:53:16 +00:00
|
|
|
return core.Block{}, repositories.ErrBlockDoesNotExist(blockNumber)
|
2017-11-06 16:42:33 +00:00
|
|
|
}
|
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
|
|
|
|
}
|