Skip to content

Commit

Permalink
chore(blockstate): change GetRuntime to take common.Hash as argum…
Browse files Browse the repository at this point in the history
…ent (#2759)
  • Loading branch information
qdm12 authored Nov 9, 2022
1 parent 86b4fcc commit 879d065
Show file tree
Hide file tree
Showing 30 changed files with 201 additions and 174 deletions.
2 changes: 0 additions & 2 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,3 @@ func getGssmrRuntimeCode(t *testing.T) (code []byte) {

return trieState.LoadCode()
}

func hashPtr(h common.Hash) *common.Hash { return &h }
2 changes: 1 addition & 1 deletion dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type BlockState interface {
SubChain(start, end common.Hash) ([]common.Hash, error)
GetBlockBody(hash common.Hash) (*types.Body, error)
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error
GetRuntime(*common.Hash) (runtime.Instance, error)
GetRuntime(blockHash common.Hash) (instance runtime.Instance, err error)
StoreRuntime(common.Hash, runtime.Instance)
}

Expand Down
4 changes: 2 additions & 2 deletions dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (s *Service) HandleTransactionMessage(peerID peer.ID, msg *network.Transact
return false, err
}

hash := head.Hash()
rt, err := s.blockState.GetRuntime(&hash)
bestBlockHash := head.Hash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return false, err
}
Expand Down
3 changes: 2 additions & 1 deletion dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func TestService_HandleTransactionMessage(t *testing.T) {
genHeader, err := s.blockState.BestBlockHeader()
require.NoError(t, err)

rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

ts, err := s.storageState.TrieState(nil)
Expand Down
2 changes: 1 addition & 1 deletion dot/core/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 44 additions & 48 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
logger.Debugf("imported block %s and stored state trie with root %s",
block.Header.Hash(), state.MustRoot())

rt, err := s.blockState.GetRuntime(&block.Header.ParentHash)
rt, err := s.blockState.GetRuntime(block.Header.ParentHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
return fmt.Errorf("%w: for hash %s", ErrEmptyRuntimeCode, hash)
}

rt, err := s.blockState.GetRuntime(&hash)
rt, err := s.blockState.GetRuntime(hash)
if err != nil {
return fmt.Errorf("getting runtime from block state: %w", err)
}
Expand Down Expand Up @@ -352,7 +352,8 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
}

// Check transaction validation on the best block.
rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -425,7 +426,8 @@ func (s *Service) maintainTransactionPool(block *types.Block, bestBlockHash comm
// re-validate transactions in the pool and move them to the queue
txs := s.transactionState.PendingInPool()
for _, tx := range txs {
rt, err := s.blockState.GetRuntime(&bestBlockHash)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return fmt.Errorf("failed to get runtime to re-validate transactions in pool: %s", err)
}
Expand Down Expand Up @@ -477,7 +479,8 @@ func (s *Service) HasKey(pubKeyStr, keystoreType string) (bool, error) {

// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return nil, err
}
Expand All @@ -488,28 +491,10 @@ func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
// GetRuntimeVersion gets the current RuntimeVersion
func (s *Service) GetRuntimeVersion(bhash *common.Hash) (
version runtime.Version, err error) {
var stateRootHash *common.Hash

// If block hash is not nil then fetch the state root corresponding to the block.
if bhash != nil {
var err error
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
if err != nil {
return version, err
}
}

ts, err := s.storageState.TrieState(stateRootHash)
rt, err := prepareRuntime(bhash, s.storageState, s.blockState)
if err != nil {
return version, err
return version, fmt.Errorf("setting up runtime: %w", err)
}

rt, err := s.blockState.GetRuntime(bhash)
if err != nil {
return version, err
}

rt.SetContextStorage(ts)
return rt.Version(), nil
}

Expand All @@ -535,7 +520,7 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
return err
}

rt, err := s.blockState.GetRuntime(&bestBlockHash)
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
logger.Critical("failed to get runtime")
return err
Expand Down Expand Up @@ -564,30 +549,11 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
}

// GetMetadata calls runtime Metadata_metadata function
func (s *Service) GetMetadata(bhash *common.Hash) ([]byte, error) {
var (
stateRootHash *common.Hash
err error
)

// If block hash is not nil then fetch the state root corresponding to the block.
if bhash != nil {
stateRootHash, err = s.storageState.GetStateRootFromBlock(bhash)
if err != nil {
return nil, err
}
}
ts, err := s.storageState.TrieState(stateRootHash)
if err != nil {
return nil, err
}

rt, err := s.blockState.GetRuntime(bhash)
func (s *Service) GetMetadata(bhash *common.Hash) (metadata []byte, err error) {
rt, err := prepareRuntime(bhash, s.storageState, s.blockState)
if err != nil {
return nil, err
return nil, fmt.Errorf("setting up runtime: %w", err)
}

rt.SetContextStorage(ts)
return rt.Metadata()
}

Expand Down Expand Up @@ -631,3 +597,33 @@ func (s *Service) buildExternalTransaction(rt runtime.Instance, ext types.Extrin
}
return types.Extrinsic(bytes.Join(extrinsicParts, nil)), nil
}

func prepareRuntime(blockHash *common.Hash, storageState StorageState,
blockState BlockState) (instance runtime.Instance, err error) {
var stateRootHash *common.Hash
if blockHash != nil {
stateRootHash, err = storageState.GetStateRootFromBlock(blockHash)
if err != nil {
return nil, fmt.Errorf("getting state root from block hash: %w", err)
}
}

trieState, err := storageState.TrieState(stateRootHash)
if err != nil {
return nil, fmt.Errorf("getting trie state: %w", err)
}

var blockHashValue common.Hash
if blockHash != nil {
blockHashValue = *blockHash
} else {
blockHashValue = blockState.BestBlockHash()
}
instance, err = blockState.GetRuntime(blockHashValue)
if err != nil {
return nil, fmt.Errorf("getting runtime: %w", err)
}

instance.SetContextStorage(trieState)
return instance, nil
}
30 changes: 18 additions & 12 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
parent, err := bs.BestBlockHeader()
require.NoError(t, err)

rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

block1 := sync.BuildBlock(t, rt, parent, nil)
Expand Down Expand Up @@ -299,8 +300,8 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
// we prefix with []byte{2} here since that's the enum index for the old IncludeDataExt extrinsic
tx := append([]byte{2}, enc...)

bhash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(&bhash)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

validity, err := rt.ValidateTransaction(tx)
Expand Down Expand Up @@ -440,7 +441,8 @@ func TestMaintainTransactionPoolLatestTxnQueue_BlockWithExtrinsics(t *testing.T)

func TestService_GetRuntimeVersion(t *testing.T) {
s := NewTestService(t, nil)
rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

rtExpected := rt.Version()
Expand All @@ -461,7 +463,8 @@ func TestService_HandleSubmittedExtrinsic(t *testing.T) {
genHeader, err := s.blockState.BestBlockHeader()
require.NoError(t, err)

rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

ts, err := s.storageState.TrieState(nil)
Expand Down Expand Up @@ -492,7 +495,8 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
)
s := NewTestService(t, nil)

rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

v := rt.Version()
Expand Down Expand Up @@ -526,7 +530,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
ts, err := s.storageState.TrieState(nil) // Pass genesis root
require.NoError(t, err)

parentRt, err := s.blockState.GetRuntime(&hash)
parentRt, err := s.blockState.GetRuntime(hash)
require.NoError(t, err)

v = parentRt.Version()
Expand All @@ -547,13 +551,13 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
require.NoError(t, err)

// bhash1 runtime should not be updated
rt, err = s.blockState.GetRuntime(&bhash1)
rt, err = s.blockState.GetRuntime(bhash1)
require.NoError(t, err)

v = rt.Version()
require.Equal(t, v.SpecVersion, currSpecVersion)

rt, err = s.blockState.GetRuntime(&rtUpdateBhash)
rt, err = s.blockState.GetRuntime(rtUpdateBhash)
require.NoError(t, err)

v = rt.Version()
Expand All @@ -574,7 +578,8 @@ func TestService_HandleCodeSubstitutes(t *testing.T) {
blockHash: common.BytesToHex(testRuntime),
}

rt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

s.blockState.StoreRuntime(blockHash, rt)
Expand All @@ -589,7 +594,8 @@ func TestService_HandleCodeSubstitutes(t *testing.T) {
func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
s := NewTestService(t, nil)

parentRt, err := s.blockState.GetRuntime(nil)
bestBlockHash := s.blockState.BestBlockHash()
parentRt, err := s.blockState.GetRuntime(bestBlockHash)
require.NoError(t, err)

codeHashBefore := parentRt.GetCodeHash()
Expand Down Expand Up @@ -626,7 +632,7 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
err = s.blockState.HandleRuntimeChanges(ts, parentRt, rtUpdateBhash)
require.NoError(t, err)

rt, err := s.blockState.GetRuntime(&rtUpdateBhash)
rt, err := s.blockState.GetRuntime(rtUpdateBhash)
require.NoError(t, err)

// codeHash should change after runtime change
Expand Down
Loading

0 comments on commit 879d065

Please sign in to comment.