This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcemelon.go
300 lines (260 loc) · 7.46 KB
/
cemelon.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2017-2018 Vasiliy Vasilyuk. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/xorcare/blockchain"
)
var (
startBlockIndex = -1
endBlockIndex = -1
countStreams = 1
whitenAddressSize = 262144
notCollectFirstAddresses = false
notCollectAllAddresses = false
checkStatusAddress = false
saveOnlyBalanced = false
outFileBaseName = "cemelon.txt"
isWhitenAddress Map
addressFormat = "%34s, %s, %16d"
)
func init() {
fmt.Println("Program: Cemelon")
fmt.Println("Author: Vasiliy Vasilyuk")
fmt.Println("Github: https://git.io/fNhcc")
fmt.Println("License: BSD 3-Clause \"New\" or \"Revised\" License")
fmt.Println()
fmt.Println("Runned:", strings.Join(os.Args, " "))
flag.IntVar(&startBlockIndex, "s", startBlockIndex, "The block number at which to start collecting addresses")
flag.IntVar(&endBlockIndex, "e", endBlockIndex, "The block number on which program finished collecting the addresses including this number")
flag.IntVar(&whitenAddressSize, "m", whitenAddressSize, "The number of addresses stored in the card to prevent re-entry of addresses")
flag.BoolVar(¬CollectFirstAddresses, "r", notCollectFirstAddresses, "Not to collect the first address in the block")
flag.IntVar(&countStreams, "n", countStreams, "The number of threads downloading data")
flag.StringVar(&outFileBaseName, "o", outFileBaseName, "Output data file base name")
flag.StringVar(&addressFormat, "f", addressFormat, "Output data format string")
flag.BoolVar(¬CollectAllAddresses, "z", notCollectAllAddresses, "Not to collect all addresses")
flag.BoolVar(&checkStatusAddress, "c", checkStatusAddress, "To check the balance of addresses and to hash160")
flag.BoolVar(&saveOnlyBalanced, "b", saveOnlyBalanced, "Save only the addresses with a balance")
flag.Parse()
isWhitenAddress = *NewMap()
}
type Map struct {
sync.RWMutex
m map[string]bool
}
func (c *Map) Count() (i int) {
c.RLock()
i = len(c.m)
c.RUnlock()
return
}
func (c *Map) Exist(key string) bool {
c.RLock()
_, ok := c.m[key]
c.RUnlock()
return ok
}
func (c *Map) Store(key string, value bool) {
c.Lock()
c.m[key] = value
c.Unlock()
}
func (c *Map) Clear() {
c.Lock()
c.m = map[string]bool{}
c.Unlock()
}
func NewMap() *Map {
return &Map{
m: make(map[string]bool),
}
}
type InformationRecord struct {
Filename string
Message string
BlockIndex int
}
func Write2FileFromChan(cn <-chan InformationRecord, wg *sync.WaitGroup) {
var files = map[string]*os.File{}
var err error
pid := strconv.Itoa(os.Getpid())
for dan := range cn {
dan.Filename = pid + "-" + dan.Filename
for counter := 0; counter <= 64; counter++ {
if files[dan.Filename] == nil {
files[dan.Filename], err = os.OpenFile(dan.Filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
files[dan.Filename] = nil
fmt.Fprintln(os.Stderr, nowTimeRFC1123(), "|", "Block index: ", dan.BlockIndex, "[Error]", err)
continue
}
}
_, err = fmt.Fprintln(files[dan.Filename], dan.Message)
if err != nil {
fmt.Fprintln(os.Stderr, nowTimeRFC1123(), "|", "Block index: ", dan.BlockIndex, "[Error]", err)
} else {
break
}
if counter == 64 {
log.Fatalln(nowTimeRFC1123(), "|", "Block index: ", dan.BlockIndex, "[Error]", err)
}
time.Sleep(time.Millisecond)
}
}
for _, v := range files {
v.Close()
}
wg.Done()
}
func main() {
if endBlockIndex < 0 || startBlockIndex < 0 || (endBlockIndex-startBlockIndex) < 0 {
flag.Usage()
os.Exit(0)
}
if saveOnlyBalanced {
checkStatusAddress = true
}
count := endBlockIndex - startBlockIndex
step := int(count / countStreams)
var wg sync.WaitGroup
var wg2 sync.WaitGroup
chanInformationRecords := make(chan InformationRecord, 2*countStreams)
wg2.Add(1)
go Write2FileFromChan(chanInformationRecords, &wg2)
if count > 0 && step > 1 {
for i := startBlockIndex; i <= endBlockIndex; i += step {
end := i + step - 1
start := i
if end > endBlockIndex || (endBlockIndex-end) < step {
end = endBlockIndex
i = endBlockIndex
}
wg.Add(1)
go worker(&wg, chanInformationRecords, start, end)
}
} else {
wg.Add(1)
go worker(&wg, chanInformationRecords, startBlockIndex, endBlockIndex)
}
wg.Wait()
close(chanInformationRecords)
wg2.Wait()
}
func worker(wg *sync.WaitGroup, cn chan<- InformationRecord, startIndex, endIndex int) {
defer wg.Done()
var block *blockchain.Block = nil
var (
blockIndexInt = 0
blockIndexStr = ""
prevBlockIndexInt = -1
isDone = true
)
blc := blockchain.New()
blc.UserAgent = "cemelon"
blockIndexInt = startIndex
for blockIndexInt <= endIndex {
runtime.Gosched()
blockIndexStr = strconv.Itoa(blockIndexInt)
fmt.Fprintln(os.Stdout, nowTimeRFC1123(), "|", "Block index: ", blockIndexInt)
if prevBlockIndexInt != blockIndexInt {
block = nil
}
if block == nil {
res, err := blc.GetBlockHeight(blockIndexStr)
if err != nil {
fmt.Fprintln(os.Stderr, nowTimeRFC1123(), "|", "Block index: ", blockIndexInt, "[Error]", err)
block = nil
time.Sleep(time.Second * 16)
continue
}
block = &res.Blocks[0]
}
if len(block.Tx) < 1 {
fmt.Fprintln(os.Stderr, nowTimeRFC1123(), "|", "Not found address, block index:", blockIndexInt)
block = nil
time.Sleep(time.Second * 16)
continue
}
addresses := make([]string, 0, 0)
for _, tx := range block.Tx {
for _, out := range tx.Out {
addresses = append(addresses, out.Addr)
}
}
if !isWhitenAddress.Exist(block.Hash) {
cn <- InformationRecord{
Filename: "blk-" + outFileBaseName,
Message: block.Hash,
BlockIndex: blockIndexInt,
}
isWhitenAddress.Store(block.Hash, true)
}
isDone = true
for j, address := range addresses {
isBalanced := false
addressFRS := fmt.Sprint("frs", address)
if isWhitenAddress.Exist(addressFRS) || isWhitenAddress.Exist(address) || strings.Trim(address, " ") == "" {
continue
}
msg := address
if checkStatusAddress {
msg = fmt.Sprintf(addressFormat, address, "", 0)
for i := 0; i < 3; i++ {
addr, e := blc.GetAddress(address)
if e != nil {
fmt.Fprintln(os.Stderr, nowTimeRFC1123(), "|", "Could not verify address:", address, "\n", e)
time.Sleep(time.Millisecond * time.Duration(1000+rand.Int63n(10000)))
continue
}
if addr.FinalBalance != 0 {
isBalanced = true
}
msg = fmt.Sprintf(addressFormat, addr.Address, addr.Hash160, addr.FinalBalance)
break
}
}
if saveOnlyBalanced == true && isBalanced == false {
continue
}
if j == 0 && !isWhitenAddress.Exist(addressFRS) && !notCollectFirstAddresses {
cn <- InformationRecord{
Filename: "frs-" + outFileBaseName,
Message: msg,
BlockIndex: blockIndexInt,
}
isWhitenAddress.Store(addressFRS, true)
}
if notCollectAllAddresses {
isDone = true
break
}
if !isWhitenAddress.Exist(address) {
cn <- InformationRecord{
Filename: "all-" + outFileBaseName,
Message: msg,
BlockIndex: blockIndexInt,
}
isWhitenAddress.Store(address, true)
}
}
if isDone {
blockIndexInt++
if isWhitenAddress.Count() > whitenAddressSize {
isWhitenAddress.Clear()
}
}
}
}
func nowTimeRFC1123() string {
return time.Now().UTC().Format(time.RFC1123)
}