Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 99dab44

Browse files
committed
Fix potential underflow in genesis snapshot generation
1 parent 4cce1bd commit 99dab44

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pkg/testsuite/snapshotcreator/snapshotcreator.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package snapshotcreator
33
import (
44
"os"
55

6+
"github.com/iotaledger/hive.go/core/safemath"
67
"github.com/iotaledger/hive.go/ierrors"
78
"github.com/iotaledger/hive.go/lo"
89
"github.com/iotaledger/hive.go/log"
@@ -137,8 +138,18 @@ func CreateSnapshot(opts ...options.Option[Options]) error {
137138
return accumulator + details.Amount
138139
}, iotago.BaseToken(0))
139140

141+
supplyMinusAccounts, err := safemath.SafeSub(opt.ProtocolParameters.TokenSupply(), totalAccountAmount)
142+
if err != nil {
143+
return ierrors.Wrapf(err, "failed to calculate genesis output balance, remaining funds: %d, needed account funds: %d", opt.ProtocolParameters.TokenSupply(), totalAccountAmount)
144+
}
145+
146+
genesisOutputBalance, err := safemath.SafeSub(supplyMinusAccounts, totalBasicOutputAmount)
147+
if err != nil {
148+
return ierrors.Wrapf(err, "failed to calculate genesis output balance, remaining funds: %d, needed basic output funds: %d", supplyMinusAccounts, totalBasicOutputAmount)
149+
}
150+
140151
var genesisTransactionOutputs iotago.TxEssenceOutputs
141-
genesisOutput, err := createGenesisOutput(api, opt.ProtocolParameters.TokenSupply()-totalAccountAmount-totalBasicOutputAmount, iotago.MaxMana/100, opt.GenesisKeyManager)
152+
genesisOutput, err := createGenesisOutput(api, genesisOutputBalance, iotago.MaxMana/100, opt.GenesisKeyManager)
142153
if err != nil {
143154
return ierrors.Wrap(err, "failed to create genesis outputs")
144155
}

0 commit comments

Comments
 (0)