Skip to content

Commit

Permalink
Merge pull request #979 from iotaledger/payload-nil-test
Browse files Browse the repository at this point in the history
Fix panic when payload is nil
  • Loading branch information
muXxer committed May 22, 2024
2 parents 1f0661e + 32da493 commit 61046c3
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 44 deletions.
4 changes: 3 additions & 1 deletion components/restapi/core/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package core

import "github.com/iotaledger/iota.go/v4/api"
import (
"github.com/iotaledger/iota.go/v4/api"
)

func info() *api.InfoResponse {
return &api.InfoResponse{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (t *Tracker) TrackCandidateBlock(block *blocks.Block) {
t.mutex.Lock()
defer t.mutex.Unlock()

if block.Payload().PayloadType() != iotago.PayloadCandidacyAnnouncement {
if payload := block.Payload(); payload == nil || payload.PayloadType() != iotago.PayloadCandidacyAnnouncement {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o *SybilProtection) TrackBlock(block *blocks.Block) {
return
}

if block.Payload().PayloadType() != iotago.PayloadCandidacyAnnouncement {
if payload := block.Payload(); payload == nil || payload.PayloadType() != iotago.PayloadCandidacyAnnouncement {
return
}

Expand Down
1 change: 1 addition & 0 deletions pkg/testsuite/snapshotcreator/snapshotcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func createGenesisOutput(api iotago.API, genesisTokenAmount iotago.BaseToken, ge
return output, nil
}

//nolint:nilnil // we want to return nil here
return nil, nil
}

Expand Down
21 changes: 0 additions & 21 deletions tools/docker-network/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ services:
--prometheus.goMetrics=true
--prometheus.processMetrics=true
--debugAPI.enabled=true
profiles:
- minimal
- full
node-2-validator:
image: docker-network-node-1-validator:latest
Expand Down Expand Up @@ -275,9 +272,6 @@ services:
--inx.address=node-1-validator:9029
--restAPI.bindAddress=0.0.0.0:9091
--restAPI.advertiseAddress=inx-indexer:9091
profiles:
- minimal
- full
inx-mqtt:
image: iotaledger/inx-mqtt:2.0-alpha
Expand All @@ -291,9 +285,6 @@ services:
command: >
--inx.address=node-1-validator:9029
--mqtt.websocket.bindAddress=inx-mqtt:1888
profiles:
- minimal
- full
inx-blockissuer:
image: iotaledger/inx-blockissuer:1.0-alpha
Expand All @@ -313,9 +304,6 @@ services:
--restAPI.bindAddress=inx-blockissuer:9086
--blockIssuer.accountAddress=rms1prkursay9fs2qjmfctamd6yxg9x8r3ry47786x0mvwek4qr9xd9d5c6gkun
--blockIssuer.proofOfWork.targetTrailingZeros=5
profiles:
- minimal
- full
inx-faucet:
image: iotaledger/inx-faucet:2.0-alpha
Expand Down Expand Up @@ -343,9 +331,6 @@ services:
--faucet.baseTokenAmountMaxTarget=5000000000
--faucet.manaAmount=100000000
--faucet.manaAmountMinFaucet=1000000000
profiles:
- minimal
- full
inx-validator-1:
image: iotaledger/inx-validator:1.0-alpha
Expand All @@ -365,9 +350,6 @@ services:
--validator.accountAddress=rms1pzg8cqhfxqhq7pt37y8cs4v5u4kcc48lquy2k73ehsdhf5ukhya3y5rx2w6
--validator.candidacyRetryInterval=${CANDIDACY_RETRY_INTERVAL:-10s}
--validator.issueCandidacyPayload=${ISSUE_CANDIDACY_PAYLOAD_V1:-true}
profiles:
- minimal
- full
inx-validator-2:
image: iotaledger/inx-validator:1.0-alpha
Expand Down Expand Up @@ -447,9 +429,6 @@ services:
- "--dashboard.auth.username=${DASHBOARD_USERNAME:-admin}"
- "--dashboard.auth.passwordHash=${DASHBOARD_PASSWORD:-0000000000000000000000000000000000000000000000000000000000000000}"
- "--dashboard.auth.passwordSalt=${DASHBOARD_SALT:-0000000000000000000000000000000000000000000000000000000000000000}"
profiles:
- minimal
- full

inx-dashboard-2:
container_name: inx-dashboard-2
Expand Down
43 changes: 23 additions & 20 deletions tools/docker-network/run.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
#!/bin/bash

# Create a function to join an array of strings by a given character
function join { local IFS="$1"; shift; echo "$*"; }
function join {
local IFS="$1"
shift
echo "$*"
}

# Initialize variables
MONITORING=0
MINIMAL=0

# Loop over all arguments
for arg in "$@"
do
case $arg in
monitoring=*)
MONITORING="${arg#*=}"
shift
;;
minimal=*)
MINIMAL="${arg#*=}"
shift
;;
*)
# Unknown option
echo "Unknown argument: $arg"
echo 'Call with ./run.sh [monitoring=0|1] [minimal=0|1]'
exit 1
;;
esac
for arg in "$@"; do
case $arg in
monitoring=*)
MONITORING="${arg#*=}"
shift
;;
minimal=*)
MINIMAL="${arg#*=}"
shift
;;
*)
# Unknown option
echo "Unknown argument: $arg"
echo 'Call with ./run.sh [monitoring=0|1] [minimal=0|1]'
exit 1
;;
esac
done

export DOCKER_BUILDKIT=1
Expand Down Expand Up @@ -94,14 +97,14 @@ if [ $MONITORING -ne 0 ]; then
fi

if [ $MINIMAL -ne 0 ]; then
PROFILES+=("minimal")
echo "Minimal profile active"
else
PROFILES+=("full")
echo "Full profile active"
fi

export COMPOSE_PROFILES=$(join , ${PROFILES[@]})

docker compose up

echo "Clean up docker resources"
Expand Down
61 changes: 61 additions & 0 deletions tools/docker-network/tests/nil_payload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build dockertests

package tests

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/iota-core/pkg/testsuite/mock"
"github.com/iotaledger/iota-core/tools/docker-network/tests/dockertestframework"
)

// Test_AccountTransitions follows the account state transition flow described in:
// 1. Create account-1.
// 2. Create account-2.
// 3. account-1 requests faucet funds then allots 1000 mana to account-2.
// 4. account-2 requests faucet funds then creates native tokens.
func Test_Payload_Nil_Test(t *testing.T) {
d := dockertestframework.NewDockerTestFramework(t,
dockertestframework.WithProtocolParametersOptions(dockertestframework.ShortSlotsAndEpochsProtocolParametersOptionsFunc()...),
)
defer d.Stop()

d.AddValidatorNode("V1", "docker-network-inx-validator-1-1", "http://localhost:8050", "rms1pzg8cqhfxqhq7pt37y8cs4v5u4kcc48lquy2k73ehsdhf5ukhya3y5rx2w6")
d.AddValidatorNode("V2", "docker-network-inx-validator-2-1", "http://localhost:8060", "rms1pqm4xk8e9ny5w5rxjkvtp249tfhlwvcshyr3pc0665jvp7g3hc875k538hl")
d.AddValidatorNode("V3", "docker-network-inx-validator-3-1", "http://localhost:8070", "rms1pp4wuuz0y42caz48vv876qfpmffswsvg40zz8v79sy8cp0jfxm4kunflcgt")
d.AddValidatorNode("V4", "docker-network-inx-validator-4-1", "http://localhost:8040", "rms1pr8cxs3dzu9xh4cduff4dd4cxdthpjkpwmz2244f75m0urslrsvtsshrrjw")
d.AddNode("node5", "docker-network-node-5-1", "http://localhost:8080")

err := d.Run()
require.NoError(t, err)

d.WaitUntilNetworkReady()

ctx, cancel := context.WithCancel(context.Background())

// cancel the context when the test is done
t.Cleanup(cancel)

// create account-1
accounts := d.CreateAccountsFromFaucet(ctx, 2, "account-1", "account-2")
account1 := accounts[0]
account2 := accounts[1]

// allot 1000 mana from account-1 to account-2
fmt.Println("Allotting mana from account-1 to account-2")
d.RequestFaucetFundsAndAllotManaTo(account1.Wallet(), account2.Account(), 1000)

// create native token
fmt.Println("Creating native token")
d.CreateNativeToken(account1.Wallet(), 5_000_000, 10_000_000_000)

blk := lo.PanicOnErr(account1.Wallet().CreateBasicBlock(ctx, "something", mock.WithPayload(nil)))
d.SubmitBlock(ctx, blk.ProtocolBlock())

d.AwaitEpochFinalized()
}
2 changes: 2 additions & 0 deletions tools/docker-network/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mkdir -p docker-network-snapshots/
# Allow 'others' to write, so a snapshot can be created via the management API from within docker containers.
chmod o+w docker-network-snapshots/

export COMPOSE_PROFILES="full"

# Allow docker compose to build and cache an image
docker compose build --build-arg DOCKER_BUILD_CONTEXT=${DOCKER_BUILD_CONTEXT} --build-arg DOCKERFILE_PATH=${DOCKERFILE_PATH}

Expand Down

0 comments on commit 61046c3

Please sign in to comment.