Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix V-DOGE-VUL-005 & V-DOGE-VUL-006 #39

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions protocol/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package protocol

import (
"context"
"errors"
"fmt"

"github.com/dogechain-lab/jury/protocol/proto"
"github.com/dogechain-lab/jury/types"
)

var (
ErrNilHeaderRequest = errors.New("header request spec is nil")
)

func getHeaders(clt proto.V1Client, req *proto.GetHeadersRequest) ([]*types.Header, error) {
resp, err := clt.GetHeaders(context.Background(), req)
if err != nil {
Expand All @@ -17,6 +22,11 @@ func getHeaders(clt proto.V1Client, req *proto.GetHeadersRequest) ([]*types.Head
headers := []*types.Header{}

for _, obj := range resp.Objs {
abrahamcruise321 marked this conversation as resolved.
Show resolved Hide resolved
if obj == nil || obj.Spec == nil {
// this nil header comes from a faulty node, reject all blocks of it.
return nil, ErrNilHeaderRequest
}

header := &types.Header{}
if err := header.UnmarshalRLP(obj.Spec.Value); err != nil {
return nil, err
Expand Down
17 changes: 13 additions & 4 deletions protocol/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
ErrForkNotFound = errors.New("fork not found")
ErrPopTimeout = errors.New("timeout")
ErrConnectionClosed = errors.New("connection closed")
ErrTooManyHeaders = errors.New("unexpected more than 1 result")
ErrDecodeDifficulty = errors.New("failed to decode difficulty")
ErrInvalidTypeAssertion = errors.New("invalid type assertion")
)

// SyncPeer is a representation of the peer the node is syncing with
Expand Down Expand Up @@ -187,7 +190,7 @@ func statusFromProto(p *proto.V1Status) (*Status, error) {

diff, ok := new(big.Int).SetString(p.Difficulty, 10)
if !ok {
return nil, fmt.Errorf("failed to decode difficulty")
return nil, ErrDecodeDifficulty
}

s.Difficulty = diff
Expand Down Expand Up @@ -494,7 +497,7 @@ func (s *Syncer) DeletePeer(peerID peer.ID) error {
if ok {
syncPeer, ok := p.(*SyncPeer)
if !ok {
return errors.New("invalid type assertion")
return ErrInvalidTypeAssertion
}

if err := syncPeer.conn.Close(); err != nil {
Expand Down Expand Up @@ -719,12 +722,18 @@ func getHeader(clt proto.V1Client, num *uint64, hash *types.Hash) (*types.Header
}

if len(resp.Objs) != 1 {
abrahamcruise321 marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("unexpected more than 1 result")
return nil, ErrTooManyHeaders
}

obj := resp.Objs[0]

if obj == nil || obj.Spec == nil || len(obj.Spec.Value) == 0 {
return nil, ErrNilHeaderRequest
}

header := &types.Header{}

if err := header.UnmarshalRLP(resp.Objs[0].Spec.Value); err != nil {
if err := header.UnmarshalRLP(obj.Spec.Value); err != nil {
return nil, err
}

Expand Down