Skip to content

Commit

Permalink
go-filecoin show and go-filecoin chain combined (filecoin-project#1587)
Browse files Browse the repository at this point in the history
Fixes filecoin-project#1587

Signed-off-by: wanghui <[email protected]>
  • Loading branch information
deaswang committed Jul 31, 2019
1 parent 2ae7aa7 commit 34a6b5f
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 186 deletions.
79 changes: 77 additions & 2 deletions commands/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ var chainCmd = &cmds.Command{
Tagline: "Inspect the filecoin blockchain",
},
Subcommands: map[string]*cmds.Command{
"head": chainHeadCmd,
"ls": chainLsCmd,
"head": chainHeadCmd,
"ls": chainLsCmd,
"block": chainBlockCmd,
},
}

Expand Down Expand Up @@ -56,19 +57,33 @@ var chainLsCmd = &cmds.Command{
},
Options: []cmdkit.Option{
cmdkit.BoolOption("long", "l", "List blocks in long format, including CID, Miner, StateRoot, block height and message count respectively"),
cmdkit.Uint64Option("begin", "b", "The block height of smallest height, default for genesis"),
cmdkit.Uint64Option("end", "e", "The block height of largest height, default for head"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
iter, err := GetPorcelainAPI(env).ChainLs(req.Context)
if err != nil {
return err
}
beginHeight, _ := req.Options["begin"].(uint64)
endHeight, _ := req.Options["end"].(uint64)

fmt.Println("beginHeight", beginHeight, "endHeight", endHeight)

for ; !iter.Complete(); err = iter.Next() {
if err != nil {
return err
}
if !iter.Value().Defined() {
panic("tipsets from this iterator should have at least one member")
}
height, _ := iter.Value().Height()
if height < beginHeight {
return nil
}
if endHeight != 0 && height > endHeight {
continue
}
if err := re.Emit(iter.Value().ToSlice()); err != nil {
return err
}
Expand Down Expand Up @@ -108,3 +123,63 @@ var chainLsCmd = &cmds.Command{
}),
},
}

var chainBlockCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Show a filecoin block by its CID",
ShortDescription: `Prints the miner, parent weight, height,
and nonce of a given block. If JSON encoding is specified with the --enc flag,
all other block properties will be included as well.`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("cid", true, false, "CID of block to show"),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("messages", "m", "show messages in block"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
cid, err := cid.Decode(req.Arguments[0])
if err != nil {
return err
}

block, err := GetPorcelainAPI(env).ChainGetBlock(req.Context, cid)
if err != nil {
return err
}

return re.Emit(block)
},
Type: types.Block{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, block *types.Block) error {
wStr, err := types.FixedStr(uint64(block.ParentWeight))
if err != nil {
return err
}

_, err = fmt.Fprintf(w, `Block Details
Miner: %s
Weight: %s
Height: %s
Nonce: %s
Timestamp: %s
`,
block.Miner,
wStr,
strconv.FormatUint(uint64(block.Height), 10),
strconv.FormatUint(uint64(block.Nonce), 10),
strconv.FormatUint(uint64(block.Timestamp), 10),
)
if err != nil {
return err
}

showMessages, _ := req.Options["messages"].(bool)
if showMessages == true {
_, err = fmt.Fprintf(w, `Messages: %s`+"\n", block.Messages)
}
return err
}),
},
}
79 changes: 79 additions & 0 deletions commands/chain_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,83 @@ func TestChainLs(t *testing.T) {
assert.Contains(t, chainLsResult, `"height":"1"`)
assert.Contains(t, chainLsResult, `"nonce":"0"`)
})

t.Run("chain ls --begin --end with height range", func(t *testing.T) {
daemon := makeTestDaemonWithMinerAndStart(t)
defer daemon.ShutdownSuccess()

daemon.RunSuccess("mining", "once", "--enc", "text")
daemon.RunSuccess("mining", "once", "--enc", "text")
daemon.RunSuccess("mining", "once", "--enc", "text")
daemon.RunSuccess("mining", "once", "--enc", "text")
chainLsResult := daemon.RunSuccess("chain", "ls", "--long", "-b", "1", "-e", "3", "--enc", "json").ReadStdoutTrimNewlines()
assert.Contains(t, chainLsResult, `"height":"1"`)
assert.Contains(t, chainLsResult, `"height":"2"`)
assert.Contains(t, chainLsResult, `"height":"3"`)
assert.NotContains(t, chainLsResult, `"height":"0"`)
assert.NotContains(t, chainLsResult, `"height":"4"`)
assert.Contains(t, chainLsResult, `"nonce":"0"`)
})
}

func TestBlockDaemon(t *testing.T) {
tf.IntegrationTest(t)

t.Run("chain block <cid-of-genesis-block> returns human readable output for the filecoin block", func(t *testing.T) {
d := makeTestDaemonWithMinerAndStart(t)
defer d.ShutdownSuccess()

// mine a block and get its CID
minedBlockCidStr := th.RunSuccessFirstLine(d, "mining", "once")

// get the mined block by its CID
output := d.RunSuccess("chain", "block", minedBlockCidStr).ReadStdoutTrimNewlines()

assert.Contains(t, output, "Block Details")
assert.Contains(t, output, "Weight: 0")
assert.Contains(t, output, "Height: 1")
assert.Contains(t, output, "Nonce: 0")
assert.Contains(t, output, "Timestamp: ")
})

t.Run("chain block --messages <cid-of-genesis-block> returns human readable output for the filecoin block including messages", func(t *testing.T) {
d := makeTestDaemonWithMinerAndStart(t)
defer d.ShutdownSuccess()

// mine a block and get its CID
minedBlockCidStr := th.RunSuccessFirstLine(d, "mining", "once")

// get the mined block by its CID
output := d.RunSuccess("chain", "block", "--messages", minedBlockCidStr).ReadStdoutTrimNewlines()

assert.Contains(t, output, "Block Details")
assert.Contains(t, output, "Weight: 0")
assert.Contains(t, output, "Height: 1")
assert.Contains(t, output, "Nonce: 0")
assert.Contains(t, output, "Timestamp: ")
assert.Contains(t, output, "Messages: ")
})

t.Run("chain block <cid-of-genesis-block> --enc json returns JSON for a filecoin block", func(t *testing.T) {
d := th.NewDaemon(t,
th.KeyFile(fixtures.KeyFilePaths()[0]),
th.WithMiner(fixtures.TestMiners[0])).Start()
defer d.ShutdownSuccess()

// mine a block and get its CID
minedBlockCidStr := th.RunSuccessFirstLine(d, "mining", "once")

// get the mined block by its CID
blockGetLine := th.RunSuccessFirstLine(d, "chain", "block", minedBlockCidStr, "--enc", "json")
var blockGetBlock types.Block
require.NoError(t, json.Unmarshal([]byte(blockGetLine), &blockGetBlock))

// ensure that we were returned the correct block

require.Equal(t, minedBlockCidStr, blockGetBlock.Cid().String())

// ensure that the JSON we received from block get conforms to schema

requireSchemaConformance(t, []byte(blockGetLine), "filecoin_block")
})
}
2 changes: 0 additions & 2 deletions commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ VIEW DATA STRUCTURES
go-filecoin chain - Inspect the filecoin blockchain
go-filecoin dag - Interact with IPLD DAG objects
go-filecoin deals - Manage deals made by or with this node
go-filecoin show - Get human-readable representations of filecoin objects
NETWORK COMMANDS
go-filecoin bitswap - Explore libp2p bitswap
Expand Down Expand Up @@ -179,7 +178,6 @@ var rootSubcmdsDaemon = map[string]*cmds.Command{
"ping": pingCmd,
"protocol": protocolCmd,
"retrieval-client": retrievalClientCmd,
"show": showCmd,
"stats": statsCmd,
"swarm": swarmCmd,
"wallet": walletCmd,
Expand Down
2 changes: 1 addition & 1 deletion commands/message_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestMessageSendBlockGasLimit(t *testing.T) {

blockCid := d.RunSuccess("mining", "once").ReadStdoutTrimNewlines()

blockInfo := d.RunSuccess("show", "block", blockCid, "--enc", "json").ReadStdoutTrimNewlines()
blockInfo := d.RunSuccess("chain", "block", blockCid, "--enc", "json").ReadStdoutTrimNewlines()

require.NoError(t, json.Unmarshal([]byte(blockInfo), &result))
assert.NotEmpty(t, result.Messages, "msg under the block gas limit passes validation and is run in the block")
Expand Down
82 changes: 0 additions & 82 deletions commands/show.go

This file was deleted.

76 changes: 0 additions & 76 deletions commands/show_test.go

This file was deleted.

Loading

0 comments on commit 34a6b5f

Please sign in to comment.