-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
195 lines (166 loc) · 5.98 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
package main
import (
"context"
"crypto/rand"
"flag"
"fmt"
"math/big"
"sync"
"time"
"Powerc20Worker/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/fatih/color"
"github.com/gosuri/uilive"
"github.com/sirupsen/logrus"
)
var (
infuraURL = "https://rpc.ankr.com/eth"
privateKey string
contractAddress string
workerCount int
logger = logrus.New()
)
func init() {
flag.StringVar(&privateKey, "privateKey", "", "Private key for the Ethereum account")
flag.StringVar(&contractAddress, "contractAddress", "0xca9b78435Be8267922E7Ac5cDE70401e7502c9cc", "Address of the Ethereum contract")
flag.IntVar(&workerCount, "workerCount", 10, "Number of concurrent mining workers")
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
}
func mineWorker(ctx context.Context, wg *sync.WaitGroup, contract *abi.PoWERC20, fromAddress common.Address, client *ethclient.Client, auth *bind.TransactOpts, resultChan chan<- *big.Int, errorChan chan<- error, challenge *big.Int, target *big.Int, hashCountChan chan<- int) {
defer wg.Done()
var nonce *big.Int
var err error
for {
select {
case <-ctx.Done():
return
default:
nonce, err = rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 256))
if err != nil {
errorChan <- fmt.Errorf("failed to generate random nonce: %v", err)
return
}
noncePadded := common.LeftPadBytes(nonce.Bytes(), 32)
challengePadded := common.LeftPadBytes(challenge.Bytes(), 32)
addressBytes := fromAddress.Bytes()
data := append(challengePadded, append(addressBytes, noncePadded...)...)
hash := crypto.Keccak256Hash(data)
if hash.Big().Cmp(target) == -1 {
resultChan <- nonce
return
}
hashCountChan <- 1
}
}
}
func main() {
banner := `
// ____ __ _______ ____ ____ ____ ___ __ __ _
// | _ \ __\ \ / / ____| _ \ / ___|___ \ / _ \ | \/ (_)_ __ ___ _ __
// | |_) / _ \ \ /\ / /| _| | |_) | | __) | | | | | |\/| | | '_ \ / _ \ '__|
// | __/ (_) \ V V / | |___| _ <| |___ / __/| |_| | | | | | | | | | __/ |
// |_| \___/ \_/\_/ |_____|_| \_\\____|_____|\___/ |_| |_|_|_| |_|\___|_|
`
fmt.Println(banner)
flag.Parse()
writer := uilive.New()
writer.Start()
defer writer.Stop()
logger.Info(color.GreenString("Establishing connection with Ethereum client..."))
client, err := ethclient.Dial(infuraURL)
if err != nil {
logger.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
logger.Info(color.GreenString("Successfully connected to Ethereum client."))
privateKeyECDSA, err := crypto.HexToECDSA(privateKey)
if err != nil {
logger.Fatalf("Error in parsing private key: %v", err)
}
chainID, err := client.NetworkID(context.Background())
if err != nil {
logger.Fatalf("Failed to get chainID: %v", err)
}
logger.Infof(color.GreenString("Successfully connected to Ethereum network with Chain ID: %v"), chainID)
auth, err := bind.NewKeyedTransactorWithChainID(privateKeyECDSA, chainID)
if err != nil {
logger.Fatalf("Failed to create transactor: %v", err)
}
contractAddr := common.HexToAddress(contractAddress)
contract, err := abi.NewPoWERC20(contractAddr, client)
if err != nil {
logger.Fatalf("Failed to instantiate a Token contract: %v", err)
}
logger.Info(color.GreenString("PoWERC20 token contract successfully instantiated."))
contractName, err := contract.Name(nil)
if err != nil {
logger.Fatalf("Failed to get contract name: %v", err)
}
logger.Infof(color.GreenString("Contract Name: %s"), color.RedString(contractName))
challenge, err := contract.Challenge(nil)
if err != nil {
logger.Fatalf("Failed to get challenge: %v", err)
}
logger.Infof(color.GreenString("Current mining challenge number: %d"), challenge)
difficulty, err := contract.Difficulty(nil)
if err != nil {
logger.Fatalf("Failed to get difficulty: %v", err)
}
logger.Infof(color.GreenString("Current mining difficulty level: %d"), difficulty)
difficultyUint := uint(difficulty.Uint64())
target := new(big.Int).Lsh(big.NewInt(1), 256-difficultyUint)
logger.Infof(color.GreenString("Target number is: %d"), target)
resultChan := make(chan *big.Int)
errorChan := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger.Info(color.YellowString("Mining workers started..."))
hashCountChan := make(chan int)
totalHashCount := 0
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
timestamp := time.Now().Format("2006-01-02 15:04:05")
hashesPerSecond := float64(totalHashCount) / 1000.0
fmt.Fprintf(writer, "%s[%s] %s\n", color.BlueString("Mining"), timestamp, color.GreenString("Total hashes per second: %8.2f K/s", hashesPerSecond))
totalHashCount = 0
case count := <-hashCountChan:
totalHashCount += count
}
}
}()
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go mineWorker(ctx, &wg, contract, auth.From, client, auth, resultChan, errorChan, challenge, target, hashCountChan)
}
select {
case nonce := <-resultChan:
ticker.Stop()
cancel()
wg.Wait()
logger.Infof(color.GreenString("Successfully discovered a valid nonce: %d"), nonce)
logger.Info(color.YellowString("Submitting mining transaction with nonce..."))
tx, err := contract.Mine(auth, nonce)
if err != nil {
logger.Fatalf("Failed to submit mine transaction: %v", err)
}
receipt, err := bind.WaitMined(context.Background(), client, tx)
if err != nil {
logger.Fatalf("Failed to mine the transaction: %v", err)
}
logger.Infof(color.GreenString("Mining transaction successfully confirmed, Transaction Hash: %s"), color.CyanString(receipt.TxHash.Hex()))
case err := <-errorChan:
cancel()
wg.Wait()
logger.Fatalf("Mining operation failed due to an error: %v", err)
}
logger.Info(color.GreenString("Mining process successfully completed"))
}