Skip to content

Commit

Permalink
Supporting linkdefs in bucket without ids (#533)
Browse files Browse the repository at this point in the history
* Supporting linkdefs in bucket without ids

Signed-off-by: Kevin Hoffman <[email protected]>

* Removing comments

Signed-off-by: Kevin Hoffman <[email protected]>

Signed-off-by: Kevin Hoffman <[email protected]>
  • Loading branch information
autodidaddict authored Jan 19, 2023
1 parent f0ee6a6 commit 512c8af
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 32 deletions.
21 changes: 21 additions & 0 deletions host_core/lib/host_core/jetstream/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ defmodule HostCore.Jetstream.Client do
|> HostCore.Nats.safe_req(del_topic, <<>>)
end

def ensure_linkdef_id(linkdef) do
if Map.has_key?(linkdef, :id) do
linkdef
else
Map.put(
linkdef,
:id,
linkdef_hash(linkdef.actor_id, linkdef.contract_id, linkdef.link_name)
)
end
end

def linkdef_hash(actor_id, contract_id, link_name) do
sha = :crypto.hash_init(:sha256)
sha = :crypto.hash_update(sha, actor_id)
sha = :crypto.hash_update(sha, contract_id)
sha = :crypto.hash_update(sha, link_name)
sha_binary = :crypto.hash_final(sha)
sha_binary |> Base.encode16() |> String.upcase()
end

defp create_bucket_topic(lattice_prefix, nil),
do: "$JS.API.STREAM.CREATE.KV_LATTICEDATA_#{lattice_prefix}"

Expand Down
3 changes: 3 additions & 0 deletions host_core/lib/host_core/jetstream/metadata_cache_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule HostCore.Jetstream.MetadataCacheLoader do
alias HostCore.Refmaps.Manager, as: RefmapsManager
alias Phoenix.PubSub

import HostCore.Jetstream.Client, only: [ensure_linkdef_id: 1]

require Logger
use Gnat.Server

Expand Down Expand Up @@ -70,6 +72,7 @@ defmodule HostCore.Jetstream.MetadataCacheLoader do
defp handle_action(:key_added, %{key: @linkdef_prefix <> _ldid, prefix: lattice_prefix}, body) do
case Jason.decode(body, keys: :atoms) do
{:ok, ld} ->
ld = ensure_linkdef_id(ld)
Logger.debug("Caching link definition from #{ld.actor_id} on contract #{ld.contract_id}")

# This data came from the bucket, so we don't need to re-write it to the bucket, just:
Expand Down
31 changes: 12 additions & 19 deletions host_core/lib/host_core/linkdefs/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule HostCore.Linkdefs.Manager do
require Logger

import HostCore.Jetstream.MetadataCacheLoader, only: [broadcast_event: 3]
import HostCore.Jetstream.Client, only: [linkdef_hash: 3]

alias HostCore.CloudEvent
alias HostCore.Jetstream.Client, as: JetstreamClient
Expand All @@ -16,16 +17,10 @@ defmodule HostCore.Linkdefs.Manager do
link_name :: String.t()
) :: map() | nil
def lookup_link_definition(lattice_prefix, actor, contract_id, link_name) do
predicates = [
{:==, {:map_get, :actor_id, :"$2"}, actor},
{:==, {:map_get, :contract_id, :"$2"}, contract_id},
{:==, {:map_get, :link_name, :"$2"}, link_name}
]

lattice_prefix
|> table_atom()
|> :ets.select([{{:"$1", :"$2"}, predicates, [:"$2"]}])
|> List.first()
case lookup_link_definition(lattice_prefix, linkdef_hash(actor, contract_id, link_name)) do
{:ok, ld} -> ld
:error -> nil
end
end

def lookup_link_definition(lattice_prefix, ldid) do
Expand All @@ -44,6 +39,13 @@ defmodule HostCore.Linkdefs.Manager do
provider_key,
values
) do
ldid =
if is_nil(ldid) do
linkdef_hash(actor, contract_id, link_name)
else
ldid
end

map = %{
actor_id: actor,
contract_id: contract_id,
Expand Down Expand Up @@ -170,15 +172,6 @@ defmodule HostCore.Linkdefs.Manager do
end
end

defp linkdef_hash(actor_id, contract_id, link_name) do
sha = :crypto.hash_init(:sha256)
sha = :crypto.hash_update(sha, actor_id)
sha = :crypto.hash_update(sha, contract_id)
sha = :crypto.hash_update(sha, link_name)
sha_binary = :crypto.hash_final(sha)
sha_binary |> Base.encode16() |> String.upcase()
end

# Publishes the removal of a link definition to the event stream and sends an indication
# of the removal to the appropriate capability provider. Other hosts will already have been
# informed of the deletion via key subscriptions on the bucket
Expand Down
152 changes: 139 additions & 13 deletions host_core/native/hostcore_wasmcloud_native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 512c8af

Please sign in to comment.