Skip to content

Commit

Permalink
checking hash info when syncing (#746)
Browse files Browse the repository at this point in the history
* checking hash info when syncing

Co-authored-by: Will <will@cypherpunk.email>
  • Loading branch information
nikkolasg and willscott committed Oct 1, 2020
1 parent 5b9a55a commit c23577f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 8 additions & 6 deletions core/drand_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,6 @@ func (d *Drand) StartFollowChain(req *drand.StartFollowRequest, stream drand.Con
}()

addr := net.RemoteAddress(stream.Context())
// TODO put that hash verification back
// hash := req.GetInfoHash()
peers := make([]net.Peer, 0, len(req.GetNodes()))
for _, addr := range req.GetNodes() {
// XXX add TLS disable later
Expand All @@ -817,10 +815,14 @@ func (d *Drand) StartFollowChain(req *drand.StartFollowRequest, stream drand.Con
}
d.log.Debug("start_follow_chain", "fetched chain info", "hash", fmt.Sprintf("%x", info.Hash()))

// TODO UNCOMMENT WHEN HASH INCONSISTENCY FIXED
// if !bytes.Equal(info.Hash(),hash) {
// return errors.New("invalid chain info hash!")
// }
hashStr := req.GetInfoHash()
hash, err := hex.DecodeString(hashStr)
if err != nil {
return fmt.Errorf("invalid hash info hex: %v", err)
}
if !bytes.Equal(info.Hash(), hash) {
return errors.New("invalid chain info hash!")
}

store, err := d.createBoltStore()
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion core/drand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func TestDrandFollowChain(tt *testing.T) {

dt.MoveToTime(group.GenesisTime)
// do a few periods
for i := 0; i < 10; i++ {
for i := 0; i < 6; i++ {
dt.MoveTime(group.Period)
}

Expand All @@ -499,6 +499,21 @@ func TestDrandFollowChain(tt *testing.T) {
addrToFollow := []string{rootID.Address()}
hash := fmt.Sprintf("%x", chain.NewChainInfo(group).Hash())
tls := true
// First try with an invalid hash info
ctx, cancel = context.WithCancel(context.Background())
_, errCh, _ := newClient.StartFollowChain(ctx, "deadbeef", addrToFollow, tls, 10000)
select {
case <-errCh:
case <-time.After(100 * time.Millisecond):
tt.Fatal("should have errored")
}
_, errCh, _ = newClient.StartFollowChain(ctx, "tutu", addrToFollow, tls, 10000)
select {
case <-errCh:
case <-time.After(100 * time.Millisecond):
tt.Fatal("should have errored")
}

fn := func(upTo, exp uint64) {
ctx, cancel = context.WithCancel(context.Background())
progress, errCh, err := newClient.StartFollowChain(ctx, hash, addrToFollow, tls, upTo)
Expand Down

0 comments on commit c23577f

Please sign in to comment.