@ -2,8 +2,8 @@ package lite
import (
import (
"fmt"
"fmt"
"sync"
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
@ -49,7 +49,7 @@ func TestInquirerValidPath(t *testing.T) {
// This should fail validation:
// This should fail validation:
sh := fcz [ count - 1 ] . SignedHeader
sh := fcz [ count - 1 ] . SignedHeader
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
require . NotNil ( err )
require . NotNil ( err )
// Adding a few commits in the middle should be insufficient.
// Adding a few commits in the middle should be insufficient.
@ -57,7 +57,7 @@ func TestInquirerValidPath(t *testing.T) {
err := source . SaveFullCommit ( fcz [ i ] )
err := source . SaveFullCommit ( fcz [ i ] )
require . Nil ( err )
require . Nil ( err )
}
}
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
assert . NotNil ( err )
assert . NotNil ( err )
// With more info, we succeed.
// With more info, we succeed.
@ -65,7 +65,7 @@ func TestInquirerValidPath(t *testing.T) {
err := source . SaveFullCommit ( fcz [ i ] )
err := source . SaveFullCommit ( fcz [ i ] )
require . Nil ( err )
require . Nil ( err )
}
}
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
assert . Nil ( err , "%+v" , err )
assert . Nil ( err , "%+v" , err )
}
}
@ -115,18 +115,18 @@ func TestInquirerVerifyHistorical(t *testing.T) {
err = source . SaveFullCommit ( fcz [ 7 ] )
err = source . SaveFullCommit ( fcz [ 7 ] )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
sh := fcz [ 8 ] . SignedHeader
sh := fcz [ 8 ] . SignedHeader
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
assert . Equal ( fcz [ 7 ] . Height ( ) , cert . LastTrustedHeight ( ) )
assert . Equal ( fcz [ 7 ] . Height ( ) , cert . LastTrustedHeight ( ) )
fc_ , err := trust . LatestFullCommit ( chainID , fcz [ 8 ] . Height ( ) , fcz [ 8 ] . Height ( ) )
fc_ , err := trust . LatestFullCommit ( chainID , fcz [ 8 ] . Height ( ) , fcz [ 8 ] . Height ( ) )
require . NotNil ( err , "%+v" , err )
require . NotNil ( err , "%+v" , err )
assert . Equal ( fc_ , ( FullCommit { } ) )
assert . Equal ( fc_ , ( FullCommit { } ) )
// With fcz[9] Cert ify will update last trusted height.
// With fcz[9] Ver ify will update last trusted height.
err = source . SaveFullCommit ( fcz [ 9 ] )
err = source . SaveFullCommit ( fcz [ 9 ] )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
sh = fcz [ 8 ] . SignedHeader
sh = fcz [ 8 ] . SignedHeader
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
assert . Equal ( fcz [ 8 ] . Height ( ) , cert . LastTrustedHeight ( ) )
assert . Equal ( fcz [ 8 ] . Height ( ) , cert . LastTrustedHeight ( ) )
fc_ , err = trust . LatestFullCommit ( chainID , fcz [ 8 ] . Height ( ) , fcz [ 8 ] . Height ( ) )
fc_ , err = trust . LatestFullCommit ( chainID , fcz [ 8 ] . Height ( ) , fcz [ 8 ] . Height ( ) )
@ -141,13 +141,70 @@ func TestInquirerVerifyHistorical(t *testing.T) {
// Try to check an unknown seed in the past.
// Try to check an unknown seed in the past.
sh = fcz [ 3 ] . SignedHeader
sh = fcz [ 3 ] . SignedHeader
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
assert . Equal ( fcz [ 8 ] . Height ( ) , cert . LastTrustedHeight ( ) )
assert . Equal ( fcz [ 8 ] . Height ( ) , cert . LastTrustedHeight ( ) )
// Jump all the way forward again.
// Jump all the way forward again.
sh = fcz [ count - 1 ] . SignedHeader
sh = fcz [ count - 1 ] . SignedHeader
err = cert . Cert ify( sh )
err = cert . Ver ify( sh )
require . Nil ( err , "%+v" , err )
require . Nil ( err , "%+v" , err )
assert . Equal ( fcz [ 9 ] . Height ( ) , cert . LastTrustedHeight ( ) )
assert . Equal ( fcz [ 9 ] . Height ( ) , cert . LastTrustedHeight ( ) )
}
}
func TestConcurrencyInquirerVerify ( t * testing . T ) {
_ , require := assert . New ( t ) , require . New ( t )
trust := NewDBProvider ( "trust" , dbm . NewMemDB ( ) ) . SetLimit ( 10 )
source := NewDBProvider ( "source" , dbm . NewMemDB ( ) )
// Set up the validators to generate test blocks.
var vote int64 = 10
keys := genPrivKeys ( 5 )
nkeys := keys . Extend ( 1 )
// Construct a bunch of commits, each with one more height than the last.
chainID := "inquiry-test"
count := 10
consHash := [ ] byte ( "special-params" )
fcz := make ( [ ] FullCommit , count )
for i := 0 ; i < count ; i ++ {
vals := keys . ToValidators ( vote , 0 )
nextVals := nkeys . ToValidators ( vote , 0 )
h := int64 ( 1 + i )
appHash := [ ] byte ( fmt . Sprintf ( "h=%d" , h ) )
resHash := [ ] byte ( fmt . Sprintf ( "res=%d" , h ) )
fcz [ i ] = keys . GenFullCommit (
chainID , h , nil ,
vals , nextVals ,
appHash , consHash , resHash , 0 , len ( keys ) )
// Extend the keys by 1 each time.
keys = nkeys
nkeys = nkeys . Extend ( 1 )
}
// Initialize a Verifier with the initial state.
err := trust . SaveFullCommit ( fcz [ 0 ] )
require . Nil ( err )
cert := NewDynamicVerifier ( chainID , trust , source )
cert . SetLogger ( log . TestingLogger ( ) )
err = source . SaveFullCommit ( fcz [ 7 ] )
err = source . SaveFullCommit ( fcz [ 8 ] )
require . Nil ( err , "%+v" , err )
sh := fcz [ 8 ] . SignedHeader
var wg sync . WaitGroup
count = 100
errList := make ( [ ] error , count )
for i := 0 ; i < count ; i ++ {
wg . Add ( 1 )
go func ( index int ) {
errList [ index ] = cert . Verify ( sh )
defer wg . Done ( )
} ( i )
}
wg . Wait ( )
for _ , err := range errList {
require . Nil ( err )
}
}