Skip to content

Commit

Permalink
Added PGX driver
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Jul 18, 2024
1 parent e87dfc5 commit 8f3f4f4
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 18 deletions.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/hyperledger/fabric v1.4.0-rc1.0.20230405174026-695dd57e01c2
github.com/hyperledger/fabric-chaincode-go v0.0.0-20230228194215-b84622ba6a7a
github.com/hyperledger/fabric-protos-go v0.3.0
github.com/jackc/pgx/v5 v5.5.4
github.com/json-iterator/go v1.1.12
github.com/lib/pq v1.10.9
github.com/libp2p/go-libp2p v0.31.0
Expand Down Expand Up @@ -45,7 +46,12 @@ require (

replace github.com/hyperledger-labs/fabric-token-sdk/txgen => ./cmd/txgen

require github.com/ale-linux/aries-framework-go/component/kmscrypto v0.0.0-20231023164747-f3f972769504 // indirect
require (
github.com/ale-linux/aries-framework-go/component/kmscrypto v0.0.0-20231023164747-f3f972769504 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
)

require (
code.cloudfoundry.org/clock v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down
2 changes: 2 additions & 0 deletions integration/nwo/token/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (p *NetworkHandler) GetTokensSQLDataSource(opts *sfcnode.Options, uniqueNam
case "sqlite":
return p.DBPath(p.TokensDBSQLDataSourceDir(uniqueName), tms)
case "postgres":
case "pgx":
return GetPostgresDataSource(opts)
}
panic("unknown driver type")
Expand All @@ -212,6 +213,7 @@ func (p *NetworkHandler) GetSQLDataSource(opts *sfcnode.Options, uniqueName stri
case "sqlite":
return p.DBPath(p.TTXDBSQLDataSourceDir(uniqueName), tms)
case "postgres":
case "pgx":
return GetPostgresDataSource(opts)
}
panic("unknown driver type")
Expand Down
2 changes: 1 addition & 1 deletion integration/nwo/token/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func WithPostgresPersistence(config *sql.PostgresConfig) fsc.Option {
return func(o *fsc.Options) error {
if config != nil {
o.Put("token.persistence.sql", config.DataSource())
o.Put("token.persistence.driver", "postgres")
o.Put("token.persistence.driver", "pgx")
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions integration/nwo/token/orion/orion.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (p *NetworkHandler) GetTokensSQLDataSource(opts *sfcnode.Options, uniqueNam
case "sqlite":
return p.DBPath(p.TokensDBSQLDataSourceDir(uniqueName), tms)
case "postgres":
case "pgx":
return GetPostgresDataSource(opts)
}
panic("unknown driver type")
Expand All @@ -223,6 +224,7 @@ func (p *NetworkHandler) GetSQLDataSource(opts *sfcnode.Options, uniqueName stri
case "sqlite":
return p.DBPath(p.TTXDBSQLDataSourceDir(uniqueName), tms)
case "postgres":
case "pgx":
return GetPostgresDataSource(opts)
}
panic("unknown driver type")
Expand Down
1 change: 0 additions & 1 deletion token/services/auditdb/db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db"
dbdriver "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
sqldb "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql"
_ "modernc.org/sqlite"
)

func NewDriver() db.NamedDriver[dbdriver.AuditDBDriver] {
Expand Down
2 changes: 2 additions & 0 deletions token/services/db/sql/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"github.com/hyperledger-labs/fabric-token-sdk/token/services/config"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/utils"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pkg/errors"
_ "modernc.org/sqlite"
)

const sqlitePragmas = `
Expand Down
2 changes: 1 addition & 1 deletion token/services/db/sql/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestIdentityPostgres(t *testing.T) {
defer terminate()

for _, c := range IdentityCases {
db, err := initIdentityDB("postgres", pgConnStr, c.Name, 10)
db, err := initIdentityDB("pgx", pgConnStr, c.Name, 10)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion token/services/db/sql/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTokensPostgres(t *testing.T) {
defer terminate()

for _, c := range TokensCases {
db, err := initTokenDB("postgres", pgConnStr, c.Name, 10)
db, err := initTokenDB("pgx", pgConnStr, c.Name, 10)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion token/services/db/sql/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestTransactionsPostgres(t *testing.T) {
defer terminate()

for _, c := range dbtest.Cases {
db, err := initTransactionsDB("postgres", pgConnStr, c.Name, 10)
db, err := initTransactionsDB("pgx", pgConnStr, c.Name, 10)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions token/services/db/sql/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewWalletDB(db *sql.DB, tablePrefix string, createSchema bool) (driver.Wall
}

func (db *WalletDB) GetWalletID(identity token.Identity, roleID int) (driver.WalletID, error) {
idHash := identity.Hash()
idHash := identity.UniqueID()
result, err := QueryUnique[driver.WalletID](db.db,
fmt.Sprintf("SELECT wallet_id FROM %s WHERE identity_hash=$1 AND role_id=$2", db.table.Wallets),
idHash, roleID,
Expand Down Expand Up @@ -91,7 +91,7 @@ func (db *WalletDB) StoreIdentity(identity token.Identity, eID string, wID drive
query := fmt.Sprintf("INSERT INTO %s (identity_hash, meta, wallet_id, role_id, created_at, enrollment_id) VALUES ($1, $2, $3, $4, $5, $6)", db.table.Wallets)
logger.Debug(query)

idHash := identity.Hash()
idHash := identity.UniqueID()
_, err := db.db.Exec(query, idHash, meta, wID, roleID, time.Now().UTC(), eID)
if err != nil {
return errors.Wrapf(err, "failed storing wallet [%v] for identity [%v]", wID, idHash)
Expand All @@ -101,7 +101,7 @@ func (db *WalletDB) StoreIdentity(identity token.Identity, eID string, wID drive
}

func (db *WalletDB) LoadMeta(identity token.Identity, wID driver.WalletID, roleID int) ([]byte, error) {
idHash := identity.Hash()
idHash := identity.UniqueID()
result, err := QueryUnique[[]byte](db.db,
fmt.Sprintf("SELECT meta FROM %s WHERE identity_hash=$1 AND wallet_id=$2 AND role_id=$3", db.table.Wallets),
idHash, wID, roleID,
Expand All @@ -114,7 +114,7 @@ func (db *WalletDB) LoadMeta(identity token.Identity, wID driver.WalletID, roleI
}

func (db *WalletDB) IdentityExists(identity token.Identity, wID driver.WalletID, roleID int) bool {
idHash := identity.Hash()
idHash := identity.UniqueID()
result, err := QueryUnique[driver.WalletID](db.db,
fmt.Sprintf("SELECT wallet_id FROM %s WHERE identity_hash=$1 AND wallet_id=$2 AND role_id=$3", db.table.Wallets),
idHash, wID, roleID,
Expand All @@ -131,9 +131,9 @@ func (db *WalletDB) GetSchema() string {
return fmt.Sprintf(`
-- Wallets
CREATE TABLE IF NOT EXISTS %s (
identity_hash BYTEA NOT NULL,
identity_hash TEXT NOT NULL,
wallet_id TEXT NOT NULL,
meta TEXT DEFAULT '',
meta BYTEA,
role_id INT NOT NULL,
enrollment_id TEXT NOT NULL,
created_at TIMESTAMP,
Expand Down
2 changes: 1 addition & 1 deletion token/services/db/sql/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestWalletPostgres(t *testing.T) {
defer terminate()

for _, c := range WalletCases {
db, err := initWalletDB("postgres", pgConnStr, c.Name, 10)
db, err := initWalletDB("pgx", pgConnStr, c.Name, 10)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions token/services/selector/sherdlock/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func startManagers(t *testing.T, number int, backoff time.Duration) ([]testutils
}

func createManager(pgConnStr string, backoff time.Duration) (testutils.EnhancedManager, error) {
lockDB, err := initDB("postgres", pgConnStr, "test", 10, sql.NewTokenLockDB)
lockDB, err := initDB("pgx", pgConnStr, "test", 10, sql.NewTokenLockDB)
if err != nil {
return nil, err
}

tokenDB, err := initDB("postgres", pgConnStr, "test", 10, sql.NewTokenDB)
tokenDB, err := initDB("pgx", pgConnStr, "test", 10, sql.NewTokenDB)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion token/services/tokendb/db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
dbdriver "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
sqldb "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/tokendb/db/sql"
_ "modernc.org/sqlite"
)

func NewDriver() db.NamedDriver[dbdriver.TokenDBDriver] {
Expand Down
1 change: 0 additions & 1 deletion token/services/tokenlockdb/db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
dbdriver "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
sqldb "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/tokenlockdb/db/sql"
_ "modernc.org/sqlite"
)

func NewDriver() db.NamedDriver[dbdriver.TokenLockDBDriver] {
Expand Down
1 change: 0 additions & 1 deletion token/services/ttxdb/db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hyperledger-labs/fabric-token-sdk/token/services/db/driver"
sqldb "github.com/hyperledger-labs/fabric-token-sdk/token/services/db/sql"
"github.com/hyperledger-labs/fabric-token-sdk/token/services/ttxdb/db/sql"
_ "modernc.org/sqlite"
)

func NewDriver() db.NamedDriver[driver.TTXDBDriver] {
Expand Down

0 comments on commit 8f3f4f4

Please sign in to comment.