Merge old private repo into vulcanize

This commit is contained in:
Matt Krump
2018-01-25 18:08:26 -06:00
55 changed files with 1718 additions and 579 deletions
+75
View File
@@ -0,0 +1,75 @@
package cmd
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/vulcanize/vulcanizedb/pkg/filters"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/utils"
"github.com/spf13/cobra"
)
// addFilterCmd represents the addFilter command
var addFilterCmd = &cobra.Command{
Use: "addFilter",
Short: "Adds event filter to vulcanizedb",
Long: `An event filter is added to the vulcanize_db.
All events matching the filter conitions will be tracked
in vulcanizedb.
vulcanizedb addFilter --config config.toml --filter-filepath filter.json
The event filters are expected to match
the format described in the ethereum RPC wiki:
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
[{
"fromBlock": "0x1",
"toBlock": "0x2",
"address": "0x8888f1f195afa192cfee860698584c030f4c9db1",
"topics": ["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
null,
"0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"]
}]
`,
Run: func(cmd *cobra.Command, args []string) {
addFilter()
},
}
var filterFilepath string
func init() {
rootCmd.AddCommand(addFilterCmd)
addFilterCmd.PersistentFlags().StringVar(&filterFilepath, "filter-filepath", "", "path/to/filter.json")
addFilterCmd.MarkFlagRequired("filter-filepath")
}
func addFilter() {
if filterFilepath == "" {
log.Fatal("filter-filepath required")
}
var logFilters filters.LogFilters
blockchain := geth.NewBlockchain(ipc)
repository := utils.LoadPostgres(databaseConfig, blockchain.Node())
absFilePath := utils.AbsFilePath(filterFilepath)
logFilterBytes, err := ioutil.ReadFile(absFilePath)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(logFilterBytes, &logFilters)
if err != nil {
log.Fatal(err)
}
for _, filter := range logFilters {
err = repository.AddFilter(filter)
if err != nil {
log.Fatal(err)
}
}
}
-71
View File
@@ -1,71 +0,0 @@
package main
import (
"log"
"flag"
"math/big"
"time"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
)
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
const (
windowSize = 24
pollingInterval = 10 * time.Second
)
func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
lastBlockNumber := blockchain.LastBlock().Int64()
stepSize := int64(1000)
go func() {
for i := int64(0); i < lastBlockNumber; i = min(i+stepSize, lastBlockNumber) {
logs, err := blockchain.GetLogs(core.Contract{Hash: *contractHash}, big.NewInt(i), big.NewInt(i+stepSize))
log.Println("Backfilling Logs:", i)
if err != nil {
log.Println(err)
}
repository.CreateLogs(logs)
}
}()
done := make(chan struct{})
go func() { done <- struct{}{} }()
for range ticker.C {
select {
case <-done:
go func() {
z := &big.Int{}
z.Sub(blockchain.LastBlock(), big.NewInt(25))
log.Printf("Logs Window: %d - %d", z.Int64(), blockchain.LastBlock().Int64())
logs, _ := blockchain.GetLogs(core.Contract{Hash: *contractHash}, z, blockchain.LastBlock())
repository.CreateLogs(logs)
done <- struct{}{}
}()
default:
}
}
}
-22
View File
@@ -1,22 +0,0 @@
package main
import (
"flag"
"fmt"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/history"
)
func main() {
environment := flag.String("environment", "", "Environment name")
startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
numberOfBlocksCreated := history.PopulateMissingBlocks(blockchain, repository, int64(*startingBlockNumber))
fmt.Printf("Populated %d blocks", numberOfBlocksCreated)
}
+74
View File
@@ -0,0 +1,74 @@
package cmd
import (
"fmt"
"os"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var databaseConfig config.Database
var ipc string
var rootCmd = &cobra.Command{
Use: "vulcanizedb",
PersistentPreRun: database,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func database(cmd *cobra.Command, args []string) {
ipc = viper.GetString("client.ipcpath")
databaseConfig = config.Database{
Name: viper.GetString("database.name"),
Hostname: viper.GetString("database.hostname"),
Port: viper.GetInt("database.port"),
}
viper.Set("database.config", databaseConfig)
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "environment/public.toml", "config file location")
rootCmd.PersistentFlags().String("database-name", "vulcanize_public", "database name")
rootCmd.PersistentFlags().Int("database-port", 5432, "database port")
rootCmd.PersistentFlags().String("database-hostname", "localhost", "database hostname")
rootCmd.PersistentFlags().String("client-ipcPath", "", "location of geth.ipc file")
viper.BindPFlag("database.name", rootCmd.PersistentFlags().Lookup("database-name"))
viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port"))
viper.BindPFlag("database.hostname", rootCmd.PersistentFlags().Lookup("database-hostname"))
viper.BindPFlag("client.ipcPath", rootCmd.PersistentFlags().Lookup("client-ipcPath"))
}
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(home)
viper.SetConfigName(".vulcanizedb")
}
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
fmt.Printf("Using config file: %s\n\n", viper.ConfigFileUsed())
}
}
-34
View File
@@ -1,34 +0,0 @@
package main
import (
"flag"
"time"
"os"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/history"
)
const (
pollingInterval = 7 * time.Second
)
func main() {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
environment := flag.String("environment", "", "Environment name")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
validator := history.NewBlockValidator(blockchain, repository, 15)
for range ticker.C {
window := validator.ValidateBlocks()
validator.Log(os.Stdout, window)
}
}
-31
View File
@@ -1,31 +0,0 @@
package main
import (
"flag"
"log"
"fmt"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/contract_summary"
"github.com/vulcanize/vulcanizedb/pkg/geth"
)
func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
_blockNumber := flag.Int64("block-number", -1, "Block number of summary")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
blockNumber := cmd.RequestedBlockNumber(_blockNumber)
contractSummary, err := contract_summary.NewSummary(blockchain, repository, *contractHash, blockNumber)
if err != nil {
log.Fatalln(err)
}
output := contract_summary.GenerateConsoleOutput(contractSummary)
fmt.Println(output)
}
+77
View File
@@ -0,0 +1,77 @@
package cmd
import (
"os"
"time"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/history"
"github.com/vulcanize/vulcanizedb/pkg/repositories"
"github.com/vulcanize/vulcanizedb/utils"
"github.com/spf13/cobra"
)
// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Syncs vulcanizedb with local ethereum node",
Long: `Syncs vulcanizedb with local ethereum node.
vulcanizedb sync --startingBlockNumber 0 --config public.toml
Expects ethereum node to be running and requires a .toml config:
[database]
name = "vulcanize_public"
hostname = "localhost"
port = 5432
[client]
ipcPath = "/Users/mattkrump/Library/Ethereum/geth.ipc"
`,
Run: func(cmd *cobra.Command, args []string) {
sync()
},
}
const (
pollingInterval = 7 * time.Second
)
var startingBlockNumber int
func init() {
rootCmd.AddCommand(syncCmd)
syncCmd.Flags().IntVarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block number to start syncing from")
}
func backFillAllBlocks(blockchain core.Blockchain, repository repositories.Postgres, missingBlocksPopulated chan int, startingBlockNumber int64) {
go func() {
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, startingBlockNumber)
}()
}
func sync() {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
blockchain := geth.NewBlockchain(ipc)
repository := utils.LoadPostgres(databaseConfig, blockchain.Node())
validator := history.NewBlockValidator(blockchain, repository, 15)
missingBlocksPopulated := make(chan int)
_startingBlockNumber := int64(startingBlockNumber)
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated, _startingBlockNumber)
for {
select {
case <-ticker.C:
window := validator.ValidateBlocks()
validator.Log(os.Stdout, window)
case <-missingBlocksPopulated:
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated, _startingBlockNumber)
}
}
}
-69
View File
@@ -1,69 +0,0 @@
package cmd
import (
"log"
"path/filepath"
"fmt"
"math/big"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/repositories"
)
func LoadConfig(environment string) config.Config {
cfg, err := config.NewConfig(environment)
if err != nil {
log.Fatalf("Error loading config\n%v", err)
}
return cfg
}
func LoadPostgres(database config.Database, node core.Node) repositories.Postgres {
repository, err := repositories.NewPostgres(database, node)
if err != nil {
log.Fatalf("Error loading postgres\n%v", err)
}
return repository
}
func ReadAbiFile(abiFilepath string) string {
if !filepath.IsAbs(abiFilepath) {
abiFilepath = filepath.Join(config.ProjectRoot(), abiFilepath)
}
abi, err := geth.ReadAbiFile(abiFilepath)
if err != nil {
log.Fatalf("Error reading ABI file at \"%s\"\n %v", abiFilepath, err)
}
return abi
}
func GetAbi(abiFilepath string, contractHash string) string {
var contractAbiString string
if abiFilepath != "" {
contractAbiString = ReadAbiFile(abiFilepath)
} else {
etherscan := geth.NewEtherScanClient("https://api.etherscan.io")
fmt.Println("No ABI supplied. Retrieving ABI from Etherscan")
contractAbiString, _ = etherscan.GetAbi(contractHash)
}
_, err := geth.ParseAbi(contractAbiString)
if err != nil {
log.Fatalln("Invalid ABI")
}
return contractAbiString
}
func RequestedBlockNumber(blockNumber *int64) *big.Int {
var _blockNumber *big.Int
if *blockNumber == -1 {
_blockNumber = nil
} else {
_blockNumber = big.NewInt(*blockNumber)
}
return _blockNumber
}
-50
View File
@@ -1,50 +0,0 @@
package main
import (
"flag"
"time"
"os"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/history"
"github.com/vulcanize/vulcanizedb/pkg/repositories"
)
const (
pollingInterval = 7 * time.Second
)
func backFillAllBlocks(blockchain core.Blockchain, repository repositories.Postgres, missingBlocksPopulated chan int) {
go func() {
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, 0)
}()
}
func main() {
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
environment := flag.String("environment", "", "Environment name")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
validator := history.NewBlockValidator(blockchain, repository, 15)
missingBlocksPopulated := make(chan int)
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated)
for {
select {
case <-ticker.C:
window := validator.ValidateBlocks()
validator.Log(os.Stdout, window)
case <-missingBlocksPopulated:
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated)
}
}
}
-26
View File
@@ -1,26 +0,0 @@
package main
import (
"flag"
"github.com/vulcanize/vulcanizedb/cmd"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/geth"
)
func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
abiFilepath := flag.String("abi-filepath", "", "path/to/abifile.json")
flag.Parse()
contractAbiString := cmd.GetAbi(*abiFilepath, *contractHash)
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
watchedContract := core.Contract{
Abi: contractAbiString,
Hash: *contractHash,
}
repository.CreateContract(watchedContract)
}