-
Notifications
You must be signed in to change notification settings - Fork 31
/
blockchain_clean.go
209 lines (173 loc) · 4.85 KB
/
blockchain_clean.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2021 The Alaya Network Authors
// This file is part of the Alaya-Go library.
//
// The Alaya-Go library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Alaya-Go library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Alaya-Go library. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"sync"
"sync/atomic"
"time"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/core/rawdb"
"github.com/AlayaNetwork/Alaya-Go/ethdb"
"github.com/AlayaNetwork/Alaya-Go/event"
"github.com/AlayaNetwork/Alaya-Go/log"
)
var (
lastNumberKey = []byte("last-clean-number")
minCleanTimeout = time.Minute
cleanDistance uint64 = 1
)
type CleanupEvent struct{}
type CleanBatch struct {
lock sync.Mutex
batch ethdb.Batch
}
func (cb *CleanBatch) Delete(key []byte) error {
cb.lock.Lock()
defer cb.lock.Unlock()
return cb.batch.Delete(key)
}
func (cb *CleanBatch) ValueSize() int {
cb.lock.Lock()
defer cb.lock.Unlock()
return cb.batch.ValueSize()
}
func (cb *CleanBatch) WriteAndRest() error {
cb.lock.Lock()
defer cb.lock.Unlock()
if err := cb.batch.Write(); err != nil {
cb.batch.Reset()
return err
}
cb.batch.Reset()
return nil
}
type Cleaner struct {
stopped common.AtomicBool
cleaning common.AtomicBool
interval uint64
lastNumber uint64
cleanTimeout time.Duration
gcMpt bool
wg sync.WaitGroup
exit chan struct{}
cleanFeed event.Feed
scope event.SubscriptionScope
cleanCh chan *CleanupEvent
batch CleanBatch
lock sync.RWMutex
blockchain *BlockChain
}
func NewCleaner(blockchain *BlockChain, interval uint64, cleanTimeout time.Duration, gcMpt bool) *Cleaner {
c := &Cleaner{
interval: interval,
lastNumber: 0,
cleanTimeout: cleanTimeout,
gcMpt: gcMpt,
exit: make(chan struct{}),
cleanCh: make(chan *CleanupEvent, 1),
batch: CleanBatch{
batch: blockchain.db.NewBatch(),
},
blockchain: blockchain,
}
if c.cleanTimeout < minCleanTimeout {
c.cleanTimeout = minCleanTimeout
}
buf, err := c.blockchain.db.Get(lastNumberKey)
if err == nil && len(buf) > 0 {
lastNumber := common.BytesToUint64(buf)
atomic.StoreUint64(&c.lastNumber, lastNumber)
}
c.scope.Track(c.cleanFeed.Subscribe(c.cleanCh))
c.wg.Add(1)
go c.loop()
return c
}
func (c *Cleaner) Stop() {
if c.stopped.IsSet() {
return
}
c.scope.Close()
close(c.exit)
c.stopped.Set(true)
c.wg.Wait()
}
func (c *Cleaner) Cleanup() {
if c.cleaning.IsSet() {
return
}
c.cleaning.Set(true)
c.cleanFeed.Send(&CleanupEvent{})
}
func (c *Cleaner) NeedCleanup() bool {
lastNumber := atomic.LoadUint64(&c.lastNumber)
return c.blockchain.CurrentBlock().NumberU64()-lastNumber >= 2*c.interval && !c.cleaning.IsSet()
}
func (c *Cleaner) loop() {
defer c.wg.Done()
for {
select {
case <-c.cleanCh:
c.cleanup()
case <-c.exit:
return
}
}
}
func (c *Cleaner) cleanup() {
defer c.cleaning.Set(false)
db := c.blockchain.db
lastNumber := atomic.LoadUint64(&c.lastNumber)
currentBlock := c.blockchain.CurrentBlock()
if currentBlock.NumberU64()-lastNumber <= cleanDistance {
return
}
var (
receipts = 0
txs = 0
keys = 0
)
t := time.Now()
log.Info("Start cleanup database", "interval", c.interval, "cleanTimeout", c.cleanTimeout, "gcMpt", c.gcMpt, "lastNumber", atomic.LoadUint64(&c.lastNumber), "number", currentBlock.NumberU64(), "hash", currentBlock.Hash())
defer func() {
log.Info("Finish cleanup database", "lastNumber", atomic.LoadUint64(&c.lastNumber), "receipts", receipts, "txs", txs, "keys", keys, "elapsed", time.Since(t))
}()
if currentBlock.NumberU64()-c.lastNumber >= 2*c.interval {
number := lastNumber + 1
for ; number <= currentBlock.NumberU64()-c.interval; number++ {
block := c.blockchain.GetBlockByNumber(number)
if block == nil {
log.Error("Found bad header", "number", number)
return
}
rawdb.DeleteReceipts(db, block.Hash(), block.NumberU64())
//batch := c.blockchain.db.NewBatch()
//for _, tx := range block.Transactions() {
// txs++
// rawdb.DeleteTxLookupEntry(batch, tx.Hash())
//}
//batch.Write()
receipts++
if time.Since(t) >= c.cleanTimeout || c.stopped.IsSet() {
atomic.StoreUint64(&c.lastNumber, number)
db.Put(lastNumberKey, common.Uint64ToBytes(number))
return
}
}
atomic.StoreUint64(&c.lastNumber, number-1)
db.Put(lastNumberKey, common.Uint64ToBytes(number-1))
}
}