2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package test_helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2019-03-11 23:18:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/contract_watcher/shared/helpers/test_helpers/mocks"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/client"
|
|
|
|
rpc2 "github.com/vulcanize/vulcanizedb/pkg/eth/converters/rpc"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/node"
|
2019-03-14 20:07:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
2018-11-07 21:50:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TransferLog struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
|
|
|
VulcanizeLogID int64 `db:"vulcanize_log_id"`
|
2018-11-07 21:50:43 +00:00
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
Block int64 `db:"block"`
|
|
|
|
Tx string `db:"tx"`
|
|
|
|
From string `db:"from_"`
|
|
|
|
To string `db:"to_"`
|
|
|
|
Value string `db:"value_"`
|
|
|
|
}
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
type NewOwnerLog struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
|
|
|
VulcanizeLogID int64 `db:"vulcanize_log_id"`
|
2018-12-14 17:52:02 +00:00
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
Block int64 `db:"block"`
|
|
|
|
Tx string `db:"tx"`
|
|
|
|
Node string `db:"node_"`
|
|
|
|
Label string `db:"label_"`
|
|
|
|
Owner string `db:"owner_"`
|
|
|
|
}
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
type HeaderSyncTransferLog struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
2018-11-24 04:26:07 +00:00
|
|
|
HeaderID int64 `db:"header_id"`
|
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
LogIndex int64 `db:"log_idx"`
|
|
|
|
TxIndex int64 `db:"tx_idx"`
|
|
|
|
From string `db:"from_"`
|
|
|
|
To string `db:"to_"`
|
|
|
|
Value string `db:"value_"`
|
|
|
|
RawLog []byte `db:"raw_log"`
|
|
|
|
}
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
type HeaderSyncNewOwnerLog struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
2018-12-14 17:52:02 +00:00
|
|
|
HeaderID int64 `db:"header_id"`
|
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
LogIndex int64 `db:"log_idx"`
|
|
|
|
TxIndex int64 `db:"tx_idx"`
|
|
|
|
Node string `db:"node_"`
|
|
|
|
Label string `db:"label_"`
|
|
|
|
Owner string `db:"owner_"`
|
|
|
|
RawLog []byte `db:"raw_log"`
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
type BalanceOf struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
2018-11-20 16:38:23 +00:00
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
Block int64 `db:"block"`
|
|
|
|
Address string `db:"who_"`
|
|
|
|
Balance string `db:"returned"`
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
type Resolver struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
2018-12-19 18:42:59 +00:00
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
Block int64 `db:"block"`
|
|
|
|
Node string `db:"node_"`
|
|
|
|
Address string `db:"returned"`
|
|
|
|
}
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
type Owner struct {
|
2019-10-18 16:16:19 +00:00
|
|
|
ID int64 `db:"id"`
|
2018-12-14 17:52:02 +00:00
|
|
|
TokenName string `db:"token_name"`
|
|
|
|
Block int64 `db:"block"`
|
|
|
|
Node string `db:"node_"`
|
|
|
|
Address string `db:"returned"`
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
func SetupDBandBC() (*postgres.DB, core.BlockChain) {
|
2019-10-31 16:19:34 +00:00
|
|
|
con := test_config.TestClient
|
|
|
|
testIPC := con.IPCPath
|
2019-10-18 16:16:19 +00:00
|
|
|
rawRPCClient, err := rpc.Dial(testIPC)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-10-18 16:16:19 +00:00
|
|
|
rpcClient := client.NewRPCClient(rawRPCClient, testIPC)
|
|
|
|
ethClient := ethclient.NewClient(rawRPCClient)
|
2018-11-07 21:50:43 +00:00
|
|
|
blockChainClient := client.NewEthClient(ethClient)
|
2019-09-24 03:39:04 +00:00
|
|
|
madeNode := node.MakeNode(rpcClient)
|
2019-10-18 16:16:19 +00:00
|
|
|
transactionConverter := rpc2.NewRPCTransactionConverter(ethClient)
|
2019-10-28 11:30:24 +00:00
|
|
|
blockChain := eth.NewBlockChain(blockChainClient, rpcClient, madeNode, transactionConverter)
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
db, err := postgres.NewDB(config.Database{
|
|
|
|
Hostname: "localhost",
|
2019-10-31 16:19:34 +00:00
|
|
|
Name: "vulcanize_testing",
|
2018-11-07 21:50:43 +00:00
|
|
|
Port: 5432,
|
|
|
|
}, blockChain.Node())
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
return db, blockChain
|
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func SetupTusdRepo(vulcanizeLogID *int64, wantedEvents, wantedMethods []string) (*postgres.DB, *contract.Contract) {
|
2018-11-07 21:50:43 +00:00
|
|
|
db, err := postgres.NewDB(config.Database{
|
|
|
|
Hostname: "localhost",
|
2019-10-31 16:19:34 +00:00
|
|
|
Name: "vulcanize_testing",
|
2018-11-07 21:50:43 +00:00
|
|
|
Port: 5432,
|
|
|
|
}, core.Node{})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-08-05 15:38:37 +00:00
|
|
|
receiptRepository := repositories.FullSyncReceiptRepository{DB: db}
|
2019-08-06 21:57:08 +00:00
|
|
|
logRepository := repositories.FullSyncLogRepository{DB: db}
|
2018-11-07 21:50:43 +00:00
|
|
|
blockRepository := *repositories.NewBlockRepository(db)
|
|
|
|
|
|
|
|
blockNumber := rand.Int63()
|
2019-10-18 16:16:19 +00:00
|
|
|
blockID := CreateBlock(blockNumber, blockRepository)
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
receipts := []core.Receipt{{Logs: []core.FullSyncLog{{}}}}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
err = receiptRepository.CreateReceiptsAndLogs(blockID, receipts)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
err = logRepository.Get(vulcanizeLogID, `SELECT id FROM full_sync_logs`)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
info := SetupTusdContract(wantedEvents, wantedMethods)
|
|
|
|
|
|
|
|
return db, info
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupTusdContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
2018-11-24 04:26:07 +00:00
|
|
|
p := mocks.NewParser(constants.TusdAbiString)
|
2019-10-18 17:34:22 +00:00
|
|
|
err := p.Parse(constants.TusdContractAddress)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
return contract.Contract{
|
2018-12-07 15:38:46 +00:00
|
|
|
Name: "TrueUSD",
|
|
|
|
Address: constants.TusdContractAddress,
|
|
|
|
Abi: p.Abi(),
|
|
|
|
ParsedAbi: p.ParsedAbi(),
|
|
|
|
StartingBlock: 6194634,
|
|
|
|
Events: p.GetEvents(wantedEvents),
|
2018-12-14 17:52:02 +00:00
|
|
|
Methods: p.GetSelectMethods(wantedMethods),
|
|
|
|
MethodArgs: map[string]bool{},
|
|
|
|
FilterArgs: map[string]bool{},
|
|
|
|
}.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupENSContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
|
|
|
p := mocks.NewParser(constants.ENSAbiString)
|
2019-10-18 17:34:22 +00:00
|
|
|
err := p.Parse(constants.EnsContractAddress)
|
2018-12-14 17:52:02 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
return contract.Contract{
|
|
|
|
Name: "ENS-Registry",
|
|
|
|
Address: constants.EnsContractAddress,
|
|
|
|
Abi: p.Abi(),
|
|
|
|
ParsedAbi: p.ParsedAbi(),
|
|
|
|
StartingBlock: 6194634,
|
|
|
|
Events: p.GetEvents(wantedEvents),
|
|
|
|
Methods: p.GetSelectMethods(wantedMethods),
|
2018-12-07 15:38:46 +00:00
|
|
|
MethodArgs: map[string]bool{},
|
|
|
|
FilterArgs: map[string]bool{},
|
2018-12-14 17:52:02 +00:00
|
|
|
}.Init()
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-20 18:48:43 +00:00
|
|
|
func SetupMarketPlaceContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
|
|
|
p := mocks.NewParser(constants.MarketPlaceAbiString)
|
2019-10-18 17:34:22 +00:00
|
|
|
err := p.Parse(constants.MarketPlaceContractAddress)
|
2019-09-20 18:48:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
return contract.Contract{
|
|
|
|
Name: "Marketplace",
|
|
|
|
Address: constants.MarketPlaceContractAddress,
|
|
|
|
StartingBlock: 6496012,
|
|
|
|
Abi: p.Abi(),
|
|
|
|
ParsedAbi: p.ParsedAbi(),
|
|
|
|
Events: p.GetEvents(wantedEvents),
|
|
|
|
Methods: p.GetSelectMethods(wantedMethods),
|
|
|
|
FilterArgs: map[string]bool{},
|
|
|
|
MethodArgs: map[string]bool{},
|
|
|
|
}.Init()
|
|
|
|
}
|
|
|
|
|
2019-09-21 02:37:59 +00:00
|
|
|
func SetupMolochContract(wantedEvents, wantedMethods []string) *contract.Contract {
|
|
|
|
p := mocks.NewParser(constants.MolochAbiString)
|
2019-10-18 17:34:22 +00:00
|
|
|
err := p.Parse(constants.MolochContractAddress)
|
2019-09-21 02:37:59 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
return contract.Contract{
|
|
|
|
Name: "Moloch",
|
|
|
|
Address: constants.MolochContractAddress,
|
|
|
|
StartingBlock: 7218566,
|
|
|
|
Abi: p.Abi(),
|
|
|
|
ParsedAbi: p.ParsedAbi(),
|
|
|
|
Events: p.GetEvents(wantedEvents),
|
|
|
|
Methods: p.GetSelectMethods(wantedMethods),
|
|
|
|
FilterArgs: map[string]bool{},
|
|
|
|
MethodArgs: map[string]bool{},
|
|
|
|
}.Init()
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
// TODO: tear down/setup DB from migrations so this doesn't alter the schema between tests
|
2018-11-07 21:50:43 +00:00
|
|
|
func TearDown(db *postgres.DB) {
|
2019-03-14 21:49:27 +00:00
|
|
|
tx, err := db.Beginx()
|
2018-11-24 04:26:07 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-08-02 15:09:59 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM addresses`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-06-10 17:17:14 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM eth_blocks`)
|
2018-11-24 04:26:07 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
_, err = tx.Exec(`DELETE FROM headers`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-07-18 21:24:25 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM full_sync_logs`)
|
2018-11-24 04:26:07 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM log_filters`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-03-19 19:11:26 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM full_sync_transactions`)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
_, err = tx.Exec("DELETE FROM header_sync_transactions")
|
2019-03-19 19:39:26 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-03-22 03:43:06 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM full_sync_receipts`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
_, err = tx.Exec(`DELETE FROM header_sync_receipts`)
|
2018-11-23 18:12:24 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-02-25 04:26:42 +00:00
|
|
|
_, err = tx.Exec(`DROP TABLE checked_headers`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-08-06 21:57:08 +00:00
|
|
|
_, err = tx.Exec(`CREATE TABLE checked_headers (
|
|
|
|
id SERIAL PRIMARY KEY,
|
2019-10-08 16:48:15 +00:00
|
|
|
header_id INTEGER UNIQUE NOT NULL REFERENCES headers (id) ON DELETE CASCADE);`)
|
2019-01-23 06:55:26 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
_, err = tx.Exec(`DROP SCHEMA IF EXISTS full_0x8dd5fbce2f6a956c3022ba3663759011dd51e73e CASCADE`)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
_, err = tx.Exec(`DROP SCHEMA IF EXISTS header_0x8dd5fbce2f6a956c3022ba3663759011dd51e73e CASCADE`)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
_, err = tx.Exec(`DROP SCHEMA IF EXISTS full_0x314159265dd8dbb310642f98f50c066173c1259b CASCADE`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-05-01 06:02:30 +00:00
|
|
|
_, err = tx.Exec(`DROP SCHEMA IF EXISTS header_0x314159265dd8dbb310642f98f50c066173c1259b CASCADE`)
|
2018-12-14 17:52:02 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2018-11-24 04:26:07 +00:00
|
|
|
err = tx.Commit()
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-02-08 16:35:46 +00:00
|
|
|
|
|
|
|
_, err = db.Exec(`VACUUM checked_headers`)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
func CreateBlock(blockNumber int64, repository repositories.BlockRepository) int64 {
|
|
|
|
blockID, err := repository.CreateOrUpdateBlock(core.Block{Number: blockNumber})
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
return blockID
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|