Skip to content

Commit

Permalink
Merge pull request #29 from 0xPolygon/type-refactor
Browse files Browse the repository at this point in the history
Minor type refactor
christophercampbell authored Oct 23, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 3488606 + 7e20cf1 commit a06b523
Showing 14 changed files with 50 additions and 54 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ package client
import (
"context"

"github.com/0xPolygon/cdk-data-availability/sequence"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
)

@@ -15,7 +15,7 @@ type ClientFactoryInterface interface {
// ClientInterface is the interface that defines the implementation of all the endpoints
type ClientInterface interface {
GetOffChainData(ctx context.Context, hash common.Hash) ([]byte, error)
SignSequence(signedSequence sequence.SignedSequence) ([]byte, error)
SignSequence(signedSequence types.SignedSequence) ([]byte, error)
}

// ClientFactory is the implementation of the data committee client factory
4 changes: 2 additions & 2 deletions client/datacom.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,12 @@ import (
"fmt"

"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/sequence"
"github.com/0xPolygon/cdk-data-availability/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) {
func (c *Client) SignSequence(signedSequence types.SignedSequence) ([]byte, error) {
response, err := rpc.JSONRPCCall(c.url, "datacom_signSequence", signedSequence)
if err != nil {
return nil, err
4 changes: 2 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"context"
"errors"

"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
@@ -34,7 +34,7 @@ func (db *DB) BeginStateTransaction(ctx context.Context) (pgx.Tx, error) {
}

// StoreOffChainData stores and array of key values in the Db
func (db *DB) StoreOffChainData(ctx context.Context, od []offchaindata.OffChainData, dbTx pgx.Tx) error {
func (db *DB) StoreOffChainData(ctx context.Context, od []types.OffChainData, dbTx pgx.Tx) error {
const storeOffChainDataSQL = `
INSERT INTO data_node.offchain_data (key, value)
VALUES ($1, $2)
4 changes: 2 additions & 2 deletions services/datacom/datacom.go
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ import (
"crypto/ecdsa"

"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/sequence"
"github.com/0xPolygon/cdk-data-availability/synchronizer"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/jackc/pgx/v4"
)

@@ -35,7 +35,7 @@ func NewDataComEndpoints(
// SignSequence generates the accumulated input hash aka accInputHash of the sequence and sign it.
// After storing the data that will be sent hashed to the contract, it returns the signature.
// This endpoint is only accessible to the sequencer
func (d *DataComEndpoints) SignSequence(signedSequence sequence.SignedSequence) (interface{}, rpc.Error) {
func (d *DataComEndpoints) SignSequence(signedSequence types.SignedSequence) (interface{}, rpc.Error) {
// Verify that the request comes from the sequencer
sender, err := signedSequence.Signer()
if err != nil {
4 changes: 2 additions & 2 deletions services/datacom/interfaces.go
Original file line number Diff line number Diff line change
@@ -3,12 +3,12 @@ package datacom
import (
"context"

"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/jackc/pgx/v4"
)

// DBInterface is the interface needed by the datacom service
type DBInterface interface {
BeginStateTransaction(ctx context.Context) (pgx.Tx, error)
StoreOffChainData(ctx context.Context, od []offchaindata.OffChainData, dbTx pgx.Tx) error
StoreOffChainData(ctx context.Context, od []types.OffChainData, dbTx pgx.Tx) error
}
12 changes: 6 additions & 6 deletions synchronizer/batches.go
Original file line number Diff line number Diff line change
@@ -11,8 +11,8 @@ import (
"github.com/0xPolygon/cdk-data-availability/etherman"
"github.com/0xPolygon/cdk-data-availability/etherman/smartcontracts/cdkvalidium"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
@@ -211,10 +211,10 @@ func (bs *BatchSynchronizer) handleEvent(event *cdkvalidium.CdkvalidiumSequenceB
return nil
}

var data []offchaindata.OffChainData
var data []types.OffChainData
for _, key := range missing {
log.Infof("resolving missing key %v", key.Hex())
var value offchaindata.OffChainData
var value types.OffChainData
value, err = bs.resolve(key)
if err != nil {
return err
@@ -226,14 +226,14 @@ func (bs *BatchSynchronizer) handleEvent(event *cdkvalidium.CdkvalidiumSequenceB
return store(bs.db, data)
}

func (bs *BatchSynchronizer) resolve(key common.Hash) (offchaindata.OffChainData, error) {
func (bs *BatchSynchronizer) resolve(key common.Hash) (types.OffChainData, error) {
log.Debugf("resolving missing data for key %v", key.Hex())
if len(bs.committee) == 0 {
// committee is resolved again once all members are evicted. They can be evicted
// for not having data, or their config being malformed
err := bs.resolveCommittee()
if err != nil {
return offchaindata.OffChainData{}, err
return types.OffChainData{}, err
}
}
// pull out the members, iterating will change the map on error
@@ -257,5 +257,5 @@ func (bs *BatchSynchronizer) resolve(key common.Hash) (offchaindata.OffChainData
}
return value, nil
}
return offchaindata.OffChainData{}, rpc.NewRPCError(rpc.NotFoundErrorCode, "no data found for key %v", key)
return types.OffChainData{}, rpc.NewRPCError(rpc.NotFoundErrorCode, "no data found for key %v", key)
}
6 changes: 3 additions & 3 deletions synchronizer/rpc.go
Original file line number Diff line number Diff line change
@@ -7,21 +7,21 @@ import (
"github.com/0xPolygon/cdk-data-availability/client"
"github.com/0xPolygon/cdk-data-availability/etherman"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
)

const rpcTimeout = 3 * time.Second

func resolveWithMember(key common.Hash, member etherman.DataCommitteeMember) (offchaindata.OffChainData, error) {
func resolveWithMember(key common.Hash, member etherman.DataCommitteeMember) (types.OffChainData, error) {
cm := client.New(member.URL)
ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
defer cancel()

log.Debugf("trying member %v at %v for key %v", member.Addr.Hex(), member.URL, key.Hex())

bytes, err := cm.GetOffChainData(ctx, key)
return offchaindata.OffChainData{
return types.OffChainData{
Key: key,
Value: bytes,
}, err
4 changes: 2 additions & 2 deletions synchronizer/store.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (

"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
"github.com/jackc/pgx/v4"
)
@@ -55,7 +55,7 @@ func exists(db *db.DB, key common.Hash) bool {
return db.Exists(ctx, key)
}

func store(db *db.DB, data []offchaindata.OffChainData) error {
func store(db *db.DB, data []types.OffChainData) error {
ctx, cancel := context.WithTimeout(context.Background(), dbTimeout)
defer cancel()
var (
21 changes: 10 additions & 11 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -6,11 +6,10 @@ import (
"errors"
"testing"

"github.com/0xPolygon/cdk-data-availability/batch"
"github.com/0xPolygon/cdk-data-availability/client"
"github.com/0xPolygon/cdk-data-availability/config"
cfgTypes "github.com/0xPolygon/cdk-data-availability/config/types"
"github.com/0xPolygon/cdk-data-availability/sequence"
"github.com/0xPolygon/cdk-data-availability/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
@@ -40,11 +39,11 @@ func TestSignSequence(t *testing.T) {
tc, pk := initTest(t)
type testSequences struct {
name string
sequence sequence.SignedSequence
sequence types.SignedSequence
expectedErr error
}
expectedSequence := sequence.Sequence{
Batches: []batch.Batch{
expectedSequence := types.Sequence{
Batches: []types.Batch{
{
Number: 3,
GlobalExitRoot: common.HexToHash("0x678343456734678"),
@@ -69,28 +68,28 @@ func TestSignSequence(t *testing.T) {
tSequences := []testSequences{
{
name: "invalid_signature",
sequence: sequence.SignedSequence{
Sequence: sequence.Sequence{},
sequence: types.SignedSequence{
Sequence: types.Sequence{},
Signature: common.Hex2Bytes("f00"),
},
expectedErr: errors.New("-32000 failed to verify sender"),
},
{
name: "signature_not_from_sender",
sequence: sequence.SignedSequence{
sequence: types.SignedSequence{
Sequence: expectedSequence,
Signature: unexpectedSenderSignedSequence.Signature,
},
expectedErr: errors.New("-32000 unauthorized"),
},
{
name: "empty_batch",
sequence: sequence.SignedSequence{},
sequence: types.SignedSequence{},
expectedErr: nil,
},
{
name: "success",
sequence: sequence.SignedSequence{
sequence: types.SignedSequence{
Sequence: expectedSequence,
Signature: nil,
},
@@ -121,7 +120,7 @@ func newTestClient(url string, addr common.Address) *testClient {
}
}

func (tc *testClient) signSequence(t *testing.T, expected *sequence.SignedSequence, expectedErr error) {
func (tc *testClient) signSequence(t *testing.T, expected *types.SignedSequence, expectedErr error) {
if signature, err := tc.client.SignSequence(*expected); err != nil {
assert.Equal(t, expectedErr.Error(), err.Error())
} else {
2 changes: 1 addition & 1 deletion batch/batch.go → types/batch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package batch
package types

import (
"crypto/ecdsa"
8 changes: 4 additions & 4 deletions batch/batch_test.go → types/batch_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package batch
package types

import (
"crypto/ecdsa"
@@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

var testCases = []Batch{
var testBatchCases = []Batch{
{
L2Data: common.Hex2Bytes(""),
GlobalExitRoot: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
@@ -44,14 +44,14 @@ var testCases = []Batch{
},
}

func TestSigning(t *testing.T) {
func TestBatchSigning(t *testing.T) {
privKeys := []*ecdsa.PrivateKey{}
for i := 0; i < 5; i++ {
pk, err := crypto.GenerateKey()
require.NoError(t, err)
privKeys = append(privKeys, pk)
}
for _, c := range testCases {
for _, c := range testBatchCases {
for _, pk := range privKeys {
signedBatch, err := c.Sign(pk)
require.NoError(t, err)
2 changes: 1 addition & 1 deletion offchaindata/offchaindata.go → types/offchaindata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package offchaindata
package types

import "github.com/ethereum/go-ethereum/common"

14 changes: 6 additions & 8 deletions sequence/sequence.go → types/sequence.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package sequence
package types

import (
"crypto/ecdsa"
"errors"
"math/big"
"strings"

"github.com/0xPolygon/cdk-data-availability/batch"
"github.com/0xPolygon/cdk-data-availability/offchaindata"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@@ -21,8 +19,8 @@ const (
// Sequence represents the data that the sequencer will send to L1
// and other metadata needed to build the accumulated input hash aka accInputHash
type Sequence struct {
Batches []batch.Batch `json:"batches"`
OldAccInputHash common.Hash `json:"oldAccInputhash"`
Batches []Batch `json:"batches"`
OldAccInputHash common.Hash `json:"oldAccInputhash"`
}

// HashToSign returns the accumulated input hash of the sequence.
@@ -88,10 +86,10 @@ func (s *Sequence) Sign(privateKey *ecdsa.PrivateKey) (*SignedSequence, error) {
}

// OffChainData returns the data that needs to be stored off chain from a given sequence
func (s *Sequence) OffChainData() []offchaindata.OffChainData {
od := []offchaindata.OffChainData{}
func (s *Sequence) OffChainData() []OffChainData {
od := []OffChainData{}
for _, b := range s.Batches {
od = append(od, offchaindata.OffChainData{
od = append(od, OffChainData{
Key: crypto.Keccak256Hash(b.L2Data),
Value: b.L2Data,
})
15 changes: 7 additions & 8 deletions sequence/sequence_test.go → types/sequence_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package sequence
package types

import (
"crypto/ecdsa"
"testing"

"github.com/0xPolygon/cdk-data-availability/batch"
"github.com/0xPolygon/cdk-data-availability/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type testCase struct {
type testSequenceCase struct {
s Sequence
expectedHash common.Hash
}

var testCases = []testCase{
var testSequenceCases = []testSequenceCase{
{
s: Sequence{
OldAccInputHash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Batches: []batch.Batch{
Batches: []Batch{
{
L2Data: common.Hex2Bytes(""),
GlobalExitRoot: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
@@ -41,7 +40,7 @@ var testCases = []testCase{
{
s: Sequence{
OldAccInputHash: common.HexToHash("0x2fc6a6ec18e61a98867a2536c306ca7fbdc4da2febd671d9de8fad50b56c6a16"),
Batches: []batch.Batch{
Batches: []Batch{
{
L2Data: common.Hex2Bytes("f905688080830f4240945904257b03bb7bf01a704a233090cc904e1c164580b905442cffd02e0000000000000000000000000000000000000000000000000000000000000000ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d3021ddb9a356815c3fac1026b6dec5df3124afbadb485c9ba5a3e3398a04b7ba85e58769b32a1beaf1ea27375a44095a0d1fb664ce2dd358e7fcbfb78c26a193440eb01ebfc9ed27500cd4dfc979272d1f0913cc9f66540d7e8005811109e1cf2d887c22bd8750d34016ac3c66b5ff102dacdd73f6b014e710b51e8022af9a1968ffd70157e48063fc33c97a050f7f640233bf646cc98d9524c6b92bcf3ab56f839867cc5f7f196b93bae1e27e6320742445d290f2263827498b54fec539f756afcefad4e508c098b9a7e1d8feb19955fb02ba9675585078710969d3440f5054e0f9dc3e7fe016e050eff260334f18a5d4fe391d82092319f5964f2e2eb7c1c3a5f8b13a49e282f609c317a833fb8d976d11517c571d1221a265d25af778ecf8923490c6ceeb450aecdc82e28293031d10c7d73bf85e57bf041a97360aa2c5d99cc1df82d9c4b87413eae2ef048f94b4d3554cea73d92b0f7af96e0271c691e2bb5c67add7c6caf302256adedf7ab114da0acfe870d449a3a489f781d659e8beccda7bce9f4e8618b6bd2f4132ce798cdc7a60e7e1460a7299e3c6342a579626d22733e50f526ec2fa19a22b31e8ed50f23cd1fdf94c9154ed3a7609a2f1ff981fe1d3b5c807b281e4683cc6d6315cf95b9ade8641defcb32372f1c126e398ef7a5a2dce0a8a7f68bb74560f8f71837c2c2ebbcbf7fffb42ae1896f13f7c7479a0b46a28b6f55540f89444f63de0378e3d121be09e06cc9ded1c20e65876d36aa0c65e9645644786b620e2dd2ad648ddfcbf4a7e5b1a3a4ecfe7f64667a3f0b7e2f4418588ed35a2458cffeb39b93d26f18d2ab13bdce6aee58e7b99359ec2dfd95a9c16dc00d6ef18b7933a6f8dc65ccb55667138776f7dea101070dc8796e3774df84f40ae0c8229d0d6069e5c8f39a7c299677a09d367fc7b05e3bc380ee652cdc72595f74c7b1043d0e1ffbab734648c838dfb0527d971b602bc216c9619ef0abf5ac974a1ed57f4050aa510dd9c74f508277b39d7973bb2dfccc5eeb0618db8cd74046ff337f0a7bf2c8e03e10f642c1886798d71806ab1e888d9e5ee87d0838c5655cb21c6cb83313b5a631175dff4963772cce9108188b34ac87c81c41e662ee4dd2dd7b2bc707961b1e646c4047669dcb6584f0d8d770daf5d7e7deb2e388ab20e2573d171a88108e79d820e98f26c0b84aa8b2f4aa4968dbb818ea32293237c50ba75ee485f4c22adf2f741400bdf8d6a9cc7df7ecae576221665d7358448818bb4ae4562849e949e17ac16e0be16688e156b5cf15e098c627c0056a9000000000000000000000000000000000000000000000000000000000000000095b76b43196f04d39cc922a7933240fa6cfc3fb1502ecfdf693e0ad078f668e100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000001f1ccb74c0e2e84e657f4c4b7f1f2cdc3d2114d5000000000000000000000000000000000000000000000000002386f26fc100000000000000000000000000000000000000000000000000000000000000000520000000000000000000000000000000000000000000000000000000000000000082084a80801ca5229fb6abcf5febe08c28268626cd1aa9adce9069410bbdf5cb958d52f23a15077324cb889bf4e37063540fcf4d1b9c7f523d54f2182fdc217de4e7a82b491b"),
GlobalExitRoot: common.HexToHash("0xbd6ad630a17775e238d412044d37868838687ba4c6346b917551f4f60def4a2a"),
@@ -67,7 +66,7 @@ var testCases = []testCase{
}

func TestHashToSign(t *testing.T) {
for _, c := range testCases {
for _, c := range testSequenceCases {
assert.Equal(
t, c.expectedHash.Hex(),
"0x"+common.Bytes2Hex(c.s.HashToSign()),
@@ -82,7 +81,7 @@ func TestSigning(t *testing.T) {
require.NoError(t, err)
privKeys = append(privKeys, pk)
}
for _, c := range testCases {
for _, c := range testSequenceCases {
for _, pk := range privKeys {
signedSequence, err := c.s.Sign(pk)
require.NoError(t, err)

0 comments on commit a06b523

Please sign in to comment.