Skip to content
Snippets Groups Projects
Commit 3b8b8bec authored by Miguel Montes's avatar Miguel Montes
Browse files

Cambiada estructura para obtener status de sealers

Agregada la posibilidad de obtener timestamp del sellado de un bloque.
parent 942e35a9
No related branches found
No related tags found
No related merge requests found
......@@ -44,6 +44,11 @@ type SealerInfo struct {
LastBlock int64
}
type SealerStatus struct {
LastBlock int64 `json:"lastBlockSigned"`
Time uint64 `json:"timestamp"`
}
const (
Latest = "latest"
Self = "self"
......@@ -60,12 +65,15 @@ func (node *Node) blockNumber() int64 {
}
func (node *Node) Coinbase() (coinbase string, err error) {
defer func(){
defer func() {
if e := recover(); e != nil {
switch s := e.(type){
case string: err = fmt.Errorf(s)
case error: err = s
default: err = fmt.Errorf("unknown error while getting coinbase: %v", e)
switch s := e.(type) {
case string:
err = fmt.Errorf(s)
case error:
err = s
default:
err = fmt.Errorf("unknown error while getting coinbase: %v", e)
}
}
}()
......@@ -199,13 +207,13 @@ func (node *Node) GetVotes(blockNumber int64) (votes Proposals) {
return
}
func (node *Node) SealersStatus(blockNumber int64) (status map[string]int64) {
func (node *Node) SealersStatus(blockNumber int64) (status map[string]*SealerStatus) {
if blockNumber == 0 { // Genesis block doesn't have signer
return
}
status = make(map[string]int64)
status = make(map[string]*SealerStatus)
for _, address := range node.GetSignersAtBlock(blockNumber) {
status[address] = -1
status[address] = &SealerStatus{}
}
notSeen := int64(len(status))
block := node.GetBlockByNumber(blockNumber)
......@@ -213,8 +221,9 @@ func (node *Node) SealersStatus(blockNumber int64) (status map[string]int64) {
until := Max(1, blockNumber-5*notSeen)
for notSeen > 0 {
signer, _ := GetSigner(&block)
if status[signer] == -1 {
status[signer] = block.Number.Int64()
if status[signer].LastBlock == 0 {
status[signer].LastBlock = block.Number.Int64()
status[signer].Time = block.Time.Uint64()
notSeen--
}
if blockNumber == until {
......@@ -295,7 +304,7 @@ func (node *Node) getSignerFirstBlock(signer string, since int64, until int64) (
case found := <-ch:
switch {
case found < 0:
n-- // a goroutine has ended
n-- // a goroutine has ended
if n == 0 { // all goroutines have ended
return
}
......@@ -359,7 +368,7 @@ func (node *Node) getSignerLastBlock(signer string, since int64, until int64) (b
case found := <-ch:
switch {
case found < 0:
n-- // a goroutine has ended
n-- // a goroutine has ended
if n == 0 { // all goroutines have ended
return
}
......
......@@ -106,11 +106,14 @@ func sealers() {
var (
blockNumber int64
status bool
timestamp bool
length int64 = 10 // timestamp length
)
description = "Presenta la lista de selladores. Opcionalmente indica el último bloque sellado por cada uno."
setFlags()
flags.Int64Var(&blockNumber, "block-number", latest, "Número del bloque en el cual se quiere conocer la lista de selladores (-1 para el último)")
flags.BoolVar(&status, "status", false, "Indica el último bloque sellado por cada sellador, o -1 si un nodo no ha sellado en las últimas 5 rondas.")
flags.BoolVar(&status, "status", false, "Indica el último bloque sellado por cada sellador, o 0 si un nodo no ha sellado en las últimas 5 rondas.")
flags.BoolVar(&timestamp, "timestamp", false, "Muestra el timestamp del sellado en lugar del número de bloque.")
parseFlags()
if blockNumber == 0 {
panic("El bloque génesis no tiene firmantes")
......@@ -129,10 +132,18 @@ func sealers() {
for sealer := range sealers {
list = append(list, sealer)
}
sort.Slice(list, func(i, j int) bool { return sealers[list[i]] > sealers[list[j]] })
length := util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]], 10))))
sort.Slice(list, func(i, j int) bool { return sealers[list[i]].LastBlock > sealers[list[j]].LastBlock })
if !timestamp {
length = util.Max(2, int64(len(strconv.FormatInt(sealers[list[0]].LastBlock, 10))))
}
for _, sealer := range list {
fmt.Printf("%v: %*d\n", sealer, length, sealers[sealer])
var output interface{}
if timestamp {
output = sealers[sealer].Time
} else {
output = sealers[sealer].LastBlock
}
fmt.Printf("%v: %*d\n", sealer, length, output)
}
} else {
sealers := node.GetSignersAtBlock(blockNumber)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment