Skip to content

Commit

Permalink
Add forks to status (ethereum#278)
Browse files Browse the repository at this point in the history
* Add forks to status

* Track also bor config forks
  • Loading branch information
ferranbt authored Jan 13, 2022
1 parent 8a3508a commit 3b2bfa0
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 32 deletions.
157 changes: 125 additions & 32 deletions command/server/proto/server.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions command/server/proto/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ message StatusResponse {
int64 numPeers = 3;
string syncMode = 4;
Syncing syncing = 5;
repeated Fork forks = 6;

message Fork {
string name = 1;
int64 block = 2;
bool disabled = 3;
}

message Syncing {
int64 startingBlock = 1;
Expand Down
54 changes: 54 additions & 0 deletions command/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/hex"
"fmt"
"math/big"
"reflect"
"strings"

"github.com/ethereum/go-ethereum/command/server/pprof"
Expand Down Expand Up @@ -124,6 +126,7 @@ func (s *Server) Status(ctx context.Context, _ *empty.Empty) (*proto.StatusRespo
HighestBlock: int64(syncProgress.HighestBlock),
CurrentBlock: int64(syncProgress.CurrentBlock),
},
Forks: gatherForks(s.config.chain.Genesis.Config, s.config.chain.Genesis.Config.Bor),
}
return resp, nil
}
Expand All @@ -134,3 +137,54 @@ func headerToProtoHeader(h *types.Header) *proto.Header {
Number: h.Number.Uint64(),
}
}

var bigIntT = reflect.TypeOf(new(big.Int)).Kind()

// gatherForks gathers all the fork numbers via reflection
func gatherForks(configList ...interface{}) []*proto.StatusResponse_Fork {
var forks []*proto.StatusResponse_Fork

for _, config := range configList {
kind := reflect.TypeOf(config)
for kind.Kind() == reflect.Ptr {
kind = kind.Elem()
}

skip := "DAOForkBlock"

conf := reflect.ValueOf(config).Elem()
for i := 0; i < kind.NumField(); i++ {
// Fetch the next field and skip non-fork rules
field := kind.Field(i)
if strings.Contains(field.Name, skip) {
continue
}
if !strings.HasSuffix(field.Name, "Block") {
continue
}

fork := &proto.StatusResponse_Fork{
Name: strings.TrimSuffix(field.Name, "Block"),
}

val := conf.Field(i)
switch field.Type.Kind() {
case bigIntT:
rule := val.Interface().(*big.Int)
if rule != nil {
fork.Block = rule.Int64()
} else {
fork.Disabled = true
}
case reflect.Uint64:
fork.Block = int64(val.Uint())

default:
continue
}

forks = append(forks, fork)
}
}
return forks
}
43 changes: 43 additions & 0 deletions command/server/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package server

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/command/server/proto"
"github.com/stretchr/testify/assert"
)

func TestGatherBlocks(t *testing.T) {
type c struct {
ABlock *big.Int
BBlock *big.Int
}
type d struct {
DBlock uint64
}
val := &c{
BBlock: new(big.Int).SetInt64(1),
}
val2 := &d{
DBlock: 10,
}

expect := []*proto.StatusResponse_Fork{
{
Name: "A",
Disabled: true,
},
{
Name: "B",
Block: 1,
},
{
Name: "D",
Block: 10,
},
}

res := gatherForks(val, val2)
assert.Equal(t, res, expect)
}
9 changes: 9 additions & 0 deletions command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func printStatus(status *proto.StatusResponse) string {
fmt.Sprintf("Number|%d", h.Number),
})
}

forks := make([]string, len(status.Forks)+1)
forks[0] = "Name|Block|Enabled"
for i, d := range status.Forks {
forks[i+1] = fmt.Sprintf("%s|%d|%v", d.Name, d.Block, !d.Disabled)
}

full := []string{
"General",
formatKV([]string{
Expand All @@ -73,6 +80,8 @@ func printStatus(status *proto.StatusResponse) string {
fmt.Sprintf("Highest block|%d", status.Syncing.HighestBlock),
fmt.Sprintf("Starting block|%d", status.Syncing.StartingBlock),
}),
"\nForks",
formatList(forks),
}
return strings.Join(full, "\n")
}

0 comments on commit 3b2bfa0

Please sign in to comment.