-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmain.go
310 lines (268 loc) · 7.92 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
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
301
302
303
304
305
306
307
308
309
310
package main
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip13"
)
var sk string
var pk string
var numberOfWorkers int
var nonceFound int32 = 0
var blockNumber uint64
var hash atomic.Value
var messageId atomic.Value
var currentWorkers int32
var arbRpcUrl string
var (
ErrDifficultyTooLow = errors.New("nip13: insufficient difficulty")
ErrGenerateTimeout = errors.New("nip13: generating proof of work took too long")
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
sk = os.Getenv("sk")
pk = os.Getenv("pk")
numberOfWorkers, _ = strconv.Atoi(os.Getenv("numberOfWorkers"))
arbRpcUrl = os.Getenv("arbRpcUrl")
}
func generateRandomString(length int) (string, error) {
charset := "abcdefghijklmnopqrstuvwxyz0123456789" // 字符集
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return "", err
}
for i := 0; i < length; i++ {
b[i] = charset[int(b[i])%len(charset)]
}
return string(b), nil
}
func Generate(event nostr.Event, targetDifficulty int) (nostr.Event, error) {
tag := nostr.Tag{"nonce", "", strconv.Itoa(targetDifficulty)}
event.Tags = append(event.Tags, tag)
start := time.Now()
for {
nonce, err := generateRandomString(10)
if err != nil {
fmt.Println(err)
}
tag[1] = nonce
event.CreatedAt = nostr.Now()
if nip13.Difficulty(event.GetID()) >= targetDifficulty {
// fmt.Print(time.Since(start))
return event, nil
}
if time.Since(start) >= 1*time.Second {
return event, ErrGenerateTimeout
}
}
}
type Message struct {
EventId string `json:"eventId"`
}
type EV struct {
Sig string `json:"sig"`
Id string `json:"id"`
Kind int `json:"kind"`
CreatedAt nostr.Timestamp `json:"created_at"`
Tags nostr.Tags `json:"tags"`
Content string `json:"content"`
PubKey string `json:"pubkey"`
}
func mine(ctx context.Context, messageId string, client *ethclient.Client) {
replayUrl := "wss://relay.noscription.org/"
difficulty := 21
// Create a channel to signal the finding of a valid nonce
foundEvent := make(chan nostr.Event, 1)
notFound := make(chan nostr.Event, 1)
// Create a channel to signal all workers to stop
content := "{\"p\":\"nrc-20\",\"op\":\"mint\",\"tick\":\"noss\",\"amt\":\"10\"}"
startTime := time.Now()
ev := nostr.Event{
Content: content,
CreatedAt: nostr.Now(),
ID: "",
Kind: nostr.KindTextNote,
PubKey: pk,
Sig: "",
Tags: nil,
}
ev.Tags = ev.Tags.AppendUnique(nostr.Tag{"p", "9be107b0d7218c67b4954ee3e6bd9e4dba06ef937a93f684e42f730a0c3d053c"})
ev.Tags = ev.Tags.AppendUnique(nostr.Tag{"e", "51ed7939a984edee863bfbb2e66fdc80436b000a8ddca442d83e6a2bf1636a95", replayUrl, "root"})
ev.Tags = ev.Tags.AppendUnique(nostr.Tag{"e", messageId, replayUrl, "reply"})
ev.Tags = ev.Tags.AppendUnique(nostr.Tag{"seq_witness", strconv.Itoa(int(blockNumber)), hash.Load().(string)})
// Start multiple worker goroutines
go func() {
select {
case <-ctx.Done():
return
default:
evCopy := ev
evCopy, err := Generate(evCopy, difficulty)
if err != nil {
// fmt.Println(err)
notFound <- evCopy
}
foundEvent <- evCopy
}
}()
select {
case <-notFound:
case evNew := <-foundEvent:
evNew.Sign(sk)
evNewInstance := EV{
Sig: evNew.Sig,
Id: evNew.ID,
Kind: evNew.Kind,
CreatedAt: evNew.CreatedAt,
Tags: evNew.Tags,
Content: evNew.Content,
PubKey: evNew.PubKey,
}
// 将ev转为Json格式
eventJSON, err := json.Marshal(evNewInstance)
if err != nil {
log.Fatal(err)
}
wrapper := map[string]json.RawMessage{
"event": eventJSON,
}
// 将包装后的对象序列化成JSON
wrapperJSON, err := json.Marshal(wrapper)
if err != nil {
log.Fatalf("Error marshaling wrapper: %v", err)
}
url := "https://api-worker.noscription.org/inscribe/postEvent"
// fmt.Print(bytes.NewBuffer(wrapperJSON))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(wrapperJSON)) // 修改了弱智项目方不识别美化Json的bug
if err != nil {
log.Fatalf("Error creating request: %v", err)
}
// 设置HTTP Header
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0")
req.Header.Set("Sec-ch-ua", "\"Not A(Brand\";v=\"99\", \"Microsoft Edge\";v=\"121\", \"Chromium\";v=\"121\"")
req.Header.Set("Sec-ch-ua-mobile", "?0")
req.Header.Set("Sec-ch-ua-platform", "\"Windows\"")
req.Header.Set("Sec-fetch-dest", "empty")
req.Header.Set("Sec-fetch-mode", "cors")
req.Header.Set("Sec-fetch-site", "same-site")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending request: %v", err)
}
defer resp.Body.Close()
fmt.Println("Response Status:", resp.Status)
spendTime := time.Since(startTime)
// fmt.Println("Response Body:", string(body))
fmt.Println(nostr.Now().Time(), "spend: ", spendTime, "!!!!!!!!!!!!!!!!!!!!!published to:", evNew.ID)
atomic.StoreInt32(&nonceFound, 0)
case <-ctx.Done():
fmt.Print("done")
}
}
func connectToWSS(url string) (*websocket.Conn, error) {
var conn *websocket.Conn
var err error
headers := http.Header{}
headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0")
headers.Add("Origin", "https://noscription.org")
headers.Add("Host", "report-worker-2.noscription.org")
for {
// 使用gorilla/websocket库建立连接
conn, _, err = websocket.DefaultDialer.Dial(url, headers)
fmt.Println("Connecting to wss")
if err != nil {
// 连接失败,打印错误并等待一段时间后重试
fmt.Println("Error connecting to WebSocket:", err)
// time.Sleep(1 * time.Second) // 5秒重试间隔
continue
}
// 连接成功,退出循环
break
}
return conn, nil
}
func main() {
wssAddr := "wss://report-worker-2.noscription.org"
// relayUrl := "wss://relay.noscription.org/"
ctx := context.Background()
var err error
client, err := ethclient.Dial(arbRpcUrl)
if err != nil {
log.Fatalf("无法连接到Arbitrum节点: %v", err)
}
c, err := connectToWSS(wssAddr)
if err != nil {
panic(err)
}
defer c.Close()
// initialize an empty cancel function
// get block
go func() {
for {
header, err := client.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatalf("无法获取最新区块号: %v", err)
}
if header.Number.Uint64() >= blockNumber {
hash.Store(header.Hash().Hex())
atomic.StoreUint64(&blockNumber, header.Number.Uint64())
}
}
}()
go func() {
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
var messageDecode Message
if err := json.Unmarshal(message, &messageDecode); err != nil {
fmt.Println(err)
continue
}
messageId.Store(messageDecode.EventId)
}
}()
atomic.StoreInt32(¤tWorkers, 0)
// 初始化一个取消上下文和它的取消函数
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 监听blockNumber和messageId变化
go func() {
for {
select {
case <-ctx.Done(): // 如果上下文被取消,则退出协程
return
default:
if atomic.LoadInt32(¤tWorkers) < int32(numberOfWorkers) && messageId.Load() != nil && blockNumber > 0 {
atomic.AddInt32(¤tWorkers, 1) // 增加工作者数量
go func(bn uint64, mid string) {
defer atomic.AddInt32(¤tWorkers, -1) // 完成后减少工作者数量
mine(ctx, mid, client)
}(blockNumber, messageId.Load().(string))
}
}
}
}()
select {}
}