add format flag to chain list command
This commit is contained in:
parent
1f9b3755bd
commit
5a8d11b85e
@ -5,9 +5,10 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/filecoin-project/go-lotus/chain/vm"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/build"
|
"github.com/filecoin-project/go-lotus/build"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
"github.com/filecoin-project/go-lotus/chain/state"
|
"github.com/filecoin-project/go-lotus/chain/state"
|
||||||
@ -271,7 +272,7 @@ func (cs *ChainStore) takeHeaviestTipSet(ts *types.TipSet) error {
|
|||||||
new: ts,
|
new: ts,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Warn("no heaviest tipset found, using %s", ts.Cids())
|
log.Warnf("no heaviest tipset found, using %s", ts.Cids())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("New heaviest tipset! %s", ts.Cids())
|
log.Debugf("New heaviest tipset! %s", ts.Cids())
|
||||||
|
40
cli/chain.go
40
cli/chain.go
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cid "github.com/ipfs/go-cid"
|
cid "github.com/ipfs/go-cid"
|
||||||
@ -279,7 +280,12 @@ var chainListCmd = &cli.Command{
|
|||||||
Usage: "View a segment of the chain",
|
Usage: "View a segment of the chain",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.Uint64Flag{Name: "height"},
|
&cli.Uint64Flag{Name: "height"},
|
||||||
&cli.UintFlag{Name: "count", Value: 30},
|
&cli.IntFlag{Name: "count", Value: 30},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "format",
|
||||||
|
Usage: "specify the format to print out tipsets",
|
||||||
|
Value: "<height>: (<time>) <blocks>",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: func(cctx *cli.Context) error {
|
Action: func(cctx *cli.Context) error {
|
||||||
api, closer, err := GetFullNodeAPI(cctx)
|
api, closer, err := GetFullNodeAPI(cctx)
|
||||||
@ -300,15 +306,15 @@ var chainListCmd = &cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
count := cctx.Uint("count")
|
count := cctx.Int("count")
|
||||||
if count < 1 {
|
if count < 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tss := make([]*types.TipSet, count)
|
tss := make([]*types.TipSet, 0, count)
|
||||||
tss[0] = head
|
tss = append(tss, head)
|
||||||
|
|
||||||
for i := 1; i < len(tss); i++ {
|
for i := 1; i < count; i++ {
|
||||||
if head.Height() == 0 {
|
if head.Height() == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -318,18 +324,26 @@ var chainListCmd = &cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tss[i] = head
|
tss = append(tss, head)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := len(tss) - 1; i >= 0; i-- {
|
for i := len(tss) - 1; i >= 0; i-- {
|
||||||
mints := tss[i].MinTimestamp()
|
printTipSet(cctx.String("format"), tss[i])
|
||||||
t := time.Unix(int64(mints), 0)
|
|
||||||
fmt.Printf("%d: (%s) [ ", tss[i].Height(), t.Format(time.Stamp))
|
|
||||||
for _, b := range tss[i].Blocks() {
|
|
||||||
fmt.Printf("%s: %s,", b.Cid(), b.Miner)
|
|
||||||
}
|
|
||||||
fmt.Println("]")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printTipSet(format string, ts *types.TipSet) {
|
||||||
|
format = strings.ReplaceAll(format, "<height>", fmt.Sprint(ts.Height()))
|
||||||
|
format = strings.ReplaceAll(format, "<time>", time.Unix(int64(ts.MinTimestamp()), 0).Format(time.Stamp))
|
||||||
|
blks := "[ "
|
||||||
|
for _, b := range ts.Blocks() {
|
||||||
|
blks += fmt.Sprintf("%s: %s,", b.Cid(), b.Miner)
|
||||||
|
}
|
||||||
|
blks += " ]"
|
||||||
|
format = strings.ReplaceAll(format, "<blocks>", blks)
|
||||||
|
format = strings.ReplaceAll(format, "<weight>", fmt.Sprint(ts.Blocks()[0].ParentWeight))
|
||||||
|
|
||||||
|
fmt.Println(format)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user