Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/faucet: fix nonce-gap problem #22145

Merged
merged 9 commits into from
Jan 8, 2021
42 changes: 27 additions & 15 deletions cmd/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,24 +613,22 @@ func (f *faucet) loop() {
continue
}
// Faucet state retrieved, update locally and send to clients
f.lock.RLock()
log.Info("Updated faucet state", "number", head.Number, "hash", head.Hash(), "age", common.PrettyAge(timestamp), "balance", f.balance, "nonce", f.nonce, "price", f.price)

balance := new(big.Int).Div(f.balance, ether)
peers := f.stack.Server().PeerCount()

data, _ := json.Marshal(map[string]interface{}{
"funds": new(big.Int).Div(f.balance, ether),
"funded": f.nonce,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the f.nonce, f.reqs, etc internal states need the lock protection?

"peers": f.stack.Server().PeerCount(),
"requests": f.reqs,
})
headData, _ := json.Marshal(head)
f.lock.RLock()
for _, conn := range f.conns {
if err := send(conn, map[string]interface{}{
"funds": balance,
"funded": f.nonce,
"peers": peers,
"requests": f.reqs,
}, time.Second); err != nil {
if err := sendMarshalled(conn, data, time.Second); err != nil {
log.Warn("Failed to send stats to client", "err", err)
conn.conn.Close()
continue
}
if err := send(conn, head, time.Second); err != nil {
if err := sendMarshalled(conn, headData, time.Second); err != nil {
log.Warn("Failed to send header to client", "err", err)
conn.conn.Close()
}
Expand All @@ -650,9 +648,10 @@ func (f *faucet) loop() {

case <-f.update:
// Pending requests updated, stream to clients
reqData, _ := json.Marshal(map[string]interface{}{"requests": f.reqs})
MariusVanDerWijden marked this conversation as resolved.
Show resolved Hide resolved
f.lock.RLock()
for _, conn := range f.conns {
if err := send(conn, map[string]interface{}{"requests": f.reqs}, time.Second); err != nil {
if err := sendMarshalled(conn, reqData, time.Second); err != nil {
log.Warn("Failed to send requests to client", "err", err)
conn.conn.Close()
}
Expand All @@ -665,14 +664,27 @@ func (f *faucet) loop() {
// sends transmits a data packet to the remote end of the websocket, but also
// setting a write deadline to prevent waiting forever on the node.
func send(conn *wsConn, value interface{}, timeout time.Duration) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
sendMarshalled(conn, data, timeout)
}

// sendMarshalled transmits already marshalled json to the remote end of the websocket, but also
// setting a write deadline to prevent waiting forever on the node.
func sendMarshalled(conn *wsConn, data []byte, timeout time.Duration) error {
if timeout == 0 {
timeout = 60 * time.Second
}
conn.wlock.Lock()
defer conn.wlock.Unlock()

conn.conn.SetWriteDeadline(time.Now().Add(timeout))
return conn.conn.WriteJSON(value)
w, err := conn.conn.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
return w.Write(data)
}

// sendError transmits an error to the remote end of the websocket, also setting
Expand Down