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/>.
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
package test_config
|
|
|
|
|
|
|
|
import (
|
2019-03-14 21:49:27 +00:00
|
|
|
"errors"
|
2019-01-23 18:44:09 +00:00
|
|
|
"fmt"
|
2019-08-06 21:57:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2019-08-06 21:57:08 +00:00
|
|
|
"os"
|
2018-02-13 16:31:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var TestConfig *viper.Viper
|
|
|
|
var DBConfig config.Database
|
2018-08-07 20:17:29 +00:00
|
|
|
var TestClient config.Client
|
2018-02-13 16:31:57 +00:00
|
|
|
var ABIFilePath string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
setTestConfig()
|
|
|
|
setABIPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setTestConfig() {
|
|
|
|
TestConfig = viper.New()
|
2019-09-19 23:48:38 +00:00
|
|
|
TestConfig.SetConfigName("testing")
|
2018-02-13 16:31:57 +00:00
|
|
|
TestConfig.AddConfigPath("$GOPATH/src/github.com/vulcanize/vulcanizedb/environments/")
|
|
|
|
err := TestConfig.ReadInConfig()
|
|
|
|
if err != nil {
|
2019-08-06 21:57:08 +00:00
|
|
|
logrus.Fatal(err)
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
2019-03-14 21:49:27 +00:00
|
|
|
ipc := TestConfig.GetString("client.ipcPath")
|
2019-10-29 05:56:55 +00:00
|
|
|
|
|
|
|
// If we don't have an ipc path in the config file, check the env variable
|
|
|
|
if ipc == "" {
|
|
|
|
TestConfig.BindEnv("url", "INFURA_URL")
|
|
|
|
ipc = TestConfig.GetString("url")
|
|
|
|
}
|
|
|
|
if ipc == "" {
|
|
|
|
logrus.Fatal(errors.New("testing.toml IPC path or $INFURA_URL env variable need to be set"))
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
hn := TestConfig.GetString("database.hostname")
|
|
|
|
port := TestConfig.GetInt("database.port")
|
|
|
|
name := TestConfig.GetString("database.name")
|
2019-02-08 16:35:46 +00:00
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
DBConfig = config.Database{
|
|
|
|
Hostname: hn,
|
|
|
|
Name: name,
|
|
|
|
Port: port,
|
|
|
|
}
|
2018-08-07 20:17:29 +00:00
|
|
|
TestClient = config.Client{
|
|
|
|
IPCPath: ipc,
|
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setABIPath() {
|
|
|
|
gp := os.Getenv("GOPATH")
|
2019-10-28 11:30:24 +00:00
|
|
|
ABIFilePath = gp + "/src/github.com/vulcanize/vulcanizedb/pkg/eth/testing/"
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestDB(node core.Node) *postgres.DB {
|
2019-01-23 18:44:09 +00:00
|
|
|
db, err := postgres.NewDB(DBConfig, node)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Could not create new test db: %v", err))
|
|
|
|
}
|
2018-08-07 20:17:29 +00:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func CleanTestDB(db *postgres.DB) {
|
2019-08-02 15:09:59 +00:00
|
|
|
db.MustExec("DELETE FROM addresses")
|
2018-02-13 16:31:57 +00:00
|
|
|
db.MustExec("DELETE FROM blocks")
|
2018-10-10 10:39:20 +00:00
|
|
|
db.MustExec("DELETE FROM checked_headers")
|
2020-02-10 15:00:55 +00:00
|
|
|
// can't delete from nodes since this function is called after the required node is persisted
|
2019-03-08 17:34:06 +00:00
|
|
|
db.MustExec("DELETE FROM goose_db_version")
|
2019-07-23 19:57:52 +00:00
|
|
|
db.MustExec("DELETE FROM header_sync_logs")
|
2019-07-18 21:24:25 +00:00
|
|
|
db.MustExec("DELETE FROM header_sync_receipts")
|
2019-05-01 06:02:30 +00:00
|
|
|
db.MustExec("DELETE FROM header_sync_transactions")
|
2019-07-18 21:24:25 +00:00
|
|
|
db.MustExec("DELETE FROM headers")
|
2019-03-08 17:34:06 +00:00
|
|
|
db.MustExec("DELETE FROM queued_storage")
|
2019-10-31 16:04:53 +00:00
|
|
|
db.MustExec("DELETE FROM storage_diff")
|
2019-02-25 04:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CleanCheckedHeadersTable(db *postgres.DB, columnNames []string) {
|
|
|
|
for _, name := range columnNames {
|
|
|
|
db.MustExec("ALTER TABLE checked_headers DROP COLUMN IF EXISTS " + name)
|
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
}
|
2018-05-05 20:25:54 +00:00
|
|
|
|
2019-01-11 09:58:41 +00:00
|
|
|
// Returns a new test node, with the same ID
|
2018-08-07 20:17:29 +00:00
|
|
|
func NewTestNode() core.Node {
|
|
|
|
return core.Node{
|
|
|
|
GenesisBlock: "GENESIS",
|
2020-02-10 15:00:55 +00:00
|
|
|
NetworkID: "1",
|
2018-08-07 20:17:29 +00:00
|
|
|
ID: "b6f90c0fdd8ec9607aed8ee45c69322e47b7063f0bfb7a29c8ecafab24d0a22d24dd2329b5ee6ed4125a03cb14e57fd584e67f9e53e6c631055cbbd82f080845",
|
|
|
|
ClientName: "Geth/v1.7.2-stable-1db4ecdc/darwin-amd64/go1.9",
|
|
|
|
}
|
|
|
|
}
|