-
Notifications
You must be signed in to change notification settings - Fork 71
/
private.go
166 lines (136 loc) · 5.07 KB
/
private.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
// Package private maintains the group of handlers for node to node access.
package private
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"github.com/ardanlabs/blockchain/business/web/errs"
"github.com/ardanlabs/blockchain/foundation/blockchain/database"
"github.com/ardanlabs/blockchain/foundation/blockchain/peer"
"github.com/ardanlabs/blockchain/foundation/blockchain/state"
"github.com/ardanlabs/blockchain/foundation/nameservice"
"github.com/ardanlabs/blockchain/foundation/web"
"go.uber.org/zap"
)
// Handlers manages the set of bar ledger endpoints.
type Handlers struct {
Log *zap.SugaredLogger
State *state.State
NS *nameservice.NameService
}
// SubmitNodeTransaction adds new node transactions to the mempool.
func (h Handlers) SubmitNodeTransaction(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
v, err := web.GetValues(ctx)
if err != nil {
return web.NewShutdownError("web value missing from context")
}
// Decode the JSON in the post call into a block transaction.
var tx database.BlockTx
if err := web.Decode(r, &tx); err != nil {
return fmt.Errorf("unable to decode payload: %w", err)
}
// Ask the state package to add this transaction to the mempool and perform
// any other business logic.
h.Log.Infow("add tran", "traceid", v.TraceID, "sig:nonce", tx, "fron", tx.FromID, "to", tx.ToID, "value", tx.Value, "tip", tx.Tip)
if err := h.State.UpsertNodeTransaction(tx); err != nil {
return errs.NewTrusted(err, http.StatusBadRequest)
}
resp := struct {
Status string `json:"status"`
}{
Status: "transactions added to mempool",
}
return web.Respond(ctx, w, resp, http.StatusOK)
}
// ProposeBlock takes a block received from a peer, validates it and
// if that passes, adds the block to the local blockchain.
func (h Handlers) ProposeBlock(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
// Decode the JSON in the post call into a file system block.
var blockData database.BlockData
if err := web.Decode(r, &blockData); err != nil {
return fmt.Errorf("unable to decode payload: %w", err)
}
// Convert the block data into a block. This action will create a merkle
// tree for the set of transactions required for blockchain operations.
block, err := database.ToBlock(blockData)
if err != nil {
return fmt.Errorf("unable to decode block: %w", err)
}
// Ask the state package to validate the proposed block. If the block
// passes validation, it will be added to the blockchain database.
if err := h.State.ProcessProposedBlock(block); err != nil {
if errors.Is(err, database.ErrChainForked) {
h.State.Reorganize()
}
return errs.NewTrusted(errors.New("block not accepted"), http.StatusNotAcceptable)
}
resp := struct {
Status string `json:"status"`
}{
Status: "accepted",
}
return web.Respond(ctx, w, resp, http.StatusOK)
}
// SubmitPeer is called by a node so they can be added to the known peer list.
func (h Handlers) SubmitPeer(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
v, err := web.GetValues(ctx)
if err != nil {
return web.NewShutdownError("web value missing from context")
}
var peer peer.Peer
if err := web.Decode(r, &peer); err != nil {
return fmt.Errorf("unable to decode payload: %w", err)
}
if !h.State.AddKnownPeer(peer) {
h.Log.Infow("adding peer", "traceid", v.TraceID, "host", peer.Host)
}
return web.Respond(ctx, w, nil, http.StatusOK)
}
// Status returns the current status of the node.
func (h Handlers) Status(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
latestBlock := h.State.LatestBlock()
status := peer.PeerStatus{
LatestBlockHash: latestBlock.Hash(),
LatestBlockNumber: latestBlock.Header.Number,
KnownPeers: h.State.KnownExternalPeers(),
}
return web.Respond(ctx, w, status, http.StatusOK)
}
// BlocksByNumber returns all the blocks based on the specified to/from values.
func (h Handlers) BlocksByNumber(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
fromStr := web.Param(r, "from")
if fromStr == "latest" || fromStr == "" {
fromStr = fmt.Sprintf("%d", state.QueryLastest)
}
toStr := web.Param(r, "to")
if toStr == "latest" || toStr == "" {
toStr = fmt.Sprintf("%d", state.QueryLastest)
}
from, err := strconv.ParseUint(fromStr, 10, 64)
if err != nil {
return errs.NewTrusted(err, http.StatusBadRequest)
}
to, err := strconv.ParseUint(toStr, 10, 64)
if err != nil {
return errs.NewTrusted(err, http.StatusBadRequest)
}
if from > to {
return errs.NewTrusted(errors.New("from greater than to"), http.StatusBadRequest)
}
blocks := h.State.QueryBlocksByNumber(from, to)
if len(blocks) == 0 {
return web.Respond(ctx, w, nil, http.StatusNoContent)
}
blockData := make([]database.BlockData, len(blocks))
for i, block := range blocks {
blockData[i] = database.NewBlockData(block)
}
return web.Respond(ctx, w, blockData, http.StatusOK)
}
// Mempool returns the set of uncommitted transactions.
func (h Handlers) Mempool(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
txs := h.State.Mempool()
return web.Respond(ctx, w, txs, http.StatusOK)
}