2018-11-07 21:50:43 +00:00
// VulcanizeDB
// Copyright © 2018 Vulcanize
// 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 repository
import (
2018-11-20 16:38:23 +00:00
"errors"
2018-11-07 21:50:43 +00:00
"fmt"
2018-11-20 16:38:23 +00:00
"strings"
2018-11-07 21:50:43 +00:00
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/omni/types"
)
type MethodDatastore interface {
2018-11-20 16:38:23 +00:00
PersistResult ( method types . Result , contractAddr , contractName string ) error
CreateMethodTable ( contractAddr string , method types . Result ) ( bool , error )
CreateContractSchema ( contractAddr string ) ( bool , error )
2018-11-07 21:50:43 +00:00
}
type methodDatastore struct {
* postgres . DB
}
func NewMethodDatastore ( db * postgres . DB ) * methodDatastore {
return & methodDatastore {
DB : db ,
}
}
2018-11-20 16:38:23 +00:00
func ( d * methodDatastore ) PersistResult ( method types . Result , contractAddr , contractName string ) error {
if len ( method . Args ) != len ( method . Inputs ) {
return errors . New ( "error: given number of inputs does not match number of method arguments" )
}
if len ( method . Return ) != 1 {
return errors . New ( "error: given number of outputs does not match number of method return values" )
}
_ , err := d . CreateContractSchema ( contractAddr )
2018-11-07 21:50:43 +00:00
if err != nil {
return err
}
2018-11-20 16:38:23 +00:00
_ , err = d . CreateMethodTable ( contractAddr , method )
if err != nil {
return err
2018-11-07 21:50:43 +00:00
}
2018-11-20 16:38:23 +00:00
return d . persistResult ( method , contractAddr , contractName )
2018-11-07 21:50:43 +00:00
}
// Creates a custom postgres command to persist logs for the given event
2018-11-20 16:38:23 +00:00
func ( d * methodDatastore ) persistResult ( method types . Result , contractAddr , contractName string ) error {
// Begin postgres string
pgStr := fmt . Sprintf ( "INSERT INTO c%s.%s_method " , strings . ToLower ( contractAddr ) , strings . ToLower ( method . Name ) )
pgStr = pgStr + "(token_name, block"
// Pack the corresponding variables in a slice
var data [ ] interface { }
data = append ( data ,
contractName ,
method . Block )
// Iterate over method args and return value, adding names
// to the string and pushing values to the slice
counter := 0 // Keep track of number of inputs
for i , arg := range method . Args {
counter += 1
pgStr = pgStr + fmt . Sprintf ( ", %s_" , strings . ToLower ( arg . Name ) ) // Add underscore after to avoid any collisions with reserved pg words
data = append ( data , method . Inputs [ i ] )
}
counter += 1
pgStr = pgStr + ", returned) VALUES ($1, $2"
data = append ( data , method . Output )
// For each input entry we created we add its postgres command variable to the string
for i := 0 ; i < counter ; i ++ {
pgStr = pgStr + fmt . Sprintf ( ", $%d" , i + 3 )
}
pgStr = pgStr + ")"
_ , err := d . DB . Exec ( pgStr , data ... )
if err != nil {
return err
2018-11-07 21:50:43 +00:00
}
return nil
}
// Checks for event table and creates it if it does not already exist
2018-11-20 16:38:23 +00:00
func ( d * methodDatastore ) CreateMethodTable ( contractAddr string , method types . Result ) ( bool , error ) {
2018-11-07 21:50:43 +00:00
tableExists , err := d . checkForTable ( contractAddr , method . Name )
if err != nil {
2018-11-20 16:38:23 +00:00
return false , err
2018-11-07 21:50:43 +00:00
}
if ! tableExists {
err = d . newMethodTable ( contractAddr , method )
if err != nil {
2018-11-20 16:38:23 +00:00
return false , err
2018-11-07 21:50:43 +00:00
}
}
2018-11-20 16:38:23 +00:00
return ! tableExists , nil
2018-11-07 21:50:43 +00:00
}
// Creates a table for the given contract and event
2018-11-20 16:38:23 +00:00
func ( d * methodDatastore ) newMethodTable ( contractAddr string , method types . Result ) error {
2018-11-07 21:50:43 +00:00
// Begin pg string
2018-11-20 16:38:23 +00:00
pgStr := fmt . Sprintf ( "CREATE TABLE IF NOT EXISTS c%s.%s_method " , strings . ToLower ( contractAddr ) , strings . ToLower ( method . Name ) )
2018-11-07 21:50:43 +00:00
pgStr = pgStr + "(id SERIAL, token_name CHARACTER VARYING(66) NOT NULL, block INTEGER NOT NULL,"
// Iterate over method inputs and outputs, using their name and pgType to grow the string
2018-11-20 16:38:23 +00:00
for _ , arg := range method . Args {
pgStr = pgStr + fmt . Sprintf ( " %s_ %s NOT NULL," , strings . ToLower ( arg . Name ) , arg . PgType )
2018-11-07 21:50:43 +00:00
}
2018-11-20 16:38:23 +00:00
pgStr = pgStr + fmt . Sprintf ( " returned %s NOT NULL)" , method . Return [ 0 ] . PgType )
2018-11-07 21:50:43 +00:00
_ , err := d . DB . Exec ( pgStr )
return err
}
// Checks if a table already exists for the given contract and event
func ( d * methodDatastore ) checkForTable ( contractAddr string , methodName string ) ( bool , error ) {
2018-11-20 16:38:23 +00:00
pgStr := fmt . Sprintf ( "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'c%s' AND table_name = '%s_method')" , strings . ToLower ( contractAddr ) , strings . ToLower ( methodName ) )
2018-11-07 21:50:43 +00:00
var exists bool
err := d . DB . Get ( & exists , pgStr )
return exists , err
}
// Checks for contract schema and creates it if it does not already exist
2018-11-20 16:38:23 +00:00
func ( d * methodDatastore ) CreateContractSchema ( contractAddr string ) ( bool , error ) {
if contractAddr == "" {
return false , errors . New ( "error: no contract address specified" )
}
schemaExists , err := d . checkForSchema ( contractAddr )
2018-11-07 21:50:43 +00:00
if err != nil {
2018-11-20 16:38:23 +00:00
return false , err
2018-11-07 21:50:43 +00:00
}
if ! schemaExists {
2018-11-20 16:38:23 +00:00
err = d . newContractSchema ( contractAddr )
2018-11-07 21:50:43 +00:00
if err != nil {
2018-11-20 16:38:23 +00:00
return false , err
2018-11-07 21:50:43 +00:00
}
}
2018-11-20 16:38:23 +00:00
return ! schemaExists , nil
2018-11-07 21:50:43 +00:00
}
// Creates a schema for the given contract
func ( d * methodDatastore ) newContractSchema ( contractAddr string ) error {
2018-11-20 16:38:23 +00:00
_ , err := d . DB . Exec ( "CREATE SCHEMA IF NOT EXISTS c" + strings . ToLower ( contractAddr ) )
2018-11-07 21:50:43 +00:00
return err
}
// Checks if a schema already exists for the given contract
func ( d * methodDatastore ) checkForSchema ( contractAddr string ) ( bool , error ) {
2018-11-20 16:38:23 +00:00
pgStr := fmt . Sprintf ( "SELECT EXISTS (SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'c%s')" , strings . ToLower ( contractAddr ) )
2018-11-07 21:50:43 +00:00
var exists bool
err := d . DB . Get ( & exists , pgStr )
return exists , err
}