Skip to content

Commit 1749790

Browse files
authored
fix(ledger): preallocate return structs (#915)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ca737df commit 1749790

File tree

13 files changed

+26
-25
lines changed

13 files changed

+26
-25
lines changed

ledger/allegra/allegra.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (b *AllegraBlock) Transactions() []common.Transaction {
110110
}
111111

112112
func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
113-
var txs []*utxorpc.Tx
113+
txs := []*utxorpc.Tx{}
114114
tmpHash, _ := hex.DecodeString(b.Hash())
115115
for _, t := range b.Transactions() {
116116
tx := t.Utxorpc()
@@ -274,7 +274,7 @@ func (t AllegraTransaction) Consumed() []common.TransactionInput {
274274
}
275275

276276
func (t AllegraTransaction) Produced() []common.Utxo {
277-
var ret []common.Utxo
277+
ret := []common.Utxo{}
278278
for idx, output := range t.Outputs() {
279279
ret = append(
280280
ret,

ledger/alonzo/alonzo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (b *AlonzoBlock) Transactions() []common.Transaction {
119119
}
120120

121121
func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
122-
var txs []*utxorpc.Tx
122+
txs := []*utxorpc.Tx{}
123123
tmpHash, _ := hex.DecodeString(b.Hash())
124124
for _, t := range b.Transactions() {
125125
tx := t.Utxorpc()

ledger/babbage/babbage.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (b *BabbageBlock) Transactions() []common.Transaction {
120120
}
121121

122122
func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
123-
var txs []*utxorpc.Tx
123+
txs := []*utxorpc.Tx{}
124124
tmpHash, _ := hex.DecodeString(b.Hash())
125125
for _, t := range b.Transactions() {
126126
tx := t.Utxorpc()
@@ -265,8 +265,9 @@ func (b *BabbageTransactionBody) TotalCollateral() uint64 {
265265
}
266266

267267
func (b *BabbageTransactionBody) Utxorpc() *utxorpc.Tx {
268-
var txi, txri []*utxorpc.TxInput
269-
var txo []*utxorpc.TxOutput
268+
txi := []*utxorpc.TxInput{}
269+
txri := []*utxorpc.TxInput{}
270+
txo := []*utxorpc.TxOutput{}
270271
for _, i := range b.Inputs() {
271272
input := i.Utxorpc()
272273
txi = append(txi, input)

ledger/byron/byron.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (t *ByronTransaction) Consumed() []common.TransactionInput {
274274
}
275275

276276
func (t *ByronTransaction) Produced() []common.Utxo {
277-
var ret []common.Utxo
277+
ret := []common.Utxo{}
278278
for idx, output := range t.Outputs() {
279279
ret = append(
280280
ret,

ledger/byron/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (g *ByronGenesis) GenesisUtxos() ([]common.Utxo, error) {
104104
}
105105

106106
func (g *ByronGenesis) avvmUtxos() ([]common.Utxo, error) {
107-
var ret []common.Utxo
107+
ret := []common.Utxo{}
108108
for pubkey, amount := range g.AvvmDistr {
109109
// Build address from redeem pubkey
110110
pubkeyBytes, err := base64.URLEncoding.DecodeString(pubkey)
@@ -145,7 +145,7 @@ func (g *ByronGenesis) avvmUtxos() ([]common.Utxo, error) {
145145
}
146146

147147
func (g *ByronGenesis) nonAvvmUtxos() ([]common.Utxo, error) {
148-
var ret []common.Utxo
148+
ret := []common.Utxo{}
149149
for address, amount := range g.NonAvvmBalances {
150150
tmpAddr, err := common.NewAddress(address)
151151
if err != nil {

ledger/common/certs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -480,7 +480,7 @@ func (c *MoveInstantaneousRewardsCertificate) UnmarshalCBOR(
480480
}
481481

482482
func (c *MoveInstantaneousRewardsCertificate) Utxorpc() *utxorpc.Certificate {
483-
var tmpMirTargets []*utxorpc.MirTarget
483+
tmpMirTargets := []*utxorpc.MirTarget{}
484484
for stakeCred, deltaCoin := range c.Reward.Rewards {
485485
tmpMirTargets = append(
486486
tmpMirTargets,

ledger/common/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (m MultiAsset[T]) MarshalJSON() ([]byte, error) {
183183
}
184184

185185
func (m *MultiAsset[T]) Policies() []Blake2b224 {
186-
var ret []Blake2b224
186+
ret := []Blake2b224{}
187187
for policyId := range m.data {
188188
ret = append(ret, policyId)
189189
}
@@ -195,7 +195,7 @@ func (m *MultiAsset[T]) Assets(policyId Blake2b224) [][]byte {
195195
if !ok {
196196
return nil
197197
}
198-
var ret [][]byte
198+
ret := [][]byte{}
199199
for assetName := range assets {
200200
ret = append(ret, assetName.Bytes())
201201
}

ledger/conway/conway.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (b *ConwayBlock) Transactions() []common.Transaction {
119119
}
120120

121121
func (b *ConwayBlock) Utxorpc() *utxorpc.Block {
122-
var txs []*utxorpc.Tx
122+
txs := []*utxorpc.Tx{}
123123
tmpHash, _ := hex.DecodeString(b.Hash())
124124
for _, t := range b.Transactions() {
125125
tx := t.Utxorpc()

ledger/mary/mary.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (b *MaryBlock) Transactions() []common.Transaction {
112112
}
113113

114114
func (b *MaryBlock) Utxorpc() *utxorpc.Block {
115-
var txs []*utxorpc.Tx
115+
txs := []*utxorpc.Tx{}
116116
tmpHash, _ := hex.DecodeString(b.Hash())
117117
for _, t := range b.Transactions() {
118118
tx := t.Utxorpc()
@@ -286,7 +286,7 @@ func (t MaryTransaction) Consumed() []common.TransactionInput {
286286
}
287287

288288
func (t MaryTransaction) Produced() []common.Utxo {
289-
var ret []common.Utxo
289+
ret := []common.Utxo{}
290290
for idx, output := range t.Outputs() {
291291
ret = append(
292292
ret,

ledger/mary/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func UtxoValidateOutputTooBigUtxo(
4848
_ common.LedgerState,
4949
_ common.ProtocolParameters,
5050
) error {
51-
var badOutputs []common.TransactionOutput
51+
badOutputs := []common.TransactionOutput{}
5252
for _, txOutput := range tx.Outputs() {
5353
tmpOutput, ok := txOutput.(*MaryTransactionOutput)
5454
if !ok {

0 commit comments

Comments
 (0)