Skip to content

Commit

Permalink
feat: Use ListOffChainData instead of GetOffChainData (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbpol authored Nov 7, 2024
1 parent d00229d commit d7d9947
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions dataavailability/datacommittee/datacommittee.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,16 @@ func (d *Backend) Init() error {

// GetSequence gets backend data one hash at a time. This should be optimized on the DAC side to get them all at once.
func (d *Backend) GetSequence(_ context.Context, hashes []common.Hash, _ []byte) ([][]byte, error) {
// TODO: optimize this on the DAC side by implementing a multi batch retrieve api)
batchData := make([][]byte, 0, len(hashes))
for _, h := range hashes {
data, err := d.GetBatchL2Data(h)
if err != nil {
return nil, err
}
batchData = append(batchData, data)
}

return batchData, nil
}

// GetBatchL2Data returns the data from the DAC. It checks that it matches with the expected hash
func (d *Backend) GetBatchL2Data(hash common.Hash) ([]byte, error) {
intialMember := d.selectedCommitteeMember
found := false

var found bool
for !found && intialMember != -1 {
member := d.committeeMembers[d.selectedCommitteeMember]
d.logger.Infof("trying to get data from %s at %s", member.Addr.Hex(), member.URL)

c := d.dataCommitteeClientFactory.New(member.URL)
data, err := c.GetOffChainData(d.ctx, hash)

dataMap, err := c.ListOffChainData(d.ctx, hashes)
if err != nil {
d.logger.Warnf(
"error getting data from DAC node %s at %s: %s",
Expand All @@ -141,25 +129,32 @@ func (d *Backend) GetBatchL2Data(hash common.Hash) ([]byte, error) {

continue
}
actualTransactionsHash := crypto.Keccak256Hash(data)
if actualTransactionsHash != hash {
unexpectedHash := fmt.Errorf(
unexpectedHashTemplate, hash, actualTransactionsHash,
)
d.logger.Warnf(
"error getting data from DAC node %s at %s: %s",
member.Addr.Hex(), member.URL, unexpectedHash,
)
d.selectedCommitteeMember = (d.selectedCommitteeMember + 1) % len(d.committeeMembers)
if d.selectedCommitteeMember == intialMember {
break

batchData := make([][]byte, 0, len(hashes))
for _, hash := range hashes {
actualTransactionsHash := crypto.Keccak256Hash(dataMap[hash])
if actualTransactionsHash != hash {
unexpectedHash := fmt.Errorf(
unexpectedHashTemplate, hash, actualTransactionsHash,
)
d.logger.Warnf(
"error getting data from DAC node %s at %s: %s",
member.Addr.Hex(), member.URL, unexpectedHash,
)
d.selectedCommitteeMember = (d.selectedCommitteeMember + 1) % len(d.committeeMembers)
if d.selectedCommitteeMember == intialMember {
break
}

continue
}

continue
batchData = append(batchData, dataMap[hash])
}

return data, nil
return batchData, nil
}

if err := d.Init(); err != nil {
return nil, fmt.Errorf("error loading data committee: %w", err)
}
Expand Down

0 comments on commit d7d9947

Please sign in to comment.