Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix invalid MinerPayouts in testutil.MineBlock #46

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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