Skip to content

Commit

Permalink
Improved Output readability and debugged Gossip_PoM generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tfn18001 committed Apr 25, 2022
1 parent dcb417e commit 86109d3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 34 deletions.
4 changes: 3 additions & 1 deletion CTng/ctng.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"CTng/gossip"
"CTng/monitor"
"CTng/server"
"CTng/testData/fakeCA"
fakeLogger "CTng/testData/fakeLogger"
"fmt"
"os"
Expand Down Expand Up @@ -51,13 +52,14 @@ func main() {
Config: &conf,
Storage: storage,
StorageFile: "monitor_data.json",
HasPom: make(map[string]bool),
}
ctx.Config = &conf
server.StartMonitorServer(&ctx)
case "logger":
fakeLogger.RunLogger(os.Args[2])
case "ca":
// fakeCA.runCA(os.Args[2])
fakeCA.RunCA(os.Args[2])
default:
fmt.Println(helpText)
}
Expand Down
14 changes: 9 additions & 5 deletions CTng/gossip/gossiper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func GossipData(c *GossiperContext, gossip_obj Gossip_object) error {

// Send the gossip object to all connected gossipers.
for _, url := range c.Config.Connected_Gossipers {
if c.HasPom[url] {
continue
}
// if c.HasPom[url] {
// continue
// }
// HTTP POST the data to the url or IP address.
resp, err := c.Client.Post("http://"+url+"/gossip/push-data", "application/json", bytes.NewBuffer(msg))
if err != nil {
Expand All @@ -58,7 +58,9 @@ func GossipData(c *GossiperContext, gossip_obj Gossip_object) error {
// Alernatively, we could return the response from this function.
defer resp.Body.Close()
// do something with the response?
fmt.Println("Gossiped to " + url + " and recieved " + resp.Status)
if c.Verbose {
fmt.Println("Gossiped to " + url + " and recieved " + resp.Status)
}
}
return nil
}
Expand All @@ -78,7 +80,9 @@ func SendToOwner(c *GossiperContext, obj Gossip_object) {
// Close the response, mentioned by http.Post
// Alernatively, we could return the response from this function.
defer resp.Body.Close()
fmt.Println("Owner responded with " + resp.Status)
if c.Verbose {
fmt.Println("Owner responded with " + resp.Status)
}
}
// Handling errors from owner could go here.
}
8 changes: 5 additions & 3 deletions CTng/gossip/process_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func ProcessValidObject(c *GossiperContext, obj Gossip_object) {
SendToOwner(c, obj)
err = GossipData(c, obj)
case GOSSIP_POM:
c.HasPom[obj.Payload[0]] = true
SendToOwner(c, obj)
err = GossipData(c, obj)
case REVOCATION_FRAG:
Expand All @@ -33,6 +34,7 @@ func ProcessValidObject(c *GossiperContext, obj Gossip_object) {
ProcessAccusation(c, obj)
err = GossipData(c, obj)
case APPLICATION_POM:
c.HasPom[obj.Payload[0]] = true
SendToOwner(c, obj)
err = GossipData(c, obj)
default:
Expand All @@ -47,8 +49,8 @@ func ProcessValidObject(c *GossiperContext, obj Gossip_object) {
// 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) error {
if obj.Signature == dup.Signature &&
obj.Payload == dup.Payload {
if obj.Signature[0] == dup.Signature[0] &&
obj.Payload[0] == dup.Payload[0] {
return nil
} else {
// Generate PoM
Expand Down Expand Up @@ -77,7 +79,7 @@ func ProcessInvalidObject(obj Gossip_object, e error) {

func ProcessAccusation(c *GossiperContext, acc Gossip_object) {
pom, shouldGossip, err := Process_Accusation(acc, c.Accusations, c.Config.Crypto)
if err != nil {
if err != nil && shouldGossip {
fmt.Println(util.RED+err.Error(), util.RESET)
} else {
fmt.Println(util.YELLOW+"Processed accusation against", acc.Payload[0], util.RESET)
Expand Down
32 changes: 30 additions & 2 deletions CTng/gossip/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ const (
APPLICATION_POM = "http://ctng.uconn.edu/304"
)

func TypeString(t string) string {
switch t {
case GOSSIP_POM:
return "GOSSIP_POM"
case STH:
return "STH"
case REVOCATION:
return "REVOCATION"
case STH_FRAG:
return "STH_FRAG"
case REVOCATION_FRAG:
return "REVOCATION_FRAG"
case ACCUSATION_FRAG:
return "ACCUSATION_FRAG"
case STH_FULL:
return "STH_FULL"
case REVOCATION_FULL:
return "REVOCATION_FULL"
case ACCUSATION_POM:
return "ACCUSATION_POM"
case APPLICATION_POM:
return "APPLICATION_POM"
default:
return "UNKNOWN"
}
}

// Types of errors that can occur when parsing a Gossip_object
const (
No_Sig_Match = "Signatures don't match"
Expand Down Expand Up @@ -83,8 +110,9 @@ type GossiperContext struct {
StorageFile string // Where storage can be stored.
// Client: used for HTTP connections, allows for timeouts
// and more control over the connections we make.
Client *http.Client
HasPom map[string]bool
Client *http.Client
HasPom map[string]bool
Verbose bool
}

// Saves the Storage object to the value in c.StorageFile.
Expand Down
18 changes: 6 additions & 12 deletions CTng/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"CTng/util"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand All @@ -27,7 +26,7 @@ func QueryLoggers(c *MonitorContext) {
sthResp, err := http.Get(PROTOCOL + logger + "/ctng/v2/get-sth/")
if err != nil {
log.Println(err)
AccuseEntity(c, logger)
// AccuseEntity(c, logger)
continue
}

Expand All @@ -36,13 +35,13 @@ func QueryLoggers(c *MonitorContext) {
err = json.Unmarshal(sthBody, &STH)
if err != nil {
log.Println(util.RED+err.Error(), util.RESET)
AccuseEntity(c, logger)
// AccuseEntity(c, logger)
continue
}
err = STH.Verify(c.Config.Crypto)
if err != nil {
log.Println(util.RED+"STH signature verification failed", err.Error(), util.RESET)
AccuseEntity(c, logger)
// AccuseEntity(c, logger)
} else {
Process_valid_object(c, STH)
}
Expand Down Expand Up @@ -134,20 +133,15 @@ func Send_to_gossiper(c *MonitorContext, g gossip.Gossip_object) {
// Close the response, mentioned by http.Post
// Alernatively, we could return the response from this function.
defer resp.Body.Close()
fmt.Println(util.BLUE+"Sent Object to Gossiper, Recieved "+resp.Status, util.RESET)
fmt.Println(util.BLUE+"Sent", gossip.TypeString(g.Type), "to Gossiper, Recieved "+resp.Status, util.RESET)
}

}

//this function takes the name of the entity as input and check if there is a POM against it
//this should be invoked after the monitor receives the information from its loggers and CAs prior to threshold signning it
func Check_entity_pom(c *MonitorContext, name string) error {
for _, v := range *c.Storage {
if v.Payload[0] == name && (v.Type == gossip.ACCUSATION_POM || v.Type == gossip.APPLICATION_POM || v.Type == gossip.GOSSIP_POM) {
return errors.New("There exists a proof of misbehavior against this entity")
}
}
return nil
func Check_entity_pom(c *MonitorContext, name string) bool {
return c.HasPom[name]
}

func IsLogger(c *MonitorContext, loggerURL string) bool {
Expand Down
20 changes: 14 additions & 6 deletions CTng/monitor/monitor_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"CTng/gossip"
"CTng/util"
"fmt"
"time"
)
Expand All @@ -13,13 +14,14 @@ func Process_valid_object(c *MonitorContext, g gossip.Gossip_object) {
if IsLogger(c, g.Signer) && g.Type == gossip.STH {
Send_to_gossiper(c, g)
f := func() {
fmt.Println(util.BLUE, "Signing Revocation of", g.Signer, util.RESET)
sig_frag, err := c.Config.Crypto.ThresholdSign(g.Payload[0])
if err != nil {
fmt.Println(err.Error())
}
pom_err := Check_entity_pom(c, g.Signer)
//if there is no conflicting information/PoM send the Threshold signed version to the gossiper
if pom_err == nil {
if pom_err == false {
g.Type = gossip.STH_FRAG
g.Signature[0] = sig_frag.String()
g.Signer = c.Config.Crypto.SelfID.String()
Expand All @@ -39,22 +41,28 @@ func Process_valid_object(c *MonitorContext, g gossip.Gossip_object) {
}
Send_to_gossiper(c, g)
f := func() {
fmt.Println(util.BLUE, "Signing Revocation of", g.Signer, util.RESET)
pom_err := Check_entity_pom(c, g.Signer)
if pom_err == nil {
if pom_err == false {
g.Type = gossip.REVOCATION_FRAG
g.Signature[0] = sig_frag.String()
g.Signer = c.Config.Crypto.SelfID.String()
Send_to_gossiper(c, g)
}

}
time.AfterFunc(time.Duration(c.Config.Public.Gossip_wait_time), f)
time.AfterFunc(time.Duration(c.Config.Public.Gossip_wait_time)*time.Second, f)
return

}
//if the object is from its own gossiper
// Note didn't implement the directory separation here***************************************************************
if g.Type == gossip.ACCUSATION_POM || g.Type == gossip.GOSSIP_POM || g.Type == gossip.APPLICATION_POM || g.Type == gossip.REVOCATION_FULL || g.Type == gossip.STH_FULL {
if g.Type == gossip.ACCUSATION_POM || g.Type == gossip.GOSSIP_POM || g.Type == gossip.APPLICATION_POM {
fmt.Println("Processing POM")
c.HasPom[g.Payload[0]] = true
c.StoreObject(g)
return
}
//if the object is from its own gossiper
// Note didn't implement the directory separation here***************************************************************
c.StoreObject(g)
return
}
2 changes: 2 additions & 0 deletions CTng/monitor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type MonitorContext struct {
// The below could be used to prevent a Monitor from sending duplicate Accusations,
// Currently, if a monitor accuses two entities in the same Period, it will trigger a gossip PoM.
// Therefore, a monitor can only accuse once per Period. I believe this is a temporary solution.
HasPom map[string]bool
HasAccused bool
Verbose bool
Client *http.Client
}

Expand Down
10 changes: 6 additions & 4 deletions CTng/server/Gossiper_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,17 @@ func handleGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http.Requ
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)+".")
if c.Verbose {
fmt.Println("Ignoring Duplicate ", gossip_obj.Type)
}
err := gossip.ProcessDuplicateObject(c, gossip_obj, stored_obj)
if err != nil {
http.Error(w, err.Error(), 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)
fmt.Println(util.GREEN+"Recieved new, valid", gossip.TypeString(gossip_obj.Type), "from "+getSenderURL(r)+".", util.RESET)
gossip.ProcessValidObject(c, gossip_obj)
c.SaveStorage()
}
Expand Down Expand Up @@ -179,12 +181,12 @@ func handleOwnerGossip(c *gossip.GossiperContext, w http.ResponseWriter, r *http
if err != nil {
http.Error(w, "Duplicate Object recieved!", http.StatusOK)
} else {
http.Error(w, err.Error(), http.StatusOK)
http.Error(w, "", http.StatusOK)
}
return
} else {
// Prints the body of the post request to the server console
fmt.Println(util.GREEN+"Recieved new, valid", gossip_obj.Type, "from owner.", util.RESET)
fmt.Println(util.GREEN+"Recieved new, valid", gossip.TypeString(gossip_obj.Type), "from owner.", util.RESET)
gossip.ProcessValidObject(c, gossip_obj)
c.SaveStorage()
}
Expand Down
2 changes: 1 addition & 1 deletion CTng/server/Monitor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func handle_gossip(c *monitor.MonitorContext, w http.ResponseWriter, r *http.Req
// processDuplicateObject(c, gossip_obj, stored_obj)
return
} else {
fmt.Println("Recieved new, valid", gossip_obj.Type, "from "+getSenderURL(r)+".")
fmt.Println("Recieved new, valid", gossip.TypeString(gossip_obj.Type), "from "+getSenderURL(r)+".")
monitor.Process_valid_object(c, gossip_obj)
c.SaveStorage()
}
Expand Down

0 comments on commit 86109d3

Please sign in to comment.