forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the pool persistence for transactions and gas price
- Loading branch information
Showing
23 changed files
with
986 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.PHONY: build | ||
build: lint test ## Build the binary | ||
go build -o ./dist/hezcore ./cmd/main.go | ||
|
||
.PHONY: test | ||
test: ## runs tests | ||
go test ./... -p 1 | ||
|
||
.PHONY: install-linter | ||
install-linter: ## install linter | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.30.0 | ||
|
||
.PHONY: lint | ||
lint: ## runs linter | ||
$$(go env GOPATH)/bin/golangci-lint run --timeout=5m -E whitespace -E gosec -E gci -E misspell -E gomnd -E gofmt -E goimports -E golint --exclude-use-default=false --max-same-issues 0 | ||
|
||
.PHONY: run-db | ||
run-db: ## runs the db instance | ||
docker run --rm -p 5432:5432 -e POSTGRES_DB="polygon-hermez" -e POSTGRES_USER="hermez" -e POSTGRES_PASSWORD="polygon" -d postgres | ||
|
||
## Help display. | ||
## Pulls comments from beside commands and prints a nicely formatted | ||
## display with the commands and their usage information. | ||
.DEFAULT_GOAL := help | ||
|
||
.PHONY: help | ||
help: ## Prints this help | ||
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ | ||
| sort \ | ||
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package db | ||
|
||
// Config provide fields to configure the pool | ||
type Config struct { | ||
// Database name | ||
Database string | ||
|
||
// User name | ||
User string | ||
|
||
// Password of the user | ||
Password string | ||
|
||
// Host address | ||
Host string | ||
|
||
// Port Number | ||
Port string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
package database | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gobuffalo/packr/v2" | ||
"github.com/hermeznetwork/hermez-core/log" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/pgxpool" | ||
"github.com/jackc/pgx/v4/stdlib" | ||
migrate "github.com/rubenv/sql-migrate" | ||
) | ||
|
||
// NewSQLDB creates a new SQL DB | ||
func NewSQLDB(dbName, dbUser, dbPassword, dbHost, dbPort string) (*pgxpool.Pool, error) { | ||
return pgxpool.Connect(context.Background(), "postgres://"+dbUser+":"+dbPassword+"@"+dbHost+":"+dbPort+"/"+dbName) | ||
func NewSQLDB(cfg Config) (*pgxpool.Pool, error) { | ||
return pgxpool.Connect(context.Background(), "postgres://"+cfg.User+":"+cfg.Password+"@"+cfg.Host+":"+cfg.Port+"/"+cfg.Database) | ||
} | ||
|
||
// RunMigrations will execute pending migrations if needed to keep | ||
// the database updated with the latest changes | ||
func RunMigrations(cfg Config) error { | ||
c, err := pgx.ParseConfig("postgres://" + cfg.User + ":" + cfg.Password + "@" + cfg.Host + ":" + cfg.Port + "/" + cfg.Database) | ||
if err != nil { | ||
return err | ||
} | ||
db := stdlib.OpenDB(*c) | ||
|
||
var migrations = &migrate.PackrMigrationSource{Box: packr.New("hermez-db-migrations", "./migrations")} | ||
nMigrations, err := migrate.Exec(db, "postgres", migrations, migrate.Up) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info("successfully ran ", nMigrations, " migrations Up") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- +migrate Up | ||
|
||
CREATE SCHEMA pool | ||
|
||
CREATE TABLE pool.txs ( | ||
hash VARCHAR PRIMARY KEY, | ||
encoded VARCHAR, | ||
decoded jsonb, | ||
state varchar(15) | ||
); | ||
|
||
-- create json indexes to query ordered by nonce and by tx state | ||
|
||
CREATE TABLE pool.gas_price ( | ||
item_id SERIAL PRIMARY KEY, | ||
price DECIMAL(78,0), | ||
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.