forked from NebulousLabs/Sia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
62 lines (55 loc) · 1.77 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package miner
import (
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/encoding"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"
)
// ReceiveTransactionPoolUpdate listens to the transaction pool for changes in
// the transaction pool. These changes will be applied to the blocks being
// mined.
func (m *Miner) ReceiveTransactionPoolUpdate(cc modules.ConsensusChange, unconfirmedTransactions []types.Transaction, _ []modules.SiacoinOutputDiff) {
m.mu.Lock()
defer m.mu.Unlock()
defer m.notifySubscribers()
m.height -= types.BlockHeight(len(cc.RevertedBlocks))
m.height += types.BlockHeight(len(cc.AppliedBlocks))
// The total encoded size of the transactions cannot exceed the block size.
m.transactions = nil
remainingSize := int(types.BlockSizeLimit - 5e3)
for {
if len(unconfirmedTransactions) == 0 {
break
}
remainingSize -= len(encoding.Marshal(unconfirmedTransactions[0]))
if remainingSize < 0 {
break
}
m.transactions = append(m.transactions, unconfirmedTransactions[0])
unconfirmedTransactions = unconfirmedTransactions[1:]
}
// If no blocks have been applied, the block variables do not need to be
// updated.
if len(cc.AppliedBlocks) == 0 {
if build.DEBUG {
if len(cc.RevertedBlocks) != 0 {
panic("blocks reverted without being added")
}
}
return
}
// Update the parent, target, and earliest timestamp fields for the miner.
m.parent = cc.AppliedBlocks[len(cc.AppliedBlocks)-1].ID()
target, exists1 := m.cs.ChildTarget(m.parent)
timestamp, exists2 := m.cs.EarliestChildTimestamp(m.parent)
if build.DEBUG {
if !exists1 {
panic("could not get child target")
}
if !exists2 {
panic("could not get child earliest timestamp")
}
}
m.target = target
m.earliestTimestamp = timestamp
}