-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
719f413
commit 4e9c427
Showing
4 changed files
with
142 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"time" | ||
|
||
"go.sia.tech/core/consensus" | ||
"go.sia.tech/core/types" | ||
) | ||
|
||
// 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(ctx context.Context, cs consensus.State, txns []types.Transaction, v2Txns []types.V2Transaction, addr types.Address) (types.Block, error) { | ||
b := types.Block{ | ||
ParentID: cs.Index.ID, | ||
Timestamp: types.CurrentTimestamp(), | ||
MinerPayouts: []types.SiacoinOutput{{ | ||
Value: cs.BlockReward(), | ||
Address: addr, | ||
}}, | ||
} | ||
|
||
if cs.Index.Height >= cs.Network.HardforkV2.AllowHeight { | ||
b.V2 = &types.V2BlockData{ | ||
Height: cs.Index.Height + 1, | ||
} | ||
} | ||
|
||
var weight uint64 | ||
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 v2Txns { | ||
if weight += cs.V2TransactionWeight(txn); weight > cs.MaxBlockWeight() { | ||
break | ||
} | ||
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) | ||
} | ||
|
||
b.Nonce = 0 | ||
buf := make([]byte, 32+8+8+32) | ||
binary.LittleEndian.PutUint64(buf[32:], b.Nonce) | ||
binary.LittleEndian.PutUint64(buf[40:], uint64(b.Timestamp.Unix())) | ||
if b.V2 != nil { | ||
copy(buf[:32], "sia/id/block|") | ||
copy(buf[48:], b.V2.Commitment[:]) | ||
} else { | ||
root := b.MerkleRoot() | ||
copy(buf[:32], b.ParentID[:]) | ||
copy(buf[48:], root[:]) | ||
} | ||
factor := cs.NonceFactor() | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) | ||
defer cancel() | ||
|
||
for types.BlockID(types.HashBytes(buf)).CmpWork(cs.ChildTarget) < 0 { | ||
select { | ||
case <-ctx.Done(): | ||
return types.Block{}, ctx.Err() | ||
default: | ||
} | ||
|
||
b.Nonce += factor | ||
binary.LittleEndian.PutUint64(buf[32:], b.Nonce) | ||
} | ||
return b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
module go.sia.tech/cpuminerd | ||
|
||
go 1.21.8 | ||
go 1.22.5 | ||
|
||
toolchain go1.21.11 | ||
toolchain go1.23.0 | ||
|
||
require ( | ||
go.sia.tech/core v0.2.7 | ||
go.sia.tech/coreutils v0.0.5 | ||
go.sia.tech/walletd v0.1.1-alpha.0.20240610172105-eb95d4161b91 | ||
go.sia.tech/core v0.4.5 | ||
go.sia.tech/walletd v0.8.1-0.20240901222528-4a6f567605f6 | ||
go.uber.org/zap v1.27.0 | ||
lukechampine.com/frand v1.4.2 | ||
) | ||
|
||
require ( | ||
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect | ||
github.com/julienschmidt/httprouter v1.3.0 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
go.etcd.io/bbolt v1.3.10 // indirect | ||
go.sia.tech/jape v0.11.2-0.20240306154058-9832414a5385 // indirect | ||
go.sia.tech/coreutils v0.3.1 // indirect | ||
go.sia.tech/jape v0.12.1 // indirect | ||
go.sia.tech/mux v1.2.0 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.org/x/crypto v0.24.0 // indirect | ||
golang.org/x/sys v0.21.0 // indirect | ||
golang.org/x/crypto v0.26.0 // indirect | ||
golang.org/x/sys v0.24.0 // indirect | ||
golang.org/x/tools v0.22.0 // indirect | ||
lukechampine.com/frand v1.4.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters