-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
93 lines (81 loc) · 2.61 KB
/
server.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
package node
import (
"context"
"encoding/hex"
"github.com/harrybrwn/vine/block"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// Head implement the Head service for the BlockStoreServer grpc service.
func (n *Node) Head(ctx context.Context, e *Empty) (*BlockMsg, error) {
log.Info("rpc Head")
blk, err := n.store.Head()
if err != nil {
return nil, err
}
return &BlockMsg{Sender: n.host.ID().Pretty(), Block: blk}, nil
}
// Base is receive requests for the base of the blockchain.
func (n *Node) Base(ctx context.Context, e *Empty) (*BlockMsg, error) {
log.Info("rpc Base")
it := n.store.Iter()
for {
blk := it.Next()
if blk == nil {
break
}
if block.IsGenisis(blk) {
return &BlockMsg{Sender: n.host.ID().Pretty(), Block: blk}, nil
}
}
return nil, errors.New("could not find genisis block")
}
// GetBlock implement the GetBlock service for the BlockStoreServer grpc service.
func (n *Node) GetBlock(ctx context.Context, req *BlockReq) (*BlockMsg, error) {
log.Info("rpc GetBlock")
blk, err := n.store.Get(req.Hash)
if err != nil {
return nil, err
}
return &BlockMsg{Sender: n.host.ID().Pretty(), Block: blk}, nil
}
// GetTx implement the GetTx service for the BlockStoreServer grpc service.
func (n *Node) GetTx(ctx context.Context, req *TxReq) (*TxMsg, error) {
log.Info("rpc GetTx")
tx := n.txdb.Transaction(req.Hash)
if tx == nil {
return nil, errors.New("could not find transaction")
}
return &TxMsg{Sender: n.host.ID().Pretty(), Tx: tx}, nil
}
// Tx implement the Tx service for the BlockStoreServer grpc service.
func (n *Node) Tx(ctx context.Context, msg *TxMsg) (*Status, error) {
log.WithFields(log.Fields{
"from": msg.Sender,
"tx.hash": hex.EncodeToString(msg.Tx.ID),
"tx.lock": msg.Tx.Lock,
"tx.fee": msg.Tx.Fee(n.txdb),
}).Info("rpc Tx")
err := msg.Tx.VerifySig(n.txdb)
if err != nil {
log.WithError(err).Warn("could not verify new tx signature")
}
// TODO follow the following logic instead of just adding the block
// If we have seen this tx hash before, then
// we skip and return OK
// If we have not seen this tx, then
// we store it in a pending transaction pool
// and send it to our peers
head, err := n.store.Head()
if err != nil {
return nil, err
}
blk := block.New([]*block.Transaction{msg.Tx}, head.Hash)
return &Status{Code: Status_Ok}, n.store.Push(blk)
}
// Mined implement the Mined service for the BlockStoreServer grpc service.
func (n *Node) Mined(ctx context.Context, msg *BlockMsg) (*Status, error) {
log.WithFields(log.Fields{"from": msg.Sender}).Info("rpc Mined")
return nil, nil
}
var _ BlockStoreServer = (*Node)(nil)