Skip to content

Commit

Permalink
Merge pull request #27 from 0xPolygon/feature/remove-node-dependency
Browse files Browse the repository at this point in the history
Remove cdk-validium-node dependency
  • Loading branch information
arnaubennassar authored Oct 16, 2023
2 parents 762f2e9 + 109aa3e commit d1ba59b
Show file tree
Hide file tree
Showing 55 changed files with 10,548 additions and 1,065 deletions.
16 changes: 8 additions & 8 deletions batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package batch
import (
"crypto/ecdsa"

"github.com/0xPolygon/cdk-validium-node/jsonrpc/types"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)

// Batch represents a batch used for synchronization
type Batch struct {
Number types.ArgUint64 `json:"number"`
GlobalExitRoot common.Hash `json:"globalExitRoot"`
Timestamp types.ArgUint64 `json:"timestamp"`
Coinbase common.Address `json:"coinbase"`
L2Data types.ArgBytes `json:"batchL2Data"`
Number rpc.ArgUint64 `json:"number"`
GlobalExitRoot common.Hash `json:"globalExitRoot"`
Timestamp rpc.ArgUint64 `json:"timestamp"`
Coinbase common.Address `json:"coinbase"`
L2Data rpc.ArgBytes `json:"batchL2Data"`
}

// HashToSign returns a hash that uniquely identifies the batch
Expand Down Expand Up @@ -43,8 +43,8 @@ func (b *Batch) Sign(privateKey *ecdsa.PrivateKey) (*SignedBatch, error) {

// SignedBatch is a batch but signed
type SignedBatch struct {
Batch Batch `json:"batch"`
Signature types.ArgBytes `json:"signature"`
Batch Batch `json:"batch"`
Signature rpc.ArgBytes `json:"signature"`
}

// Signer returns the address of the signer
Expand Down
8 changes: 4 additions & 4 deletions batch/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/ecdsa"
"testing"

"github.com/0xPolygon/cdk-validium-node/jsonrpc/types"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
Expand All @@ -19,7 +19,7 @@ var testCases = []Batch{
Coinbase: common.HexToAddress("0x1f1CCb74C0e2e84E657f4C4b7f1F2CDC3D2114d5"),
},
{
L2Data: types.ArgBytes(common.Hex2Bytes("f9056880808301ba5a945904257b03bb7bf01a704a233090cc904e1c164580b905442cffd02e0000000000000000000000000000000000000000000000000000000000000000ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d3021ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85e58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a193440eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d887c22bd8750d34016ac3c66b5ff102dacdd73f6b014e710b51e8022af9a1968ffd70157e48063fc33c97a050f7f640233bf646cc98d9524c6b92bcf3ab56f839867cc5f7f196b93bae1e27e6320742445d290f2263827498b54fec539f756afcefad4e508c098b9a7e1d8feb19955fb02ba9675585078710969d3440f5054e0f9dc3e7fe016e050eff260334f18a5d4fe391d82092319f5964f2e2eb7c1c3a5f8b13a49e282f609c317a833fb8d976d11517c571d1221a265d25af778ecf8923490c6ceeb450aecdc82e28293031d10c7d73bf85e57bf041a97360aa2c5d99cc1df82d9c4b87413eae2ef048f94b4d3554cea73d92b0f7af96e0271c691e2bb5c67add7c6caf302256adedf7ab114da0acfe870d449a3a489f781d659e8beccda7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d22733e50f526ec2fa19a22b31e8ed50f23cd1fdf94c9154ed3a7609a2f1ff981fe1d3b5c807b281e4683cc6d6315cf95b9ade8641defcb32372f1c126e398ef7a5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0b46a28b6f55540f89444f63de0378e3d121be09e06cc9ded1c20e65876d36aa0c65e9645644786b620e2dd2ad648ddfcbf4a7e5b1a3a4ecfe7f64667a3f0b7e2f4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd95a9c16dc00d6ef18b7933a6f8dc65ccb55667138776f7dea101070dc8796e3774df84f40ae0c8229d0d6069e5c8f39a7c299677a09d367fc7b05e3bc380ee652cdc72595f74c7b1043d0e1ffbab734648c838dfb0527d971b602bc216c9619ef0abf5ac974a1ed57f4050aa510dd9c74f508277b39d7973bb2dfccc5eeb0618db8cd74046ff337f0a7bf2c8e03e10f642c1886798d71806ab1e888d9e5ee87d0838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e662ee4dd2dd7b2bc707961b1e646c4047669dcb6584f0d8d770daf5d7e7deb2e388ab20e2573d171a88108e79d820e98f26c0b84aa8b2f4aa4968dbb818ea32293237c50ba75ee485f4c22adf2f741400bdf8d6a9cc7df7ecae576221665d7358448818bb4ae4562849e949e17ac16e0be16688e156b5cf15e098c627c0056a9000000000000000000000000000000000000000000000000000000000000000095b76b43196f04d39cc922a7933240fa6cfc3fb1502ecfdf693e0ad078f668e100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001f1ccb74c0e2e84e657f4c4b7f1f2cdc3d2114d5000000000000000000000000000000000000000000000000002386f26fc100000000000000000000000000000000000000000000000000000000000000000520000000000000000000000000000000000000000000000000000000000000000082084a8080213c87d62b9620bccb33eed6e22861ce4f112ec89f40a66111bbec222bfb313e37dc77d691d1436916b3dd8519eb3e572dd2962c7317732c38443d86390f988d1c")),
L2Data: rpc.ArgBytes(common.Hex2Bytes("f9056880808301ba5a945904257b03bb7bf01a704a233090cc904e1c164580b905442cffd02e0000000000000000000000000000000000000000000000000000000000000000ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d3021ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85e58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a193440eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d887c22bd8750d34016ac3c66b5ff102dacdd73f6b014e710b51e8022af9a1968ffd70157e48063fc33c97a050f7f640233bf646cc98d9524c6b92bcf3ab56f839867cc5f7f196b93bae1e27e6320742445d290f2263827498b54fec539f756afcefad4e508c098b9a7e1d8feb19955fb02ba9675585078710969d3440f5054e0f9dc3e7fe016e050eff260334f18a5d4fe391d82092319f5964f2e2eb7c1c3a5f8b13a49e282f609c317a833fb8d976d11517c571d1221a265d25af778ecf8923490c6ceeb450aecdc82e28293031d10c7d73bf85e57bf041a97360aa2c5d99cc1df82d9c4b87413eae2ef048f94b4d3554cea73d92b0f7af96e0271c691e2bb5c67add7c6caf302256adedf7ab114da0acfe870d449a3a489f781d659e8beccda7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d22733e50f526ec2fa19a22b31e8ed50f23cd1fdf94c9154ed3a7609a2f1ff981fe1d3b5c807b281e4683cc6d6315cf95b9ade8641defcb32372f1c126e398ef7a5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0b46a28b6f55540f89444f63de0378e3d121be09e06cc9ded1c20e65876d36aa0c65e9645644786b620e2dd2ad648ddfcbf4a7e5b1a3a4ecfe7f64667a3f0b7e2f4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd95a9c16dc00d6ef18b7933a6f8dc65ccb55667138776f7dea101070dc8796e3774df84f40ae0c8229d0d6069e5c8f39a7c299677a09d367fc7b05e3bc380ee652cdc72595f74c7b1043d0e1ffbab734648c838dfb0527d971b602bc216c9619ef0abf5ac974a1ed57f4050aa510dd9c74f508277b39d7973bb2dfccc5eeb0618db8cd74046ff337f0a7bf2c8e03e10f642c1886798d71806ab1e888d9e5ee87d0838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e662ee4dd2dd7b2bc707961b1e646c4047669dcb6584f0d8d770daf5d7e7deb2e388ab20e2573d171a88108e79d820e98f26c0b84aa8b2f4aa4968dbb818ea32293237c50ba75ee485f4c22adf2f741400bdf8d6a9cc7df7ecae576221665d7358448818bb4ae4562849e949e17ac16e0be16688e156b5cf15e098c627c0056a9000000000000000000000000000000000000000000000000000000000000000095b76b43196f04d39cc922a7933240fa6cfc3fb1502ecfdf693e0ad078f668e100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001f1ccb74c0e2e84e657f4c4b7f1f2cdc3d2114d5000000000000000000000000000000000000000000000000002386f26fc100000000000000000000000000000000000000000000000000000000000000000520000000000000000000000000000000000000000000000000000000000000000082084a8080213c87d62b9620bccb33eed6e22861ce4f112ec89f40a66111bbec222bfb313e37dc77d691d1436916b3dd8519eb3e572dd2962c7317732c38443d86390f988d1c")),
GlobalExitRoot: common.HexToHash("0xbd6ad630a17775e238d412044d37868838687ba4c6346b917551f4f60def4a2a"),
Timestamp: 1685349728,
Coinbase: common.HexToAddress("0x1f1CCb74C0e2e84E657f4C4b7f1F2CDC3D2114d5"),
Expand All @@ -31,13 +31,13 @@ var testCases = []Batch{
Coinbase: common.HexToAddress("0x1f1CCb74C0e2e84E657f4C4b7f1F2CDC3D2114d5"),
},
{
L2Data: types.ArgBytes(common.Hex2Bytes("e1808082520894400f2117d43f95f63a8e06dcf813dd746c4b5aba808082084a8080a6c23693c74f1492b1b1b580d0333e980532dcd0c7407a8c2134e28065bc8ceb273fd0b759cf1194ab221bb1dcada4b6f43251ae9909621152c450b062b4a3431ce1018082520894400f2117d43f95f63a8e06dcf813dd746c4b5aba808082084a80801c15bf03602f2907d0ab1fc027b98104c7625a36008b53c93a44191fadc68c305d9d9d65fdc2a108c71cd586ca7d58ec6da1de735aef6a0434196f966f777b191c")),
L2Data: rpc.ArgBytes(common.Hex2Bytes("e1808082520894400f2117d43f95f63a8e06dcf813dd746c4b5aba808082084a8080a6c23693c74f1492b1b1b580d0333e980532dcd0c7407a8c2134e28065bc8ceb273fd0b759cf1194ab221bb1dcada4b6f43251ae9909621152c450b062b4a3431ce1018082520894400f2117d43f95f63a8e06dcf813dd746c4b5aba808082084a80801c15bf03602f2907d0ab1fc027b98104c7625a36008b53c93a44191fadc68c305d9d9d65fdc2a108c71cd586ca7d58ec6da1de735aef6a0434196f966f777b191c")),
GlobalExitRoot: common.HexToHash("0xbd6ad630a17775e238d412044d37868838687ba4c6346b917551f4f60def4a2a"),
Timestamp: 1685374947,
Coinbase: common.HexToAddress("0x1f1CCb74C0e2e84E657f4C4b7f1F2CDC3D2114d5"),
},
{
L2Data: types.ArgBytes(common.Hex2Bytes("e102808252089445be386709a8f3a056651bbb32752e0c3bebd8f0808082084a8080b429fc3ab2e3a947dd69921696779a6acf85abeb858b2e918ed047df27ed389c2407a7550ae4df61c05eb4f4cf2a410ff064ca73e3437074f7685d6f849799921c")),
L2Data: rpc.ArgBytes(common.Hex2Bytes("e102808252089445be386709a8f3a056651bbb32752e0c3bebd8f0808082084a8080b429fc3ab2e3a947dd69921696779a6acf85abeb858b2e918ed047df27ed389c2407a7550ae4df61c05eb4f4cf2a410ff064ca73e3437074f7685d6f849799921c")),
GlobalExitRoot: common.HexToHash("0xbd6ad630a17775e238d412044d37868838687ba4c6346b917551f4f60def4a2a"),
Timestamp: 1685375541,
Coinbase: common.HexToAddress("0x1f1CCb74C0e2e84E657f4C4b7f1F2CDC3D2114d5"),
Expand Down
7 changes: 3 additions & 4 deletions client/datacom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"encoding/json"
"fmt"

"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/sequence"
"github.com/0xPolygon/cdk-validium-node/jsonrpc/client"
"github.com/0xPolygon/cdk-validium-node/jsonrpc/types"
)

// SignSequence sends a request to sign the given sequence by the data committee member
// if successful returns the signature. The signature should be validated after using this method!
func (c *Client) SignSequence(signedSequence sequence.SignedSequence) ([]byte, error) {
response, err := client.JSONRPCCall(c.url, "datacom_signSequence", signedSequence)
response, err := rpc.JSONRPCCall(c.url, "datacom_signSequence", signedSequence)
if err != nil {
return nil, err
}
Expand All @@ -21,7 +20,7 @@ func (c *Client) SignSequence(signedSequence sequence.SignedSequence) ([]byte, e
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message)
}

var result types.ArgBytes
var result rpc.ArgBytes
err = json.Unmarshal(response.Result, &result)
if err != nil {
return nil, err
Expand Down
7 changes: 3 additions & 4 deletions client/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"encoding/json"
"fmt"

"github.com/0xPolygon/cdk-validium-node/jsonrpc/client"
"github.com/0xPolygon/cdk-validium-node/jsonrpc/types"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
)

// GetOffChainData returns data based on it's hash
func (c *Client) GetOffChainData(ctx context.Context, hash common.Hash) ([]byte, error) {
response, err := client.JSONRPCCall(c.url, "sync_getOffChainData", hash)
response, err := rpc.JSONRPCCall(c.url, "sync_getOffChainData", hash)
if err != nil {
return nil, err
}
Expand All @@ -21,7 +20,7 @@ func (c *Client) GetOffChainData(ctx context.Context, hash common.Hash) ([]byte,
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message)
}

var result types.ArgBytes
var result rpc.ArgBytes
err = json.Unmarshal(response.Result, &result)
if err != nil {
return nil, err
Expand Down
27 changes: 14 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
dataavailability "github.com/0xPolygon/cdk-data-availability"
"github.com/0xPolygon/cdk-data-availability/config"
"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/dummyinterfaces"
"github.com/0xPolygon/cdk-data-availability/etherman"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/services/datacom"
"github.com/0xPolygon/cdk-data-availability/services/sync"
"github.com/0xPolygon/cdk-data-availability/synchronizer"
dbConf "github.com/0xPolygon/cdk-validium-node/db"
"github.com/0xPolygon/cdk-validium-node/jsonrpc"
"github.com/0xPolygon/cdk-validium-node/log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -62,7 +61,7 @@ func start(cliCtx *cli.Context) error {
setupLog(c.Log)

// Prepare DB
pg, err := dbConf.NewSQLDB(c.DB)
pg, err := db.NewSQLDB(c.DB)
if err != nil {
log.Fatal(err)
}
Expand All @@ -76,12 +75,18 @@ func start(cliCtx *cli.Context) error {
if err != nil {
log.Fatal(err)
}
// Load EtherMan
etherman, err := etherman.New(c.L1)
if err != nil {
log.Fatal(err)
}

// derive address
selfAddr := crypto.PubkeyToAddress(pk.PublicKey)

var cancelFuncs []context.CancelFunc

sequencerTracker, err := synchronizer.NewSequencerTracker(c.L1)
sequencerTracker, err := synchronizer.NewSequencerTracker(c.L1, etherman)
if err != nil {
log.Fatal(err)
}
Expand All @@ -100,21 +105,17 @@ func start(cliCtx *cli.Context) error {

cancelFuncs = append(cancelFuncs, detector.Stop)

batchSynchronizer, err := synchronizer.NewBatchSynchronizer(c.L1, selfAddr, storage, detector.Subscribe())
batchSynchronizer, err := synchronizer.NewBatchSynchronizer(c.L1, selfAddr, storage, detector.Subscribe(), etherman)
if err != nil {
log.Fatal(err)
}
go batchSynchronizer.Start()
cancelFuncs = append(cancelFuncs, batchSynchronizer.Stop)

// Register services
server := jsonrpc.NewServer(
server := rpc.NewServer(
c.RPC,
0,
&dummyinterfaces.DummyPool{},
&dummyinterfaces.DummyState{},
&dummyinterfaces.DummyStorage{},
[]jsonrpc.Service{
[]rpc.Service{
{
Name: sync.APISYNC,
Service: sync.NewSyncEndpoints(storage),
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"path/filepath"
"strings"

"github.com/0xPolygon/cdk-validium-node/config/types"
"github.com/0xPolygon/cdk-validium-node/db"
"github.com/0xPolygon/cdk-validium-node/jsonrpc"
"github.com/0xPolygon/cdk-validium-node/log"
"github.com/0xPolygon/cdk-data-availability/config/types"
"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
Expand All @@ -26,7 +26,7 @@ type Config struct {
PrivateKey types.KeystoreFileConfig
DB db.Config
Log log.Config
RPC jsonrpc.Config
RPC rpc.Config
L1 L1Config
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/0xPolygon/cdk-validium-node/config/types"
"github.com/0xPolygon/cdk-data-availability/config/types"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
Expand Down
4 changes: 0 additions & 4 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ Port = 8444
ReadTimeout = "60s"
WriteTimeout = "60s"
MaxRequestsPerIPAndSecond = 500
SequencerNodeURI = ""
EnableL2SuggestedGasPricePolling = false
[RPC.WebSockets]
Enabled = false
`

// Default parses the default configuration values.
Expand Down
50 changes: 50 additions & 0 deletions config/types/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types

import (
"time"

"github.com/invopop/jsonschema"
)

// Duration is a wrapper type that parses time duration from text.
type Duration struct {
time.Duration `validate:"required"`
}

// MarshalJSON marshalls time duration into text.
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}

// MarshalText marshalls time duration into text.
func (d *Duration) MarshalText() ([]byte, error) {
return []byte(d.String()), nil
}

// UnmarshalText unmarshalls time duration from text.
func (d *Duration) UnmarshalText(data []byte) error {
duration, err := time.ParseDuration(string(data))
if err != nil {
return err
}
d.Duration = duration
return nil
}

// NewDuration returns Duration wrapper
func NewDuration(duration time.Duration) Duration {
return Duration{time.Duration(duration)}
}

// JSONSchema returns a custom schema to be used for the JSON Schema generation of this type
func (Duration) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Title: "Duration",
Description: "Duration expressed in units: [ns, us, ms, s, m, h, d]",
Examples: []interface{}{
"1m",
"300ms",
},
}
}
10 changes: 10 additions & 0 deletions config/types/keystore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

// KeystoreFileConfig has all the information needed to load a private key from a key store file
type KeystoreFileConfig struct {
// Path is the file path for the key store file
Path string `mapstructure:"Path"`

// Password is the password to decrypt the key store file
Password string `mapstructure:"Password"`
}
51 changes: 51 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package db

import (
"context"
"fmt"

"github.com/0xPolygon/cdk-data-availability/log"
"github.com/jackc/pgx/v4/pgxpool"
)

// Config provide fields to configure the pool
type Config struct {
// Database name
Name string `mapstructure:"Name"`

// Database User name
User string `mapstructure:"User"`

// Database Password of the user
Password string `mapstructure:"Password"`

// Host address of database
Host string `mapstructure:"Host"`

// Port Number of database
Port string `mapstructure:"Port"`

// EnableLog
EnableLog bool `mapstructure:"EnableLog"`

// MaxConns is the maximum number of connections in the pool.
MaxConns int `mapstructure:"MaxConns"`
}

// NewSQLDB creates a new SQL DB
func NewSQLDB(cfg Config) (*pgxpool.Pool, error) {
config, err := pgxpool.ParseConfig(fmt.Sprintf("postgres://%s:%s@%s:%s/%s?pool_max_conns=%d", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Name, cfg.MaxConns))
if err != nil {
log.Errorf("Unable to parse DB config: %v\n", err)
return nil, err
}
if cfg.EnableLog {
config.ConnConfig.Logger = logger{}
}
conn, err := pgxpool.ConnectConfig(context.Background(), config)
if err != nil {
log.Errorf("Unable to connect to database: %v\n", err)
return nil, err
}
return conn, nil
}
12 changes: 8 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"errors"

"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-validium-node/jsonrpc/types"
"github.com/0xPolygon/cdk-validium-node/state"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)

var (
// ErrStateNotSynchronized indicates the state database may be empty
ErrStateNotSynchronized = errors.New("state not synchronized")
)

// DB is the database layer of the data node
type DB struct {
pg *pgxpool.Pool
Expand Down Expand Up @@ -51,7 +55,7 @@ func (db *DB) StoreOffChainData(ctx context.Context, od []offchaindata.OffChainD
}

// GetOffChainData returns the value identified by the key
func (db *DB) GetOffChainData(ctx context.Context, key common.Hash, dbTx pgx.Tx) (types.ArgBytes, error) {
func (db *DB) GetOffChainData(ctx context.Context, key common.Hash, dbTx pgx.Tx) (rpc.ArgBytes, error) {
const getOffchainDataSQL = `
SELECT value
FROM data_node.offchain_data
Expand All @@ -63,7 +67,7 @@ func (db *DB) GetOffChainData(ctx context.Context, key common.Hash, dbTx pgx.Tx)

if err := dbTx.QueryRow(ctx, getOffchainDataSQL, key.Hex()).Scan(&hexValue); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, state.ErrStateNotSynchronized
return nil, ErrStateNotSynchronized
}
return nil, err
}
Expand Down
Loading

0 comments on commit d1ba59b

Please sign in to comment.