From 5086f11a5a386a4192a23324d56239b8daebfbaf Mon Sep 17 00:00:00 2001 From: balasan Date: Wed, 4 Nov 2020 19:35:32 -0800 Subject: [PATCH] full lint --- deterministic/graph.go | 26 +++++++++++++------------- deterministic/test_utils.go | 3 +++ non-deterministic/graph.go | 30 +++++++++++++++--------------- non-deterministic/pagerank_test.go | 8 ++++---- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/deterministic/graph.go b/deterministic/graph.go index e329df7..ff618bf 100644 --- a/deterministic/graph.go +++ b/deterministic/graph.go @@ -30,10 +30,10 @@ const ( Negative ) -// Default decimals used in computation +// Decimals is the default decimals used in computation const Decimals = 18 -// MAX_NEG_OFFSET defines the cutoff for when a node will have it's outging links counted +// MaxNegOffset defines the cutoff for when a node will have it's outging links counted // if previously NegativeRank / PositiveRank > MaxNegOffset / (MaxNegOffset + 1) we will not consider // any outgoing links // otherwise, we counter the outgoing lings with one 'heavy' link proportional to the MaxNegOffset ratio @@ -42,12 +42,12 @@ const MaxNegOffset = 10 // NodeInput is struct passing data to the graph // TODO does it make sense to just use the Node type? type NodeInput struct { - Id string + ID string PRank sdk.Uint NRank sdk.Uint } -// Internal node struct +// Node is an internal node struct type Node struct { id string rank sdk.Uint // pos page rank of the node @@ -87,7 +87,7 @@ func NewGraph(α sdk.Uint, ε sdk.Uint, negConsumerRank sdk.Uint) *Graph { ε: ε, personalization: make([]string, 0), }, - negConsumer: NodeInput{Id: "negConsumer", PRank: negConsumerRank, NRank: sdk.ZeroUint()}, + negConsumer: NodeInput{ID: "negConsumer", PRank: negConsumerRank, NRank: sdk.ZeroUint()}, Precision: sdk.NewUintFromBigInt(sdk.NewIntWithDecimal(1, Decimals).BigInt()), MaxNegOffset: sdk.NewUintFromBigInt(sdk.NewIntWithDecimal(MaxNegOffset, Decimals).BigInt()), } @@ -95,14 +95,14 @@ func NewGraph(α sdk.Uint, ε sdk.Uint, negConsumerRank sdk.Uint) *Graph { // NewNodeInput is ahelper method to create a node input struct func NewNodeInput(id string, pRank sdk.Uint, nRank sdk.Uint) NodeInput { - return NodeInput{Id: id, PRank: pRank, NRank: nRank} + return NodeInput{ID: id, PRank: pRank, NRank: nRank} } // AddPersonalizationNode adds a node to the pagerank personlization vector // these nodes will have high rank by default and all other rank will stem from them // this makes non-personalaziation nodes sybil resistant (they cannot increase their own rank) func (graph *Graph) AddPersonalizationNode(pNode NodeInput) { - graph.params.personalization = append(graph.params.personalization, pNode.Id) + graph.params.personalization = append(graph.params.personalization, pNode.ID) // this to ensures source nodes exist graph.InitPosNode(pNode) } @@ -120,7 +120,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) { } } - sourceKey := getKey(source.Id, Positive) + sourceKey := getKey(source.ID, Positive) sourceNode := graph.initNode(sourceKey, source, Positive) // if weight is negative we use negative receiving node @@ -133,7 +133,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) { nodeType = Positive weightUint = sdk.NewUintFromBigInt(weight.BigInt()) } - targetKey := getKey(target.Id, nodeType) + targetKey := getKey(target.ID, nodeType) graph.initNode(targetKey, target, nodeType) @@ -150,7 +150,7 @@ func (graph *Graph) Link(source, target NodeInput, weight sdk.Int) { graph.edges[sourceKey][targetKey] = graph.edges[sourceKey][targetKey].Add(weightUint) // note: use target.id here to make sure we reference the original id - graph.cancelOpposites(*sourceNode, target.Id, nodeType) + graph.cancelOpposites(*sourceNode, target.ID, nodeType) } // Finalize is the method that runs after all other inits and before pagerank @@ -179,7 +179,7 @@ func (graph *Graph) processNegatives() { if posNode.rank.IsZero() || negNode.rank.IsZero() { return } - negConsumer := graph.initNode(negConsumerInput.Id, negConsumerInput, Positive) + negConsumer := graph.initNode(negConsumerInput.ID, negConsumerInput, Positive) one := graph.Precision @@ -251,14 +251,14 @@ func (graph *Graph) cancelOpposites(sourceNode Node, target string, nodeType Nod // InitPosNode initialized a positive node func (graph *Graph) InitPosNode(inputNode NodeInput) *Node { - return graph.initNode(inputNode.Id, inputNode, Positive) + return graph.initNode(inputNode.ID, inputNode, Positive) } // initNode initialized a node func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType) *Node { if _, ok := graph.nodes[key]; ok == false { graph.nodes[key] = &Node{ - id: inputNode.Id, // id is independent of pos/neg keys + id: inputNode.ID, // id is independent of pos/neg keys degree: sdk.ZeroUint(), rank: sdk.ZeroUint(), rankNeg: sdk.ZeroUint(), diff --git a/deterministic/test_utils.go b/deterministic/test_utils.go index 13c00fb..9fef8bb 100644 --- a/deterministic/test_utils.go +++ b/deterministic/test_utils.go @@ -11,14 +11,17 @@ func FtoBD(n float64) sdk.Uint { return sdk.NewUint(uint64(n * math.Pow(10, float64(Decimals)))) } +// NewGraphHelper is test helper that allows use of floats func NewGraphHelper(α, ε float64, negConsumerRank sdk.Uint) *Graph { return NewGraph(FtoBD(α), FtoBD(ε), negConsumerRank) } +// NewNodeInputHelper is test helper that allows use of floats func NewNodeInputHelper(id string, pRank float64, nRank float64) NodeInput { return NewNodeInput(id, FtoBD(pRank), FtoBD(nRank)) } +// LinkHelper is test helper that allows use of floats func (graph Graph) LinkHelper(source, target NodeInput, weight float64) { weightInt := sdk.NewInt(int64(weight * math.Pow(10, float64(Decimals/2)))) weightInt = weightInt.Mul(sdk.NewInt(int64(math.Pow(10, float64(Decimals/2))))) diff --git a/non-deterministic/graph.go b/non-deterministic/graph.go index cca25be..8d989d2 100644 --- a/non-deterministic/graph.go +++ b/non-deterministic/graph.go @@ -17,11 +17,11 @@ import ( "strconv" ) -// this is defines the cutoff for when a node will have it's outging links counted +// MaxNegOffset defines the cutoff for when a node will have it's outging links counted // if previously NegativeRank / PositiveRank > MaxNegOffset / (MaxNegOffset + 1) we will not consider // any outgoing links // otherwise, we counter the outgoing lings with one 'heavy' link proportional to the MaxNegOffset ratio -const MAX_NEG_OFFSET = float64(10) +const MaxNegOffset = float64(10) // NodeType is positive or negative // each node in the graph can be represented by two nodes, @@ -38,7 +38,7 @@ const ( // NodeInput is struct passing data to the graph // TODO does it make sense to just use the Node type? type NodeInput struct { - Id string + ID string PRank float64 NRank float64 } @@ -81,20 +81,20 @@ func NewGraph(α, ε, negConsumerRank float64) *Graph { ε: ε, personalization: make([]string, 0), }, - negConsumer: NodeInput{Id: "negConsumer", PRank: negConsumerRank, NRank: 0}, + negConsumer: NodeInput{ID: "negConsumer", PRank: negConsumerRank, NRank: 0}, } } // NewNodeInput is ahelper method to create a node input struct func NewNodeInput(id string, pRank float64, nRank float64) NodeInput { - return NodeInput{Id: id, PRank: pRank, NRank: nRank} + return NodeInput{ID: id, PRank: pRank, NRank: nRank} } // AddPersonalizationNode adds a node to the pagerank personlization vector // these nodes will have high rank by default and all other rank will stem from them // this makes non-personalaziation nodes sybil resistant (they cannot increase their own rank) func (graph *Graph) AddPersonalizationNode(pNode NodeInput) { - graph.params.personalization = append(graph.params.personalization, pNode.Id) + graph.params.personalization = append(graph.params.personalization, pNode.ID) // this to ensures source nodes exist graph.InitPosNode(pNode) } @@ -104,11 +104,11 @@ func (graph *Graph) AddPersonalizationNode(pNode NodeInput) { func (graph *Graph) Link(source, target NodeInput, weight float64) { // if a node's neg/post rank ration is too high we don't process its links - if source.PRank > 0 && source.NRank/source.PRank > MAX_NEG_OFFSET/(MAX_NEG_OFFSET+1) { + if source.PRank > 0 && source.NRank/source.PRank > MaxNegOffset/(MaxNegOffset+1) { return } - sourceKey := getKey(source.Id, Positive) + sourceKey := getKey(source.ID, Positive) sourceNode := graph.initNode(sourceKey, source, Positive) // if weight is negative we use negative receiving node @@ -116,7 +116,7 @@ func (graph *Graph) Link(source, target NodeInput, weight float64) { if nodeType = Positive; weight < 0 { nodeType = Negative } - targetKey := getKey(target.Id, nodeType) + targetKey := getKey(target.ID, nodeType) graph.initNode(targetKey, target, nodeType) @@ -128,7 +128,7 @@ func (graph *Graph) Link(source, target NodeInput, weight float64) { graph.edges[sourceKey][targetKey] += math.Abs(weight) // note: use target.id here to make sure we reference the original id - graph.cancelOpposites(*sourceNode, target.Id, nodeType) + graph.cancelOpposites(*sourceNode, target.ID, nodeType) } // Finalize is the method that runs after all other inits and before pagerank @@ -161,14 +161,14 @@ func (graph *Graph) processNegatives() { panic("negative ranking nodes should not have any degree") // this should never happen } - negConsumer := graph.initNode(negConsumerInput.Id, negConsumerInput, Positive) + negConsumer := graph.initNode(negConsumerInput.ID, negConsumerInput, Positive) var negMultiple float64 // this first case should not happen because we ignore these links - if negNode.rank/posNode.rank > MAX_NEG_OFFSET/(MAX_NEG_OFFSET+1) { + if negNode.rank/posNode.rank > MaxNegOffset/(MaxNegOffset+1) { // cap the degree multiple at MAX_NEG_OFFSET - negMultiple = MAX_NEG_OFFSET + negMultiple = MaxNegOffset } else { negMultiple = 1/(1-negNode.rank/posNode.rank) - 1 } @@ -221,14 +221,14 @@ func (graph *Graph) cancelOpposites(sourceNode Node, target string, nodeType Nod // InitPosNode is a helper method that initializes a positive node func (graph *Graph) InitPosNode(inputNode NodeInput) *Node { - return graph.initNode(inputNode.Id, inputNode, Positive) + return graph.initNode(inputNode.ID, inputNode, Positive) } // initNode initializes a node func (graph *Graph) initNode(key string, inputNode NodeInput, nodeType NodeType) *Node { if _, ok := graph.nodes[key]; ok == false { graph.nodes[key] = &Node{ - id: inputNode.Id, // id is independent of pos/neg keys + id: inputNode.ID, // id is independent of pos/neg keys degree: 0, nodeType: nodeType, } diff --git a/non-deterministic/pagerank_test.go b/non-deterministic/pagerank_test.go index e931527..b5c8e52 100644 --- a/non-deterministic/pagerank_test.go +++ b/non-deterministic/pagerank_test.go @@ -328,8 +328,8 @@ func TestMaxNeg(t *testing.T) { graph.AddPersonalizationNode(a) - graph.Link(a, b, MAX_NEG_OFFSET+1) - graph.Link(a, c, MAX_NEG_OFFSET+2) + graph.Link(a, b, MaxNegOffset+1) + graph.Link(a, c, MaxNegOffset+2) graph.Link(c, d, 1.0) graph.Link(b, d, -1.0) graph.Link(d, e, 1.0) @@ -357,8 +357,8 @@ func TestMaxNeg(t *testing.T) { graph.AddPersonalizationNode(a) - graph.Link(a, b, MAX_NEG_OFFSET+1) - graph.Link(a, c, MAX_NEG_OFFSET+2) + graph.Link(a, b, MaxNegOffset+1) + graph.Link(a, c, MaxNegOffset+2) graph.Link(c, d, 1.0) graph.Link(b, d, -1.0) graph.Link(d, e, 1.0)