Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed May 14, 2024
1 parent 543e0d4 commit 4374ed9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 18 deletions.
46 changes: 43 additions & 3 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ var (
Zero = Address{&ZeroExternal} // For utility purposes only. It is out-of-scope for state purposes.
)

var (
ErrInvalidLocation = errors.New("invalid location")
)

// Hash represents the 32 byte Keccak256 hash of arbitrary data.
type Hash [HashLength]byte

Expand Down Expand Up @@ -539,10 +543,46 @@ func (l Location) MarshalJSON() ([]byte, error) {

// NewLocation verifies the inputs for region and zone and returns a valid location
func NewLocation(region, zone int) (Location, error) {
if (region < 0 || region >= 0xf) || (zone < 0 || zone >= 0xf) {
return nil, fmt.Errorf("invalid location")
loc := Location{}
err := loc.SetRegion(region)
if err != nil {
return nil, err
}
err = loc.SetZone(zone)
if err != nil {
return nil, err
}

return loc, nil
}

func (l *Location) SetRegion(region int) error {
if region < 0 || region >= 0xf {
return ErrInvalidLocation
}
return Location{byte(region), byte(zone)}, nil
if len(*l) < 1 {
// Extend location to include region if its too short
newLoc := make([]byte, 1)
*l = newLoc
}
(*l)[0] = byte(region)
return nil
}

func (l *Location) SetZone(zone int) error {
if zone < 0 || zone > 0xf {
return ErrInvalidLocation
}
if len(*l) < 2 {
// Extend the slice while preserving the first byte, if it exists
newSlice := make([]byte, 2)
if len(*l) > 0 {
newSlice[0] = (*l)[0] // Preserve existing first byte
}
*l = newSlice
}
(*l)[1] = byte(zone)
return nil
}

// LocationFromName parses a location name and returns a Location.
Expand Down
7 changes: 7 additions & 0 deletions p2p/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func (p *P2PNode) requestAndWait(peerID peer.ID, topic *pubsubManager.Topic, req

// Request a data from the network for the specified slice
func (p *P2PNode) Request(location common.Location, requestData interface{}, responseDataType interface{}) chan interface{} {
if location.Context() == common.REGION_CTX {
log.Global.Print("fake")
}
topic, err := pubsubManager.NewTopic(p.pubsub.GetGenesis(), location, responseDataType)
if err != nil {
log.Global.WithFields(log.Fields{
Expand Down Expand Up @@ -321,6 +324,10 @@ func (p *P2PNode) handleBroadcast(sourcePeer peer.ID, topic string, data interfa
switch v := data.(type) {
case types.WorkObject:
p.cacheAdd(v.Hash(), &v, nodeLocation)
log.Global.WithField("context", v.Location().Context()).Print("")
if v.Location().Context() <= common.REGION_CTX {
log.Global.Print("sum ting wong")
}
// TODO: send it to consensus
case types.Transactions:
default:
Expand Down
5 changes: 3 additions & 2 deletions p2p/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package node
import (
"context"
"fmt"
"reflect"
"time"

"github.com/libp2p/go-libp2p"
Expand Down Expand Up @@ -234,14 +235,14 @@ func (p *P2PNode) p2pAddress() (multiaddr.Multiaddr, error) {
// Helper to access the corresponding data cache
func (p *P2PNode) pickCache(datatype interface{}, location common.Location) *lru.Cache[common.Hash, interface{}] {
switch datatype.(type) {
case *types.WorkObject:
case *types.WorkObject, *types.WorkObjectHeaderView, *types.WorkObjectBlockView:
return p.cache[location.Name()]["blocks"]
case *types.Transaction:
return p.cache[location.Name()]["transactions"]
case *types.Header:
return p.cache[location.Name()]["headers"]
default:
log.Global.Fatalf("unsupported type")
log.Global.WithField("type", reflect.TypeOf(datatype)).Fatalf("unsupported type")
return nil
}
}
Expand Down
3 changes: 3 additions & 0 deletions p2p/node/pubsubManager/gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func (g *PubsubManager) Unsubscribe(location common.Location, datatype interface

// broadcasts data to subscribing peers
func (g *PubsubManager) Broadcast(location common.Location, datatype interface{}) error {
if location.Context() == common.REGION_CTX {
log.Global.Print("fake")
}
topicName, err := NewTopic(g.genesis, location, datatype)
if err != nil {
return err
Expand Down
30 changes: 19 additions & 11 deletions p2p/node/pubsubManager/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,27 @@ func TopicFromString(genesis common.Hash, topic string) (*Topic, error) {
if len(topicParts) < 3 {
return nil, ErrMalformedTopic
}
var location common.Location
locationStr := strings.Split(topicParts[1], ",")
region, err := strconv.Atoi(locationStr[0])
if err != nil {
return nil, err
}
zone, err := strconv.Atoi(locationStr[1])
if err != nil {
return nil, err
}
location, err := common.NewLocation(region, zone)
if err != nil {
return nil, err
if len(locationStr) > 0 {
if len(locationStr) >= 1 {
// Region specified
region, err := strconv.Atoi(locationStr[0])
if err != nil {
return nil, err
}
location.SetRegion(region)
}
if len(locationStr) == 2 {
// Zone specified
zone, err := strconv.Atoi(locationStr[1])
if err != nil {
return nil, err
}
location.SetZone(zone)
}
}

switch topicParts[2] {
case C_headerType:
return NewTopic(genesis, location, &types.WorkObjectHeaderView{})
Expand Down
4 changes: 2 additions & 2 deletions quai/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func (h *handler) missingBlockLoop() {
}).Fatal("Go-Quai Panicked")
}
}()
resultCh := h.p2pBackend.Request(h.nodeLocation, blockRequest.Hash, &types.WorkObject{})
resultCh := h.p2pBackend.Request(h.nodeLocation, blockRequest.Hash, &types.WorkObjectBlockView{})
block := <-resultCh
if block != nil {
h.core.WriteBlock(block.(*types.WorkObject))
h.core.WriteBlock(block.(*types.WorkObjectBlockView).WorkObject)
}
}()
case <-h.missingBlockSub.Err():
Expand Down

0 comments on commit 4374ed9

Please sign in to comment.