Skip to content

Commit

Permalink
feat(db): add "network" column to content table
Browse files Browse the repository at this point in the history
  • Loading branch information
ogenev committed Dec 13, 2023
1 parent 6dbb845 commit 319c983
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
40 changes: 40 additions & 0 deletions ethportal-api/src/types/portal_wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ impl TryFrom<ProtocolId> for Vec<u8> {
}
}

impl From<ProtocolId> for u8 {
fn from(protocol_id: ProtocolId) -> Self {
match protocol_id {
ProtocolId::State => 2,
ProtocolId::History => 0,
ProtocolId::TransactionGossip => 3,
ProtocolId::CanonicalIndices => 4,
ProtocolId::Beacon => 1,
ProtocolId::Utp => 99,
}
}
}

/// A Portal protocol message.
#[derive(Debug, PartialEq, Clone, Encode, Decode)]
#[ssz(enum_behaviour = "union")]
Expand Down Expand Up @@ -592,6 +605,33 @@ mod test {
assert_eq!(hex, expected_hex);
}

#[test]
fn prtocol_id_to_u8() {
let protocol_id = ProtocolId::History;
let expected_u8 = 0;
assert_eq!(expected_u8, u8::from(protocol_id));

let protocol_id = ProtocolId::Beacon;
let expected_u8 = 1;
assert_eq!(expected_u8, u8::from(protocol_id));

let protocol_id = ProtocolId::State;
let expected_u8 = 2;
assert_eq!(expected_u8, u8::from(protocol_id));

let protocol_id = ProtocolId::TransactionGossip;
let expected_u8 = 3;
assert_eq!(expected_u8, u8::from(protocol_id));

let protocol_id = ProtocolId::CanonicalIndices;
let expected_u8 = 4;
assert_eq!(expected_u8, u8::from(protocol_id));

let protocol_id = ProtocolId::Utp;
let expected_u8 = 99;
assert_eq!(expected_u8, u8::from(protocol_id));
}

// Wire message test vectors available in Ethereum Portal Network specs repo:
// github.com/ethereum/portal-network-specs

Expand Down
23 changes: 15 additions & 8 deletions portalnet/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub struct PortalStorage {
sql_connection_pool: Pool<SqliteConnectionManager>,
distance_fn: DistanceFunction,
metrics: StorageMetricsReporter,
network: ProtocolId,
}

impl ContentStore for PortalStorage {
Expand Down Expand Up @@ -274,6 +275,7 @@ impl PortalStorage {
sql_connection_pool: config.sql_connection_pool,
distance_fn: config.distance_fn,
metrics,
network: protocol,
};

// Set the metrics to the default radius, to start
Expand Down Expand Up @@ -578,6 +580,7 @@ impl PortalStorage {
content_id_as_u32,
content_key,
hex_encode(value),
u8::from(self.network),
value_size
],
) {
Expand Down Expand Up @@ -635,11 +638,12 @@ impl PortalStorage {
let conn = self.sql_connection_pool.get()?;
let mut query = conn.prepare(XOR_FIND_FARTHEST_QUERY)?;

let mut result = query.query_map([node_id_u32], |row| {
Ok(ContentId {
id_long: row.get(0)?,
})
})?;
let mut result =
query.query_map([node_id_u32, u8::from(self.network).into()], |row| {
Ok(ContentId {
id_long: row.get(0)?,
})
})?;

let result = match result.next() {
Some(row) => row,
Expand Down Expand Up @@ -746,22 +750,25 @@ const CREATE_QUERY: &str = "CREATE TABLE IF NOT EXISTS content_data (
content_id_short INTEGER NOT NULL,
content_key TEXT NOT NULL,
content_value TEXT NOT NULL,
network INTEGER NOT NULL DEFAULT 0,
content_size INTEGER
);
CREATE INDEX content_size_idx ON content_data(content_size);
CREATE INDEX content_id_short_idx ON content_data(content_id_short);
CREATE INDEX content_id_long_idx ON content_data(content_id_long);";
CREATE INDEX content_id_long_idx ON content_data(content_id_long);
CREATE INDEX network_idx ON content_data(network);";

const INSERT_QUERY: &str =
"INSERT OR IGNORE INTO content_data (content_id_long, content_id_short, content_key, content_value, content_size)
VALUES (?1, ?2, ?3, ?4, ?5)";
"INSERT OR IGNORE INTO content_data (content_id_long, content_id_short, content_key, content_value, network, content_size)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)";

const DELETE_QUERY: &str = "DELETE FROM content_data
WHERE content_id_long = (?1)";

const XOR_FIND_FARTHEST_QUERY: &str = "SELECT
content_id_long
FROM content_data
WHERE network = (?2)
ORDER BY ((?1 | content_id_short) - (?1 & content_id_short)) DESC";

const CONTENT_KEY_LOOKUP_QUERY: &str =
Expand Down

0 comments on commit 319c983

Please sign in to comment.