/
receive.go
53 lines (51 loc) 路 1.57 KB
/
receive.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
package main
import (
"github.com/accept-nano/accept-nano/nano"
"github.com/cenkalti/log"
"github.com/shopspring/decimal"
)
func receiveBlock(hash, amount, account, privateKey, publicKey string) error {
sentAmount, err := decimal.NewFromString(amount)
if err != nil {
return err
}
log.Debugln("amount:", sentAmount)
var newReceiverBlockPreviousHash string
var newReceiverBalance decimal.Decimal
var workHash string
receiverAccountInfo, err := node.AccountInfo(account)
switch err {
case nano.ErrAccountNotFound:
// First block in account chain. This is the common case.
newReceiverBlockPreviousHash = "00000000000000000000000000000000"
newReceiverBalance = sentAmount
workHash = publicKey
case nil:
// More than one payment is made to the account.
log.Debugf("account info: %#v", receiverAccountInfo)
newReceiverBlockPreviousHash = receiverAccountInfo.Frontier
currentReceiverBalance, err2 := decimal.NewFromString(receiverAccountInfo.Balance)
if err2 != nil {
return err2
}
newReceiverBalance = currentReceiverBalance.Add(sentAmount)
workHash = newReceiverBlockPreviousHash
default:
return err
}
work, err := nano.GenerateWork(workHash, false)
if err != nil {
return err
}
newReceiverBlock, err := node.BlockCreate(newReceiverBlockPreviousHash, account, config.Representative, newReceiverBalance.String(), hash, privateKey, work)
if err != nil {
return err
}
log.Debugf("new block: %#v", newReceiverBlock)
newHash, err := node.Process(newReceiverBlock)
if err != nil {
return err
}
log.Debugln("published new block:", newHash)
return nil
}