Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Support GenesisBatch in API endpoints (#391)
Browse files Browse the repository at this point in the history
* Return empty slice in GetCommitmentsByBatchID if not found

* Fix underflow in getSubmissionBlock for genesis batch

* Add tests for API methods that return genesis batch

* Fix underflow in API getBatches genesisBatch

* Set GenesisBatch submissionBlock correctly

* Reuse getSubmissionBlock

Co-authored-by: Michał Sieczkowski <[email protected]>
  • Loading branch information
b-tarczynski and Michał Sieczkowski authored Oct 14, 2021
1 parent 83335b3 commit 6b948db
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
12 changes: 8 additions & 4 deletions api/get_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (a *API) unsafeGetBatchByHash(hash common.Hash) (*dto.BatchWithRootAndCommi
if err != nil {
return nil, err
}
submissionBlock, err := a.getSubmissionBlock(*batch.FinalisationBlock)
submissionBlock, err := a.getSubmissionBlock(batch)
if err != nil {
return nil, err
}
Expand All @@ -51,7 +51,7 @@ func (a *API) unsafeGetBatchByID(id models.Uint256) (*dto.BatchWithRootAndCommit
if err != nil {
return nil, err
}
submissionBlock, err := a.getSubmissionBlock(*batch.FinalisationBlock)
submissionBlock, err := a.getSubmissionBlock(batch)
if err != nil {
return nil, err
}
Expand All @@ -74,10 +74,14 @@ func createBatchWithCommitments(
return dto.MakeBatchWithRootAndCommitments(batch, submissionBlock, commitments), nil
}

func (a *API) getSubmissionBlock(finalisationBlock uint32) (uint32, error) {
func (a *API) getSubmissionBlock(batch *models.Batch) (uint32, error) {
if batch.ID.IsZero() {
return *batch.FinalisationBlock, nil
}

blocks, err := a.client.GetBlocksToFinalise()
if err != nil {
return 0, err
}
return finalisationBlock - uint32(*blocks), nil
return *batch.FinalisationBlock - uint32(*blocks), nil
}
41 changes: 30 additions & 11 deletions api/get_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,23 @@ func (s *GetBatchTestSuite) TestGetBatchByHash() {
s.Equal(s.batch.SubmissionTime, result.SubmissionTime)
}

func (s *GetBatchTestSuite) TestGetBatchByHash_NoCommitments() {
s.addStateLeaf()
err := s.storage.AddBatch(&s.batch)
func (s *GetBatchTestSuite) TestGetBatchByHash_GenesisBatch() {
genesisBatch := models.Batch{
ID: models.MakeUint256(0),
Type: batchtype.Genesis,
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
}
err := s.storage.AddBatch(&genesisBatch)
s.NoError(err)

result, err := s.api.GetBatchByHash(*s.batch.Hash)
s.Equal(s.batchNotFoundAPIErr, err)
s.Nil(result)
result, err := s.api.GetBatchByHash(*genesisBatch.Hash)
s.NoError(err)
s.Equal(genesisBatch.Hash, result.Hash)
s.Equal(genesisBatch.Type, result.Type)
s.Equal(*genesisBatch.FinalisationBlock, result.SubmissionBlock)
s.Len(result.Commitments, 0)
}

func (s *GetBatchTestSuite) TestGetBatchByHash_NonExistentBatch() {
Expand Down Expand Up @@ -121,13 +130,23 @@ func (s *GetBatchTestSuite) TestGetBatchByID() {
s.Equal(s.batch.SubmissionTime, result.SubmissionTime)
}

func (s *GetBatchTestSuite) TestGetBatchByID_NoCommitments() {
err := s.storage.AddBatch(&s.batch)
func (s *GetBatchTestSuite) TestGetBatchByID_GenesisBatch() {
genesisBatch := models.Batch{
ID: models.MakeUint256(0),
Type: batchtype.Genesis,
TransactionHash: utils.RandomHash(),
Hash: utils.NewRandomHash(),
FinalisationBlock: ref.Uint32(10),
}
err := s.storage.AddBatch(&genesisBatch)
s.NoError(err)

result, err := s.api.GetBatchByID(models.MakeUint256(0))
s.Equal(s.batchNotFoundAPIErr, err)
s.Nil(result)
result, err := s.api.GetBatchByID(genesisBatch.ID)
s.NoError(err)
s.Equal(genesisBatch.Hash, result.Hash)
s.Equal(genesisBatch.Type, result.Type)
s.Equal(*genesisBatch.FinalisationBlock, result.SubmissionBlock)
s.Len(result.Commitments, 0)
}

func (s *GetBatchTestSuite) TestGetBatchByID_NonExistentBatch() {
Expand Down
10 changes: 4 additions & 6 deletions api/get_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ func (a *API) unsafeGetBatches(from, to *models.Uint256) ([]dto.Batch, error) {
return []dto.Batch{}, err
}

blocksToFinalise, err := a.client.GetBlocksToFinalise()
if err != nil {
return nil, err
}

batchesWithSubmission := make([]dto.Batch, 0, len(batches))
for i := range batches {
if batches[i].Hash == nil {
continue
}
submissionBlock := *batches[i].FinalisationBlock - uint32(*blocksToFinalise)
submissionBlock, err := a.getSubmissionBlock(&batches[i])
if err != nil {
return nil, err
}
batchesWithSubmission = append(batchesWithSubmission, *dto.MakeBatch(&batches[i], submissionBlock))
}
return batchesWithSubmission, nil
Expand Down
3 changes: 0 additions & 3 deletions storage/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func (s *Storage) GetCommitmentsByBatchID(batchID models.Uint256) ([]models.Comm
if err != nil && err != db.ErrIteratorFinished {
return nil, err
}
if len(commitments) == 0 {
return nil, errors.WithStack(NewNotFoundError("commitments"))
}

commitmentsWithToken := make([]models.CommitmentWithTokenID, 0, len(commitments))
for i := range commitments {
Expand Down
9 changes: 5 additions & 4 deletions storage/commitment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func (s *CommitmentTestSuite) TestGetCommitmentsByBatchID() {
func (s *CommitmentTestSuite) TestGetCommitmentsByBatchID_NonExistentCommitments() {
batchID := s.addRandomBatch()
commitments, err := s.storage.GetCommitmentsByBatchID(batchID)
s.ErrorIs(err, NewNotFoundError("commitments"))
s.Nil(commitments)
s.NoError(err)
s.Len(commitments, 0)
}

func (s *CommitmentTestSuite) TestDeleteCommitmentsByBatchIDs() {
Expand Down Expand Up @@ -173,8 +173,9 @@ func (s *CommitmentTestSuite) TestDeleteCommitmentsByBatchIDs() {
err := s.storage.DeleteCommitmentsByBatchIDs(batches[0].ID, batches[1].ID)
s.NoError(err)
for i := range batches {
_, err = s.storage.GetCommitmentsByBatchID(batches[i].ID)
s.ErrorIs(err, NewNotFoundError("commitments"))
commitments, err := s.storage.GetCommitmentsByBatchID(batches[i].ID)
s.NoError(err)
s.Len(commitments, 0)
}
}

Expand Down

0 comments on commit 6b948db

Please sign in to comment.