entropy/actor-broker.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
2a31435a77cd76a0a83b6449bfe90e4f98dbe8b7
Source
View exact source on GitHub
/*
 * THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
 * ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
 * DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
 * DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
 * OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
 * THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
 * OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
 * THE CONTENTS OF THIS FILE.
 */

// I157 / D243-C: secure Entropy broker through ordinary ActorRef messages.
//
// Launch as a RootActor message service:
//     bin/protos --allow-entropy --service=service protos/examples/entropy/actor-broker.protos
//
// The initial module alone receives `entropy`. Child Actors do not inherit
// that capability and cannot receive it through Actor transfer. A client
// sends a request to the RootActor and receives ordinary Bytes by value.
//
// For HTTP Host.owningWithFactory, pass only a communication reference:
//     dependencies: { service: rootGroup }
// Never put `entropy` in Worker initialization. A Worker may call:
//     dependencies.service.request("bytes", 16).value()
// in an ordinary suspendible request-handling turn.

ProcessStreams: import("std:io/process_streams")

// The bootstrap binding runs once, installs a behavior and returns.
// No pending entropy Future is observed during service initialization.
service: () => {
    out: ProcessStreams.stdoutWriter(process)
    rootGroup: Actor.group(Actor.current())

    visitor: Actor.spawn("./actor-broker.protos", "visitor")
    visitor.send("fetch", rootGroup)

    behavior: {
        bytes: (count) => {
            // This is an ordinary service message turn, not bootstrap.
            // The generated Bytes may cross Actor boundaries by value.
            entropy.bytes(count).value()
        }

        report: (message) => {
            // Report only sizes/status, never generated octets.
            out.writeLine(message).value()
            null
        }
    }

    behavior
}

// Runs in the visitor's distinct Actor, without bootstrap Entropy authority.
visitor: () => {
    hasAmbientEntropy: context.hasSlot("entropy")

    behavior: {
        fetch: (serviceRef) => {
            // Requesting results is not transferring the Entropy grant.
            sixteen: serviceRef.request("bytes", 16).value()
            thirtyTwo: serviceRef.request("bytes", 32).value()

            valid: !hasAmbientEntropy &&
                (sixteen.size() == 16) &&
                (thirtyTwo.size() == 32)

            status: "failed"
            if(valid) {
                status = "ok"
            }

            serviceRef.send("report", "entropy-broker " + status)
            null
        }
    }

    behavior
}