-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
103 lines (87 loc) · 2.41 KB
/
main.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
package main
import (
"fmt"
"flag"
"github.com/onrik/ethrpc"
"sync"
"log"
"time"
)
func getBlockRequest(blockNumber int, ch chan<-map[string]bool) {
// TODO consider using same client for each request
client := ethrpc.New("http://127.0.0.1:8545")
_, err := client.Web3ClientVersion()
if err != nil {
log.Fatal(err)
}
// fetch block with transactions
block, err := client.EthGetBlockByNumber(blockNumber, true)
if err != nil {
log.Fatal(err)
}
addresses := make(map[string]bool)
for _, element := range block.Transactions {
sender := element.From
receiver := element.To
if sender != "" {
addresses[sender] = true
}
if receiver != "" {
addresses[receiver] = true
}
}
ch <- addresses
}
func main() {
start := time.Now()
var start_block int
var end_block int
all_addresses := make(map[string]bool)
// command line args
startPtr := flag.Int("start", 0, "start block")
endPtr := flag.Int("end", 0, "end block")
flag.Parse()
start_block = *startPtr
end_block = *endPtr
address_chan := make(chan map[string]bool)
// this will allow our program to stay alive until all requests are completed
var wg sync.WaitGroup
block_range_size := end_block-start_block
wg.Add(block_range_size + 1)
// limit number of go routines running at once so we don't go over open file limit
current_block := start_block
batch_size := 500
if block_range_size < batch_size {
batch_size = block_range_size
}
// do each rpc call as a concurrent request
for i := 0; i <= batch_size; i++ {
current_block++
go getBlockRequest(current_block, address_chan)
}
go func() {
for address_map := range address_chan {
// mark a response as received when we add to our master mapping of addresses
for address := range address_map {
// add address to mapping
all_addresses[address] = true
}
// start a new request once one has finished
if current_block <= end_block {
current_block++
go getBlockRequest(current_block, address_chan)
}
wg.Done()
}
}()
// wait until we have received all addresses
wg.Wait()
// print all addresses NOTE should stdout to a file or you are going to get spammed
fmt.Println("All done")
fmt.Println(len(all_addresses))
fmt.Println(time.Since(start))
fmt.Println()
for address := range all_addresses {
fmt.Println(address)
}
}