Skip to content

Commit cf4be61

Browse files
authored
Merge pull request #25 from SiaFoundation/pj/add-txn-fee-to-miner-payout
Add txn fee to miner payout
2 parents 12c5ec4 + 374fcfc commit cf4be61

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

miner.go

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (ty
5353
break
5454
}
5555
b.Transactions = append(b.Transactions, txn)
56+
b.MinerPayouts[0].Value = b.MinerPayouts[0].Value.Add(txn.TotalFees())
5657
}
5758
for _, txn := range cm.V2PoolTransactions() {
5859
if weight += cs.V2TransactionWeight(txn); weight > cs.MaxBlockWeight() {
@@ -64,6 +65,7 @@ func MineBlock(cm *chain.Manager, addr types.Address, timeout time.Duration) (ty
6465
}
6566
}
6667
b.V2.Transactions = append(b.V2.Transactions, txn)
68+
b.MinerPayouts[0].Value = b.MinerPayouts[0].Value.Add(txn.MinerFee)
6769
}
6870
if b.V2 != nil {
6971
b.V2.Commitment = cs.Commitment(cs.TransactionsCommitment(b.Transactions, b.V2Transactions()), addr)

miner_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package coreutils
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"go.sia.tech/core/types"
8+
"go.sia.tech/coreutils/chain"
9+
)
10+
11+
func TestMiner(t *testing.T) {
12+
n, genesisBlock := chain.TestnetZen()
13+
n.InitialTarget = types.BlockID{0xFF}
14+
15+
sk := types.GeneratePrivateKey()
16+
genesisBlock.Transactions = []types.Transaction{{
17+
SiacoinOutputs: []types.SiacoinOutput{
18+
{
19+
Address: types.StandardUnlockHash(sk.PublicKey()),
20+
Value: types.Siacoins(10),
21+
},
22+
},
23+
}}
24+
25+
store, tipState, err := chain.NewDBStore(chain.NewMemDB(), n, genesisBlock)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
defer store.Close()
30+
cm := chain.NewManager(store, tipState)
31+
32+
// create a transaction
33+
txn := types.Transaction{
34+
SiacoinInputs: []types.SiacoinInput{{
35+
ParentID: genesisBlock.Transactions[0].SiacoinOutputID(0),
36+
UnlockConditions: types.StandardUnlockConditions(sk.PublicKey()),
37+
}},
38+
SiacoinOutputs: []types.SiacoinOutput{{
39+
Address: types.StandardUnlockHash(sk.PublicKey()),
40+
Value: types.Siacoins(9),
41+
}},
42+
MinerFees: []types.Currency{types.Siacoins(1)},
43+
}
44+
45+
// sign the inputs
46+
for _, sci := range txn.SiacoinInputs {
47+
sig := sk.SignHash(cm.TipState().WholeSigHash(txn, types.Hash256(sci.ParentID), 0, 0, nil))
48+
txn.Signatures = append(txn.Signatures, types.TransactionSignature{
49+
ParentID: types.Hash256(sci.ParentID),
50+
CoveredFields: types.CoveredFields{WholeTransaction: true},
51+
PublicKeyIndex: 0,
52+
Signature: sig[:],
53+
})
54+
}
55+
56+
// add the transaction to the pool
57+
_, err = cm.AddPoolTransactions([]types.Transaction{txn})
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
// assert the minerpayout includes the txn fee
63+
b, found := MineBlock(cm, types.VoidAddress, time.Second)
64+
if !found {
65+
t.Fatal("PoW failed")
66+
} else if len(b.MinerPayouts) != 1 {
67+
t.Fatal("expected one miner payout")
68+
} else if b.MinerPayouts[0].Value.Cmp(types.Siacoins(1).Add(cm.TipState().BlockReward())) != 0 {
69+
t.Fatal("unexpected miner payout", b.MinerPayouts[0].Value.ExactString())
70+
}
71+
}

0 commit comments

Comments
 (0)