Skip to content

Commit

Permalink
Merge pull request #3823 from oasisprotocol/ptrus/feature/consensus-r…
Browse files Browse the repository at this point in the history
…untime

Consensus layer state access from runtimes
  • Loading branch information
ptrus authored Apr 16, 2021
2 parents d4a97fa + f2912f8 commit 209a89c
Show file tree
Hide file tree
Showing 40 changed files with 1,455 additions and 53 deletions.
1 change: 1 addition & 0 deletions .changelog/3564.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: Implement consensus staking layer state accessors
5 changes: 5 additions & 0 deletions .changelog/3823.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
runtime-host-protocol: Ensure compatible consensus versions

Runtime now ensures host is running a compatible consensus version.
This is a breaking runtime host protocol change as the version types in
protocol are changed.
161 changes: 161 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Oasis Core client library.
// Allow until oasis-core#3572.
#![allow(deprecated)]

#[cfg(not(target_env = "sgx"))]
#[macro_use]
pub mod grpc;
Expand Down
2 changes: 1 addition & 1 deletion go/common/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var (
// the runtime.
//
// NOTE: This version must be synced with runtime/src/common/version.rs.
RuntimeHostProtocol = Version{Major: 2, Minor: 0, Patch: 0}
RuntimeHostProtocol = Version{Major: 3, Minor: 0, Patch: 0}

// RuntimeCommitteeProtocol versions the P2P protocol used by the runtime
// committee members.
Expand Down
7 changes: 7 additions & 0 deletions go/consensus/tendermint/apps/staking/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (s *ImmutableState) loadStoredBalance(ctx context.Context, key *keyformat.K
return &q, nil
}

// TotalSupply returns the total supply balance.
func (s *ImmutableState) TotalSupply(ctx context.Context) (*quantity.Quantity, error) {
return s.loadStoredBalance(ctx, totalSupplyKeyFmt)
}
Expand All @@ -109,6 +110,7 @@ func (s *ImmutableState) CommonPool(ctx context.Context) (*quantity.Quantity, er
return s.loadStoredBalance(ctx, commonPoolKeyFmt)
}

// ConsensusParameters returns the consensus parameters.
func (s *ImmutableState) ConsensusParameters(ctx context.Context) (*staking.ConsensusParameters, error) {
raw, err := s.is.Get(ctx, parametersKeyFmt.Encode())
if err != nil {
Expand Down Expand Up @@ -162,6 +164,7 @@ func (s *ImmutableState) Thresholds(ctx context.Context) (map[staking.ThresholdK
return params.Thresholds, nil
}

// Addresses returns the non-empty addresses from the staking ledger.
func (s *ImmutableState) Addresses(ctx context.Context) ([]staking.Address, error) {
it := s.is.NewIterator(ctx)
defer it.Close()
Expand Down Expand Up @@ -211,6 +214,7 @@ func (s *ImmutableState) EscrowBalance(ctx context.Context, address staking.Addr
return &account.Escrow.Active.Balance, nil
}

// Delegations returns all active delegations.
func (s *ImmutableState) Delegations(
ctx context.Context,
) (map[staking.Address]map[staking.Address]*staking.Delegation, error) {
Expand Down Expand Up @@ -241,6 +245,7 @@ func (s *ImmutableState) Delegations(
return delegations, nil
}

// Delegation returns the delegation descriptor.
func (s *ImmutableState) Delegation(
ctx context.Context,
delegatorAddr, escrowAddr staking.Address,
Expand Down Expand Up @@ -483,10 +488,12 @@ func (s *ImmutableState) Slashing(ctx context.Context) (map[staking.SlashReason]
return params.Slashing, nil
}

// LastBlockFees returns the last block fees balance.
func (s *ImmutableState) LastBlockFees(ctx context.Context) (*quantity.Quantity, error) {
return s.loadStoredBalance(ctx, lastBlockFeesKeyFmt)
}

// GovernanceDeposits returns the governance deposits balance.
func (s *ImmutableState) GovernanceDeposits(ctx context.Context) (*quantity.Quantity, error) {
return s.loadStoredBalance(ctx, governanceDepositsKeyFmt)
}
Expand Down
11 changes: 8 additions & 3 deletions go/runtime/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,17 @@ func (c *runtimeClient) Query(ctx context.Context, request *api.QueryRequest) (*
return nil, api.ErrNoHostedRuntime
}

blk, err := c.GetBlock(ctx, &api.GetBlockRequest{RuntimeID: request.RuntimeID, Round: request.Round})
// Get current blocks.
rs, err := c.common.consensus.RootHash().GetRuntimeState(ctx, request.RuntimeID, consensus.HeightLatest)
if err != nil {
return nil, err
return nil, fmt.Errorf("client: failed to get runtime %s state: %w", request.RuntimeID, err)
}
lb, err := c.common.consensus.GetLightBlock(ctx, rs.CurrentBlockHeight)
if err != nil {
return nil, fmt.Errorf("client: failed to get light block at height %d: %w", rs.CurrentBlockHeight, err)
}

data, err := rt.Query(ctx, blk, request.Method, request.Args)
data, err := rt.Query(ctx, rs.CurrentBlock, lb, request.Method, request.Args)
if err != nil {
return nil, err
}
Expand Down
11 changes: 6 additions & 5 deletions go/runtime/host/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RichRuntime interface {
CheckTx(ctx context.Context, rb *block.Block, lb *consensus.LightBlock, tx []byte) error

// Query requests the runtime to answer a runtime-specific query.
Query(ctx context.Context, rb *block.Block, method string, args cbor.RawMessage) (cbor.RawMessage, error)
Query(ctx context.Context, rb *block.Block, lb *consensus.LightBlock, method string, args cbor.RawMessage) (cbor.RawMessage, error)
}

type richRuntime struct {
Expand Down Expand Up @@ -68,16 +68,17 @@ func (r *richRuntime) CheckTx(ctx context.Context, rb *block.Block, lb *consensu
}

// Implements RichRuntime.
func (r *richRuntime) Query(ctx context.Context, rb *block.Block, method string, args cbor.RawMessage) (cbor.RawMessage, error) {
func (r *richRuntime) Query(ctx context.Context, rb *block.Block, lb *consensus.LightBlock, method string, args cbor.RawMessage) (cbor.RawMessage, error) {
if rb == nil {
return nil, ErrInvalidArgument
}

resp, err := r.Call(ctx, &protocol.Body{
RuntimeQueryRequest: &protocol.RuntimeQueryRequest{
Method: method,
Header: rb.Header,
Args: args,
ConsensusBlock: *lb,
Method: method,
Header: rb.Header,
Args: args,
},
})
switch {
Expand Down
Loading

0 comments on commit 209a89c

Please sign in to comment.