-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock.go
185 lines (178 loc) · 4.47 KB
/
block.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
package main
import (
"bytes"
"context"
"encoding/hex"
"errors"
"math/big"
"time"
"github.com/hectorchu/gonano/pow"
"github.com/hectorchu/gonano/rpc"
"github.com/hectorchu/gonano/util"
"github.com/hectorchu/gonano/wallet"
"github.com/hectorchu/gonano/wallet/ed25519"
"github.com/hectorchu/gonano/websocket"
)
func validateBlock(block *rpc.Block, account string, amount *big.Int) (hash rpc.BlockHash, err error) {
if block.Type != "state" {
return nil, errors.New("invalid block type")
}
destAccount, err := util.PubkeyToAddress(block.Link)
if err != nil {
return
}
if destAccount != account {
return nil, errors.New("incorrect destination account")
}
client := rpc.Client{URL: *rpcURL}
ai, err := client.AccountInfo(block.Account)
if err != nil {
return
}
if !bytes.Equal(block.Previous, ai.Frontier) {
return nil, errors.New("previous block is not frontier")
}
if block.Balance.Cmp(&ai.Balance.Int) >= 0 {
return nil, errors.New("invalid block balance for send")
}
sendAmount := new(big.Int).Sub(&ai.Balance.Int, &block.Balance.Int)
if sendAmount.Cmp(amount) != 0 {
return nil, errors.New("incorrect payment amount")
}
pubkey, err := util.AddressToPubkey(block.Account)
if err != nil {
return
}
if hash, err = block.Hash(); err != nil {
return
}
if !ed25519.Verify(pubkey, hash, block.Signature) {
return nil, errors.New("invalid signature")
}
return
}
func sendBlock(block *rpc.Block) (err error) {
if err = generatePoW(block); err != nil {
return
}
client := rpc.Client{URL: *rpcURL}
_, err = client.Process(block, "send")
return
}
func waitReceive(
ctx context.Context, ws *wsMux, a *wallet.Account,
account string, amount *big.Int, timeout time.Duration,
) (hash rpc.BlockHash, err error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
msg, err := ws.connect(a.Address())
if err != nil {
return
}
defer ws.disconnect(a.Address())
if err = a.ReceivePendings(); err != nil {
return
}
client := rpc.Client{URL: *rpcURL, Ctx: ctx}
if ai, err := client.AccountInfo(a.Address()); err == nil {
if excess := new(big.Int).Sub(&ai.Balance.Int, amount); excess.Sign() >= 0 {
if excess.Sign() > 0 {
for hash := ai.Frontier; ; {
bi, err := client.BlockInfo(hash)
if err != nil {
return nil, err
}
if bi.Subtype == "receive" {
if bi, err = client.BlockInfo(bi.Contents.Link); err != nil {
return nil, err
}
if _, err = a.Send(bi.BlockAccount, excess); err != nil {
return nil, err
}
break
}
hash = bi.Contents.Previous
}
}
return a.Send(account, amount)
}
} else if err.Error() != "Account not found" {
return nil, err
}
for {
select {
case m := <-msg:
switch m := m.(type) {
case *websocket.Confirmation:
switch a.Address() {
case m.Block.LinkAsAccount:
if _, err = a.ReceivePending(m.Hash); err != nil && err.Error() != "Unreceivable" {
return
}
case m.Block.Account:
if excess := new(big.Int).Sub(&m.Block.Balance.Int, amount); excess.Sign() >= 0 {
if excess.Sign() > 0 {
bi, err := client.BlockInfo(m.Block.Link)
if err != nil {
return nil, err
}
if _, err = a.Send(bi.BlockAccount, excess); err != nil {
return nil, err
}
}
return a.Send(account, amount)
}
}
case error:
return nil, m
}
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func refund(a *wallet.Account) (err error) {
client := rpc.Client{URL: *rpcURL}
if err = a.ReceivePendings(); err != nil {
return
}
ai, err := client.AccountInfo(a.Address())
if err != nil {
if err.Error() == "Account not found" {
err = nil
}
return
}
for hash, balance := ai.Frontier, &ai.Balance.Int; balance.Sign() > 0; {
bi, err := client.BlockInfo(hash)
if err != nil {
return err
}
if bi.Subtype == "receive" {
bi, err := client.BlockInfo(bi.Contents.Link)
if err != nil {
return err
}
amount := &bi.Amount.Int
if amount.Cmp(balance) > 0 {
amount = balance
}
if _, err = a.Send(bi.BlockAccount, amount); err != nil {
return err
}
balance.Sub(balance, amount)
}
hash = bi.Contents.Previous
}
return
}
func generatePoW(block *rpc.Block) (err error) {
difficulty, _ := hex.DecodeString("fffffff800000000")
if *powURL != "" {
client := rpc.Client{URL: *powURL}
block.Work, _, _, err = client.WorkGenerate(block.Previous, difficulty)
} else {
block.Work, err = pow.Generate(block.Previous, difficulty)
}
return
}