-
Notifications
You must be signed in to change notification settings - Fork 1
/
receive.go
46 lines (44 loc) · 1.5 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
package main
import (
"github.com/bitcoinnanolabs/accept-btco/internal/btco"
"github.com/bitcoinnanolabs/accept-btco/internal/units"
"github.com/cenkalti/log"
"github.com/shopspring/decimal"
)
func receiveBlock(hash string, amount decimal.Decimal, account, privateKey, publicKey string) error {
log.Debugln("amount:", units.RawToNano(amount).String())
var newReceiverBlockPreviousHash string
var newReceiverBalance decimal.Decimal
var workHash string
receiverAccountInfo, err := node.AccountInfo(account)
switch err {
case btco.ErrAccountNotFound:
// First block in account chain. This is the common case.
newReceiverBlockPreviousHash = "0000000000000000000000000000000000000000000000000000000000000000"
newReceiverBalance = amount
workHash = publicKey
case nil:
// More than one payment is made to the account.
log.Debugf("account info: %#v", receiverAccountInfo)
newReceiverBlockPreviousHash = receiverAccountInfo.Frontier
newReceiverBalance = receiverAccountInfo.Balance.Add(amount)
workHash = newReceiverBlockPreviousHash
default:
return err
}
work, err := btco.GenerateWork(workHash, false)
if err != nil {
return err
}
newReceiverBlock, err := node.BlockCreate(newReceiverBlockPreviousHash, account, config.Representative, newReceiverBalance, 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
}