forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash-xdrs.sh
More file actions
executable file
·61 lines (54 loc) · 2.1 KB
/
Copy pathhash-xdrs.sh
File metadata and controls
executable file
·61 lines (54 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
#
# This file generates a C++ file which contains (filename, hash) pairs for XDR
# files included in the build. These are included in the stellar-core build to
# be checked against similar hashes produced by xdrgen when it is compiling XDR
# definitions for Rust code.
#
# The goal is to detect the (unfortunately easy) condition of C++ and Rust code
# communicating with each other using different XDR definitions.
#
# Hashes are computed after stripping #ifdef/#endif blocks and removing all
# whitespace, so they are stable regardless of feature ifdefs or formatting.
set -o errexit
set -o pipefail
if [ ! -d $1/xdr ]; then
echo "usage: $0 XDR_PROTOCOL_DIR"
exit 1
fi
cat <<EOF
// DO NOT EDIT: this file is automatically generated from $0
#include <string>
#include <vector>
#include <filesystem>
namespace stellar {
extern const std::vector<std::pair<std::filesystem::path, std::string>> XDR_FILES_SHA256 = {
EOF
# Hashes to ignore
IGNORE="Stellar-internal\|Stellar-overlay\|Stellar-contract-spec\|Stellar-contract-meta\|Stellar-contract-env-meta"
# Strip #ifdef/#endif blocks (inclusive) and remove all whitespace before
# hashing. This produces a canonical hash of the base XDR content.
# Content between #ifdef and #else (the feature-on branch) is stripped,
# while content between #else and #endif (the feature-off branch) is kept,
# since it represents the base types when no features are enabled.
# Note: this does not hash the feature-gated XDR content. The feature flag
# validation in checkXDRFileIdentity() separately verifies that C++ and Rust
# have the same XDR features enabled.
xdr_hash() {
awk 'BEGIN{skip=0} /#ifdef/{skip=1; next} /#else/{skip=0; next} /#endif/{skip=0; next} skip==0{print}' "$1" \
| tr -d '[:space:]' \
| sha256sum -b \
| cut -d' ' -f1
}
for f in $1/xdr/*.x; do
fname=$(basename "$f")
# Skip ignored files
if echo "$fname" | grep -q "${IGNORE}"; then
continue
fi
hash=$(xdr_hash "$f")
echo "{\"$f\", \"$hash\"},"
done
# Add empty entries for the 5 skipped files
echo '{"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}};'
echo '}'