Skip to content

Instantly share code, notes, and snippets.

@jugf
Last active February 12, 2021 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jugf/8cbc0513e55afea802b3446ca03ec4cf to your computer and use it in GitHub Desktop.
Save jugf/8cbc0513e55afea802b3446ca03ec4cf to your computer and use it in GitHub Desktop.
Visualizing Trust: Trustgraphs of CirclesUBI in Neo4j
// NOTE, check if statement is correct, it speeds up process dramatically (100-1000x)
CREATE CONSTRAINT ON (u:User) ASSERT u.address IS UNIQUE;
// Not necessary if 4G is available
// :auto USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM 'file:///circles_hub_trust_events.csv' AS row
MERGE (u1:User {address: row.truster})
MERGE (u2:User {address: row.trustee})
MERGE (u1)-[r:TRUSTS]->(u2)
ON CREATE SET r.blockNumber = toInteger(row.blockNumber), r.amount = toFloat(row.amount);
@Query("MATCH path = shortestPath( (you:User {address:$truster})-[*]->(other:User {address:$trustee}) )\n" +
"RETURN nodes(path)")
List<User> findTrustGraph(@Param("truster") String truster, @Param("trustee") String trustee);
// User.java
package movies.spring.data.neo4j.movies;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import java.util.ArrayList;
import java.util.List;
@Node
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)
public class User {
@Id
@EqualsAndHashCode.Include
@ToString.Include
private String address;
public User(String address) {
this.address = address;
}
@Relationship(type = "TRUSTS", direction = Relationship.Direction.OUTGOING)
@JsonIgnore
private List<TrustedUser> trustedUsers = new ArrayList<>();
}
// TrustedUser.java
package movies.spring.data.neo4j.movies;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
import java.math.BigInteger;
@RelationshipProperties
@Data
public class TrustedUser {
private BigInteger blockNumber;
private Integer amount;
@TargetNode
private User trustedUser;
public TrustedUser(User trustedUser, BigInteger blockNumber, Integer amount) {
this.trustedUser = trustedUser;
this.blockNumber = blockNumber;
this.amount = amount;
}
}
// Number of Nodes: 105937
Match (n)
Return count(n)
// Number of Relations: 602203
MATCH ()-[r]->()
RETURN count(r) as count
// Verify: Groovyscript for verification
//105936
//602204
def graph = new File("C:\\Users\\aleculum\\.Neo4jDesktop\\relate-data\\dbmss\\dbms-391b9fb8-b0d9-433d-9238-6827c0e6a034\\import\\trustgraph_complete.csv").text
def lines = graph.split("\\n")
def addresses = []
def rels = []
int counter = 0
lines.each {
def splits = it.split(",")
if (!addresses.contains(splits[1])) {
addresses.add(splits[1])
}
if (!rels.contains(splits[1]+splits[2])) {
rels.add(splits[1]+splits[2])
}
if (++counter % 1000 == 0) {
println "$counter"
}
}
println(addresses.size())
println(rels.size())
MATCH path = shortestPath( (you:User {address:"0x5648dd2ee24a9526b4868444cd714162002cbe5e"})-[*]->(other:User {address:"0x249fa3ecd95a53f742707d53688fcafbbd072f33"}) )
RETURN path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment