Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/hashicorp/consul/sdk v0.16.1
github.com/hyperledger/fabric v1.4.0-rc1.0.20250510200036-435a7f1a780a
github.com/hyperledger/fabric-chaincode-go/v2 v2.3.0
github.com/hyperledger/fabric-contract-api-go/v2 v2.2.0
Expand Down Expand Up @@ -101,7 +100,7 @@ require (
github.com/consensys/bavard v0.1.22 // indirect
github.com/consensys/gnark-crypto v0.14.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/errdefs v1.0.0
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4Zs
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/hashicorp/consul/sdk v0.16.1 h1:V8TxTnImoPD5cj0U9Spl0TUxcytjcbbJeADFF07KdHg=
github.com/hashicorp/consul/sdk v0.16.1/go.mod h1:fSXvwxB2hmh1FMZCNl6PwX0Q/1wdWtHJcZ7Ea5tns0s=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
4 changes: 2 additions & 2 deletions integration/fabric/atsa/atsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ var _ = Describe("EndToEnd", func() {
"approvers": 2,
},
SQLConfigs: map[string]*postgres.ContainerConfig{
"alice": postgres.DefaultConfig("alice-db"),
"bob": postgres.DefaultConfig("bob-db"),
"alice": postgres.DefaultConfig(postgres.WithDBName("alice-db")),
"bob": postgres.DefaultConfig(postgres.WithDBName("bob-db")),
},
})

Expand Down
4 changes: 2 additions & 2 deletions integration/fabric/iou/iou_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var _ = Describe("EndToEnd", func() {
"lender": 2,
},
SQLConfigs: map[string]*postgres.ContainerConfig{
"borrower": postgres.DefaultConfig("borrower-db"),
"lender": postgres.DefaultConfig("lender-db"),
"borrower": postgres.DefaultConfig(postgres.WithDBName("borrower-db")),
"lender": postgres.DefaultConfig(postgres.WithDBName("lender-db")),
},
},
true,
Expand Down
4 changes: 2 additions & 2 deletions integration/fabric/iouhsm/iou_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var _ = Describe("EndToEnd", func() {
"lender": 2,
},
SQLConfigs: map[string]*postgres.ContainerConfig{
"borrower": postgres.DefaultConfig("borrower-db"),
"lender": postgres.DefaultConfig("lender-db"),
"borrower": postgres.DefaultConfig(postgres.WithDBName("borrower-db")),
"lender": postgres.DefaultConfig(postgres.WithDBName("lender-db")),
},
})
BeforeEach(s.Setup)
Expand Down
9 changes: 6 additions & 3 deletions integration/nwo/fabric/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/node"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/sql/postgres"
)

var (
Expand Down Expand Up @@ -184,11 +183,15 @@ func NewTopologyWithName(name string) *topology.Topology {
}
}

func WithDefaultPostgresPersistence(config postgres.DataSourceProvider) node.Option {
type DataSourceProvider interface {
DataSource() string
}

func WithDefaultPostgresPersistence(config DataSourceProvider) node.Option {
return WithPostgresPersistence(common.DefaultPersistence, config)
}

func WithPostgresPersistence(name driver.PersistenceName, config postgres.DataSourceProvider) node.Option {
func WithPostgresPersistence(name driver.PersistenceName, config DataSourceProvider) node.Option {
return func(o *node.Options) error {
if config != nil {
o.PutPostgresPersistence(name, node.SQLOpts{
Expand Down
81 changes: 73 additions & 8 deletions integration/nwo/fsc/fsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package fsc
import (
"bytes"
"cmp"
"context"
"fmt"
"io"
"net"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/monitoring/otlp"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/grpc"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver"
common2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/storage/driver/common"
Expand Down Expand Up @@ -138,6 +138,8 @@ func (p *Platform) GenerateArtifacts() {

p.GenerateResolverMap()

p.generatePostgresConfiguration()

// Generate core.yaml for all fsc nodes by including all the additional configurations coming
// from other platforms
for _, peer := range p.Peers {
Expand Down Expand Up @@ -172,6 +174,34 @@ func (p *Platform) GenerateArtifacts() {
}
}

// generatePostgresConfiguration allocates network ports for the postgres databases spawned by NWO.
// It finds all postgres databases via their unique db names and assigns a free network port to it,
// then it updates each fsc node configuration with the updated port details.
func (p *Platform) generatePostgresConfiguration() {
dbPorts := make(map[string]string)

// search all unique postgres instances available in our topology
for _, peer := range p.Peers {
for n, pp := range peer.Options.GetPostgresPersistences() {
cfg, err := postgres2.ConfigFromDataSource(pp.DataSource)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// if we have seen this db already, we update the node's configuration; otherwise reserve a free port
port, ok := dbPorts[cfg.DBName]
if !ok {
port = strconv.Itoa(int(p.Context.ReservePort()))
dbPorts[cfg.DBName] = port
}

// update the port in the node options,
// so we store it below in the core.yaml
cfg.Port = port
pp.DataSource = cfg.DataSource()
peer.Options.PutPostgresPersistence(n, *pp)
}
}
}

func (p *Platform) setIdentities(address string, peer *node2.Replica) {
cc := &grpc.ConnectionConfig{
Address: address,
Expand Down Expand Up @@ -212,22 +242,57 @@ func (p *Platform) Members() []grouper.Member {
}

func (p *Platform) PreRun() {
// Start DBs
configs := map[string]*postgres2.ContainerConfig{}
startPostgres(p)
}

// startPostgres spawns a Postgres container for each database as specified via the platform topology.
// It sets Platform.cleanDB function to allow shutdown of all spawned databases. If an error occurs, we panic
func startPostgres(p *Platform) {
// find all postgres databases we need to start via node configurations
configs := make(map[string]*postgres2.ContainerConfig)
for _, node := range p.Peers {
for _, sqlOpts := range node.Options.GetPostgresPersistences() {
if _, ok := configs[sqlOpts.DataSource]; !ok {
c, err := postgres2.ReadDataSource(sqlOpts.DataSource)
c, err := postgres2.ConfigFromDataSource(sqlOpts.DataSource)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
configs[sqlOpts.DataSource] = c
}

}
}

logger.Infof("Starting DBs for following data sources: [%s]...", logging.Keys(configs))
closeF, err := postgres2.StartPostgresWithFmt(collections.Values(configs))
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to start dbs")
p.cleanDB = closeF

closeFuncs := make([]func(), 0, len(configs))

// start all postgres instances
for _, c := range configs {
// get a logger for our postgres db
l := logging.MustGetLogger(c.DBName)

// we ignore the returned connection str
closeFunc, _, err := postgres2.StartPostgres(context.TODO(), c, l)
if err != nil {
// close all started instances
for _, f := range closeFuncs {
if f != nil {
f()
}
}
// we cannot start the database, thus no way to move on, time for panic! :P
gomega.Expect(err).ToNot(gomega.HaveOccurred())
}

closeFuncs = append(closeFuncs, closeFunc)
}

// merge all close functions
p.cleanDB = func() {
for _, f := range closeFuncs {
if f != nil {
f()
}
}
}
}

func (p *Platform) PostRun(bool) {
Expand Down
7 changes: 4 additions & 3 deletions platform/fabric/services/storage/vault/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package vault

import (
"context"
"fmt"
"path"

Expand All @@ -33,14 +34,14 @@ func OpenSqliteVault(key, tempDir string) (driver.VaultStore, error) {
}

func OpenPostgresVault(name string) (driver.VaultStore, func(), error) {
postgresConfig := postgres3.DefaultConfig(fmt.Sprintf("%s-db", name))
terminate, err := postgres3.StartPostgresWithFmt([]*postgres3.ContainerConfig{postgresConfig})
cfg := postgres3.DefaultConfig(postgres3.WithDBName(fmt.Sprintf("%s-db", name)))
terminate, pgConnStr, err := postgres3.StartPostgres(context.TODO(), cfg, nil)
if err != nil {
return nil, nil, err
}

cp := postgres3.NewConfigProvider(testing.MockConfig(postgres3.Config{
DataSource: postgresConfig.DataSource(),
DataSource: pgConnStr,
MaxOpenConns: 50,
}))
persistence, err := postgres2.NewPersistenceWithOpts(cp, postgres3.NewDbProvider(), "", postgres2.NewVaultStore)
Expand Down
32 changes: 5 additions & 27 deletions platform/view/services/storage/driver/sql/postgres/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import (
)

func BenchmarkReadExistingPostgres(b *testing.B) {
terminate, pgConnStr, err := StartPostgres(b, false)
if err != nil {
b.Fatal(err)
}
defer terminate()
pgConnStr := setupDB(b)
cp := NewConfigProvider(testing2.MockConfig(Config{
DataSource: pgConnStr,
MaxOpenConns: 50,
Expand All @@ -35,12 +31,7 @@ func BenchmarkReadExistingPostgres(b *testing.B) {
}

func BenchmarkReadNonExistingPostgres(b *testing.B) {
terminate, pgConnStr, err := StartPostgres(b, false)
if err != nil {
b.Fatal(err)
}
defer terminate()

pgConnStr := setupDB(b)
cp := NewConfigProvider(testing2.MockConfig(Config{
DataSource: pgConnStr,
MaxOpenConns: 50,
Expand All @@ -55,12 +46,7 @@ func BenchmarkReadNonExistingPostgres(b *testing.B) {
}

func BenchmarkWriteOnePostgres(b *testing.B) {
terminate, pgConnStr, err := StartPostgres(b, false)
if err != nil {
b.Fatal(err)
}
defer terminate()

pgConnStr := setupDB(b)
cp := NewConfigProvider(testing2.MockConfig(Config{
DataSource: pgConnStr,
MaxOpenConns: 50,
Expand All @@ -75,11 +61,7 @@ func BenchmarkWriteOnePostgres(b *testing.B) {
}

func BenchmarkWriteManyPostgres(b *testing.B) {
terminate, pgConnStr, err := StartPostgres(b, false)
if err != nil {
b.Fatal(err)
}
defer terminate()
pgConnStr := setupDB(b)
cp := NewConfigProvider(testing2.MockConfig(Config{
DataSource: pgConnStr,
MaxOpenConns: 50,
Expand All @@ -94,11 +76,7 @@ func BenchmarkWriteManyPostgres(b *testing.B) {
}

func BenchmarkWriteManyPostgresWithIdle(b *testing.B) {
terminate, pgConnStr, err := StartPostgres(b, false)
if err != nil {
b.Fatal(err)
}
defer terminate()
pgConnStr := setupDB(b)
cp := NewConfigProvider(testing2.MockConfig(Config{
DataSource: pgConnStr,
MaxOpenConns: 50,
Expand Down
17 changes: 12 additions & 5 deletions platform/view/services/storage/driver/sql/postgres/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ import (
_ "modernc.org/sqlite"
)

func TestPostgres(t *testing.T) {
t.Log("starting postgres")
terminate, pgConnStr, err := StartPostgres(t, false)
func setupDB(tb testing.TB) string {
tb.Helper()

terminate, pgConnStr, err := StartPostgres(tb.Context(), ConfigFromEnv(), nil)
if err != nil {
t.Fatal(err)
tb.Fatal(err)
}
defer terminate()
tb.Cleanup(terminate)

return pgConnStr
}

func TestPostgres(t *testing.T) {
pgConnStr := setupDB(t)
t.Log("postgres ready")

cp := NewConfigProvider(testing2.MockConfig(Config{
Expand Down
Loading
Loading