-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (188 loc) · 5.01 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
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
package main
import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"github.com/nknorg/nkn/v2/api/httpjson/client"
"github.com/nknorg/nkn/v2/common"
"github.com/nknorg/nkn/v2/pb"
"github.com/nknorg/nkn/v2/program"
"google.golang.org/protobuf/proto"
)
type GetBlockResp struct {
Result struct {
Header struct {
SignerPk string `json:"signerPk"`
} `json:"header"`
Transactions []struct {
PayloadData string `json:"payloadData"`
Programs []struct {
Code string `json:"code"`
Parameter string `json:"parameter"`
} `json:"programs"`
TxType string `json:"txType"`
} `json:"transactions"`
} `json:"result"`
}
func main() {
startHeight := flag.Int("s", 0, "Start block height (inclusive)")
endHeight := flag.Int("e", 0, "End block height (inclusive)")
rpcAddr := flag.String("rpc", "http://127.0.0.1:30003", "RPC address")
verbose := flag.Bool("v", false, "Verbose: print transaction count of each address")
flag.Parse()
counter := make(map[string]int, 0)
for height := *startHeight; height < *endHeight; height++ {
if *verbose {
log.Println(height)
}
err := countActiveAddrAtHeight(height, *rpcAddr, counter)
if err != nil {
log.Fatal(err)
}
}
if *verbose {
for addr, n := range counter {
fmt.Printf("%v\t%v\n", addr, n)
}
}
fmt.Printf("Active address: %v\n", len(counter))
}
func countActiveAddrAtHeight(height int, rpcAddr string, counter map[string]int) error {
resp, err := client.Call(rpcAddr, "getblock", 0, map[string]interface{}{"height": height})
if err != nil {
return err
}
v := &GetBlockResp{}
err = json.Unmarshal(resp, v)
if err != nil {
return err
}
if len(v.Result.Header.SignerPk) == 0 {
return errors.New("no signer")
}
pk, err := hex.DecodeString(v.Result.Header.SignerPk)
if err != nil {
return err
}
ph, err := program.CreateProgramHash(pk)
if err != nil {
return err
}
blockDigner, err := ph.ToAddress()
if err != nil {
return err
}
counter[blockDigner]++
addrs := make([][]byte, 0)
for _, tx := range v.Result.Transactions {
buf, err := hex.DecodeString(tx.PayloadData)
if err != nil {
return err
}
switch tx.TxType {
case pb.PayloadType_name[int32(pb.PayloadType_SIG_CHAIN_TXN_TYPE)]:
payload := &pb.SigChainTxn{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Submitter)
case pb.PayloadType_name[int32(pb.PayloadType_TRANSFER_ASSET_TYPE)]:
payload := &pb.TransferAsset{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Sender, payload.Recipient)
case pb.PayloadType_name[int32(pb.PayloadType_COINBASE_TYPE)]:
payload := &pb.Coinbase{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Sender, payload.Recipient)
case pb.PayloadType_name[int32(pb.PayloadType_REGISTER_NAME_TYPE)]:
payload := &pb.RegisterName{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Registrant)
case pb.PayloadType_name[int32(pb.PayloadType_TRANSFER_NAME_TYPE)]:
payload := &pb.TransferName{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Registrant)
case pb.PayloadType_name[int32(pb.PayloadType_DELETE_NAME_TYPE)]:
payload := &pb.DeleteName{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Registrant)
case pb.PayloadType_name[int32(pb.PayloadType_SUBSCRIBE_TYPE)]:
payload := &pb.Subscribe{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Subscriber)
case pb.PayloadType_name[int32(pb.PayloadType_UNSUBSCRIBE_TYPE)]:
payload := &pb.Unsubscribe{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
addrs = append(addrs, payload.Subscriber)
case pb.PayloadType_name[int32(pb.PayloadType_GENERATE_ID_TYPE)]:
payload := &pb.GenerateID{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
if len(payload.Sender) > 0 {
addrs = append(addrs, payload.Sender)
}
addrs = append(addrs, payload.PublicKey)
case pb.PayloadType_name[int32(pb.PayloadType_NANO_PAY_TYPE)]:
payload := &pb.NanoPay{}
err = proto.Unmarshal(buf, payload)
if err != nil {
return err
}
if len(payload.Sender) > 0 {
addrs = append(addrs, payload.Sender)
}
addrs = append(addrs, payload.Sender, payload.Recipient)
default:
log.Printf("Unknown txn type: %v", tx.TxType)
continue
}
}
for _, v := range addrs {
var ph common.Uint160
if len(v) == common.UINT160SIZE {
ph = common.BytesToUint160(v)
} else if len(v) == ed25519.PublicKeySize {
ph, err = program.CreateProgramHash(v)
if err != nil {
return err
}
} else {
log.Printf("Unknown size: %v", len(v))
continue
}
addr, err := ph.ToAddress()
if err != nil {
return err
}
counter[addr]++
}
return nil
}