Skip to content

Commit baa3f73

Browse files
committed
feat(hbs2-cli): add --announce flag to block:put (#5)
block:put writes to local storage only; other peers do not learn the block exists until an RpcAnnounce fires. Add an opt-in --announce flag that broadcasts a BlockAnnounce right after putBlock, so a "put on A, get on B" flow works without a separate `hbs2-peer announce`. Off by default so encrypted-refchan and group-key workflows that gate publication are not surprised. Document it in COOKBOOK.md.
1 parent 7f1de44 commit baa3f73

2 files changed

Lines changed: 61 additions & 33 deletions

File tree

docs/COOKBOOK.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,23 @@ Merkle layer:
140140
echo "some content" | hbs2-cli hbs2:peer:storage:block:put
141141
```
142142

143-
The output is the block hash. Read it back with:
143+
The output is the block hash.
144+
145+
By default `block:put` only writes to your local peer's storage; other
146+
peers do not learn the block exists until something emits a
147+
`BlockAnnounce`. Pass `--announce` to broadcast one right after
148+
storing, so a "put on A, get on B" flow works without a separate
149+
`hbs2-peer announce`:
150+
151+
```
152+
hbs2-cli "(hbs2:peer:storage:block:put --announce \"some content\")"
153+
```
154+
155+
It is off by default so encrypted-refchan and group-key workflows that
156+
gate publication are not surprised. Higher-level flows (git push, sync)
157+
do not need it: they emit reflog transactions that announce internally.
158+
159+
Read it back with:
144160

145161
```
146162
hbs2-cli "(hbs2:peer:storage:block:get \"<HASH>\")"

hbs2-cli/lib/HBS2/CLI/Run/Peer.hs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,38 +88,50 @@ peerEntries = do
8888

8989
_ -> throwIO $ BadFormException @c nil
9090

91-
-- stores *small* block
92-
entry $ bindMatch "hbs2:peer:storage:block:put" $ \case
93-
94-
[isOpaqueOf @LBS.ByteString -> Just lbs] -> do
95-
sto <- getStorage
96-
(putBlock sto lbs <&> fmap (mkSym . show . pretty . HashRef) )
97-
>>= orThrowUser "storage error"
98-
99-
[isOpaqueOf @BS.ByteString -> Just bs] -> do
100-
sto <- getStorage
101-
(putBlock sto (LBS.fromStrict bs) <&> fmap (mkSym . show . pretty . HashRef) )
102-
>>= orThrowUser "storage error"
103-
104-
-- FIXME: deprecate-this
105-
[ListVal [SymbolVal "blob", LitStrVal s]] -> do
106-
flip runContT pure do
107-
sto <- getStorage
108-
lift $ putTextLit sto s
109-
110-
[LitStrVal s] -> do
111-
flip runContT pure do
112-
sto <- getStorage
113-
lift $ putTextLit sto s
114-
115-
[] -> do
116-
bs <- liftIO BS.getContents
117-
sto <- getStorage
118-
putBlock sto (LBS.fromStrict bs) >>= \case
119-
Nothing -> pure nil
120-
Just h -> pure $ mkSym (show $ pretty $ HashRef h)
121-
122-
e -> throwIO $ BadFormException @c (mkList e)
91+
-- stores *small* block. With the optional --announce flag, emits a
92+
-- BlockAnnounce after storing so other peers learn the block exists.
93+
-- Off by default so encrypted-refchan and group-key workflows that
94+
-- gate publication are not surprised.
95+
brief "stores a small block; with --announce also broadcasts a BlockAnnounce"
96+
$ entry $ bindMatch "hbs2:peer:storage:block:put" $ \syn -> do
97+
98+
let isAnnounceFlag = \case { SymbolVal "--announce" -> True; _ -> False }
99+
(flags, rest) = L.partition isAnnounceFlag syn
100+
doAnnounce = not (L.null flags)
101+
102+
let putArgs = \case
103+
104+
[isOpaqueOf @LBS.ByteString -> Just lbs] -> do
105+
sto <- getStorage
106+
putBlock sto lbs >>= orThrowUser "storage error" <&> (Just . HashRef)
107+
108+
[isOpaqueOf @BS.ByteString -> Just bs] -> do
109+
sto <- getStorage
110+
putBlock sto (LBS.fromStrict bs) >>= orThrowUser "storage error" <&> (Just . HashRef)
111+
112+
-- FIXME: deprecate-this
113+
[ListVal [SymbolVal "blob", LitStrVal s]] -> do
114+
sto <- getStorage
115+
putBlock sto (LBS8.pack (Text.unpack s)) >>= orThrowUser "storage error" <&> (Just . HashRef)
116+
117+
[LitStrVal s] -> do
118+
sto <- getStorage
119+
putBlock sto (LBS8.pack (Text.unpack s)) >>= orThrowUser "storage error" <&> (Just . HashRef)
120+
121+
[] -> do
122+
bs <- liftIO BS.getContents
123+
sto <- getStorage
124+
putBlock sto (LBS.fromStrict bs) <&> fmap HashRef
125+
126+
e -> throwIO $ BadFormException @c (mkList e)
127+
128+
putArgs rest >>= \case
129+
Nothing -> pure nil
130+
Just h -> do
131+
when doAnnounce do
132+
api <- getClientAPI @PeerAPI @UNIX
133+
void $ callRpcWaitMay @RpcAnnounce (TimeoutSec 1) api h
134+
pure $ mkSym (show $ pretty h)
123135

124136
brief "checks if peer available"
125137
$ noArgs

0 commit comments

Comments
 (0)