Skip to content

Commit

Permalink
Types for storage, blocks and headers for M2 (#23)
Browse files Browse the repository at this point in the history
* Adds AccountId, AccountIndex  and AccountInfo types

* Add Data type

* Fix linting errors

* Implement Moment type

* Implements Null type

* Change codec to make it accept any value that can be cast to a string

* Implement Text type

* Implement Origin and Weight types

* Implement WeightMultiplier type

* Fix linting errors

* Improve Data and StorageDataRaw types not to require any length information for decoding.

* Implement GetKeys

* Implement GetStorageSize

* Implement GetStorageHash

* Implement GetFinalizedHead

* Implement Header and Signature types

* Implement GetHeader

* Implement GetBlock

* Fix linting errors
  • Loading branch information
philipstanislaus authored Oct 10, 2019
1 parent 81fc3c4 commit d2702f6
Show file tree
Hide file tree
Showing 38 changed files with 1,146 additions and 98 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ test-e2e-deployed: ## runs only end-to-end (e2e) tests against a deployed test
run-substrate-docker: ## runs the Substrate Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:latest-v1.0 --dev --rpc-external --ws-external

run-substrate-docker-v2: ## runs the Substrate Default Docker image, this can be used to run the tests
docker run -p 9933:9933 -p 9944:9944 -p 30333:30333 parity/substrate:latest --dev --rpc-external --ws-external

help: ## shows this help
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)

.PHONY: clean install lint lint-fix test test-dockerized run-substrate-docker
.PHONY: clean install lint lint-fix test test-dockerized run-substrate-docker run-substrate-docker-v2
21 changes: 21 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"log"

"github.com/centrifuge/go-substrate-rpc-client/types"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -79,3 +80,23 @@ func Connect(url string) (Client, error) {
cc := client{c, url}
return &cc, nil
}

func CallWithBlockHash(c Client, target interface{}, method string, blockHash *types.Hash, args ...interface{}) error {
if blockHash == nil {
err := c.Call(target, method, args...)
if err != nil {
return err
}
return nil
}
hexHash, err := types.Hex(*blockHash)
if err != nil {
return err
}
hargs := append(args, hexHash)
err = c.Call(target, method, hargs...)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions rpc/chain/actual
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*types.SignedBlock(&types.SignedBlock{Block:types.Block{Header:types.Header{ParentHash:types.Hash{0xc1, 0x75, 0x51, 0x36, 0x74, 0x91, 0xbf, 0x6d, 0x10, 0xe8, 0xe7, 0x74, 0x84, 0x8b, 0x1b, 0x5c, 0xbb, 0xb2, 0x89, 0x56, 0xb8, 0x47, 0x88, 0xa8, 0xbf, 0x6c, 0x3, 0x38, 0x5f, 0xa2, 0x96, 0x3e}, Number:0x0, StateRoot:types.Hash{0x9d, 0xfb, 0x54, 0xaa, 0x59, 0x7f, 0x1f, 0xec, 0xa4, 0x50, 0x54, 0xcb, 0xfe, 0x9, 0xfc, 0xe1, 0x76, 0x23, 0x2a, 0xe3, 0x38, 0xd6, 0xc, 0x1f, 0xcd, 0x94, 0xf3, 0xe0, 0xd5, 0x15, 0x73, 0xf9}, ExtrinsicsRoot:types.Hash{0xe3, 0x6f, 0xae, 0x7b, 0x50, 0xc6, 0x3a, 0x73, 0x8c, 0x38, 0x7, 0xa8, 0x38, 0xa, 0x38, 0x83, 0x83, 0xaa, 0xfa, 0x5c, 0x37, 0x1d, 0xef, 0x5b, 0x85, 0x54, 0xba, 0xce, 0x15, 0x96, 0x90, 0x98}, Digest:types.Digest{types.DigestItem{IsOther:false, AsOther:types.Bytes(nil), IsAuthoritiesChange:false, AsAuthoritiesChange:[]types.AuthorityID(nil), IsChangesTrieRoot:false, AsChangesTrieRoot:types.Hash{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsSealV0:false, AsSealV0:types.SealV0{Signer:0x0, Signature:types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, IsConsensus:false, AsConsensus:types.Consensus{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsSeal:false, AsSeal:types.Seal{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsPreRuntime:true, AsPreRuntime:types.PreRuntime{ConsensusEngineID:0x45424142, Bytes:types.Bytes{0x2, 0x0, 0x0, 0x0, 0x0, 0xa7, 0xeb, 0x33, 0x1f, 0x0, 0x0, 0x0, 0x0}}}, types.DigestItem{IsOther:false, AsOther:types.Bytes(nil), IsAuthoritiesChange:false, AsAuthoritiesChange:[]types.AuthorityID(nil), IsChangesTrieRoot:false, AsChangesTrieRoot:types.Hash{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsSealV0:false, AsSealV0:types.SealV0{Signer:0x0, Signature:types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, IsConsensus:false, AsConsensus:types.Consensus{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsSeal:true, AsSeal:types.Seal{ConsensusEngineID:0x45424142, Bytes:types.Bytes{0x4a, 0x47, 0x5, 0x3, 0x5, 0x2c, 0x53, 0x8b, 0x5b, 0xd7, 0x5, 0x25, 0x47, 0xcc, 0x1f, 0xae, 0x4, 0x3b, 0x8a, 0xee, 0x56, 0xe7, 0x50, 0xd6, 0xde, 0x87, 0x4d, 0xff, 0x90, 0x4d, 0x7a, 0x2b, 0x48, 0x8c, 0xcb, 0x77, 0x49, 0xfa, 0x74, 0x2e, 0x54, 0x76, 0x91, 0xc5, 0x84, 0xa7, 0x5f, 0x2e, 0xa0, 0x8d, 0x28, 0xf7, 0xd9, 0xf6, 0xb, 0xb7, 0x33, 0xf1, 0x18, 0xc5, 0x4, 0x2c, 0x6f, 0x8e}}, IsPreRuntime:false, AsPreRuntime:types.PreRuntime{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}}}}, Extrinsics:[]types.Extrinsic{"0x280302000b088d71a86d01", "0x14030d001916"}}, Justification:types.Justification(nil)})
30 changes: 24 additions & 6 deletions rpc/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/centrifuge/go-substrate-rpc-client/client"
"github.com/centrifuge/go-substrate-rpc-client/rpcmocksrv"
"github.com/centrifuge/go-substrate-rpc-client/types"
)

var chain *Chain
Expand All @@ -34,6 +35,7 @@ func TestMain(m *testing.M) {
}

cl, err := client.Connect(s.URL)
// cl, err := client.Connect(config.NewDefaultConfig().RPCURL)
if err != nil {
panic(err)
}
Expand All @@ -44,21 +46,37 @@ func TestMain(m *testing.M) {

// MockSrv holds data and methods exposed by the RPC Mock Server used in integration tests
type MockSrv struct {
blockHash string
blockHashLatest string
blockHash types.Hash
blockHashLatest types.Hash
header types.Header
signedBlock types.SignedBlock
}

func (s *MockSrv) GetBlockHash(height *uint64) string {
if height != nil {
return mockSrv.blockHash
return mockSrv.blockHash.Hex()
}
return mockSrv.blockHashLatest
return mockSrv.blockHashLatest.Hex()
}

func (s *MockSrv) GetBlock(hash *string) types.SignedBlock {
return mockSrv.signedBlock
}

func (s *MockSrv) GetHeader(hash *string) types.Header {
return mockSrv.header
}

func (s *MockSrv) GetFinalizedHead() string {
return mockSrv.blockHashLatest.Hex()
}

// mockSrv sets default data used in tests. This data might become stale when substrate is updated – just run the tests
// against real servers and update the values stored here. To do that, replace s.URL with
// config.NewDefaultConfig().RPCURL
var mockSrv = MockSrv{
blockHash: "0xc407ff9f28da7e8cedda956195d3e911c8615a2ecf0dbd6c25cf2667fb09a72a",
blockHashLatest: "0xc407ff9f28da7e8cedda956195d3e911c8615a2ecf0dbd6c25cf2667fb09a72b",
blockHash: types.Hash{0xc4, 0x07, 0xff, 0x9f, 0x28, 0xda, 0x7e, 0x8c, 0xed, 0xda, 0x95, 0x61, 0x95, 0xd3, 0xe9, 0x11, 0xc8, 0x61, 0x5a, 0x2e, 0xcf, 0x0d, 0xbd, 0x6c, 0x25, 0xcf, 0x26, 0x67, 0xfb, 0x09, 0xa7, 0x2a}, //nolint:lll
blockHashLatest: types.Hash{0xc4, 0x07, 0xff, 0x9f, 0x28, 0xda, 0x7e, 0x8c, 0xed, 0xda, 0x95, 0x61, 0x95, 0xd3, 0xe9, 0x11, 0xc8, 0x61, 0x5a, 0x2e, 0xcf, 0x0d, 0xbd, 0x6c, 0x25, 0xcf, 0x26, 0x67, 0xfb, 0x09, 0xa7, 0x2b}, //nolint:lll
header: types.ExamplaryHeader,
signedBlock: types.ExamplarySignedBlock,
}
1 change: 1 addition & 0 deletions rpc/chain/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*types.Header(&types.Header{ParentHash:types.Hash{0xf6, 0xe0, 0xf9, 0xd, 0x27, 0x57, 0x8a, 0x18, 0xfc, 0x22, 0xcf, 0x10, 0x83, 0x3d, 0x7f, 0xb2, 0x86, 0xf9, 0x78, 0xce, 0x4e, 0x41, 0x1e, 0x87, 0xb2, 0x19, 0xc1, 0xd4, 0x98, 0xf6, 0xd3, 0x2c}, Number:0x0, StateRoot:types.Hash{0x2e, 0x5e, 0x1e, 0xa0, 0x5f, 0xcd, 0xec, 0xca, 0xa, 0xa, 0xf0, 0xe9, 0x8d, 0xc5, 0xc3, 0x20, 0xcb, 0x62, 0x13, 0xad, 0xc2, 0x1f, 0x4f, 0xad, 0x2c, 0xf, 0xfc, 0x74, 0x7a, 0x18, 0x64, 0xc}, ExtrinsicsRoot:types.Hash{0xc4, 0x5f, 0x66, 0x5d, 0x47, 0xe4, 0x8e, 0x54, 0xf, 0x1c, 0x89, 0xd8, 0x7e, 0x8, 0x79, 0xb8, 0x0, 0x53, 0x34, 0xf0, 0x6e, 0x53, 0x7d, 0xaa, 0x7c, 0xe8, 0xab, 0x51, 0xcc, 0x12, 0x39, 0x1b}, Digest:types.Digest{types.DigestItem{IsOther:false, AsOther:types.Bytes(nil), IsAuthoritiesChange:false, AsAuthoritiesChange:[]types.AuthorityID(nil), IsChangesTrieRoot:false, AsChangesTrieRoot:types.Hash{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsSealV0:false, AsSealV0:types.SealV0{Signer:0x0, Signature:types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, IsConsensus:false, AsConsensus:types.Consensus{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsSeal:false, AsSeal:types.Seal{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsPreRuntime:true, AsPreRuntime:types.PreRuntime{ConsensusEngineID:0x45424142, Bytes:types.Bytes{0x2, 0x0, 0x0, 0x0, 0x0, 0xd9, 0xe8, 0x33, 0x1f, 0x0, 0x0, 0x0, 0x0}}}, types.DigestItem{IsOther:false, AsOther:types.Bytes(nil), IsAuthoritiesChange:false, AsAuthoritiesChange:[]types.AuthorityID(nil), IsChangesTrieRoot:false, AsChangesTrieRoot:types.Hash{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, IsSealV0:false, AsSealV0:types.SealV0{Signer:0x0, Signature:types.Signature{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, IsConsensus:false, AsConsensus:types.Consensus{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}, IsSeal:true, AsSeal:types.Seal{ConsensusEngineID:0x45424142, Bytes:types.Bytes{0xaa, 0x3f, 0x2f, 0x1, 0x50, 0x15, 0x4a, 0x9a, 0x6c, 0xe6, 0xb8, 0xf5, 0x18, 0x28, 0x8b, 0xc2, 0x92, 0xb2, 0x1, 0xf8, 0x36, 0x32, 0xdc, 0xf9, 0xeb, 0xd, 0x2d, 0x0, 0x5a, 0x38, 0xde, 0x2a, 0xa3, 0x67, 0x45, 0x31, 0xc7, 0x4, 0x46, 0x4e, 0xe6, 0x76, 0x88, 0x76, 0x83, 0x68, 0xba, 0xb8, 0x40, 0x11, 0x7, 0x6a, 0x35, 0xe8, 0xe6, 0xdd, 0x4a, 0xf5, 0x9d, 0xb4, 0x15, 0x5, 0x2f, 0x8d}}, IsPreRuntime:false, AsPreRuntime:types.PreRuntime{ConsensusEngineID:0x0, Bytes:types.Bytes(nil)}}}})
39 changes: 39 additions & 0 deletions rpc/chain/get_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chain

import (
"github.com/centrifuge/go-substrate-rpc-client/client"
"github.com/centrifuge/go-substrate-rpc-client/types"
)

func (c *Chain) GetBlock(blockHash types.Hash) (*types.SignedBlock, error) {
return c.getBlock(&blockHash)
}

func (c *Chain) GetBlockLatest() (*types.SignedBlock, error) {
return c.getBlock(nil)
}

func (c *Chain) getBlock(blockHash *types.Hash) (*types.SignedBlock, error) {
var SignedBlock types.SignedBlock
err := client.CallWithBlockHash(*c.client, &SignedBlock, "chain_getBlock", blockHash)
if err != nil {
return nil, err
}
return &SignedBlock, err
}
19 changes: 1 addition & 18 deletions rpc/chain/get_block_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
package chain

import (
"encoding/hex"
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/types"
)

Expand All @@ -45,19 +42,5 @@ func (c *Chain) getBlockHash(blockNumber *uint64) (types.Hash, error) {
return types.Hash{}, err
}

bz, err := hex.DecodeString(res[2:])
if err != nil {
return types.Hash{}, err
}

if len(bz) != 32 {
return types.Hash{}, fmt.Errorf("required result to be 32 bytes, but got %v", len(bz))
}

var bz32 [32]byte
copy(bz32[:], bz)

hash := types.NewHash(bz32)

return hash, nil
return types.NewHashFromHexString(res)
}
4 changes: 2 additions & 2 deletions rpc/chain/get_block_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestChain_GetBlockHash(t *testing.T) {
assert.NoError(t, err)
hex, err := types.Hex(res)
assert.NoError(t, err)
assert.Equal(t, mockSrv.blockHash, hex)
assert.Equal(t, mockSrv.blockHash.Hex(), hex)
}

func TestChain_GetBlockHashLatest(t *testing.T) {
res, err := chain.GetBlockHashLatest()
assert.NoError(t, err)
hex, err := types.Hex(res)
assert.NoError(t, err)
assert.Equal(t, mockSrv.blockHashLatest, hex)
assert.Equal(t, mockSrv.blockHashLatest.Hex(), hex)
}
37 changes: 37 additions & 0 deletions rpc/chain/get_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
// Copyright (C) 2019 Centrifuge GmbH
//
// This file is part of Go Substrate RPC Client (GSRPC).
//
// GSRPC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GSRPC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package chain

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestChain_GetBlockLatest(t *testing.T) {
rv, err := chain.GetBlockLatest()
assert.NoError(t, err)
assert.Equal(t, &mockSrv.signedBlock, rv)
}

func TestChain_GetBlock(t *testing.T) {
rv, err := chain.GetBlock(mockSrv.blockHashLatest)
assert.NoError(t, err)
assert.Equal(t, &mockSrv.signedBlock, rv)
}
32 changes: 32 additions & 0 deletions rpc/chain/get_finalized_head.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chain

import (
"github.com/centrifuge/go-substrate-rpc-client/types"
)

func (c *Chain) GetFinalizedHead() (types.Hash, error) {
var res string

err := (*c.client).Call(&res, "chain_getFinalizedHead")
if err != nil {
return types.Hash{}, err
}

return types.NewHashFromHexString(res)
}
32 changes: 32 additions & 0 deletions rpc/chain/get_finalized_head_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chain

import (
"testing"

"github.com/centrifuge/go-substrate-rpc-client/types"
"github.com/stretchr/testify/assert"
)

func TestChain_GetFinalizedHead(t *testing.T) {
res, err := chain.GetFinalizedHead()
assert.NoError(t, err)
hex, err := types.Hex(res)
assert.NoError(t, err)
assert.Equal(t, mockSrv.blockHashLatest.Hex(), hex)
}
39 changes: 39 additions & 0 deletions rpc/chain/get_header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chain

import (
"github.com/centrifuge/go-substrate-rpc-client/client"
"github.com/centrifuge/go-substrate-rpc-client/types"
)

func (c *Chain) GetHeader(blockHash types.Hash) (*types.Header, error) {
return c.getHeader(&blockHash)
}

func (c *Chain) GetHeaderLatest() (*types.Header, error) {
return c.getHeader(nil)
}

func (c *Chain) getHeader(blockHash *types.Hash) (*types.Header, error) {
var Header types.Header
err := client.CallWithBlockHash(*c.client, &Header, "chain_getHeader", blockHash)
if err != nil {
return nil, err
}
return &Header, err
}
37 changes: 37 additions & 0 deletions rpc/chain/get_header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
// Copyright (C) 2019 Centrifuge GmbH
//
// This file is part of Go Substrate RPC Client (GSRPC).
//
// GSRPC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GSRPC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package chain

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestChain_GetHeaderLatest(t *testing.T) {
rv, err := chain.GetHeaderLatest()
assert.NoError(t, err)
assert.Equal(t, &mockSrv.header, rv)
}

func TestChain_GetHeader(t *testing.T) {
rv, err := chain.GetHeader(mockSrv.blockHashLatest)
assert.NoError(t, err)
assert.Equal(t, &mockSrv.header, rv)
}
Loading

0 comments on commit d2702f6

Please sign in to comment.