Skip to content

Commit

Permalink
Merge branch 'rsksmart:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-sampaio authored Jan 9, 2024
2 parents a24d42c + 8dddfb2 commit 82bebc3
Show file tree
Hide file tree
Showing 32 changed files with 363 additions and 117 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: "Code coverage"

on:
push:
branches: [ "master" ]

jobs:
coverage:
name: Run tests and generate coverage reports
runs-on: ubuntu-20.04

steps:
- name: Checkout this repo
uses: actions/checkout@v3

- name: Build the middleware docker image
run: docker/mware/build

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.CODECOVERAGE_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.CODECOVERAGE_AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.CODECOVERAGE_AWS_REGION }}

- name: Run middleware coverage script
run: |
middleware/test-all-coverage
COVPCT=$(cat middleware/coverage/total)
COVCOL=$(utils/coverage-color.sh $COVPCT)
echo "{ \"schemaVersion\": 1, \"label\": \"Middleware coverage\", \"message\": \"$COVPCT%\", \"color\": \"$COVCOL\" }" > middleware/coverage/badge.json
- name: "Upload middleware coverage report"
run: |
aws s3 sync \
middleware/coverage/ \
s3://${{ secrets.CODECOVERAGE_S3_BUCKET }}/powhsm_4.1.x/middleware_coverage_report \
--sse aws:kms --sse-kms-key-id ${{ secrets.CODECOVERAGE_KMS_KEY_ID }} \
--no-progress --follow-symlinks --delete --only-show-errors
- name: Run firmware coverage script
run: |
ledger/coverage/gen-coverage
COVPCT=$(cat ledger/coverage/output/total)
COVCOL=$(utils/coverage-color.sh $COVPCT)
echo "{ \"schemaVersion\": 1, \"label\": \"Firmware coverage\", \"message\": \"$COVPCT%\", \"color\": \"$COVCOL\" }" > ledger/coverage/output/badge.json
- name: "Upload firmware coverage report"
run: |
aws s3 sync \
ledger/coverage/output/ \
s3://${{ secrets.CODECOVERAGE_S3_BUCKET }}/powhsm_4.1.x/firmware_coverage_report \
--sse aws:kms --sse-kms-key-id ${{ secrets.CODECOVERAGE_KMS_KEY_ID }} \
--no-progress --follow-symlinks --delete --only-show-errors
- name: Invalidate CloudFront cache
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.CODECOVERAGE_CLOUDFRONT_DIST_ID }} --paths "/*"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
![Tests](https://github.com/rsksmart/rsk-powhsm/actions/workflows/run-tests.yml/badge.svg)
![Python linter](https://github.com/rsksmart/rsk-powhsm/actions/workflows/lint-python.yml/badge.svg)
![C linter](https://github.com/rsksmart/rsk-powhsm/actions/workflows/lint-c.yml/badge.svg)
[![Middleware coverage](https://img.shields.io/endpoint?url=https://d16sboe9lzo4ru.cloudfront.net/powhsm_4.1.x/middleware_coverage_report/badge.json)](https://d16sboe9lzo4ru.cloudfront.net/powhsm_4.1.x/middleware_coverage_report/index.html)
[![Firmware coverage](https://img.shields.io/endpoint?url=https://d16sboe9lzo4ru.cloudfront.net/powhsm_4.1.x/firmware_coverage_report/badge.json)](https://d16sboe9lzo4ru.cloudfront.net/powhsm_4.1.x/firmware_coverage_report/index.html)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)

Expand Down
3 changes: 2 additions & 1 deletion docker/mware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ WORKDIR /hsm2
RUN apt-get update && \
apt-get install -y apt-utils vim && \
apt-get install -y build-essential=12.9 && \
apt-get install -y git
apt-get install -y git && \
apt-get install -y lcov

# Python package prerequisites
RUN apt-get install -y \
Expand Down
86 changes: 74 additions & 12 deletions docker/mware/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,19 @@ pycryptodomex==3.9.7 \
pyee==7.0.2 \
--hash=sha256:a0fee808414a4fc077d81be80dfe785879430b03101088747714c045292751c0 \
--hash=sha256:c908d1ecb32918bbf7dbb895a093153b0ca1ed8f04fc067d98bd4c5917aeb3d8
pyinstaller==5.4.1 \
--hash=sha256:0ac78f1145be34adda8afb5fe4c8d659172140092c055994dab57ee2190bec71 \
--hash=sha256:0c8626c2db6e2d426be3e44ae08039d638481de7b580769668dd777116d911b9 \
--hash=sha256:2a09e6bd6e121eb1a71fadb223797dc502e4fd4168931c31a5f87faa10eb5b4c \
--hash=sha256:4b4d867fbc6c35bb30b09f681d469f824c69fe32f2358b8af75247bdefdf694a \
--hash=sha256:7da11fdee216ae0b887f9d1fe0eca7bce2b8f5d039eeac2561282feff5970d24 \
--hash=sha256:806df33e2b93a69f45e4c8be72d3b51449e9e0f557a3e89e0b06f5cb9a111022 \
--hash=sha256:994b76d10892f1d3f9a4241109a3f7b1e9b74dea3198bf3f502d2f4ae744fa6e \
--hash=sha256:9a2b99f191e28dec6cb8eb5544fa436f2109073e634f8e602225ada0239aed46 \
--hash=sha256:b57e8f9ec23a48b740c5dbb19cd5499150e35a2997dfd0b8f96e2deced512ecc \
--hash=sha256:bec1d94cba89f64a61606a1eaf33202f3a52ff2ac65e25555053fd5fe55b760d \
--hash=sha256:f7f0ad1bdea828c163e2032c7ce941b2d0b68a83b3b572e6ce50c77ca7987574
pyinstaller==5.13.1 \
--hash=sha256:086e68aa1e72f6aa13b9d170a395755e2b194b8ab410caeed02d16b432410c8c \
--hash=sha256:2519db3edec87d8c33924c2c4b7e176d8c1bbd9ba892d77efb67281925e621d6 \
--hash=sha256:29341d2e86d5ce7df993e797ee96ef679041fc85376d31c35c7b714085a21299 \
--hash=sha256:3c9cfe6d5d2f392d5d47389f6d377a8f225db460cdd01048b5a3de1d99c24ebe \
--hash=sha256:5d801db3ceee58d01337473ea897e96e4bb21421a169dd7cf8716754617ff7fc \
--hash=sha256:78d1601a11475b95dceff6eaf0c9cd74d93e3f47b5ce4ad63cd76e7a369d3d04 \
--hash=sha256:a2e7a1d76a7ac26f1db849d691a374f2048b0e204233028d25d79a90ecd1fec8 \
--hash=sha256:aa609aca62edd8cdcf7740677a21525e6c23b5e9a8f821ec8a80c68947771b5d \
--hash=sha256:ad6e31a8f35a463c6140e4cf979859197edc9831a1039253408b0fe5eec274dc \
--hash=sha256:b70ebc10811b30bbea4cf5b81fd1477db992c2614cf215edc987cda9c5468911 \
--hash=sha256:b8d4000af72bf72f8185d420cd0a0aee0961f03a5c3511dc3ff08cdaef0583de \
--hash=sha256:e033218c8922f0342b6095fb444ecb3bc6747dfa58cac5eac2b985350f4b681e
pyinstaller-hooks-contrib==2022.10 \
--hash=sha256:d1dd6ea059dc30e77813cc12a5efa8b1d228e7da8f5b884fe11775f946db1784 \
--hash=sha256:e5edd4094175e78c178ef987b61be19efff6caa23d266ade456fc753e847f62e
Expand Down Expand Up @@ -352,3 +353,64 @@ yapf==0.31.0 \
charset-normalizer==2.0.6 \
--hash=sha256:5d209c0a931f215cee683b6445e2d77677e7e75e159f78def0db09d68fafcaa6 \
--hash=sha256:5ec46d183433dcbd0ab716f2d7f29d8dee50505b3fdb40c6b985c7c4f5a3591f
coverage==7.2.7 \
--hash=sha256:06a9a2be0b5b576c3f18f1a241f0473575c4a26021b52b2a85263a00f034d51f \
--hash=sha256:06fb182e69f33f6cd1d39a6c597294cff3143554b64b9825d1dc69d18cc2fff2 \
--hash=sha256:0a5f9e1dbd7fbe30196578ca36f3fba75376fb99888c395c5880b355e2875f8a \
--hash=sha256:0e1f928eaf5469c11e886fe0885ad2bf1ec606434e79842a879277895a50942a \
--hash=sha256:171717c7cb6b453aebac9a2ef603699da237f341b38eebfee9be75d27dc38e01 \
--hash=sha256:1e9d683426464e4a252bf70c3498756055016f99ddaec3774bf368e76bbe02b6 \
--hash=sha256:201e7389591af40950a6480bd9edfa8ed04346ff80002cec1a66cac4549c1ad7 \
--hash=sha256:245167dd26180ab4c91d5e1496a30be4cd721a5cf2abf52974f965f10f11419f \
--hash=sha256:2aee274c46590717f38ae5e4650988d1af340fe06167546cc32fe2f58ed05b02 \
--hash=sha256:2e07b54284e381531c87f785f613b833569c14ecacdcb85d56b25c4622c16c3c \
--hash=sha256:31563e97dae5598556600466ad9beea39fb04e0229e61c12eaa206e0aa202063 \
--hash=sha256:33d6d3ea29d5b3a1a632b3c4e4f4ecae24ef170b0b9ee493883f2df10039959a \
--hash=sha256:3d376df58cc111dc8e21e3b6e24606b5bb5dee6024f46a5abca99124b2229ef5 \
--hash=sha256:419bfd2caae268623dd469eff96d510a920c90928b60f2073d79f8fe2bbc5959 \
--hash=sha256:48c19d2159d433ccc99e729ceae7d5293fbffa0bdb94952d3579983d1c8c9d97 \
--hash=sha256:49969a9f7ffa086d973d91cec8d2e31080436ef0fb4a359cae927e742abfaaa6 \
--hash=sha256:52edc1a60c0d34afa421c9c37078817b2e67a392cab17d97283b64c5833f427f \
--hash=sha256:537891ae8ce59ef63d0123f7ac9e2ae0fc8b72c7ccbe5296fec45fd68967b6c9 \
--hash=sha256:54b896376ab563bd38453cecb813c295cf347cf5906e8b41d340b0321a5433e5 \
--hash=sha256:58c2ccc2f00ecb51253cbe5d8d7122a34590fac9646a960d1430d5b15321d95f \
--hash=sha256:5b7540161790b2f28143191f5f8ec02fb132660ff175b7747b95dcb77ac26562 \
--hash=sha256:5baa06420f837184130752b7c5ea0808762083bf3487b5038d68b012e5937dbe \
--hash=sha256:5e330fc79bd7207e46c7d7fd2bb4af2963f5f635703925543a70b99574b0fea9 \
--hash=sha256:61b9a528fb348373c433e8966535074b802c7a5d7f23c4f421e6c6e2f1697a6f \
--hash=sha256:63426706118b7f5cf6bb6c895dc215d8a418d5952544042c8a2d9fe87fcf09cb \
--hash=sha256:6d040ef7c9859bb11dfeb056ff5b3872436e3b5e401817d87a31e1750b9ae2fb \
--hash=sha256:6f48351d66575f535669306aa7d6d6f71bc43372473b54a832222803eb956fd1 \
--hash=sha256:7ee7d9d4822c8acc74a5e26c50604dff824710bc8de424904c0982e25c39c6cb \
--hash=sha256:81c13a1fc7468c40f13420732805a4c38a105d89848b7c10af65a90beff25250 \
--hash=sha256:8d13c64ee2d33eccf7437961b6ea7ad8673e2be040b4f7fd4fd4d4d28d9ccb1e \
--hash=sha256:8de8bb0e5ad103888d65abef8bca41ab93721647590a3f740100cd65c3b00511 \
--hash=sha256:8fa03bce9bfbeeef9f3b160a8bed39a221d82308b4152b27d82d8daa7041fee5 \
--hash=sha256:924d94291ca674905fe9481f12294eb11f2d3d3fd1adb20314ba89e94f44ed59 \
--hash=sha256:975d70ab7e3c80a3fe86001d8751f6778905ec723f5b110aed1e450da9d4b7f2 \
--hash=sha256:976b9c42fb2a43ebf304fa7d4a310e5f16cc99992f33eced91ef6f908bd8f33d \
--hash=sha256:9e31cb64d7de6b6f09702bb27c02d1904b3aebfca610c12772452c4e6c21a0d3 \
--hash=sha256:a342242fe22407f3c17f4b499276a02b01e80f861f1682ad1d95b04018e0c0d4 \
--hash=sha256:a3d33a6b3eae87ceaefa91ffdc130b5e8536182cd6dfdbfc1aa56b46ff8c86de \
--hash=sha256:a895fcc7b15c3fc72beb43cdcbdf0ddb7d2ebc959edac9cef390b0d14f39f8a9 \
--hash=sha256:afb17f84d56068a7c29f5fa37bfd38d5aba69e3304af08ee94da8ed5b0865833 \
--hash=sha256:b1c546aca0ca4d028901d825015dc8e4d56aac4b541877690eb76490f1dc8ed0 \
--hash=sha256:b29019c76039dc3c0fd815c41392a044ce555d9bcdd38b0fb60fb4cd8e475ba9 \
--hash=sha256:b46517c02ccd08092f4fa99f24c3b83d8f92f739b4657b0f146246a0ca6a831d \
--hash=sha256:b7aa5f8a41217360e600da646004f878250a0d6738bcdc11a0a39928d7dc2050 \
--hash=sha256:b7b4c971f05e6ae490fef852c218b0e79d4e52f79ef0c8475566584a8fb3e01d \
--hash=sha256:ba90a9563ba44a72fda2e85302c3abc71c5589cea608ca16c22b9804262aaeb6 \
--hash=sha256:cb017fd1b2603ef59e374ba2063f593abe0fc45f2ad9abdde5b4d83bd922a353 \
--hash=sha256:d22656368f0e6189e24722214ed8d66b8022db19d182927b9a248a2a8a2f67eb \
--hash=sha256:d2c2db7fd82e9b72937969bceac4d6ca89660db0a0967614ce2481e81a0b771e \
--hash=sha256:d39b5b4f2a66ccae8b7263ac3c8170994b65266797fb96cbbfd3fb5b23921db8 \
--hash=sha256:d62a5c7dad11015c66fbb9d881bc4caa5b12f16292f857842d9d1871595f4495 \
--hash=sha256:e7d9405291c6928619403db1d10bd07888888ec1abcbd9748fdaa971d7d661b2 \
--hash=sha256:e84606b74eb7de6ff581a7915e2dab7a28a0517fbe1c9239eb227e1354064dcd \
--hash=sha256:eb393e5ebc85245347950143969b241d08b52b88a3dc39479822e073a1a8eb27 \
--hash=sha256:ebba1cd308ef115925421d3e6a586e655ca5a77b5bf41e02eb0e4562a111f2d1 \
--hash=sha256:ee57190f24fba796e36bb6d3aa8a8783c643d8fa9760c89f7a98ab5455fbf818 \
--hash=sha256:f2f67fe12b22cd130d34d0ef79206061bfb5eda52feb6ce0dba0644e20a03cf4 \
--hash=sha256:f6951407391b639504e3b3be51b7ba5f3528adbf1a8ac3302b687ecababf929e \
--hash=sha256:f75f7168ab25dd93110c8a8117a22450c19976afbc44234cbf71481094c1b850 \
--hash=sha256:fdec9e8cbf13a5bf63290fc6013d216a4c7232efb51548594ca3631a7f13c3a3
2 changes: 2 additions & 0 deletions ledger/coverage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage.info
output
6 changes: 6 additions & 0 deletions ledger/coverage/coverage.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ifeq ($(COVERAGE),y)
COVFLAGS = --coverage
else
COVFLAGS =
endif
COVFILES = *.gcda *.gcno
41 changes: 41 additions & 0 deletions ledger/coverage/gen-coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

if [[ $1 == "exec" ]]; then
BASEDIR=$(realpath $(dirname $0))
SRCDIR=$(realpath $BASEDIR/../src)
REPOROOT=$(realpath $BASEDIR/../..)

# Remove any existing coverage data
rm -rf $BASEDIR/coverage.info $BASEDIR/output
find $REPOROOT/ledger -name "*.gcno" -o -name "*.gcda" | xargs rm -f

# Run firmware unit tests with coverage generation
COVERAGE=y $REPOROOT/ledger/src/signer/test/run-all.sh
COVERAGE=y $REPOROOT/ledger/src/ui/test/run-all.sh

# Run tcpsigner test suite
pushd $REPOROOT/ledger/src/tcpsigner > /dev/null
COVERAGE=y make clean all
./tcpsigner --checkpoint 0xbdcb3c17c7aee714cec8ad900341bfd987b452280220dcbd6e7191f67ea4209b --difficulty 0x32 --network regtest > /dev/null &
popd > /dev/null

pushd $REPOROOT/ledger/test > /dev/null
python run.py
err_code=$?
popd > /dev/null

lcov --capture --directory $SRCDIR --list-full-path --output-file $BASEDIR/coverage.info
genhtml $BASEDIR/coverage.info --output $BASEDIR/output -p $SRCDIR -t "powHSM firmware"
lcov --summary $BASEDIR/coverage.info | grep lines | sed -e "s/.\+lines.\+: \([[:digit:].]\+\).\+/\1/g" > $BASEDIR/output/total
mv $BASEDIR/coverage.info $BASEDIR/output
else
# Script directory
REPOROOT=$(realpath $(dirname $0)/../..)
SCRIPT=$(realpath $0 --relative-to=$REPOROOT)

# Generate coverage report
$REPOROOT/docker/mware/do-notty-nousb /hsm2 "./$SCRIPT exec"
err_code=$?
fi

exit $err_code
4 changes: 4 additions & 0 deletions ledger/src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ tcpsigner/**/*.json
# Icons hex
signer/icon.hex
ui/icon.hex

# Code coverage artifacts
*.gcda
*.gcno
33 changes: 7 additions & 26 deletions ledger/src/signer/src/bc_advance.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,6 @@ static uint8_t expected_state;
// Storage utilities
// -----------------------------------------------------------------------

/*
* Store the given buffer in the block's work area.
*
* @arg[in] buf buffer to store
* @arg[in] size buffer size in bytes
*/
static void wa_store(const uint8_t* buf, uint16_t size) {
SAFE_MEMMOVE(block.wa_buf,
sizeof(block.wa_buf),
block.wa_off,
buf,
size,
MEMMOVE_ZERO_OFFSET,
size,
FAIL(BUFFER_OVERFLOW));

block.wa_off += size;
}

/*
* Process merkle proof chunk.
*
Expand All @@ -123,15 +104,15 @@ static void process_merkle_proof(const uint8_t* chunk, uint16_t size) {
if (block.wa_off < HASH_SIZE) {
uint8_t old_offset = offset;
offset += HASH_SIZE - block.wa_off;
wa_store(chunk + old_offset, HASH_SIZE - block.wa_off);
WA_STORE(chunk + old_offset, HASH_SIZE - block.wa_off);
}
fold_left(&block.ctx, block.merkle_proof_left, block.wa_buf);
block.wa_off = 0;
}

// Copy any remaining bytes in the chunk to the work area
if (offset < size) {
wa_store(chunk + offset, size - offset);
WA_STORE(chunk + offset, size - offset);
}
}

Expand Down Expand Up @@ -573,23 +554,23 @@ static void str_chunk(const uint8_t* chunk, const size_t size) {
}

if (block.field == F_PARENT_HASH) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_BLOCK_DIFF) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_BLOCK_NUM) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_MM_HEADER) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_UMM_ROOT && HAS_FLAG(block.flags, HAS_UMM_ROOT)) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_MERKLE_PROOF && !BLOCK_ALREADY_VALID()) {
Expand Down
27 changes: 4 additions & 23 deletions ledger/src/signer/src/bc_ancestor.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ static uint32_t curr_block;
// Expected OP for next message
static uint8_t expected_state;

/*
* Store the given buffer in the block's work area.
*
* @arg[in] buf buffer to store
* @arg[in] size buffer size in bytes
*/
static void wa_store(const uint8_t* buf, uint16_t size) {
SAFE_MEMMOVE(block.wa_buf,
sizeof(block.wa_buf),
block.wa_off,
buf,
size,
MEMMOVE_ZERO_OFFSET,
size,
FAIL(BUFFER_OVERFLOW));

block.wa_off += size;
}

// -----------------------------------------------------------------------
// Update ancestor validations
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -194,20 +175,20 @@ static void str_chunk(const uint8_t* chunk, const size_t size) {
}

if (block.field == F_PARENT_HASH) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

// Store receipt only for last block
if (block.field == F_RECEIPT_ROOT && curr_block + 1 == expected_blocks) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_BLOCK_NUM) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (block.field == F_MM_HEADER) {
wa_store(chunk, size);
WA_STORE(chunk, size);
}

if (SHOULD_COMPUTE_BLOCK_HASH) {
Expand Down
15 changes: 15 additions & 0 deletions ledger/src/signer/src/bc_blockutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,19 @@
#define MM_HASH_LAST_FIELD \
(block.network_upgrade >= NU_PAPYRUS ? F_UMM_ROOT : F_UMM_ROOT - 1)

// Store the given buffer in the block's work area
#define WA_STORE(buf, bufsize) \
{ \
SAFE_MEMMOVE(block.wa_buf, \
sizeof(block.wa_buf), \
block.wa_off, \
(buf), \
(bufsize), \
MEMMOVE_ZERO_OFFSET, \
(bufsize), \
FAIL(BUFFER_OVERFLOW)); \
\
block.wa_off += (bufsize); \
}

#endif // __BC_BLOCKUTILS_H
Loading

0 comments on commit 82bebc3

Please sign in to comment.