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

Add txn fee to miner payout #25

Merged
merged 1 commit into from
Feb 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (ty
break
}
b.Transactions = append(b.Transactions, txn)
b.MinerPayouts[0].Value = b.MinerPayouts[0].Value.Add(txn.TotalFees())
}
for _, txn := range cm.V2PoolTransactions() {
if weight += cs.V2TransactionWeight(txn); weight > cs.MaxBlockWeight() {
Expand All @@ -64,6 +65,7 @@ func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (ty
}
}
b.V2.Transactions = append(b.V2.Transactions, txn)
b.MinerPayouts[0].Value = b.MinerPayouts[0].Value.Add(txn.MinerFee)
}
if b.V2 != nil {
b.V2.Commitment = cs.Commitment(cs.TransactionsCommitment(b.Transactions, b.V2Transactions()), addr)
Expand Down
71 changes: 71 additions & 0 deletions miner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package coreutils

import (
"testing"
"time"

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

func TestMiner(t *testing.T) {
n, genesisBlock := chain.TestnetZen()
n.InitialTarget = types.BlockID{0xFF}

sk := types.GeneratePrivateKey()
genesisBlock.Transactions = []types.Transaction{{
SiacoinOutputs: []types.SiacoinOutput{
{
Address: types.StandardUnlockHash(sk.PublicKey()),
Value: types.Siacoins(10),
},
},
}}

store, tipState, err := chain.NewDBStore(chain.NewMemDB(), n, genesisBlock)
if err != nil {
t.Fatal(err)
}
defer store.Close()
cm := chain.NewManager(store, tipState)

// create a transaction
txn := types.Transaction{
SiacoinInputs: []types.SiacoinInput{{
ParentID: genesisBlock.Transactions[0].SiacoinOutputID(0),
UnlockConditions: types.StandardUnlockConditions(sk.PublicKey()),
}},
SiacoinOutputs: []types.SiacoinOutput{{
Address: types.StandardUnlockHash(sk.PublicKey()),
Value: types.Siacoins(9),
}},
MinerFees: []types.Currency{types.Siacoins(1)},
}

// sign the inputs
for _, sci := range txn.SiacoinInputs {
sig := sk.SignHash(cm.TipState().WholeSigHash(txn, types.Hash256(sci.ParentID), 0, 0, nil))
txn.Signatures = append(txn.Signatures, types.TransactionSignature{
ParentID: types.Hash256(sci.ParentID),
CoveredFields: types.CoveredFields{WholeTransaction: true},
PublicKeyIndex: 0,
Signature: sig[:],
})
}

// add the transaction to the pool
_, err = cm.AddPoolTransactions([]types.Transaction{txn})
if err != nil {
t.Fatal(err)
}

// assert the minerpayout includes the txn fee
b, found := MineBlock(cm, types.VoidAddress, time.Second)
if !found {
t.Fatal("PoW failed")
} else if len(b.MinerPayouts) != 1 {
t.Fatal("expected one miner payout")
} else if b.MinerPayouts[0].Value.Cmp(types.Siacoins(1).Add(cm.TipState().BlockReward())) != 0 {
t.Fatal("unexpected miner payout", b.MinerPayouts[0].Value.ExactString())
}
}
Loading