Skip to content

Commit 3e760ba

Browse files
authored
[KLC-1667] Fix code coverage workflow (#17)
* update code coverage workflow with coverage summary and results * fix parsers tests * fix p2p test failing due to invalid pbk size * update executors tests to handle klever-blockchain client * fix mock address mismatch during tests * add grace.pem file for tests usage * add error handling in executors transaction creation * improve code-coverage workflow results in fail cases
1 parent ed8e42c commit 3e760ba

File tree

11 files changed

+144
-267
lines changed

11 files changed

+144
-267
lines changed

.github/workflows/code-coverage.yml

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,62 @@ on:
99

1010
jobs:
1111
build:
12-
strategy:
13-
matrix:
14-
runs-on: [ ubuntu-latest ]
15-
runs-on: ${{ matrix.runs-on }}
12+
runs-on: ubuntu-latest
1613
name: Build
1714
steps:
18-
- name: Set up Go 1.x
19-
uses: actions/setup-go@v3
15+
- name: Check out code into the Go module directory
16+
uses: actions/checkout@v4
17+
18+
- name: Go Setup
19+
uses: ./.github/actions/go-setup-action
2020
with:
21-
go-version: 1.20.7
22-
id: go
21+
go-version: ${{vars.GO_VERSION }}
22+
go-private: ${{vars.GO_PRIVATE_REPO }}
23+
git-user: ${{ secrets.GIT_USER }}
24+
git-pass: ${{ secrets.GIT_PASS }}
2325

24-
- name: Check out code into the Go module directory
25-
uses: actions/checkout@v3
26+
- name: Run unit tests
27+
id: run-tests
28+
run: |
29+
set +e
30+
make test-coverage
31+
echo "test_exit_code=$?" >> "$GITHUB_OUTPUT"
32+
# Always succeed so that the next step can evaluate the results
33+
exit 0
2634
27-
- name: Get dependencies
35+
- name: Process test results
36+
if: always()
37+
id: coverage
2838
run: |
29-
go get -v -t -d ./...
30-
if [ -f Gopkg.toml ]; then
31-
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
32-
dep ensure
39+
# Generate coverage summary
40+
if [ -f coverage.out ]; then
41+
go tool cover -func=coverage.out > coverage-summary.txt
42+
COVERAGE=$(grep "total:" coverage-summary.txt | awk '{print $3}')
43+
echo "total=$COVERAGE" >> $GITHUB_OUTPUT
44+
else
45+
echo "No coverage data generated" > coverage-summary.txt
46+
echo "total=0.0%" >> $GITHUB_OUTPUT
3347
fi
34-
- name: Run unit tests
35-
run: make test-coverage
3648
37-
- name: Upload coverage
38-
run: bash <(curl -s https://codecov.io/bash) -f coverage.txt -y codecov.yml
49+
# Create test summary and check for failures
50+
echo "### Test Results 🧪" >> $GITHUB_STEP_SUMMARY
51+
if [ "${{ steps.run-tests.outputs.test_exit_code }}" == "0" ]; then
52+
echo "✅ All tests passed successfully" >> $GITHUB_STEP_SUMMARY
53+
else
54+
echo "❌ Some tests failed" >> $GITHUB_STEP_SUMMARY
55+
echo "::error::🔴 Test failures detected!"
56+
57+
if [ -f test-output.json ]; then
58+
echo "#### Failed Tests" >> $GITHUB_STEP_SUMMARY
59+
echo '```' >> $GITHUB_STEP_SUMMARY
60+
jq -r 'select(.Action=="fail") | "- \(.Package) - \(.Test)"' test-output.json | tee -a $GITHUB_STEP_SUMMARY
61+
echo '```' >> $GITHUB_STEP_SUMMARY
62+
63+
# Output failures as GitHub annotations
64+
jq -r 'select(.Action=="fail") | "::error file=\(.Package)::❌ \(.Test): \(.Action)"' test-output.json
65+
fi
66+
67+
exit 1
68+
fi
69+
70+
echo "Coverage: $COVERAGE" >> $GITHUB_STEP_SUMMARY

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test: clean-test
1515

1616
test-coverage:
1717
@echo "Running unit tests"
18-
CURRENT_DIRECTORY=$(CURRENT_DIRECTORY) go test -cover -coverprofile=coverage.txt -covermode=atomic -v ${TESTS_TO_RUN}
18+
CURRENT_DIRECTORY=$(CURRENT_DIRECTORY) go test -cover -coverprofile=coverage.out -covermode=atomic -v ${TESTS_TO_RUN} -json > test-output.json
1919

2020
slow-tests: clean-test
2121
@docker compose -f docker/docker-compose.yml build

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ type PendingOperationsFilterConfig struct {
194194
type ScCallsModuleConfig struct {
195195
ScProxyBech32Address string
196196
ExtraGasToExecute uint64
197-
MaxGasLimitToUse uint64
198-
GasLimitForOutOfGasTransactions uint64
197+
MaxGasLimitToUse int64
198+
GasLimitForOutOfGasTransactions int64
199199
NetworkAddress string
200200
ProxyMaxNoncesDelta int
201201
ProxyFinalityCheck bool
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY for klv17la0vdplk320zvy9s7qps5j69ff2yl3dn0drmhyuw57dnhe23g5schkvag-----
2+
MWE0YTIwNGYxMDYzZTUxN2VjMWI0YjU2NjUyYzY2YThkYzNmM2UwNTUxNDIyODEw
3+
ZDYyMWQxZmI1NTNmNmFjNWY3ZmFmNjM0M2ZiNDU0ZjEzMDg1ODc4MDE4NTI1YTJh
4+
NTJhMjdlMmQ5YmRhM2RkYzljNzUzY2Q5ZGYyYThhMjk=
5+
-----END PRIVATE KEY for klv17la0vdplk320zvy9s7qps5j69ff2yl3dn0drmhyuw57dnhe23g5schkvag-----

executors/kleverBlockchain/scCallsExecutor.go

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type ArgsScCallExecutor struct {
3939
Filter ScCallsExecuteFilter
4040
Log logger.Logger
4141
ExtraGasToExecute uint64
42-
MaxGasLimitToUse uint64
43-
GasLimitForOutOfGasTransactions uint64
42+
MaxGasLimitToUse int64
43+
GasLimitForOutOfGasTransactions int64
4444
NonceTxHandler NonceTransactionsHandler
4545
PrivateKey crypto.PrivateKey
4646
SingleSigner crypto.SingleSigner
@@ -55,8 +55,8 @@ type scCallExecutor struct {
5555
filter ScCallsExecuteFilter
5656
log logger.Logger
5757
extraGasToExecute uint64
58-
maxGasLimitToUse uint64
59-
gasLimitForOutOfGasTransactions uint64
58+
maxGasLimitToUse int64
59+
gasLimitForOutOfGasTransactions int64
6060
nonceTxHandler NonceTransactionsHandler
6161
privateKey crypto.PrivateKey
6262
singleSigner crypto.SingleSigner
@@ -281,30 +281,29 @@ func (executor *scCallExecutor) executeOperation(
281281
return err
282282
}
283283

284-
tx := transaction.NewBaseTransaction(executor.senderAddress.Bytes(), 0, nil, 0, 0)
284+
tx := transaction.NewBaseTransaction(executor.senderAddress.Bytes(), 0, [][]byte{dataBytes}, 0, 0)
285285
err = tx.SetChainID([]byte(networkConfig.ChainID))
286286
if err != nil {
287287
return err
288288
}
289289

290-
contractRequest := transaction.SmartContract{
290+
contractRequest := &transaction.SmartContract{
291+
Type: transaction.SmartContract_SCInvoke,
291292
Address: receiverAddr.Bytes(),
292293
}
293294

294-
txArgs := transaction.TXArgs{
295-
Type: uint32(transaction.SmartContract_SCInvoke),
296-
Sender: executor.senderAddress.Bytes(),
297-
Contract: json.RawMessage(contractRequest.String()),
298-
Data: [][]byte{dataBytes},
295+
err = tx.PushContract(transaction.TXContract_SmartContractType, contractRequest)
296+
if err != nil {
297+
return err
299298
}
300299

301-
err = tx.AddTransaction(txArgs)
300+
err = executor.nonceTxHandler.ApplyNonceAndGasPrice(ctx, executor.senderAddress, tx)
302301
if err != nil {
303302
return err
304303
}
305304

306305
to := callData.To.Bech32()
307-
if tx.GasLimit > contractMaxGasLimit {
306+
if tx.GetBandwidthFee() > contractMaxGasLimit {
308307
// the contract will refund this transaction, so we will use less gas to preserve funds
309308
executor.log.Warn("setting a lower gas limit for this transaction because it will be refunded",
310309
"computed gas limit", tx.GasLimit,
@@ -316,10 +315,10 @@ func (executor *scCallExecutor) executeOperation(
316315
"amount", callData.Amount,
317316
"nonce", callData.Nonce,
318317
)
319-
tx.GasLimit = executor.gasLimitForOutOfGasTransactions
318+
tx.RawData.BandwidthFee = executor.gasLimitForOutOfGasTransactions
320319
}
321320

322-
if tx.GasLimit > executor.maxGasLimitToUse {
321+
if tx.GetBandwidthFee() > executor.maxGasLimitToUse {
323322
executor.log.Warn("can not execute transaction because the provided gas limit on the SC call exceeds "+
324323
"the maximum gas limit allowance for this executor, WILL SKIP the execution",
325324
"computed gas limit", tx.GasLimit,
@@ -335,11 +334,6 @@ func (executor *scCallExecutor) executeOperation(
335334
return nil
336335
}
337336

338-
err = executor.nonceTxHandler.ApplyNonceAndGasPrice(ctx, executor.senderAddress, tx)
339-
if err != nil {
340-
return err
341-
}
342-
343337
err = executor.signTransactionWithPrivateKey(tx)
344338
if err != nil {
345339
return err
@@ -425,7 +419,7 @@ func (executor *scCallExecutor) checkResults(ctx context.Context, hash string) (
425419
return nil, true
426420
}
427421

428-
executor.logFullTransaction(ctx, hash)
422+
executor.logFullTransaction(ctx, txResult)
429423
return fmt.Errorf("%w for tx hash %s", errTransactionFailed, hash), true
430424
}
431425

@@ -447,20 +441,14 @@ func (executor *scCallExecutor) handleError(ctx context.Context, err error) {
447441
}()
448442
}
449443

450-
func (executor *scCallExecutor) logFullTransaction(ctx context.Context, hash string) {
451-
txData, err := executor.proxy.GetTransactionInfoWithResults(ctx, hash)
452-
if err != nil {
453-
executor.log.Error("error getting the transaction for display", "error", err)
454-
return
455-
}
456-
444+
func (executor *scCallExecutor) logFullTransaction(ctx context.Context, txData *models.TransactionData) {
457445
txDataString, err := json.MarshalIndent(txData, "", " ")
458446
if err != nil {
459447
executor.log.Error("error preparing transaction for display", "error", err)
460448
return
461449
}
462450

463-
executor.log.Error("transaction failed", "hash", hash, "full transaction details", string(txDataString))
451+
executor.log.Error("transaction failed", "hash", txData.Hash, "full transaction details", string(txDataString))
464452
}
465453

466454
func (executor *scCallExecutor) waitForExtraDelay(ctx context.Context, err error) {

0 commit comments

Comments
 (0)