-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add circle progress bar (#242)
- Loading branch information
1 parent
5537c18
commit f5e727b
Showing
13 changed files
with
623 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package status | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/my-cloud/ruthenium/src/log" | ||
"github.com/my-cloud/ruthenium/src/node/clock" | ||
"github.com/my-cloud/ruthenium/src/node/network" | ||
"github.com/my-cloud/ruthenium/src/node/protocol/verification" | ||
"github.com/my-cloud/ruthenium/src/ui/server" | ||
"net/http" | ||
) | ||
|
||
type Handler struct { | ||
host network.Neighbor | ||
settings server.Settings | ||
watch clock.Watch | ||
logger log.Logger | ||
} | ||
|
||
func NewHandler(host network.Neighbor, settings server.Settings, watch clock.Watch, logger log.Logger) *Handler { | ||
return &Handler{host, settings, watch, logger} | ||
} | ||
|
||
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) { | ||
switch req.Method { | ||
case http.MethodPut: | ||
jsonWriter := server.NewIoWriter(writer, handler.logger) | ||
decoder := json.NewDecoder(req.Body) | ||
var transaction *verification.Transaction | ||
err := decoder.Decode(&transaction) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to decode transaction: %w", err).Error()) | ||
writer.WriteHeader(http.StatusBadRequest) | ||
jsonWriter.Write("invalid transaction") | ||
return | ||
} else if len(transaction.Outputs()) == 0 { | ||
handler.logger.Error(errors.New("transaction has no output").Error()) | ||
writer.WriteHeader(http.StatusBadRequest) | ||
jsonWriter.Write("invalid transaction") | ||
return | ||
} | ||
lastOutputIndex := len(transaction.Outputs()) - 1 | ||
lastOutput := transaction.Outputs()[lastOutputIndex] | ||
utxosBytes, err := handler.host.GetUtxos(lastOutput.Address()) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to get UTXOs: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
var utxos []*verification.Utxo | ||
err = json.Unmarshal(utxosBytes, &utxos) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to unmarshal UTXOs: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
genesisTimestamp, err := handler.host.GetFirstBlockTimestamp() | ||
now := handler.watch.Now().UnixNano() | ||
currentBlockHeight := (now - genesisTimestamp) / handler.settings.ValidationTimestamp() | ||
currentBlockTimestamp := genesisTimestamp + currentBlockHeight*handler.settings.ValidationTimestamp() | ||
progress := &Progress{ | ||
CurrentBlockTimestamp: currentBlockTimestamp, | ||
ValidationTimestamp: handler.settings.ValidationTimestamp(), | ||
} | ||
for _, utxo := range utxos { | ||
if utxo.TransactionId() == transaction.Id() && utxo.OutputIndex() == uint16(lastOutputIndex) { | ||
progress.TransactionStatus = "confirmed" | ||
handler.sendResponse(writer, progress) | ||
return | ||
} | ||
} | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to get genesis timestamp: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
blocksBytes, err := handler.host.GetBlocks(uint64(currentBlockHeight)) | ||
if err != nil { | ||
handler.logger.Error("failed to get blocks") | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
var blocks []*verification.Block | ||
err = json.Unmarshal(blocksBytes, &blocks) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to unmarshal blocks: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
if len(blocks) == 0 { | ||
handler.logger.Error("failed to get last block, get blocks returned an empty list") | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
for _, validatedTransaction := range blocks[0].Transactions() { | ||
if validatedTransaction.Id() == transaction.Id() { | ||
progress.TransactionStatus = "validated" | ||
handler.sendResponse(writer, progress) | ||
return | ||
} | ||
} | ||
transactionsBytes, err := handler.host.GetTransactions() | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to get transactions: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
var transactions []*verification.Transaction | ||
err = json.Unmarshal(transactionsBytes, &transactions) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to unmarshal transactions: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
for _, pendingTransaction := range transactions { | ||
if pendingTransaction.Id() == transaction.Id() { | ||
progress.TransactionStatus = "sent" | ||
handler.sendResponse(writer, progress) | ||
return | ||
} | ||
} | ||
progress.TransactionStatus = "rejected" | ||
handler.sendResponse(writer, progress) | ||
default: | ||
handler.logger.Error("invalid HTTP method") | ||
writer.WriteHeader(http.StatusBadRequest) | ||
} | ||
} | ||
|
||
func (handler *Handler) sendResponse(writer http.ResponseWriter, progress *Progress) { | ||
marshaledResponse, err := json.Marshal(progress) | ||
if err != nil { | ||
handler.logger.Error(fmt.Errorf("failed to marshal progress: %w", err).Error()) | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
writer.Header().Add("Content-Type", "application/json") | ||
writer.WriteHeader(http.StatusOK) | ||
server.NewIoWriter(writer, handler.logger).Write(string(marshaledResponse[:])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package status | ||
|
||
type Progress struct { | ||
CurrentBlockTimestamp int64 `json:"current_block_timestamp"` | ||
TransactionStatus string `json:"transaction_status"` | ||
ValidationTimestamp int64 `json:"validation_timestamp"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.