-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dot/rpc; implement RPC chain_getBlockHash #752
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3747e0f
implement chain_getBlockHash
edwardmack 37c2002
added tests for chain_getBlockHash
edwardmack 6ccce83
lint cleanup
edwardmack d86db76
cleanup comments
edwardmack 1b6bec1
Merge branch 'development' into ed/309_rpc_getBlockHash
edwardmack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,17 +23,71 @@ func newChainService(t *testing.T) *state.Service { | |
|
||
tr := trie.NewEmptyTrie() | ||
|
||
stateSrvc.UseMemDB() | ||
|
||
err = stateSrvc.Initialize(genesisHeader, tr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = stateSrvc.Start() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = loadTestBlocks(genesisHeader.Hash(), stateSrvc.Block) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return stateSrvc | ||
} | ||
|
||
func loadTestBlocks(gh common.Hash, bs *state.BlockState) error { | ||
// Create header | ||
header0 := &types.Header{ | ||
Number: big.NewInt(0), | ||
Digest: [][]byte{}, | ||
ParentHash: gh, | ||
} | ||
// Create blockHash | ||
blockHash0 := header0.Hash() | ||
// BlockBody with fake extrinsics | ||
blockBody0 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
|
||
block0 := &types.Block{ | ||
Header: header0, | ||
Body: &blockBody0, | ||
} | ||
|
||
err := bs.AddBlock(block0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create header & blockData for block 1 | ||
header1 := &types.Header{ | ||
Number: big.NewInt(1), | ||
Digest: [][]byte{}, | ||
ParentHash: blockHash0, | ||
} | ||
|
||
// Create Block with fake extrinsics | ||
blockBody1 := types.Body{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
|
||
block1 := &types.Block{ | ||
Header: header1, | ||
Body: &blockBody1, | ||
} | ||
|
||
// Add the block1 to the DB | ||
err = bs.AddBlock(block1) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestChainGetHeader_Genesis(t *testing.T) { | ||
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
|
@@ -56,10 +110,10 @@ func TestChainGetHeader_Latest(t *testing.T) { | |
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
expected := &ChainBlockHeaderResponse{ | ||
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
Number: big.NewInt(0), | ||
StateRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314", | ||
ExtrinsicsRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314", | ||
ParentHash: "0xdbfdd87392d9ee52f499610582737daceecf83dc3ad7946fcadeb01c86e1ef75", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just a comment to explain where this and the other test hashes are coming from? |
||
Number: big.NewInt(1), | ||
StateRoot: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
ExtrinsicsRoot: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
Digest: [][]byte{}, | ||
} | ||
res := &ChainBlockHeaderResponse{} | ||
|
@@ -119,10 +173,10 @@ func TestChainGetBlock_Latest(t *testing.T) { | |
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
header := &ChainBlockHeaderResponse{ | ||
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
Number: big.NewInt(0), | ||
StateRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314", | ||
ExtrinsicsRoot: "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314", | ||
ParentHash: "0xdbfdd87392d9ee52f499610582737daceecf83dc3ad7946fcadeb01c86e1ef75", | ||
Number: big.NewInt(1), | ||
StateRoot: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
ExtrinsicsRoot: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
Digest: [][]byte{}, | ||
} | ||
expected := &ChainBlockResponse{ | ||
|
@@ -159,3 +213,62 @@ func TestChainGetBlock_Error(t *testing.T) { | |
err := svc.GetBlock(nil, &req, res) | ||
require.EqualError(t, err, "could not byteify non 0x prefixed string") | ||
} | ||
|
||
func TestChainGetBlockHash_Latest(t *testing.T) { | ||
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
|
||
resString := string("") | ||
res := ChainHashResponse(resString) | ||
req := ChainBlockNumberRequest(nil) | ||
err := svc.GetBlockHash(nil, &req, &res) | ||
|
||
require.Nil(t, err) | ||
|
||
require.Equal(t, "0x80d653de440352760f89366c302c02a92ab059f396e2bfbf7f860e6e256cd698", res) | ||
} | ||
|
||
func TestChainGetBlockHash_ByNumber(t *testing.T) { | ||
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
|
||
resString := string("") | ||
res := ChainHashResponse(resString) | ||
req := ChainBlockNumberRequest("1") | ||
err := svc.GetBlockHash(nil, &req, &res) | ||
|
||
require.Nil(t, err) | ||
|
||
require.Equal(t, "0x80d653de440352760f89366c302c02a92ab059f396e2bfbf7f860e6e256cd698", res) | ||
} | ||
|
||
func TestChainGetBlockHash_ByHex(t *testing.T) { | ||
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
|
||
resString := string("") | ||
res := ChainHashResponse(resString) | ||
req := ChainBlockNumberRequest("0x01") | ||
err := svc.GetBlockHash(nil, &req, &res) | ||
|
||
require.Nil(t, err) | ||
|
||
require.Equal(t, "0x80d653de440352760f89366c302c02a92ab059f396e2bfbf7f860e6e256cd698", res) | ||
} | ||
|
||
func TestChainGetBlockHash_Array(t *testing.T) { | ||
chain := newChainService(t) | ||
svc := NewChainModule(chain.Block) | ||
|
||
resString := string("") | ||
res := ChainHashResponse(resString) | ||
nums := make([]interface{}, 2) | ||
nums[0] = float64(0) // as number | ||
nums[1] = string("0x01") // as hex string | ||
req := ChainBlockNumberRequest(nums) | ||
err := svc.GetBlockHash(nil, &req, &res) | ||
|
||
require.Nil(t, err) | ||
|
||
require.Equal(t, []string{"0xdbfdd87392d9ee52f499610582737daceecf83dc3ad7946fcadeb01c86e1ef75", "0x80d653de440352760f89366c302c02a92ab059f396e2bfbf7f860e6e256cd698"}, res) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's another test function in state called
addBlocksToState
where you can specify the depth (the branching part of that function probably isn't too relevant here) that might be useful in the future for adding more than 2 blocksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
excellent, I thought there may be something, but I didn't look deeply enough. Will come in handy.