Skip to content

Commit

Permalink
test(validator): add repository integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
torives committed Aug 30, 2024
1 parent 479e994 commit e6a5880
Show file tree
Hide file tree
Showing 2 changed files with 429 additions and 127 deletions.
127 changes: 0 additions & 127 deletions internal/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,133 +47,6 @@ func (s *ValidatorSuite) TearDownSubTest() {
validator = nil
}

func (s *ValidatorSuite) TestItCreatesClaimAndProofs() {
// returns pristine claim and no proofs
s.Run("WhenThereAreNoOutputsAndNoPreviousEpoch", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

epoch := dummyEpochs[0]

repository.On(
"GetOutputsProducedInBlockRange",
mock.Anything, epoch.AppAddress, epoch.FirstBlock, epoch.LastBlock,
).Return(nil, nil)
repository.On("GetPreviousEpoch", mock.Anything, epoch).Return(nil, nil)

claim, outputs, err := validator.createClaimAndProofs(ctx, epoch)
s.Require().Nil(err)
s.Require().NotNil(claim)

expectedClaim, _, err := merkle.CreateProofs(nil, MAX_OUTPUT_TREE_HEIGHT)
s.Require().Nil(err)
s.Require().NotNil(expectedClaim)

s.Equal(expectedClaim, *claim)
s.Nil(outputs)
repository.AssertExpectations(s.T())
})

// returns previous epoch claim and no proofs
s.Run("WhenThereAreNoOutputsAndThereIsAPreviousEpoch", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

previousEpoch := dummyEpochs[0]
expectedClaim := randomHash()
previousEpoch.ClaimHash = &expectedClaim
epoch := dummyEpochs[1]

repository.On(
"GetOutputsProducedInBlockRange",
mock.Anything, epoch.AppAddress, epoch.FirstBlock, epoch.LastBlock,
).Return(nil, nil)
repository.On("GetPreviousEpoch", mock.Anything, epoch).Return(&previousEpoch, nil)

claim, outputs, err := validator.createClaimAndProofs(ctx, epoch)
s.Require().Nil(err)
s.Require().NotNil(claim)

s.Equal(expectedClaim, *claim)
s.Nil(outputs)
repository.AssertExpectations(s.T())
})

// returns new claim and proofs
s.Run("WhenThereAreOutputsAndNoPreviousEpoch", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

epoch := dummyEpochs[0]
outputs := randomOutputs(2, 0, false)

repository.On(
"GetOutputsProducedInBlockRange",
mock.Anything, epoch.AppAddress, epoch.FirstBlock, epoch.LastBlock,
).Return(outputs, nil).Once()
repository.On("GetPreviousEpoch", mock.Anything, epoch).Return(nil, nil)

claim, updatedOutputs, err := validator.createClaimAndProofs(ctx, epoch)
s.Require().Nil(err)
s.Require().NotNil(claim)

s.Len(updatedOutputs, len(outputs))
for idx, output := range updatedOutputs {
s.Equal(outputs[idx].Id, output.Id)
s.NotNil(output.Hash)
s.NotNil(output.OutputHashesSiblings)
}
repository.AssertExpectations(s.T())
})

// returns new claim and proofs
s.Run("WhenThereAreOutputsAndAPreviousEpoch", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

previousEpoch := dummyEpochs[0]
previousEpochClaim := randomHash()
previousEpoch.ClaimHash = &previousEpochClaim
epoch := dummyEpochs[1]
previousOutputs := randomOutputs(2, 0, true)
epochOutputs := randomOutputs(2, 2, false)

repository.On(
"GetOutputsProducedInBlockRange",
mock.Anything, epoch.AppAddress, epoch.FirstBlock, epoch.LastBlock,
).Return(epochOutputs, nil)
repository.On(
"GetOutputsProducedInBlockRange",
mock.Anything, epoch.AppAddress, inputBoxDeploymentBlock, previousEpoch.LastBlock,
).Return(previousOutputs, nil)
repository.On("GetPreviousEpoch", mock.Anything, epoch).Return(&previousEpoch, nil)

claim, updatedOutputs, err := validator.createClaimAndProofs(ctx, epoch)
s.Require().Nil(err)
s.Require().NotNil(claim)

allOutputs := append(previousOutputs, epochOutputs...)
leaves := make([]Hash, 0, len(allOutputs))
for _, output := range allOutputs {
leaves = append(leaves, *output.Hash)
}
expectedClaim, allProofs, err := merkle.CreateProofs(leaves, MAX_OUTPUT_TREE_HEIGHT)
s.Require().Nil(err)

s.NotEqual(previousEpoch.ClaimHash, claim)
s.Equal(&expectedClaim, claim)
s.Len(updatedOutputs, len(epochOutputs))

for idx, output := range updatedOutputs {
s.Equal(epochOutputs[idx].Index, output.Index)
s.NotNil(output.Hash)
s.NotNil(output.OutputHashesSiblings)
s.assertProofs(output, allProofs)
}
repository.AssertExpectations(s.T())
})
}

func (s *ValidatorSuite) TestItFailsWhenClaimDoesNotMatchMachineOutputsHash() {
s.Run("OneAppSingleEpoch", func() {
ctx, cancel := context.WithCancel(context.Background())
Expand Down
Loading

0 comments on commit e6a5880

Please sign in to comment.