Skip to content

Commit

Permalink
Added Duplicate-item PoM checking to Gossiper
Browse files Browse the repository at this point in the history
  • Loading branch information
tfn18001 committed Apr 20, 2022
1 parent 787e5c1 commit c38ea10
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CTng/gossip/gossip_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (g Gossip_object) GetID(period_interval int64) Gossip_object_ID {
}
}

// This function is highly associated with this type, so I think it belongs here.
// GetCurrentTimestamp returns the current UTC timestamp in RFC3339 format
func GetCurrentTimestamp() string {
return time.Now().UTC().Format(time.RFC3339)
}
Expand Down
4 changes: 2 additions & 2 deletions CTng/gossip/gossiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Accuse(c *GossiperContext, url string) {

// Sends a gossip object to all connected gossipers.
// This function assumes you are passing valid data. ALWAYS CHECK BEFORE CALLING THIS FUNCTION.
func gossipData(c *GossiperContext, gossip_obj Gossip_object) error {
func GossipData(c *GossiperContext, gossip_obj Gossip_object) error {
// Convert gossip object to JSON
msg, err := json.Marshal(gossip_obj)
if err != nil {
Expand Down Expand Up @@ -64,7 +64,7 @@ func gossipData(c *GossiperContext, gossip_obj Gossip_object) error {
}

// Sends a gossip object to the owner of the object.
func sendToOwner(c *GossiperContext, obj Gossip_object) {
func SendToOwner(c *GossiperContext, obj Gossip_object) {
// Convert gossip object to JSON
msg, err := json.Marshal(obj)
if err != nil {
Expand Down
55 changes: 40 additions & 15 deletions CTng/gossip/process_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gossip

import (
"CTng/util"
"errors"
"fmt"
)

Expand All @@ -15,25 +16,25 @@ func ProcessValidObject(c *GossiperContext, obj Gossip_object) {
var err error = nil
switch obj.Type {
case STH:
sendToOwner(c, obj)
err = gossipData(c, obj)
SendToOwner(c, obj)
err = GossipData(c, obj)
case REVOCATION:
sendToOwner(c, obj)
err = gossipData(c, obj)
SendToOwner(c, obj)
err = GossipData(c, obj)
case STH_FRAG:
sendToOwner(c, obj)
err = gossipData(c, obj)
SendToOwner(c, obj)
err = GossipData(c, obj)
case GOSSIP_POM:
sendToOwner(c, obj)
err = gossipData(c, obj)
SendToOwner(c, obj)
err = GossipData(c, obj)
case REVOCATION_FRAG:
err = gossipData(c, obj)
err = GossipData(c, obj)
case ACCUSATION_FRAG:
ProcessAccusation(c, obj)
err = gossipData(c, obj)
err = GossipData(c, obj)
case APPLICATION_POM:
sendToOwner(c, obj)
err = gossipData(c, obj)
SendToOwner(c, obj)
err = GossipData(c, obj)
default:
fmt.Println("Recieved unsupported object type.")
}
Expand All @@ -42,10 +43,34 @@ func ProcessValidObject(c *GossiperContext, obj Gossip_object) {
}
}

// Process a valid gossip object which is a duplicate to another one.
// If the signature/payload is identical, then we can safely ignore the duplicate.
// Otherwise, we generate a PoM for two objects sent in the same period.
func ProcessDuplicateObject(c *GossiperContext, obj Gossip_object, dup Gossip_object) err {
if obj.Signature == dup.Signature &&
obj.Payload == dup.Payload {
return nil
} else {
// Generate PoM
pom := Gossip_object{
Application: obj.Application,
Type: GOSSIP_POM,
Signer: obj.Signer,
Signature: [2]string{obj.Signature[0], dup.Signature[0]},
Payload: [2]string{obj.Payload[0], dup.Payload[0]},
Timestamp: GetCurrentTimestamp(),
}
c.StoreObject(pom)
c.HasPom[obj.Payload[0]] = true
// Currently, we don't send PoMs. but if we did, we could do it here.
SendToOwner(c, pom)
return errors.New("Proof of Misbhevior Generated")
}
}

func ProcessInvalidObject(obj Gossip_object, e error) {
// TODO:
// Determine Conflict/misbehavior
// Log error
// Send neccessary accusations
}

Expand All @@ -57,13 +82,13 @@ func ProcessAccusation(c *GossiperContext, acc Gossip_object) {
fmt.Println(util.YELLOW+"Processed accusation against", acc.Payload[0], util.RESET)
}
if shouldGossip {
gossipData(c, acc)
GossipData(c, acc)
}
if pom != nil {
fmt.Println(util.RED+"Generated POM for", acc.Payload[0], util.RESET)
c.StoreObject(*pom)
c.HasPom[acc.Payload[0]] = true
// We do not currently gossip PoMs.
sendToOwner(c, *pom)
SendToOwner(c, *pom)
}
}
21 changes: 15 additions & 6 deletions CTng/server/Gossiper_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,17 @@ func handleGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http.Requ
return
}
// Check for duplicate object.
_, found := c.GetObject(gossip_obj.GetID(c.Config.Public.Period_interval))
stored_obj, found := c.GetObject(gossip_obj.GetID(c.Config.Public.Period_interval))
if found {
// If the object is already stored, still return OK.{
fmt.Println("Duplicate:", gossip_obj.Type, getSenderURL(r)+".")
http.Error(w, "Gossip object already stored.", http.StatusOK)
// gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
err := gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
if err != nil {
http.Error(w, err.Error(), http.StatusOK)
} else {
http.Error(w, "Duplicate object.", http.StatusOK)
}
http.Error(w, "Recieved Duplicate Object.", http.StatusOK)
return
} else {
fmt.Println(util.GREEN+"Recieved new, valid", gossip_obj.Type, "from "+getSenderURL(r)+".", util.RESET)
Expand Down Expand Up @@ -165,12 +170,16 @@ func handleOwnerGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http
fmt.Println(util.RED+"Owner sent invalid object.", util.RESET)
http.Error(w, err.Error(), http.StatusBadRequest)
}
_, found := c.GetObject(gossip_obj.GetID(c.Config.Public.Period_interval))
stored_obj, found := c.GetObject(gossip_obj.GetID(c.Config.Public.Period_interval))
if found {
// If the object is already stored, still return OK.{
fmt.Println("Recieved duplicate object from Owner.")
http.Error(w, "Gossip object already stored.", http.StatusOK)
// gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
err := gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
if err != nil {
http.Error(w, "Duplicate Object recieved!", http.StatusOK)
} else {
http.Error(w, err.Error(), http.StatusOK)
}
return
} else {
// Prints the body of the post request to the server console
Expand Down

0 comments on commit c38ea10

Please sign in to comment.