Skip to content

Commit

Permalink
Add reuqests from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jan 22, 2024
1 parent 9d186be commit a03bdeb
Show file tree
Hide file tree
Showing 11 changed files with 1,308 additions and 1,254 deletions.
23 changes: 14 additions & 9 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,18 @@ func newEtherman(c config.Config, st *state.State) (*etherman.Client, error) {
}

func newDataAvailability(c config.Config, st *state.State, etherman *etherman.Client, isSequenceSender bool) (*dataavailability.DataAvailability, error) {
var trustedSequencerURL string
var err error
var (
trustedSequencerURL string
err error
)
if !c.IsTrustedSequencer {
if c.Synchronizer.TrustedSequencerURL != "" {
trustedSequencerURL = c.Synchronizer.TrustedSequencerURL
} else {
log.Debug("getting trusted sequencer URL from smc")
trustedSequencerURL, err = etherman.GetTrustedSequencerURL()
if err != nil {
log.Fatal("error getting trusted sequencer URI. Error: %v", err)
return nil, fmt.Errorf("error getting trusted sequencer URI. Error: %v", err)
}
}
log.Debug("trustedSequencerURL ", trustedSequencerURL)
Expand All @@ -320,34 +322,37 @@ func newDataAvailability(c config.Config, st *state.State, etherman *etherman.Cl
// Backend specific config
daProtocolName, err := etherman.GetDAProtocolName()
if err != nil {
log.Fatal("error getting data availability protocol name: %v", err)
return nil, fmt.Errorf("error getting data availability protocol name: %v", err)
}
var daBackend dataavailability.DABackender
switch daProtocolName {
case string(dataavailability.DataAvailabilityCommittee):
var pk *ecdsa.PrivateKey
var err error
var (
pk *ecdsa.PrivateKey
err error
)
if isSequenceSender {
_, pk, err = etherman.LoadAuthFromKeyStore(c.SequenceSender.PrivateKey.Path, c.SequenceSender.PrivateKey.Password)
if err != nil {
log.Fatal(err)
return nil, err
}
}
dacAddr, err := etherman.GetDAProtocolAddr()
if err != nil {
log.Fatal("error getting trusted sequencer URI. Error: %v", err)
return nil, fmt.Errorf("error getting trusted sequencer URI. Error: %v", err)
}

daBackend, err = datacommittee.New(
c.Etherman.URL,
dacAddr,
c.SequenceSender.L2Coinbase,
pk,
&dataCommitteeClient.Factory{},
)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unexpected / unsupported DA protocol: %s", daProtocolName)
}

return dataavailability.New(
Expand Down
18 changes: 9 additions & 9 deletions dataavailability/dataavailability.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

// DataAvailability implements an abstract data avaiolability integration
const unexpectedHashTemplate = "missmatch on transaction data for batch num %d. Expected hash %s, actual hash: %s"

// DataAvailability implements an abstract data availability integration
type DataAvailability struct {
isTrustedSequencer bool

Expand All @@ -26,13 +28,13 @@ type DataAvailability struct {

// New creates a DataAvailability instance
func New(
IsTrustedSequencer bool,
isTrustedSequencer bool,
backend DABackender,
state stateInterface,
zkEVMClient syncinterfaces.ZKEVMClientInterface,
) (*DataAvailability, error) {
da := &DataAvailability{
isTrustedSequencer: IsTrustedSequencer,
isTrustedSequencer: isTrustedSequencer,
backend: backend,
state: state,
zkEVMClient: zkEVMClient,
Expand Down Expand Up @@ -79,16 +81,16 @@ func (d *DataAvailability) GetBatchL2Data(batchNum uint64, expectedTransactionsH
log.Info("trying to get data from trusted sequencer")
data, err := d.getDataFromTrustedSequencer(batchNum, expectedTransactionsHash)
if err != nil {
log.Error(err)
log.Error("failed to get data from trusted sequencer: %w", err)
} else {
return data, nil
}
}

log.Info("trying to get data from the data availability backend")
data, err := d.backend.GetData(batchNum, expectedTransactionsHash)
data, err := d.backend.GetBatchL2Data(batchNum, expectedTransactionsHash)
if err != nil {
log.Error(err)
log.Error("failed to get data from the data availability backend: %w", err)
if d.isTrustedSequencer {
return nil, fmt.Errorf("data not found on the local DB nor on any data committee member")
} else {
Expand All @@ -100,10 +102,8 @@ func (d *DataAvailability) GetBatchL2Data(batchNum uint64, expectedTransactionsH
return transactionsData, nil
}

const unexpectedHashTemplate = "missmatch on transaction data for batch num %d. Expected hash %s, actual hash: %s"

func (d *DataAvailability) getDataFromTrustedSequencer(batchNum uint64, expectedTransactionsHash common.Hash) ([]byte, error) {
b, err := d.zkEVMClient.BatchByNumber(d.ctx, big.NewInt(int64(batchNum)))
b, err := d.zkEVMClient.BatchByNumber(d.ctx, new(big.Int).SetUint64(batchNum))
if err != nil {
return nil, fmt.Errorf("failed to get batch num %d from trusted sequencer: %w", batchNum, err)
}
Expand Down
31 changes: 17 additions & 14 deletions dataavailability/datacommittee/datacommittee.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"golang.org/x/net/context"
)

const unexpectedHashTemplate = "missmatch on transaction data for batch num %d. Expected hash %s, actual hash: %s"

// DataCommitteeMember represents a member of the Data Committee
type DataCommitteeMember struct {
Addr common.Address
Expand All @@ -36,7 +38,6 @@ type DataCommittee struct {
// DataCommitteeBackend implements the DAC integration
type DataCommitteeBackend struct {
dataCommitteeContract *polygondatacommittee.Polygondatacommittee
l2Coinbase common.Address
privKey *ecdsa.PrivateKey
dataCommitteeClientFactory client.IClientFactory

Expand All @@ -49,7 +50,6 @@ type DataCommitteeBackend struct {
func New(
l1RPCURL string,
dataCommitteeAddr common.Address,
l2Coinbase common.Address,
privKey *ecdsa.PrivateKey,
dataCommitteeClientFactory client.IClientFactory,
) (*DataCommitteeBackend, error) {
Expand All @@ -64,7 +64,6 @@ func New(
}
return &DataCommitteeBackend{
dataCommitteeContract: dataCommittee,
l2Coinbase: l2Coinbase,
privKey: privKey,
dataCommitteeClientFactory: dataCommitteeClientFactory,
ctx: context.Background(),
Expand All @@ -88,10 +87,8 @@ func (d *DataCommitteeBackend) Init() error {
return nil
}

const unexpectedHashTemplate = "missmatch on transaction data for batch num %d. Expected hash %s, actual hash: %s"

// GetData returns the data from the DAC. It checks that it matches with the expected hash
func (d *DataCommitteeBackend) GetData(batchNum uint64, hash common.Hash) ([]byte, error) {
// GetBatchL2Data returns the data from the DAC. It checks that it matches with the expected hash
func (d *DataCommitteeBackend) GetBatchL2Data(batchNum uint64, hash common.Hash) ([]byte, error) {
intialMember := d.selectedCommitteeMember
found := false
for !found && intialMember != -1 {
Expand Down Expand Up @@ -167,8 +164,10 @@ func (s *DataCommitteeBackend) PostSequence(ctx context.Context, batchesData [][

// Collect signatures
msgs := []signatureMsg{}
var collectedSignatures uint64
var failedToCollect uint64
var (
collectedSignatures uint64
failedToCollect uint64
)
for collectedSignatures < committee.RequiredSignatures {
msg := <-ch
if msg.err != nil {
Expand Down Expand Up @@ -226,10 +225,14 @@ func requestSignatureFromMember(ctx context.Context, signedSequence daTypes.Sign
}
}

func buildSignaturesAndAddrs(msgs signatureMsgs, members []DataCommitteeMember) []byte {
res := []byte{}
sort.Sort(msgs)
for _, msg := range msgs {
func buildSignaturesAndAddrs(sigs signatureMsgs, members []DataCommitteeMember) []byte {
const (
sigLen = 65
addrLen = 20
)
res := make([]byte, 0, len(sigs)*sigLen+len(members)*addrLen)
sort.Sort(sigs)
for _, msg := range sigs {
log.Debugf("adding signature %s from %s", common.Bytes2Hex(msg.signature), msg.addr.Hex())
res = append(res, msg.signature...)
}
Expand Down Expand Up @@ -273,11 +276,11 @@ func (d *DataCommitteeBackend) getCurrentDataCommittee() (*DataCommittee, error)

// getCurrentDataCommitteeMembers return the currently registered data committee members
func (d *DataCommitteeBackend) getCurrentDataCommitteeMembers() ([]DataCommitteeMember, error) {
members := []DataCommitteeMember{}
nMembers, err := d.dataCommitteeContract.GetAmountOfMembers(&bind.CallOpts{Pending: false})
if err != nil {
return nil, fmt.Errorf("error getting GetAmountOfMembers from L1 SC: %w", err)
}
members := make([]DataCommitteeMember, 0, nMembers.Int64())
for i := int64(0); i < nMembers.Int64(); i++ {
member, err := d.dataCommitteeContract.Members(&bind.CallOpts{Pending: false}, big.NewInt(i))
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions dataavailability/datacommittee/datacommittee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func TestUpdateDataCommitteeEvent(t *testing.T) {
// Set up testing environment
dac, ethBackend, auth, da := newTestingEnv()
dac, ethBackend, auth, da := newTestingEnv(t)

// Update the committee
requiredAmountOfSignatures := big.NewInt(2)
Expand Down Expand Up @@ -61,12 +61,13 @@ func init() {
}

// This function prepare the blockchain, the wallet with funds and deploy the smc
func newTestingEnv() (
func newTestingEnv(t *testing.T) (
dac *DataCommitteeBackend,
ethBackend *backends.SimulatedBackend,
auth *bind.TransactOpts,
da *polygondatacommittee.Polygondatacommittee,
) {
t.Helper()
privateKey, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
Expand All @@ -75,7 +76,7 @@ func newTestingEnv() (
if err != nil {
log.Fatal(err)
}
dac, ethBackend, da, err = newSimulatedDacman(auth)
dac, ethBackend, da, err = newSimulatedDacman(t, auth)
if err != nil {
log.Fatal(err)
}
Expand All @@ -84,12 +85,13 @@ func newTestingEnv() (

// NewSimulatedEtherman creates an etherman that uses a simulated blockchain. It's important to notice that the ChainID of the auth
// must be 1337. The address that holds the auth will have an initial balance of 10 ETH
func newSimulatedDacman(auth *bind.TransactOpts) (
func newSimulatedDacman(t *testing.T, auth *bind.TransactOpts) (
dacman *DataCommitteeBackend,
ethBackend *backends.SimulatedBackend,
da *polygondatacommittee.Polygondatacommittee,
err error,
) {
t.Helper()
if auth == nil {
// read only client
return &DataCommitteeBackend{}, nil, nil, nil
Expand Down
20 changes: 14 additions & 6 deletions dataavailability/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ type stateInterface interface {
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
}

// DABackender is the interface for a DA backend
type DABackender interface {
// Init initializes the DABackend
Init() error
// GetData retrieve the data of a batch from the DA backend. The returned data must be the pre-image of the hash
GetData(batchNum uint64, hash common.Hash) ([]byte, error)
// BatchDataProvider is used to retrieve batch data
type BatchDataProvider interface {
// GetBatchL2Data retrieve the data of a batch from the DA backend. The returned data must be the pre-image of the hash
GetBatchL2Data(batchNum uint64, hash common.Hash) ([]byte, error)
}

// SequenceSender is used to send provided sequence of batches
type SequenceSender interface {
// PostSequence sends the sequence data to the data availability backend, and returns the dataAvailabilityMessage
// as expected by the contract
PostSequence(ctx context.Context, batchesData [][]byte) ([]byte, error)
}
type DABackender interface {

Check failure on line 28 in dataavailability/interfaces.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type DABackender should have comment or be unexported (revive)
BatchDataProvider
SequenceSender
// Init initializes the DABackend
Init() error
}
Loading

0 comments on commit a03bdeb

Please sign in to comment.