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

More options #52

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
48 changes: 37 additions & 11 deletions model/parts/types/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package types

import (
"fmt"
"go-incentive-simulation/config"
"go-incentive-simulation/model/general"
"math"
"sync"
)

Expand All @@ -11,7 +13,7 @@ type Graph struct {
*Network
CurState State
Edges map[NodeId]map[NodeId]*Edge
rwMutex sync.RWMutex
rwMutex sync.RWMutex
}

// Edge that connects to NodesMap with attributes about the connection
Expand All @@ -27,9 +29,10 @@ type Edge struct {
// "lastEpoch" is the epoch where it was last forgiven.
// "threshold" is for the adjustable threshold limit.
type EdgeAttrs struct {
A2B int
LastEpoch int
Threshold int
A2B int
LastEpoch int
Threshold int
Refreshrate int
}

func (g *Graph) GetNodeAdj(nodeId NodeId) [][]NodeId {
Expand All @@ -43,7 +46,7 @@ func (g *Graph) GetNodeAdj(nodeId NodeId) [][]NodeId {
}

// AddEdge will add an edge from a node to a node
func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId, attrs EdgeAttrs) error {
func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId) error {
toNode := g.NodesMap[toNodeId]
fromNode := g.NodesMap[fromNodeId]
if toNode == nil || fromNode == nil {
Expand All @@ -55,12 +58,29 @@ func (g *Graph) AddEdge(fromNodeId NodeId, toNodeId NodeId, attrs EdgeAttrs) err
if g.unsafeEdgeExists(toNodeId, fromNodeId) {
mutex = g.Edges[toNodeId][fromNodeId].Mutex
}
newEdge := &Edge{FromNodeId: fromNodeId, ToNodeId: toNodeId, Attrs: attrs, Mutex: mutex}
newEdge := &Edge{FromNodeId: fromNodeId, ToNodeId: toNodeId, Attrs: EdgeAttrs{}, Mutex: mutex}
newEdge.InitThresholdAndRefreshrate()
g.Edges[fromNodeId][toNodeId] = newEdge
return nil
}
}

func (e *Edge) InitThresholdAndRefreshrate() {
threshold := config.GetThreshold()
refreshrate := config.GetRefreshRate()
if config.IsAdjustableThreshold() {
threshold = config.GetThreshold() - config.GetBits() + general.BitLength(e.FromNodeId.ToInt()^e.ToNodeId.ToInt())
refreshrate = GetAdjustedRefreshrate(threshold, config.GetThreshold(), config.GetRefreshRate(), config.GetAdjustableThresholdExponent())
}
e.Attrs.Threshold = threshold
e.Attrs.Refreshrate = refreshrate
}

func GetAdjustedRefreshrate(adjustedThreshold, threshold, refreshRate, power int) int {
ratio := float64(adjustedThreshold) / float64(threshold)
return int(math.Ceil(float64(refreshRate) * math.Pow(ratio, float64(power))))
}

func (g *Graph) NewNode() (*Node, error) {
g.rwMutex.Lock()
node := g.Network.NewNode()
Expand All @@ -71,13 +91,11 @@ func (g *Graph) NewNode() (*Node, error) {
nodeAdj := node.AdjIds
for _, adjItems := range nodeAdj {
for _, otherNodeId := range adjItems {
threshold := general.BitLength(node.Id.ToInt() ^ otherNodeId.ToInt())
attrs := EdgeAttrs{A2B: 0, LastEpoch: 0, Threshold: threshold}
err := g.AddEdge(node.Id, otherNodeId, attrs)
err := g.AddEdge(node.Id, otherNodeId)
if err != nil {
return nil, err
}
err = g.AddEdge(otherNodeId, node.Id, attrs)
err = g.AddEdge(otherNodeId, node.Id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -116,7 +134,7 @@ func (g *Graph) GetEdge(fromNodeId NodeId, toNodeId NodeId) *Edge {
g.rwMutex.Lock()
defer g.rwMutex.Unlock()

err := g.AddEdge(fromNodeId, toNodeId, EdgeAttrs{})
err := g.AddEdge(fromNodeId, toNodeId)
if err != nil {
return nil
}
Expand Down Expand Up @@ -147,6 +165,14 @@ func (g *Graph) unsafeEdgeExists(fromNodeId NodeId, toNodeId NodeId) bool {
return false
}

func (g *Graph) SetEdgeA2B(fromNodeId NodeId, toNodeId NodeId, a2b int) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
g.Edges[fromNodeId][toNodeId].Attrs.A2B = a2b
return true
}
return false
}

func (g *Graph) SetEdgeData(fromNodeId NodeId, toNodeId NodeId, edgeAttrs EdgeAttrs) bool {
if g.EdgeExists(fromNodeId, toNodeId) {
g.Edges[fromNodeId][toNodeId].Attrs = edgeAttrs
Expand Down
17 changes: 4 additions & 13 deletions model/parts/update/update_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@ func Graph(state *types.State, requestResult types.RequestResult, curTimeStep in
continue
} else {
if !config.IsPayOnlyForCurrentRequest() {
newEdgeData1 := edgeData1
newEdgeData1.A2B = 0
state.Graph.SetEdgeData(payment.FirstNodeId, payment.PayNextId, newEdgeData1)

newEdgeData2 := edgeData2
newEdgeData2.A2B = 0
state.Graph.SetEdgeData(payment.PayNextId, payment.FirstNodeId, newEdgeData2)
state.Graph.SetEdgeA2B(payment.FirstNodeId, payment.PayNextId, 0)
state.Graph.SetEdgeA2B(payment.PayNextId, payment.FirstNodeId, 0)
} else {
// Important fix: Reduce debt here, since it debt will be added again below.
// Idea is, paying for the current request should not effect the edge balance.
newEdgeData1 := edgeData1
newEdgeData1.A2B = edgeData1.A2B - price
state.Graph.SetEdgeData(payment.FirstNodeId, payment.PayNextId, newEdgeData1)
Copy link
Collaborator

@VHPL-UIS VHPL-UIS Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you have removed some code while Arman had them in the latest version he pushed. Should we keep your code? @leandernikolaus

state.Graph.SetEdgeA2B(payment.FirstNodeId, payment.PayNextId, edgeData1.A2B-price)
}
}
// fmt.Println("Payment from ", payment.FirstNodeId, " to ", payment.PayNextId, " for chunk ", payment.ChunkId, " with price ", actualPrice)
Expand All @@ -58,9 +51,7 @@ func Graph(state *types.State, requestResult types.RequestResult, curTimeStep in
providerNode := route[i+1]
price := utils.PeerPriceChunk(providerNode, chunkId)
edgeData := state.Graph.GetEdgeData(requesterNode, providerNode)
newEdgeData := edgeData
newEdgeData.A2B += price
state.Graph.SetEdgeData(requesterNode, providerNode, newEdgeData)
state.Graph.SetEdgeA2B(requesterNode, providerNode, edgeData.A2B+price)

if config.GetMaxPOCheckEnabled() {
nodePairWithPrice = types.NodePairWithPrice{RequesterNode: requesterNode, ProviderNode: providerNode, Price: price}
Expand Down
4 changes: 1 addition & 3 deletions model/parts/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func CreateGraphNetwork(net *types.Network) (*types.Graph, error) {
nodeAdj := node.AdjIds
for _, adjItems := range nodeAdj {
for _, otherNodeId := range adjItems {
threshold := general.BitLength(nodeId.ToInt() ^ otherNodeId.ToInt())
attrs := types.EdgeAttrs{A2B: 0, LastEpoch: 0, Threshold: threshold}
err := graph.AddEdge(node.Id, otherNodeId, attrs)
err := graph.AddEdge(node.Id, otherNodeId)
if err != nil {
return nil, err
}
Expand Down
12 changes: 1 addition & 11 deletions model/routing/forgiveness.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package routing

import (
"go-incentive-simulation/config"
"go-incentive-simulation/model/parts/types"
"math"
)

func CheckForgiveness(edgeData types.EdgeAttrs, firstNodeId types.NodeId, secondNodeId types.NodeId, graph *types.Graph, request types.Request) (int, bool) {
Expand All @@ -13,10 +11,7 @@ func CheckForgiveness(edgeData types.EdgeAttrs, firstNodeId types.NodeId, second
return edgeData.A2B, false
}

refreshRate := config.GetRefreshRate()
if config.IsAdjustableThreshold() {
refreshRate = GetAdjustedRefreshrate(edgeData.Threshold, config.GetThreshold(), config.GetRefreshRate(), config.GetAdjustableThresholdExponent())
}
refreshRate := edgeData.Refreshrate

removedDeptAmount := passedTime * refreshRate
newEdgeData := edgeData
Expand All @@ -29,8 +24,3 @@ func CheckForgiveness(edgeData types.EdgeAttrs, firstNodeId types.NodeId, second

return newEdgeData.A2B, true
}

func GetAdjustedRefreshrate(adjustedThreshold, threshold, refreshRate, power int) int {
ratio := float64(adjustedThreshold) / float64(threshold)
return int(math.Ceil(float64(refreshRate) * math.Pow(ratio, float64(power))))
}
5 changes: 1 addition & 4 deletions model/routing/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ func IsThresholdFailed(firstNodeId types.NodeId, secondNodeId types.NodeId, grap
edgeDataSecond := graph.GetEdgeData(secondNodeId, firstNodeId)
p2pSecond := edgeDataSecond.A2B

threshold := config.GetThreshold()
if config.IsAdjustableThreshold() {
threshold = edgeDataFirst.Threshold
}
threshold := edgeDataFirst.Threshold

peerPriceChunk := utils.PeerPriceChunk(secondNodeId, request.ChunkId)

Expand Down