@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package mru
package feed
import (
"bytes"
@ -31,7 +31,7 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/chunk"
"github.com/ethereum/go-ethereum/swarm/storage"
"github.com/ethereum/go-ethereum/swarm/storage/mru /lookup"
"github.com/ethereum/go-ethereum/swarm/storage/feed /lookup"
)
var (
@ -40,7 +40,7 @@ var (
Time : uint64 ( 4200 ) ,
}
cleanF func ( )
resource Name = "føø.bar"
subtopic Name = "føø.bar"
hashfunc = storage . MakeHashFunc ( storage . DefaultHash )
)
@ -73,7 +73,7 @@ func (f *fakeTimeProvider) Now() Timestamp {
}
// make updates and retrieve them based on periods and versions
func TestResource Handler ( t * testing . T ) {
func TestFeeds Handler ( t * testing . T ) {
// make fake timeProvider
clock := & fakeTimeProvider {
@ -83,18 +83,18 @@ func TestResourceHandler(t *testing.T) {
// signer containing private key
signer := newAliceSigner ( )
rh , datadir , teardownTest , err := setupTest ( clock , signer )
feedsHandle r, datadir , teardownTest , err := setupTest ( clock , signer )
if err != nil {
t . Fatal ( err )
}
defer teardownTest ( )
// create a new resource
// create a new feed
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
topic , _ := NewTopic ( "Mess with mru code and see what ghost catches you" , nil )
view := View {
topic , _ := NewTopic ( "Mess with Swarm feeds code and see what ghost catches you" , nil )
fd := Feed {
Topic : topic ,
User : signer . Address ( ) ,
}
@ -107,14 +107,14 @@ func TestResourceHandler(t *testing.T) {
"clyde" , // t=4285
}
request := NewFirstRequest ( view . Topic ) // this timestamps the update at t = 4200 (start time)
resourcekey := make ( map [ string ] storage . Address )
request := NewFirstRequest ( fd . Topic ) // this timestamps the update at t = 4200 (start time)
chunkAddress := make ( map [ string ] storage . Address )
data := [ ] byte ( updates [ 0 ] )
request . SetData ( data )
if err := request . Sign ( signer ) ; err != nil {
t . Fatal ( err )
}
resourcekey [ updates [ 0 ] ] , err = rh . Update ( ctx , request )
chunkAddress [ updates [ 0 ] ] , err = feedsHandle r. Update ( ctx , request )
if err != nil {
t . Fatal ( err )
}
@ -122,7 +122,7 @@ func TestResourceHandler(t *testing.T) {
// move the clock ahead 21 seconds
clock . FastForward ( 21 ) // t=4221
request , err = rh . NewRequest ( ctx , & request . View ) // this timestamps the update at t = 4221
request , err = feedsHandle r. NewRequest ( ctx , & request . Feed ) // this timestamps the update at t = 4221
if err != nil {
t . Fatal ( err )
}
@ -136,14 +136,14 @@ func TestResourceHandler(t *testing.T) {
if err := request . Sign ( signer ) ; err != nil {
t . Fatal ( err )
}
resourcekey [ updates [ 1 ] ] , err = rh . Update ( ctx , request )
chunkAddress [ updates [ 1 ] ] , err = feedsHandle r. Update ( ctx , request )
if err == nil {
t . Fatal ( "Expected update to fail since an update in this epoch already exists" )
}
// move the clock ahead 21 seconds
clock . FastForward ( 21 ) // t=4242
request , err = rh . NewRequest ( ctx , & request . View )
request , err = feedsHandle r. NewRequest ( ctx , & request . Feed )
if err != nil {
t . Fatal ( err )
}
@ -151,14 +151,14 @@ func TestResourceHandler(t *testing.T) {
if err := request . Sign ( signer ) ; err != nil {
t . Fatal ( err )
}
resourcekey [ updates [ 1 ] ] , err = rh . Update ( ctx , request )
chunkAddress [ updates [ 1 ] ] , err = feedsHandle r. Update ( ctx , request )
if err != nil {
t . Fatal ( err )
}
// move the clock ahead 42 seconds
clock . FastForward ( 42 ) // t=4284
request , err = rh . NewRequest ( ctx , & request . View )
request , err = feedsHandle r. NewRequest ( ctx , & request . Feed )
if err != nil {
t . Fatal ( err )
}
@ -167,14 +167,14 @@ func TestResourceHandler(t *testing.T) {
if err := request . Sign ( signer ) ; err != nil {
t . Fatal ( err )
}
resourcekey [ updates [ 2 ] ] , err = rh . Update ( ctx , request )
chunkAddress [ updates [ 2 ] ] , err = feedsHandle r. Update ( ctx , request )
if err != nil {
t . Fatal ( err )
}
// move the clock ahead 1 second
clock . FastForward ( 1 ) // t=4285
request , err = rh . NewRequest ( ctx , & request . View )
request , err = feedsHandle r. NewRequest ( ctx , & request . Feed )
if err != nil {
t . Fatal ( err )
}
@ -187,56 +187,56 @@ func TestResourceHandler(t *testing.T) {
if err := request . Sign ( signer ) ; err != nil {
t . Fatal ( err )
}
resourcekey [ updates [ 3 ] ] , err = rh . Update ( ctx , request )
chunkAddress [ updates [ 3 ] ] , err = feedsHandle r. Update ( ctx , request )
if err != nil {
t . Fatal ( err )
}
time . Sleep ( time . Second )
rh . Close ( )
feedsHandle r. Close ( )
// check we can retrieve the updates after close
clock . FastForward ( 2000 ) // t=6285
rhp arams := & HandlerParams { }
feedP arams := & HandlerParams { }
rh 2 , err := NewTestHandler ( datadir , rhp arams)
feedsHandle r2, err := NewTestHandler ( datadir , feedP arams)
if err != nil {
t . Fatal ( err )
}
rsrc 2, err := rh 2 . Lookup ( ctx , NewQueryLatest ( & request . View , lookup . NoClue ) )
update 2, err := feedsHandle r2. Lookup ( ctx , NewQueryLatest ( & request . Feed , lookup . NoClue ) )
if err != nil {
t . Fatal ( err )
}
// last update should be "clyde"
if ! bytes . Equal ( rsrc 2. data , [ ] byte ( updates [ len ( updates ) - 1 ] ) ) {
t . Fatalf ( "resourc e data was %v, expected %v" , string ( rsrc 2. data ) , updates [ len ( updates ) - 1 ] )
if ! bytes . Equal ( update 2. data , [ ] byte ( updates [ len ( updates ) - 1 ] ) ) {
t . Fatalf ( "feed updat e data was %v, expected %v" , string ( update 2. data ) , updates [ len ( updates ) - 1 ] )
}
if rsrc 2. Level != 22 {
t . Fatalf ( "resource epoch level was %d, expected 22" , rsrc 2 . Level )
if update 2. Level != 22 {
t . Fatalf ( "feed update epoch level was %d, expected 22" , update 2 . Level )
}
if rsrc 2. Base ( ) != 0 {
t . Fatalf ( "resource epoch base time was %d, expected 0" , rsrc 2 . Base ( ) )
if update 2. Base ( ) != 0 {
t . Fatalf ( "feed update epoch base time was %d, expected 0" , update 2 . Base ( ) )
}
log . Debug ( "Latest lookup" , "epoch base time" , rsrc 2. Base ( ) , "epoch level" , rsrc 2. Level , "data" , rsrc 2. data )
log . Debug ( "Latest lookup" , "epoch base time" , update 2. Base ( ) , "epoch level" , update 2. Level , "data" , update 2. data )
// specific point in time
rsrc , err := rh 2 . Lookup ( ctx , NewQuery ( & request . View , 4284 , lookup . NoClue ) )
update , err := feedsHandle r2. Lookup ( ctx , NewQuery ( & request . Feed , 4284 , lookup . NoClue ) )
if err != nil {
t . Fatal ( err )
}
// check data
if ! bytes . Equal ( rsrc . data , [ ] byte ( updates [ 2 ] ) ) {
t . Fatalf ( "resourc e data (historical) was %v, expected %v" , string ( rsrc 2. data ) , updates [ 2 ] )
if ! bytes . Equal ( update . data , [ ] byte ( updates [ 2 ] ) ) {
t . Fatalf ( "feed updat e data (historical) was %v, expected %v" , string ( update 2. data ) , updates [ 2 ] )
}
log . Debug ( "Historical lookup" , "epoch base time" , rsrc 2. Base ( ) , "epoch level" , rsrc 2. Level , "data" , rsrc 2. data )
log . Debug ( "Historical lookup" , "epoch base time" , update 2. Base ( ) , "epoch level" , update 2. Level , "data" , update 2. data )
// beyond the first should yield an error
rsrc , err = rh 2 . Lookup ( ctx , NewQuery ( & request . View , startTime . Time - 1 , lookup . NoClue ) )
update , err = feedsHandle r2. Lookup ( ctx , NewQuery ( & request . Feed , startTime . Time - 1 , lookup . NoClue ) )
if err == nil {
t . Fatalf ( "expected previous to fail, returned epoch %s data %v" , rsrc . Epoch . String ( ) , rsrc . data )
t . Fatalf ( "expected previous to fail, returned epoch %s data %v" , update . Epoch . String ( ) , update . data )
}
}
@ -266,11 +266,11 @@ func TestSparseUpdates(t *testing.T) {
defer teardownTest ( )
defer os . RemoveAll ( datadir )
// create a new resource
// create a new feed
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
topic , _ := NewTopic ( "Very slow updates" , nil )
view := View {
fd := Feed {
Topic : topic ,
User : signer . Address ( ) ,
}
@ -280,7 +280,7 @@ func TestSparseUpdates(t *testing.T) {
var epoch lookup . Epoch
var lastUpdateTime uint64
for T := uint64 ( 0 ) ; T < today ; T += 5 * Year {
request := NewFirstRequest ( view . Topic )
request := NewFirstRequest ( fd . Topic )
request . Epoch = lookup . GetNextEpoch ( epoch , T )
request . data = generateData ( T ) // this generates some data that depends on T, so we can check later
request . Sign ( signer )
@ -295,14 +295,14 @@ func TestSparseUpdates(t *testing.T) {
lastUpdateTime = T
}
query := NewQuery ( & view , today , lookup . NoClue )
query := NewQuery ( & fd , today , lookup . NoClue )
_ , err = rh . Lookup ( ctx , query )
if err != nil {
t . Fatal ( err )
}
_ , content , err := rh . GetContent ( & view )
_ , content , err := rh . GetContent ( & fd )
if err != nil {
t . Fatal ( err )
}
@ -321,7 +321,7 @@ func TestSparseUpdates(t *testing.T) {
t . Fatal ( err )
}
_ , content , err = rh . GetContent ( & view )
_ , content , err = rh . GetContent ( & fd )
if err != nil {
t . Fatal ( err )
}
@ -348,13 +348,13 @@ func TestValidator(t *testing.T) {
}
defer teardownTest ( )
// create new resource
topic , _ := NewTopic ( resource Name, nil )
view := View {
// create new feed
topic , _ := NewTopic ( subtopic Name, nil )
fd := Feed {
Topic : topic ,
User : signer . Address ( ) ,
}
mr := NewFirstRequest ( view . Topic )
mr := NewFirstRequest ( fd . Topic )
// chunk with address
data := [ ] byte ( "foo" )
@ -382,7 +382,7 @@ func TestValidator(t *testing.T) {
}
// tests that the content address validator correctly checks the data
// tests that resource update chunks are passed through content address validator
// tests that feed update chunks are passed through content address validator
// there is some redundancy in this test as it also tests content addressed chunks,
// which should be evaluated as invalid chunks by this validator
func TestValidatorInStore ( t * testing . T ) {
@ -396,7 +396,7 @@ func TestValidatorInStore(t *testing.T) {
signer := newAliceSigner ( )
// set up localstore
datadir , err := ioutil . TempDir ( "" , "storage-testresource validator" )
datadir , err := ioutil . TempDir ( "" , "storage-testfeeds validator" )
if err != nil {
t . Fatal ( err )
}
@ -409,10 +409,10 @@ func TestValidatorInStore(t *testing.T) {
t . Fatal ( err )
}
// set up resource handler and add is as a validator to the localstore
r hParams := & HandlerParams { }
r h := NewHandler ( r hParams)
store . Validators = append ( store . Validators , r h)
// set up Swa rm fe ed s handler and add is as a validator to the localstore
f hParams := & HandlerParams { }
f h := NewHandler ( f hParams)
store . Validators = append ( store . Validators , f h)
// create content addressed chunks, one good, one faulty
chunks := storage . GenerateRandomChunks ( chunk . DefaultSize , 2 )
@ -420,17 +420,17 @@ func TestValidatorInStore(t *testing.T) {
badChunk := storage . NewChunk ( chunks [ 1 ] . Address ( ) , goodChunk . Data ( ) )
topic , _ := NewTopic ( "xyzzy" , nil )
view := View {
fd := Feed {
Topic : topic ,
User : signer . Address ( ) ,
}
// create a resource update chunk with correct publickey
// create a feed update chunk with correct publickey
id := ID {
Epoch : lookup . Epoch { Time : 42 ,
Level : 1 ,
} ,
View : view ,
Feed : fd ,
}
updateAddr := id . Addr ( )
@ -438,7 +438,7 @@ func TestValidatorInStore(t *testing.T) {
r := new ( Request )
r . idAddr = updateAddr
r . Resource Update. ID = id
r . Update . ID = id
r . data = data
r . Sign ( signer )
@ -451,20 +451,20 @@ func TestValidatorInStore(t *testing.T) {
// put the chunks in the store and check their error status
err = store . Put ( context . Background ( ) , goodChunk )
if err == nil {
t . Fatal ( "expected error on good content address chunk with resourc e validator only, but got nil" )
t . Fatal ( "expected error on good content address chunk with feed updat e validator only, but got nil" )
}
err = store . Put ( context . Background ( ) , badChunk )
if err == nil {
t . Fatal ( "expected error on bad content address chunk with resourc e validator only, but got nil" )
t . Fatal ( "expected error on bad content address chunk with feed updat e validator only, but got nil" )
}
err = store . Put ( context . Background ( ) , uglyChunk )
if err != nil {
t . Fatalf ( "expected no error on resource update chunk with resourc e validator only, but got: %s" , err )
t . Fatalf ( "expected no error on feed update chunk with feed updat e validator only, but got: %s" , err )
}
}
// create rpc and resourceh andler
func setupTest ( timeProvider timestampProvider , signer Signer ) ( r h * TestHandler , datadir string , teardown func ( ) , err error ) {
// create rpc and feeds H andler
func setupTest ( timeProvider timestampProvider , signer Signer ) ( f h * TestHandler , datadir string , teardown func ( ) , err error ) {
var fsClean func ( )
var rpcClean func ( )
@ -478,7 +478,7 @@ func setupTest(timeProvider timestampProvider, signer Signer) (rh *TestHandler,
}
// temp datadir
datadir , err = ioutil . TempDir ( "" , "r h" )
datadir , err = ioutil . TempDir ( "" , "f h" )
if err != nil {
return nil , "" , nil , err
}
@ -487,9 +487,9 @@ func setupTest(timeProvider timestampProvider, signer Signer) (rh *TestHandler,
}
TimestampProvider = timeProvider
rhp arams := & HandlerParams { }
r h, err = NewTestHandler ( datadir , rhp arams)
return r h, datadir , cleanF , err
fhP arams := & HandlerParams { }
f h, err = NewTestHandler ( datadir , fhP arams)
return f h, datadir , cleanF , err
}
func newAliceSigner ( ) * GenericSigner {