Fix careless typos with ::Storage index names.

This commit is contained in:
Netscape Navigator 2020-03-28 19:43:59 -05:00
parent 73f653af31
commit d2e9159c74
6 changed files with 42 additions and 25 deletions

View File

@ -35,6 +35,7 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
- [X] Move literals into `Pigeon` module as constants, again.
- [X] pigeon message find
- [X] pigeon message find-all for local feed.
- [ ] Use URNs instead of multihash?
- [ ] pigeon bundle consume (We are minimally feature complete at this point)
- [ ] Fix the diagram in the spec document
- [ ] Put all the [HEADER, string, FOOTER].join("") nonsense into Pigeon::Helpers

2
dist/pigeon.rb vendored
View File

@ -35,7 +35,7 @@ module Pigeon
USER_NS = "user"
BLCK_NS = "blocked"
MESG_NS = "messages"
DEPTH_INDEX_NS = "messages.by_depth"
MESSAGE_BY_DEPTH_NS = "messages.by_depth"
COUNT_INDEX_NS = "messages.count"
# ^ Internal namespaces for PStore keys

View File

@ -14,6 +14,7 @@ module Pigeon
end
def self.ingest(file_path)
raise "???"
end
private

View File

@ -6,12 +6,13 @@ module Pigeon
class VerificationError < StandardError; end
VERFIY_ERROR = "Expected field %s to equal %s. Got: %s"
VERFIY_ERROR = "Expected field `%s` to equal %s, got: %s"
# Author a new message.
def self.publish(draft, author: LocalIdentity.current)
msg = self.new(author: LocalIdentity.current,
kind: draft.kind,
body: draft.body)
body: draft.body,
depth: Pigeon::Storage.current.get_message_count_for(LocalIdentity.current))
# We might need to add conditional logic here
# Currently YAGNI since all Drafts we handle today
# are authored by LocalIdentity.current
@ -22,12 +23,11 @@ module Pigeon
# Store a message that someone (not the LocalIdentity)
# has authored.
def self.ingest(author:, body:, depth:, kind:, prev:, signature:)
message = new(author: RemoteIdentity.new(author),
kind: kind,
body: body,
signature: signature)
message.verify!
message.save
new(author: RemoteIdentity.new(author),
kind: kind,
body: body,
signature: signature,
depth: depth)
end
def render
@ -42,28 +42,28 @@ module Pigeon
def verify!
verify_depth_prev_and_depth
verify_signature
self.freeze
end
private
def assert(field, actual, expected)
unless actual == expected
message = VERFIY_ERROR % [field, actual, expected]
message = VERFIY_ERROR % [field, actual, expected || "nil"]
raise VerificationError, message
end
end
def verify_depth_prev_and_depth
store = Pigeon::Storage.current
count = store.get_message_count_for(self.author)
if count == 0
count = Pigeon::Storage.current.get_message_count_for(self.author)
if count == nil
assert("depth", self.depth, 0)
assert("prev", self.prev, nil)
else
# Make sure the `depth` prop is equal to count + 1
# Make sure the `prev` prop is equal to
# message_by_depth(author, (depth - 1))
binding.pry
raise "WIP"
end
end
@ -72,17 +72,16 @@ module Pigeon
Helpers.verify_string(author, signature, tpl)
end
def initialize(author:, kind:, body:, signature: nil)
def initialize(author:, kind:, body:, signature: nil, depth:)
raise MISSING_BODY if body.empty?
@author = author
@kind = kind
@body = body
# Side effects in a constructor? Hmm...
store = Pigeon::Storage.current
@depth = store.message_count
@depth = depth
@signature = signature || calculate_signature
@prev = store.get_message_by_depth(@author.public_key, @depth - 1)
self.freeze
verify!
store.save_message(self)
end
@ -93,5 +92,9 @@ module Pigeon
def calculate_signature
@author.sign(template.render_without_signature)
end
def store
Pigeon::Storage.current
end
end
end

View File

@ -15,12 +15,18 @@ module Pigeon
raise "Expected string, got #{multihash.class}" unless multihash.is_a?(String) # Delete later
store.transaction do
# Map<[multihash(str), depth(int)], Signature>
store[DEPTH_INDEX_NS][[multihash, depth]]
# Wait what? Why is there a depth and count
# index??
store[MESSAGE_BY_DEPTH_NS][[multihash, depth]]
end
end
# `nil` means "none"
#
def get_message_count_for(author_multihash)
store.transaction(true) { store[COUNT_INDEX_NS][author_multihash] || 0 }
store.transaction(true) do
count = store[COUNT_INDEX_NS][author_multihash] || 0
end
end
def message_count
@ -130,7 +136,9 @@ module Pigeon
def bootstrap
store.transaction do
store[DEPTH_INDEX_NS] ||= {}
# Wait what? Why is there a depth and count
# index??
store[MESSAGE_BY_DEPTH_NS] ||= {}
store[BLOB_NS] ||= {}
store[CONF_NS] ||= {}
store[MESG_NS] ||= {}
@ -153,6 +161,7 @@ module Pigeon
end
def insert_and_update_index(message)
pub_key = message.author.public_key
# STEP 1: Update MESG_NS, the main storage spot.
store[MESG_NS][message.multihash] = message
@ -161,10 +170,11 @@ module Pigeon
# message
# SECURITY AUDIT: How can we be certain the message is
# not lying about its depth?
key = [message.author.public_key, message.depth]
store[DEPTH_INDEX_NS][key] = message.multihash
store[COUNT_INDEX_NS][message.author] ||= 0
store[COUNT_INDEX_NS][message.author] += 1
key = [pub_key, message.depth]
store[MESSAGE_BY_DEPTH_NS][key] = message.multihash
store[COUNT_INDEX_NS][pub_key] ||= 0
store[COUNT_INDEX_NS][pub_key] += 1
puts store[COUNT_INDEX_NS].to_json
end
end
end

View File

@ -13,6 +13,8 @@ RSpec.describe Pigeon::Message do
end
it "creates a bundle" do
p = Pigeon::DEFAULT_BUNDLE_PATH
File.delete(p) if File.file?(p)
expected_bundle = (1..10)
.to_a
.map do |n| { "foo" => ["bar", 123, SecureRandom.uuid].sample } end