Skip to content

Commit e0f3631

Browse files
committed
Try creating block data using jq and printf
1 parent 83296aa commit e0f3631

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

zebra-utils/zcash-rpc-block-template-to-proposal

+50-12
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ TEMPLATE_RPC_PORT=$1
3232
shift
3333

3434
PROPOSAL_RPC_PORTS=""
35+
PROPOSAL_RPC_PORT_COUNT=0
3536
while [ -n "${1:-}" ] && [ "${1-}" != "--" ]; do
3637
PROPOSAL_RPC_PORTS="$PROPOSAL_RPC_PORTS $1"
38+
PROPOSAL_RPC_PORT_COUNT=$((PROPOSAL_RPC_PORT_COUNT + 1))
3739
shift
3840
done
3941

@@ -138,12 +140,41 @@ echo
138140

139141
echo "Querying $TEMPLATE_NODE $TEMPLATE_NET chain at height >=$TEMPLATE_HEIGHT..."
140142
time $ZCASH_CLI -rpcport="$TEMPLATE_RPC_PORT" getblocktemplate "$TEMPLATE_ARG_FULL" > "$TEMPLATE_NODE_TEMPLATE_RESPONSE"
143+
144+
echo "Block template data is in $TEMPLATE_NODE_TEMPLATE_RESPONSE"
145+
#cat "$TEMPLATE_NODE_TEMPLATE_RESPONSE"
146+
141147
echo
142148

143-
cat "$TEMPLATE_NODE_TEMPLATE_RESPONSE"
149+
PROPOSAL_DATA="$ZCASH_RPC_TMP_DIR/proposal-data.json"
150+
144151
echo "Turning the template into block proposal data using $JQ..."
145-
# TODO: build a header, transaction count, and transaction list
146-
PROPOSAL_ARG_FULL=$PROPOSAL_ARG_NO_DATA
152+
153+
# Block header
154+
# See https://zips.z.cash/protocol/protocol.pdf#blockheader for details
155+
cat "$TEMPLATE_NODE_TEMPLATE_RESPONSE" | \
156+
$JQ -r '. | "printf 0\(.version)000000\(.previousblockhash)\(.defaultroots.merkleroot)\(.defaultroots.blockcommitmentshash)%x\(.bits)%064xfd4005%02688x \(.curtime) 0 0"' | \
157+
sh \
158+
> "$PROPOSAL_DATA"
159+
160+
# Transaction list
161+
# See https://developer.bitcoin.org/reference/block_chain.html#serialized-blocks for details
162+
# This encoding only works up to 252 transactions, because it only implements 1 byte compact size
163+
# https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
164+
cat "$TEMPLATE_NODE_TEMPLATE_RESPONSE" | \
165+
$JQ -r '. | "printf %02x\(.coinbasetxn.data)\(.transactions | .[]? | {data} | join("")) \(.transactions | length + 1)"' | \
166+
sh \
167+
>> "$PROPOSAL_DATA"
168+
169+
# TODO: put this in a file if it exceeds the bash variable size limit
170+
PROPOSAL_ARG_FULL="{ \"mode\": \"proposal\", \"data\": \"$(cat $PROPOSAL_DATA)\" ${PROPOSAL_ARG:+, $PROPOSAL_ARG} }"
171+
172+
echo "Block proposal data is in $PROPOSAL_DATA"
173+
# TODO: disable verbose block data
174+
cat "$PROPOSAL_DATA"
175+
176+
echo
177+
echo
147178

148179
echo "getblocktemplate proposal submissions:"
149180
echo "getblocktemplate $PROPOSAL_ARG_NO_DATA"
@@ -156,19 +187,26 @@ for PORT in $PROPOSAL_RPC_PORTS; do
156187
PROPOSAL_NODE_PROPOSAL_RESPONSE=$PROPOSAL_NODES_PROPOSAL_RESPONSE_BASE.$PORT.json
157188
PROPOSAL_NODES_PROPOSAL_RESPONSE_LIST="$PROPOSAL_NODES_PROPOSAL_RESPONSE_LIST $PROPOSAL_NODE_PROPOSAL_RESPONSE"
158189

190+
# TODO: try modifying $PROPOSAL_DATA to contain the whole jsonrequestobject,
191+
# and then call:
192+
# xargs --max-chars=2000200 --arg-file="$PROPOSAL_DATA" time $ZCASH_CLI -rpcport="$PORT" getblocktemplate
159193
time $ZCASH_CLI -rpcport="$PORT" getblocktemplate "$PROPOSAL_ARG_FULL" > "$PROPOSAL_NODE_PROPOSAL_RESPONSE"
160194
done
161195

162196
echo
163197

164-
echo "Proposal response diffs between ports $PROPOSAL_RPC_PORTS:"
198+
if [ "$PROPOSAL_RPC_PORT_COUNT" -gt 1 ]; then
199+
echo "Proposal response diffs between ports $PROPOSAL_RPC_PORTS:"
165200

166-
$DIFF $PROPOSAL_NODES_PROPOSAL_RESPONSE_LIST \
167-
&& ( \
168-
echo "getblocktemplate proposal responses were identical"; \
169-
echo ; \
170-
echo "Final response from $PROPOSAL_NODE_PROPOSAL_RESPONSE:"; \
171-
cat "$PROPOSAL_NODE_PROPOSAL_RESPONSE"; \
172-
)
201+
$DIFF $PROPOSAL_NODES_PROPOSAL_RESPONSE_LIST \
202+
&& ( \
203+
echo "getblocktemplate proposal responses were identical"; \
204+
echo ; \
205+
echo "Final response from $PROPOSAL_NODE_PROPOSAL_RESPONSE:"; \
206+
cat "$PROPOSAL_NODE_PROPOSAL_RESPONSE"; \
207+
)
208+
else
209+
echo "Proposal response from port $PROPOSAL_RPC_PORTS:"
173210

174-
EXIT_STATUS=$?
211+
cat $PROPOSAL_NODES_PROPOSAL_RESPONSE_LIST
212+
fi

0 commit comments

Comments
 (0)