Pigeon-Ruby/lib/pigeon/storage.rb

188 lines
4.3 KiB
Ruby
Raw Normal View History

2019-11-27 01:46:50 +00:00
require "pstore"
2019-09-22 02:30:03 +00:00
module Pigeon
class Storage
attr_reader :path
def initialize(path: PIGEON_DB_PATH)
@path = path
2020-04-17 13:55:18 +00:00
store.ultra_safe = true
bootstrap
end
def reset
2020-04-17 13:55:18 +00:00
File.delete(path) if on_disk?
bootstrap
2019-12-28 04:44:10 +00:00
end
def add_peer(identity)
write { store[PEER_NS].add(identity) }
identity
end
def remove_peer(identity)
write { store[PEER_NS].delete(identity) }
identity
2020-03-26 14:00:17 +00:00
end
def block_peer(identity)
remove_peer(identity)
write { store[BLCK_NS].add(identity) }
identity
end
2019-12-05 04:11:04 +00:00
def all_peers
read { store[PEER_NS].to_a }
end
def all_blocks
read { store[BLCK_NS].to_a }
2020-04-03 10:19:42 +00:00
end
def _get_config(key)
read { store[CONF_NS][key] }
end
def _add_config(key, value)
2020-04-17 13:55:18 +00:00
write do
a = store.fetch(CONF_NS)
raise "FIX SAVED DRAFTS" if value.instance_variable_get(:@db)
2020-04-25 15:11:25 +00:00
2020-04-17 13:55:18 +00:00
a[key] = value
end
2019-09-22 02:30:03 +00:00
end
def add_blob(data)
size = data.bytesize
2020-04-25 15:11:25 +00:00
if size > BLOB_BYTE_LIMIT
raise "Blob size limit is #{BLOB_BYTE_LIMIT} bytes. Got #{size}"
end
2020-04-25 15:11:25 +00:00
raw_digest = Digest::SHA256.digest(data)
2020-04-17 13:55:18 +00:00
b32_hash = Helpers.b32_encode(raw_digest)
multihash = [BLOB_SIGIL, b32_hash, BLOB_FOOTER].join("")
2020-04-22 13:21:55 +00:00
write_to_disk(multihash, data)
multihash
2019-09-22 11:00:19 +00:00
end
def get_blob(blob_multihash)
path1 = File.join(Helpers.hash2file_path(blob_multihash))
path2 = File.join(PIGEON_BLOB_PATH, path1)
File.read(path2) if File.file?(path2)
2019-09-22 02:30:03 +00:00
end
# `nil` means "none"
def get_message_count_for(mhash)
read { store[COUNT_INDEX_NS][mhash] || 0 }
end
def all_messages(author)
if author
all = []
depth = -1
last = ""
2020-04-19 21:27:09 +00:00
# TODO: This loop may become unresponsive.
2020-04-25 15:11:25 +00:00
until last.nil? || (depth > 99_999)
last = get_message_by_depth(author, depth += 1)
all.push(last) if last
end
2020-04-25 15:11:25 +00:00
all
else
read { store["messages"].keys }
end
end
def get_message_by_depth(multihash, depth)
# Map<[multihash(str), depth(int)], Signature>
key = [multihash, depth].join(".")
read { store[MESSAGE_BY_DEPTH_NS][key] }
end
def read_message(multihash)
read { store[MESG_NS].fetch(multihash) }
end
2020-04-18 14:59:56 +00:00
def insert_message(msg)
write do
2020-04-25 15:11:25 +00:00
return msg if store[MESG_NS].fetch(msg.multihash, false)
2020-04-20 02:32:37 +00:00
if store[BLCK_NS].member?(msg.author.multihash)
2020-04-25 15:11:25 +00:00
warn("Blocked peer: #{msg.author.multihash}")
2020-04-20 02:32:37 +00:00
return msg
end
insert_and_update_index(msg)
msg
end
end
2020-04-26 13:51:14 +00:00
def have_message?(multihash)
2020-04-20 02:32:37 +00:00
read { store[MESG_NS].fetch(multihash, false) }
end
def peer_blocked?(multihash)
read { store[BLCK_NS].member?(multihash) }
end
def have_blob?(multihash)
path = File.join(PIGEON_BLOB_PATH, Helpers.hash2file_path(multihash))
File.file?(path)
end
private
2020-04-22 13:21:55 +00:00
def write_to_disk(mhash, data)
Helpers.write_to_disk(PIGEON_BLOB_PATH, mhash, data)
end
def bootstrap
write do
2020-04-17 13:55:18 +00:00
store[BLCK_NS] ||= Set.new
store[CONF_NS] ||= {}
2020-04-17 13:55:18 +00:00
store[COUNT_INDEX_NS] ||= {}
store[MESG_NS] ||= {}
2020-04-17 13:55:18 +00:00
store[MESSAGE_BY_DEPTH_NS] ||= {}
store[PEER_NS] ||= Set.new
2019-12-28 04:44:10 +00:00
end
Helpers.mkdir_p(PIGEON_BLOB_PATH)
store
2019-12-28 04:44:10 +00:00
end
2019-11-27 01:46:50 +00:00
def store
2020-04-17 13:55:18 +00:00
@store ||= PStore.new(PIGEON_DB_PATH)
end
def insert_and_update_index(message)
pub_key = message.author.multihash
# STEP 1: Update MESG_NS, the main storage spot.
store[MESG_NS][message.multihash] = message
# STEP 2: Update the "message by author and depth" index
# this index is used to find a person's nth
# message
2020-03-08 16:35:09 +00:00
# SECURITY AUDIT: How can we be certain the message is
# not lying about its depth?
key = [pub_key, message.depth].join(".")
store[MESSAGE_BY_DEPTH_NS][key] = message.multihash
store[COUNT_INDEX_NS][pub_key] ||= 0
store[COUNT_INDEX_NS][pub_key] += 1
2019-09-25 00:48:02 +00:00
end
def transaction(is_read_only)
store.transaction(is_read_only) { yield }
end
2020-04-25 15:11:25 +00:00
def write(&blk)
transaction(false, &blk)
end
def read(&blk)
transaction(true, &blk)
end
def on_disk?
File.file?(path)
end
2019-09-22 02:30:03 +00:00
end
end