Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newTestServiceWithFirstBlock(t *testing.T) *Service {

s := NewTestService(t, cfg)

preDigest, err := common.HexToBytes("0x014241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
preDigest, err := common.HexToBytes("0x064241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
require.Nil(t, err)

nextEpochData := &babe.NextEpochDescriptor{
Expand Down
37 changes: 27 additions & 10 deletions dot/rpc/modules/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package modules

import (
"encoding/hex"
"fmt"
"math/big"
"net/http"
Expand Down Expand Up @@ -45,11 +46,16 @@ type ChainBlock struct {

// ChainBlockHeaderResponse struct
type ChainBlockHeaderResponse struct {
ParentHash string `json:"parentHash"`
Number *big.Int `json:"number"`
StateRoot string `json:"stateRoot"`
ExtrinsicsRoot string `json:"extrinsicsRoot"`
Digest [][]byte `json:"digest"`
ParentHash string `json:"parentHash"`
Number string `json:"number"`
StateRoot string `json:"stateRoot"`
ExtrinsicsRoot string `json:"extrinsicsRoot"`
Digest ChainBlockHeaderDigest `json:"digest"`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this in the spec? I couldn't find it anywhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using this as spec for JSON result: https://github.com/w3f/PSPs/blob/psp-rpc-api/psp-002.md#chain_getHeader

But I see now that I should be treating number as a hex string, so I'll update that.

}

// ChainBlockHeaderDigest struct to hold digest logs
type ChainBlockHeaderDigest struct {
Logs []string `json:"logs"`
}

// ChainHashResponse interface to handle response
Expand Down Expand Up @@ -81,10 +87,16 @@ func (cm *ChainModule) GetBlock(r *http.Request, req *ChainHashRequest, res *Cha
}

res.Block.Header.ParentHash = block.Header.ParentHash.String()
res.Block.Header.Number = block.Header.Number
if block.Header.Number.Int64() == 0 {
res.Block.Header.Number = "0x0"
} else {
res.Block.Header.Number = "0x" + hex.EncodeToString(block.Header.Number.Bytes())
}
res.Block.Header.StateRoot = block.Header.StateRoot.String()
res.Block.Header.ExtrinsicsRoot = block.Header.ExtrinsicsRoot.String()
res.Block.Header.Digest = block.Header.Digest // TODO: figure out how to get Digest to be a json object (Issue #744)
for _, item := range block.Header.Digest {
res.Block.Header.Digest.Logs = append(res.Block.Header.Digest.Logs, "0x"+hex.EncodeToString(item))
}
if *block.Body != nil {
ext, err := block.Body.AsExtrinsics()
if err != nil {
Expand Down Expand Up @@ -139,11 +151,16 @@ func (cm *ChainModule) GetHeader(r *http.Request, req *ChainHashRequest, res *Ch
}

res.ParentHash = header.ParentHash.String()
res.Number = header.Number
if header.Number.Int64() == 0 {
res.Number = "0x0"
} else {
res.Number = "0x" + hex.EncodeToString(header.Number.Bytes())
}
res.StateRoot = header.StateRoot.String()
res.ExtrinsicsRoot = header.ExtrinsicsRoot.String()
res.Digest = header.Digest // TODO: figure out how to get Digest to be a json object (Issue #744)

for _, item := range header.Digest {
res.Digest.Logs = append(res.Digest.Logs, "0x"+hex.EncodeToString(item))
}
return nil
}

Expand Down
16 changes: 8 additions & 8 deletions dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func TestChainGetHeader_Genesis(t *testing.T) {
svc := NewChainModule(chain.Block)
expected := &ChainBlockHeaderResponse{
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
Number: big.NewInt(0),
Number: "0x0",
StateRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
ExtrinsicsRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
Digest: [][]byte{},
Digest: ChainBlockHeaderDigest{},
}
res := &ChainBlockHeaderResponse{}
req := ChainHashRequest("0xc375f478c6887dbcc2d1a4dbcc25f330b3df419325ece49cddfe5a0555663b7e")
Expand All @@ -115,10 +115,10 @@ func TestChainGetHeader_Latest(t *testing.T) {
svc := NewChainModule(chain.Block)
expected := &ChainBlockHeaderResponse{
ParentHash: "0xdbfdd87392d9ee52f499610582737daceecf83dc3ad7946fcadeb01c86e1ef75",
Number: big.NewInt(1),
Number: "0x01",
StateRoot: "0x0000000000000000000000000000000000000000000000000000000000000000",
ExtrinsicsRoot: "0x0000000000000000000000000000000000000000000000000000000000000000",
Digest: [][]byte{},
Digest: ChainBlockHeaderDigest{},
}
res := &ChainBlockHeaderResponse{}
req := ChainHashRequest("") // empty request should return latest hash
Expand Down Expand Up @@ -153,10 +153,10 @@ func TestChainGetBlock_Genesis(t *testing.T) {
svc := NewChainModule(chain.Block)
header := &ChainBlockHeaderResponse{
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
Number: big.NewInt(0),
Number: "0x0",
StateRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
ExtrinsicsRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
Digest: [][]byte{},
Digest: ChainBlockHeaderDigest{},
}
expected := &ChainBlockResponse{
Block: ChainBlock{
Expand All @@ -178,10 +178,10 @@ func TestChainGetBlock_Latest(t *testing.T) {
svc := NewChainModule(chain.Block)
header := &ChainBlockHeaderResponse{
ParentHash: "0xdbfdd87392d9ee52f499610582737daceecf83dc3ad7946fcadeb01c86e1ef75",
Number: big.NewInt(1),
Number: "0x01",
StateRoot: "0x0000000000000000000000000000000000000000000000000000000000000000",
ExtrinsicsRoot: "0x0000000000000000000000000000000000000000000000000000000000000000",
Digest: [][]byte{},
Digest: ChainBlockHeaderDigest{},
}
expected := &ChainBlockResponse{
Block: ChainBlock{
Expand Down
2 changes: 1 addition & 1 deletion dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestGetSlotForBlock(t *testing.T) {

bs := newTestBlockState(genesisHeader)

preDigest, err := common.HexToBytes("0x014241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
preDigest, err := common.HexToBytes("0x064241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions dot/types/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ func (h ConsensusEngineID) ToBytes() []byte {
var BabeEngineID = ConsensusEngineID{'B', 'A', 'B', 'E'}

// ChangesTrieRootDigestType is the byte representation of ChangesTrieRootDigest
var ChangesTrieRootDigestType = byte(0)
var ChangesTrieRootDigestType = byte(2)

// PreRuntimeDigestType is the byte representation of PreRuntimeDigest
var PreRuntimeDigestType = byte(1)
var PreRuntimeDigestType = byte(6)

// ConsensusDigestType is the byte representation of ConsensusDigest
var ConsensusDigestType = byte(2)
var ConsensusDigestType = byte(4)

// SealDigestType is the byte representation of SealDigest
var SealDigestType = byte(4)
var SealDigestType = byte(5)

// DecodeDigestItem will decode byte array to DigestItem
func DecodeDigestItem(in []byte) (DigestItem, error) {
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func newTestVerificationManager(t *testing.T, withBlock bool, descriptor *NextEp
if withBlock {
// preDigest with slot in epoch testEpoch = 2
// TODO: use BABE functions to do calculate pre-digest dynamically
preDigest, err := common.HexToBytes("0x014241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
preDigest, err := common.HexToBytes("0x064241424538e93dcef2efc275b72b4fa748332dc4c9f13be1125909cf90c8e9109c45da16b04bc5fdf9fe06a4f35e4ae4ed7e251ff9ee3d0d840c8237c9fb9057442dbf00f210d697a7b4959f792a81b948ff88937e30bf9709a8ab1314f71284da89a40000000000000000001100000000000000")
require.Nil(t, err)

nextEpochData := &NextEpochDescriptor{
Expand Down
2 changes: 1 addition & 1 deletion tests/stress/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestStressSync(t *testing.T) {

//TODO: #802 use the name of the authority here, this requires a map implementation (map process/pid/authority)
err = db.Write("blocks_"+strconv.Itoa(v.Process.Pid),
chainBlockResponse.Number.String(), chainBlockResponse)
chainBlockResponse.Number, chainBlockResponse)
require.Nil(t, err)

}
Expand Down