Skip to content

Commit

Permalink
Merge pull request #46 from SiaFoundation/pj/fix-miner-fee-race
Browse files Browse the repository at this point in the history
fix invalid MinerPayouts in testutil.MineBlock
  • Loading branch information
lukechampine committed Apr 17, 2024
2 parents c7958e8 + 018cccd commit a3dce82
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
11 changes: 9 additions & 2 deletions miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ func FindBlockNonce(cs consensus.State, b *types.Block, timeout time.Duration) b
// MineBlock constructs a block from the provided address and the transactions
// in the txpool, and attempts to find a nonce for it that meets the PoW target.
func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (types.Block, bool) {
retry:
cs := cm.TipState()
txns := cm.PoolTransactions()
v2Txns := cm.V2PoolTransactions()
if cs.Index != cm.Tip() {
goto retry
}

b := types.Block{
ParentID: cs.Index.ID,
Timestamp: types.CurrentTimestamp(),
Expand All @@ -48,14 +55,14 @@ func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (ty
}},
}
var weight uint64
for _, txn := range cm.PoolTransactions() {
for _, txn := range txns {
if weight += cs.TransactionWeight(txn); weight > cs.MaxBlockWeight() {
break
}
b.Transactions = append(b.Transactions, txn)
b.MinerPayouts[0].Value = b.MinerPayouts[0].Value.Add(txn.TotalFees())
}
for _, txn := range cm.V2PoolTransactions() {
for _, txn := range v2Txns {
if weight += cs.V2TransactionWeight(txn); weight > cs.MaxBlockWeight() {
break
}
Expand Down
24 changes: 0 additions & 24 deletions testutil/network.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package testutil

import (
"time"

"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
"go.sia.tech/coreutils"
"go.sia.tech/coreutils/chain"
)

Expand All @@ -24,24 +21,3 @@ func Network() (*consensus.Network, types.Block) {
n.HardforkV2.RequireHeight = 250
return n, genesisBlock
}

// MineBlock mines a block with the given transactions, transaction fees are
// added to the miner payout.
func MineBlock(cm *chain.Manager, minerAddress types.Address) types.Block {
var minerFees types.Currency
for _, txn := range cm.PoolTransactions() {
minerFees = minerFees.Add(txn.TotalFees())
}

state := cm.TipState()
b := types.Block{
ParentID: state.Index.ID,
Timestamp: types.CurrentTimestamp(),
Transactions: cm.PoolTransactions(),
MinerPayouts: []types.SiacoinOutput{{Address: minerAddress, Value: state.BlockReward().Add(minerFees)}},
}
if !coreutils.FindBlockNonce(state, &b, 5*time.Second) {
panic("failed to find nonce")
}
return b
}
4 changes: 3 additions & 1 deletion wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func syncDB(cm *chain.Manager, store wallet.SingleAddressStore) error {
func mineAndSync(cm *chain.Manager, ws wallet.SingleAddressStore, address types.Address, n uint64) error {
// mine n blocks
for i := uint64(0); i < n; i++ {
if err := cm.AddBlocks([]types.Block{testutil.MineBlock(cm, address)}); err != nil {
if block, found := coreutils.MineBlock(cm, address, 5*time.Second); !found {
panic("failed to mine block")
} else if err := cm.AddBlocks([]types.Block{block}); err != nil {
return fmt.Errorf("failed to add blocks: %w", err)
}
}
Expand Down

0 comments on commit a3dce82

Please sign in to comment.