Skip to content

Commit

Permalink
main: Less naive mining loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Dec 12, 2023
1 parent da086c4 commit 8baeb7b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/walletd/testnet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/binary"
"encoding/hex"
"fmt"
"log"
Expand Down Expand Up @@ -165,14 +166,27 @@ outer:
}
b.V2.Commitment = cs.Commitment(cs.TransactionsCommitment(b.Transactions, b.V2Transactions()), b.MinerPayouts[0].Address)
}

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() // NOTE: expensive!
copy(buf[:32], b.ParentID[:])
copy(buf[48:], root[:])
}
startBlock := time.Now()
for b.ID().CmpWork(cs.ChildTarget) < 0 {
for types.BlockID(types.HashBytes(buf)).CmpWork(cs.ChildTarget) < 0 {
b.Nonce += cs.NonceFactor()
// ensure nonce meets factor requirement
for b.Nonce%cs.NonceFactor() != 0 {
b.Nonce++
}
hashes++
binary.LittleEndian.PutUint64(buf[32:], b.Nonce)
if time.Since(startBlock) > 30*time.Second {
continue outer
}
Expand Down Expand Up @@ -308,13 +322,13 @@ func printTestnetEvents(c *api.Client, seed wallet.Seed) {
sci := t.SiacoinInputs[0].SiacoinOutput
sco := t.SiacoinOutputs[0].SiacoinOutput
if sci.Address == ourAddr {
fmt.Printf("%v (%v): Sent %v (+ %v fee) to %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, t.Fee, sco.Address)
fmt.Printf("%14v (%v): Sent %v (+ %v fee) to %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, t.Fee, sco.Address)
} else {
fmt.Printf("%v (%v): Received %v from %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, sci.Address)
fmt.Printf("%14v (%v): Received %v from %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, sci.Address)
}
case *wallet.EventMinerPayout:
sco := t.SiacoinOutput.SiacoinOutput
fmt.Printf("%v (%v): Earned %v miner payout from block %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, e.Index)
fmt.Printf("%14v (%v): Earned %v miner payout from block %v\n", e.Index, e.Timestamp.Format("Jan _2 @ 15:04:05"), sco.Value, e.Index)
}
}
}
Expand Down

0 comments on commit 8baeb7b

Please sign in to comment.