2019-11-27 01:46:50 +00:00
|
|
|
require "pstore"
|
|
|
|
|
2019-09-22 02:30:03 +00:00
|
|
|
module Pigeon
|
|
|
|
class Storage
|
2019-12-28 04:44:10 +00:00
|
|
|
def self.reset
|
2020-03-15 16:51:13 +00:00
|
|
|
File.delete(PIGEON_DB_PATH) if File.file?(PIGEON_DB_PATH)
|
2019-12-28 04:44:10 +00:00
|
|
|
@current = nil
|
|
|
|
end
|
|
|
|
|
2019-09-22 02:30:03 +00:00
|
|
|
def self.current
|
|
|
|
@current ||= self.new
|
|
|
|
end
|
|
|
|
|
2020-03-26 13:17:50 +00:00
|
|
|
def get_message_by_depth(multihash, depth)
|
|
|
|
raise "Expected string, got #{multihash.class}" unless multihash.is_a?(String) # Delete later
|
2020-03-29 15:26:41 +00:00
|
|
|
# Map<[multihash(str), depth(int)], Signature>
|
|
|
|
key = [multihash, depth].join(".")
|
|
|
|
read { store[MESSAGE_BY_DEPTH_NS][key] }
|
2020-03-06 14:42:19 +00:00
|
|
|
end
|
|
|
|
|
2020-03-29 00:43:59 +00:00
|
|
|
# `nil` means "none"
|
|
|
|
#
|
2020-03-29 15:26:41 +00:00
|
|
|
def get_message_count_for(mhash)
|
|
|
|
raise "Expected string, got #{mhash.class}" unless mhash.is_a?(String) # Delete later
|
|
|
|
read { store[COUNT_INDEX_NS][mhash] || 0 }
|
2020-03-26 14:00:17 +00:00
|
|
|
end
|
|
|
|
|
2020-03-06 14:42:19 +00:00
|
|
|
def message_count
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[MESG_NS].count }
|
2020-03-06 14:42:19 +00:00
|
|
|
end
|
2019-12-11 04:05:54 +00:00
|
|
|
|
2020-03-06 14:42:19 +00:00
|
|
|
def save_message(msg)
|
2020-03-29 15:26:41 +00:00
|
|
|
write do
|
2020-03-11 13:43:54 +00:00
|
|
|
insert_and_update_index(msg)
|
2020-03-14 02:59:13 +00:00
|
|
|
msg
|
2020-03-06 14:42:19 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-05 04:11:04 +00:00
|
|
|
|
2020-03-15 16:51:13 +00:00
|
|
|
def find_message(multihash)
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[MESG_NS].fetch(multihash) }
|
2020-03-15 16:51:13 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 18:50:00 +00:00
|
|
|
def find_all
|
|
|
|
# TODO: Ability to pass an author ID to `find-all`
|
2020-03-24 12:39:32 +00:00
|
|
|
author = Pigeon::LocalIdentity.current
|
2020-03-15 18:50:00 +00:00
|
|
|
store = Pigeon::Storage.current
|
|
|
|
all = []
|
|
|
|
depth = -1
|
|
|
|
last = ""
|
|
|
|
until (last == nil) || (depth > 999999)
|
|
|
|
last = store.get_message_by_depth(author, depth += 1)
|
|
|
|
all.push(last) if last
|
|
|
|
end
|
|
|
|
return all
|
|
|
|
end
|
|
|
|
|
2019-10-12 20:40:16 +00:00
|
|
|
def set_config(key, value)
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[CONF_NS][key] = value }
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
2019-10-26 23:13:06 +00:00
|
|
|
def delete_config(key)
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[CONF_NS].delete(key) }
|
2019-10-26 23:13:06 +00:00
|
|
|
end
|
|
|
|
|
2019-10-12 20:40:16 +00:00
|
|
|
def get_config(key)
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[CONF_NS][key] }
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_blob(data)
|
2020-03-14 02:59:13 +00:00
|
|
|
raw_digest = Digest::SHA256.hexdigest(data)
|
|
|
|
b64_digest = Base64.urlsafe_encode64(raw_digest)
|
|
|
|
multihash = [BLOB_SIGIL, b64_digest, BLOB_FOOTER].join("")
|
|
|
|
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[BLOB_NS][multihash] = data }
|
2020-03-14 02:59:13 +00:00
|
|
|
|
|
|
|
multihash
|
2019-09-22 11:00:19 +00:00
|
|
|
end
|
|
|
|
|
2020-03-14 02:59:13 +00:00
|
|
|
def get_blob(blob_multihash)
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[BLOB_NS][blob_multihash] }
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
2019-09-23 01:07:14 +00:00
|
|
|
def add_peer(identity)
|
2020-03-27 12:07:48 +00:00
|
|
|
path = Helpers.decode_multihash(identity)
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[PEER_NS].add(identity) }
|
2019-09-25 01:19:25 +00:00
|
|
|
identity
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_peer(identity)
|
2020-03-27 12:07:48 +00:00
|
|
|
path = Helpers.decode_multihash(identity)
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[PEER_NS].delete(identity) }
|
2019-09-25 01:19:25 +00:00
|
|
|
identity
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def block_peer(identity)
|
2019-11-27 01:55:52 +00:00
|
|
|
remove_peer(identity)
|
2020-03-29 15:26:41 +00:00
|
|
|
write { store[BLCK_NS].add(identity) }
|
2019-11-27 01:55:52 +00:00
|
|
|
identity
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_peers
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[PEER_NS].to_a }
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
2019-10-10 01:27:48 +00:00
|
|
|
def all_blocks
|
2020-03-29 15:26:41 +00:00
|
|
|
read { store[BLCK_NS].to_a }
|
2019-10-10 01:27:48 +00:00
|
|
|
end
|
|
|
|
|
2020-03-14 03:34:29 +00:00
|
|
|
def bootstrap
|
2020-03-29 15:26:41 +00:00
|
|
|
write do
|
2020-03-29 00:43:59 +00:00
|
|
|
# Wait what? Why is there a depth and count
|
|
|
|
# index??
|
|
|
|
store[MESSAGE_BY_DEPTH_NS] ||= {}
|
2020-03-29 15:26:41 +00:00
|
|
|
store[COUNT_INDEX_NS] ||= {}
|
2020-03-14 03:34:29 +00:00
|
|
|
store[BLOB_NS] ||= {}
|
|
|
|
store[CONF_NS] ||= {}
|
|
|
|
store[MESG_NS] ||= {}
|
|
|
|
store[BLCK_NS] ||= Set.new
|
|
|
|
store[PEER_NS] ||= Set.new
|
2019-12-28 04:44:10 +00:00
|
|
|
end
|
2020-03-14 02:59:13 +00:00
|
|
|
store
|
2019-12-28 04:44:10 +00:00
|
|
|
end
|
|
|
|
|
2019-11-27 01:46:50 +00:00
|
|
|
private
|
2019-09-23 01:07:14 +00:00
|
|
|
|
2019-11-27 01:46:50 +00:00
|
|
|
def store
|
2020-03-14 02:59:13 +00:00
|
|
|
if @store
|
|
|
|
return @store
|
|
|
|
else
|
2020-03-08 16:18:05 +00:00
|
|
|
@store = PStore.new(PIGEON_DB_PATH)
|
2020-03-14 03:34:29 +00:00
|
|
|
bootstrap
|
2020-03-08 16:18:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-11 13:43:54 +00:00
|
|
|
def insert_and_update_index(message)
|
2020-03-29 00:43:59 +00:00
|
|
|
pub_key = message.author.public_key
|
2020-03-11 13:43:54 +00:00
|
|
|
# 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?
|
2020-03-29 15:26:41 +00:00
|
|
|
key = [pub_key, message.depth].join(".")
|
2020-03-29 00:43:59 +00:00
|
|
|
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
|
2020-03-29 15:26:41 +00:00
|
|
|
|
|
|
|
def transaction(is_read_only)
|
|
|
|
store.transaction(is_read_only) { yield }
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(&blk); transaction(false, &blk); end
|
|
|
|
def read(&blk); transaction(true, &blk); end
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|