forked from obscuren/jeffcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jeffcoin.go
162 lines (137 loc) · 3.9 KB
/
jeffcoin.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
package main
import (
"bytes"
"fmt"
"github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethvm"
"math/big"
"os"
"time"
)
var (
JeffCoinAddr = ethutil.Hex2Bytes("22fa3ebce6ef9ca661a960104d3087eec040011e")
coinlogger = ethlog.NewLogger("JEFF")
)
type JeffCoin struct {
state *ethstate.State
eth *eth.Ethereum
fake *ethstate.StateObject
pub *ethpub.PEthereum
key *ethcrypto.KeyPair
}
func New(ethereum *eth.Ethereum, keyPair *ethcrypto.KeyPair) *JeffCoin {
state := ethstate.NewState(ethtrie.NewTrie(ethutil.Config.Db, ethereum.StateManager().CurrentState().Root()))
fake := state.GetOrNewStateObject(keyPair.Address())
return &JeffCoin{
eth: ethereum,
state: state,
fake: fake,
pub: ethpub.New(ethereum),
key: keyPair,
}
}
func (self *JeffCoin) getSeed() int {
stateObject := self.state.GetStateObject(JeffCoinAddr)
if stateObject != nil {
return int(stateObject.GetStorage(ethutil.Big("3")).Uint())
}
return 0
}
func (self *JeffCoin) getDiff() int {
stateObject := self.state.GetStateObject(JeffCoinAddr)
if stateObject != nil {
return int(stateObject.GetStorage(ethutil.Big("1")).Uint())
}
return 0
}
func (self *JeffCoin) createTx(nonce []byte) (err error) {
_, err = self.pub.Transact(ethutil.Bytes2Hex(self.key.PrivateKey), ethutil.Bytes2Hex(JeffCoinAddr), "0", "6000", "10000000000000", "0x"+ethutil.Bytes2Hex(nonce))
return
}
func (self *JeffCoin) Mine() {
var (
quitChan = make(chan bool, 1)
blockChan = make(chan ethutil.React, 1)
txChan = make(chan ethutil.React, 1)
reactor = self.eth.Reactor()
block = self.eth.BlockChain().CurrentBlock
env = NewEnv(self.state, block)
vm = ethvm.New(env)
)
vm.Verbose = true
reactor.Subscribe("newBlock", blockChan)
reactor.Subscribe("newTx:pre", txChan)
for {
select {
case msg := <-blockChan:
quitChan <- true
// Get the new Ethereum state
self.state = ethstate.NewState(ethtrie.NewTrie(ethutil.Config.Db, self.eth.StateManager().CurrentState().Root()))
block = msg.Resource.(*ethchain.Block)
case msg := <-txChan:
tx := msg.Resource.(*ethchain.Transaction)
if bytes.Compare(tx.Recipient, JeffCoinAddr) == 0 {
object := self.state.GetStateObject(JeffCoinAddr)
callerClosure := ethvm.NewClosure(self.fake, object, object.Code, big.NewInt(1000000), big.NewInt(0))
_, _, e := callerClosure.Call(vm, tx.Data)
if e != nil {
fmt.Println("error", e)
}
// A block has been found and thus the seed has probably changed
quitChan <- true
}
default:
seed := self.getSeed()
diff := self.getDiff()
coinlogger.Debugln("mining with diff = ", diff, " seed = ", seed)
if diff > 0 {
n := ethutil.NumberToBytes(int64(seed), 64)
b := ethutil.LeftPadBytes(n, 32)
nonce := mineJeffCoin(diff, b, quitChan)
if len(nonce) == 32 {
err := self.createTx(nonce)
if err != nil {
coinlogger.Debugln(err)
} else {
coinlogger.Debugf("seed = %d, diff = %d, nonce = %x\n", seed, diff, nonce)
time.Sleep(500 * time.Millisecond)
}
} else {
coinlogger.Debugln("invalid nonce len")
}
} else {
time.Sleep(500 * time.Millisecond)
}
}
}
}
func mineJeffCoin(diff int, seed []byte, quit chan bool) (nonce []byte) {
cmp := make([]byte, diff)
out:
for s := uint64(0); ; s++ {
select {
case <-quit:
break out
default:
n := ethutil.NumberToBytes(s, 64)
nonce = ethutil.LeftPadBytes(n, 32)
h := ethcrypto.Sha3Bin(append(nonce, seed...))
if bytes.Compare(h[:diff], cmp) == 0 {
fmt.Printf("SHA3( %x )\n", h)
break out
}
}
}
return
}
func exit(format string, v ...interface{}) {
fmt.Printf(format, v...)
os.Exit(0)
}