Store contract ABI on watched_contracts

This commit is contained in:
Eric Meyer 2017-12-04 14:13:15 -06:00
parent 52e3266495
commit fa2766b64d
7 changed files with 33 additions and 12 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE watched_contracts
DROP COLUMN contract_abi;

View File

@ -0,0 +1,2 @@
ALTER TABLE watched_contracts
ADD COLUMN contract_abi json;

View File

@ -2,8 +2,8 @@
-- PostgreSQL database dump
--
-- Dumped from database version 10.1
-- Dumped by pg_dump version 10.1
-- Dumped from database version 10.0
-- Dumped by pg_dump version 10.0
SET statement_timeout = 0;
SET lock_timeout = 0;
@ -124,7 +124,8 @@ ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
CREATE TABLE watched_contracts (
contract_id integer NOT NULL,
contract_hash character varying(66)
contract_hash character varying(66),
contract_abi json
);

View File

@ -20,18 +20,18 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
}
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
var transactions []core.Transaction
if _, ok := repository.watchedContracts[contractHash]; !ok {
watchedContract, ok := repository.watchedContracts[contractHash]
if !ok {
return nil
}
for _, block := range repository.blocks {
for _, transaction := range block.Transactions {
if transaction.To == contractHash {
transactions = append(transactions, transaction)
watchedContract.Transactions = append(watchedContract.Transactions, transaction)
}
}
}
return &WatchedContract{Hash: contractHash, Transactions: transactions}
return watchedContract
}
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {

View File

@ -32,8 +32,13 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
}
func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
abi := contract.Abi
var abiToInsert *string
if abi != "" {
abiToInsert = &abi
}
_, err := repository.Db.Exec(
`INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
`INSERT INTO watched_contracts (contract_hash, contract_abi) VALUES ($1, $2)`, contract.Hash, abiToInsert)
if err != nil {
return ErrDBInsertFailed
}
@ -50,7 +55,7 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
var savedContracts []WatchedContract
contractRows, _ := repository.Db.Query(
`SELECT contract_hash FROM watched_contracts WHERE contract_hash=$1`, contractHash)
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
savedContracts = repository.loadContract(contractRows)
if len(savedContracts) > 0 {
return &savedContracts[0]
@ -196,10 +201,11 @@ func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContrac
var savedContracts []WatchedContract
for contractRows.Next() {
var savedContractHash string
contractRows.Scan(&savedContractHash)
var savedContractAbi string
contractRows.Scan(&savedContractHash, &savedContractAbi)
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash)
transactions := repository.loadTransactions(transactionRows)
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions}
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
savedContracts = append(savedContracts, savedContract)
}
return savedContracts

View File

@ -230,7 +230,6 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil())
Expect(watchedContract.Transactions).To(BeEmpty())
})
It("returns transactions 'To' a watched contract", func() {
@ -253,6 +252,16 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
{Hash: "TRANSACTION3", To: "x123"},
}))
})
It("stores the ABI of the contract", func() {
repository.CreateWatchedContract(repositories.WatchedContract{
Abi: "{\"some\": \"json\"}",
Hash: "x123",
})
watchedContract := repository.FindWatchedContract("x123")
Expect(watchedContract).ToNot(BeNil())
Expect(watchedContract.Abi).To(Equal("{\"some\": \"json\"}"))
})
})
}

View File

@ -3,6 +3,7 @@ package repositories
import "github.com/8thlight/vulcanizedb/pkg/core"
type WatchedContract struct {
Abi string
Hash string
Transactions []core.Transaction
}