Skip to content

Commit

Permalink
Merge pull request #710 from oasisprotocol/ptrus/feature/parameters-c…
Browse files Browse the repository at this point in the history
…hange-json

api/proposals: decode parameters_change for proposals
  • Loading branch information
ptrus authored Jun 28, 2024
2 parents 1cf5545 + 34f6a8b commit 78cc90b
Show file tree
Hide file tree
Showing 25 changed files with 162 additions and 32 deletions.
1 change: 1 addition & 0 deletions .changelog/710.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
api/proposals: json encode `parameters_change` instead of cbor
3 changes: 1 addition & 2 deletions analyzer/consensus/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package consensus

import (
"encoding/hex"
"encoding/json"
"sort"

Expand Down Expand Up @@ -296,7 +295,7 @@ func (mg *GenesisProcessor) addGovernanceBackendMigrations(batch *storage.QueryB
proposal.State.String(),
proposal.Deposit.ToBigInt(),
proposal.Content.ChangeParameters.Module,
hex.EncodeToString(proposal.Content.ChangeParameters.Changes),
proposal.Content.ChangeParameters.Changes,
proposal.CreatedAt,
proposal.ClosesAt,
proposal.InvalidVotes,
Expand Down
12 changes: 7 additions & 5 deletions api/spec/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ x-examples:
- &tx_hash_1 '0d0531d6b8a468c07440182b1cdda517f5a076d69fb2199126a83082ecfc0f41'
tx-body:
- &tx_body_1 '{"amount":"1000000000","account":"oasis1qpg2xuz46g53737343r20yxeddhlvc2ldqsjh70p"}'
parameters-change:
- &parameters_change_1 '{"min_validators":null,"max_validators":"120","voting_power_distribution":null}'
epoch:
- &epoch_1 8048956
- &epoch_2 8048966
Expand Down Expand Up @@ -2259,11 +2261,11 @@ components:
The name of the module whose parameters are to be changed
by this 'parameters_change' proposal.
parameters_change:
type: string
format: byte
description: |
The base64 encoded raw cbor representing the updated parameters
which are to be changed by this 'parameters_change' proposal.
description: The parameters change proposal body. This spec does not encode the many possible types;
instead, see [the Go API](https://pkg.go.dev/github.com/oasisprotocol/oasis-core/go) of oasis-core.
This object will conform to one of the `ConsensusParameterChanges` types,
depending on the `parameters_change_module`.
example: *parameters_change_1
created_at:
type: integer
format: int64
Expand Down
23 changes: 19 additions & 4 deletions storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ func (c *StorageClient) Proposals(ctx context.Context, p apiTypes.GetConsensusPr
}
for res.rows.Next() {
p := Proposal{Target: &ProposalTarget{}}
var invalidVotesNum pgtype.Numeric
var parametersChangeCBOR *[]byte
if err := res.rows.Scan(
&p.ID,
&p.Submitter,
Expand All @@ -1097,13 +1097,20 @@ func (c *StorageClient) Proposals(ctx context.Context, p apiTypes.GetConsensusPr
&p.Epoch,
&p.Cancels,
&p.ParametersChangeModule,
&p.ParametersChange,
&parametersChangeCBOR,
&p.CreatedAt,
&p.ClosesAt,
&invalidVotesNum,
&p.InvalidVotes,
); err != nil {
return nil, wrapError(err)
}
if parametersChangeCBOR != nil && p.ParametersChangeModule != nil {
res, err := extractProposalParametersChange(*parametersChangeCBOR, *p.ParametersChangeModule)
if err != nil {
return nil, wrapError(err)
}
p.ParametersChange = &res
}

ps.Proposals = append(ps.Proposals, p)
}
Expand All @@ -1114,6 +1121,7 @@ func (c *StorageClient) Proposals(ctx context.Context, p apiTypes.GetConsensusPr
// Proposal returns a governance proposal.
func (c *StorageClient) Proposal(ctx context.Context, proposalID uint64) (*Proposal, error) {
p := Proposal{Target: &ProposalTarget{}}
var parametersChangeCBOR *[]byte
if err := c.db.QueryRow(
ctx,
queries.Proposal,
Expand All @@ -1130,13 +1138,20 @@ func (c *StorageClient) Proposal(ctx context.Context, proposalID uint64) (*Propo
&p.Epoch,
&p.Cancels,
&p.ParametersChangeModule,
&p.ParametersChange,
&parametersChangeCBOR,
&p.CreatedAt,
&p.ClosesAt,
&p.InvalidVotes,
); err != nil {
return nil, wrapError(err)
}
if parametersChangeCBOR != nil && p.ParametersChangeModule != nil {
res, err := extractProposalParametersChange(*parametersChangeCBOR, *p.ParametersChangeModule)
if err != nil {
return nil, wrapError(err)
}
p.ParametersChange = &res
}

return &p, nil
}
Expand Down
113 changes: 113 additions & 0 deletions storage/client/proposals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package client

import (
"fmt"
"reflect"

"github.com/oasisprotocol/oasis-core/go/common/cbor"

governanceV22 "github.com/oasisprotocol/nexus/coreapi/v22.2.11/governance/api"
registryV22 "github.com/oasisprotocol/nexus/coreapi/v22.2.11/registry/api"
roothashV22 "github.com/oasisprotocol/nexus/coreapi/v22.2.11/roothash/api"
schedulerV22 "github.com/oasisprotocol/nexus/coreapi/v22.2.11/scheduler/api"
stakingV22 "github.com/oasisprotocol/nexus/coreapi/v22.2.11/staking/api"
governanceEden "github.com/oasisprotocol/nexus/coreapi/v24.0/governance/api"
keymanagerEden "github.com/oasisprotocol/nexus/coreapi/v24.0/keymanager/api"
keymanagerSecretsEden "github.com/oasisprotocol/nexus/coreapi/v24.0/keymanager/secrets"
registryEden "github.com/oasisprotocol/nexus/coreapi/v24.0/registry/api"
roothashEden "github.com/oasisprotocol/nexus/coreapi/v24.0/roothash/api"
schedulerEden "github.com/oasisprotocol/nexus/coreapi/v24.0/scheduler/api"
stakingEden "github.com/oasisprotocol/nexus/coreapi/v24.0/staking/api"
vaultEden "github.com/oasisprotocol/nexus/coreapi/v24.0/vault/api"
)

func extractProposalParametersChange(raw cbor.RawMessage, module string) (interface{}, error) {
switch module {
case governanceEden.ModuleName:
for _, changesType := range []interface{}{
governanceEden.ConsensusParameterChanges{},
governanceV22.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown governance consensus parameter changes")
case keymanagerEden.ModuleName:
for _, changesType := range []interface{}{
// No keymanager consensus parameter changes in V22.
keymanagerSecretsEden.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown keymanager consensus parameter changes")
case registryEden.ModuleName:
for _, changesType := range []interface{}{
registryEden.ConsensusParameterChanges{},
registryV22.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown registry consensus parameter changes")
case roothashEden.ModuleName:
for _, changesType := range []interface{}{
roothashEden.ConsensusParameterChanges{},
roothashV22.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown roothash consensus parameter changes")
case schedulerEden.ModuleName:
for _, changesType := range []interface{}{
schedulerEden.ConsensusParameterChanges{},
schedulerV22.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown scheduler consensus parameter changes")
case stakingEden.ModuleName:
for _, changesType := range []interface{}{
stakingEden.ConsensusParameterChanges{},
stakingV22.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown staking consensus parameter changes")
case vaultEden.ModuleName:
for _, changesType := range []interface{}{
// No vault in v22.
vaultEden.ConsensusParameterChanges{},
} {
v := reflect.New(reflect.TypeOf(changesType)).Interface()
if err := cbor.Unmarshal(raw, v); err != nil {
continue
}
return v, nil
}
return nil, fmt.Errorf("CBOR unmarshal: unknown staking consensus parameter changes")
default:
return nil, fmt.Errorf("unhandled module: %s", module)
}
}
2 changes: 1 addition & 1 deletion storage/oasis/nodeapi/cobalt/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func convertProposal(p *governanceCobalt.Proposal) *governance.Proposal {
CreatedAt: p.CreatedAt,
ClosesAt: p.ClosesAt,
Results: results,
InvalidVotes: 0,
InvalidVotes: p.InvalidVotes,
}
}

Expand Down
2 changes: 1 addition & 1 deletion storage/oasis/nodeapi/eden/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func convertProposal(p *governanceEden.Proposal) *governance.Proposal {
CreatedAt: p.CreatedAt,
ClosesAt: p.ClosesAt,
Results: results,
InvalidVotes: 0,
InvalidVotes: p.InvalidVotes,
}
}

Expand Down
8 changes: 4 additions & 4 deletions storage/oasis/nodeapi/file/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ func (c *FileConsensusApiLite) Close() error {
func (c *FileConsensusApiLite) GetGenesisDocument(ctx context.Context, chainContext string) (*genesis.Document, error) {
return kvstore.GetFromCacheOrCall(
c.db, false,
// v2: Added debond end time.
kvstore.GenerateCacheKey("GetGenesisDocument.v2", chainContext),
// v3: Added proposal invalid votes.
kvstore.GenerateCacheKey("GetGenesisDocument.v3", chainContext),
func() (*genesis.Document, error) { return c.consensusApi.GetGenesisDocument(ctx, chainContext) },
)
}

func (c *FileConsensusApiLite) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error) {
return kvstore.GetFromCacheOrCall(
c.db, height == consensus.HeightLatest,
// v2: Added debond end time.
kvstore.GenerateCacheKey("StateToGenesis.v2", height),
// v3: Added proposal invalid votes.
kvstore.GenerateCacheKey("StateToGenesis.v3", height),
func() (*genesis.Document, error) { return c.consensusApi.StateToGenesis(ctx, height) },
)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_regression/damask/expected/proposals.body
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"epoch": 13402,
"handler": "mainnet-upgrade-2022-04-11",
"id": 2,
"invalid_votes": "0",
"invalid_votes": "1",
"state": "passed",
"submitter": "oasis1qpydpeyjrneq20kh2jz2809lew6d9p64yymutlee",
"target": {
Expand All @@ -24,7 +24,7 @@
"epoch": 8049,
"handler": "consensus-params-update-2021-08",
"id": 1,
"invalid_votes": "0",
"invalid_votes": "2",
"state": "passed",
"submitter": "oasis1qpydpeyjrneq20kh2jz2809lew6d9p64yymutlee",
"target": {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_regression/damask/rpc-cache/consensus/00000-1.psg
Git LFS file not shown
Binary file modified tests/e2e_regression/damask/rpc-cache/consensus/00000-1.psg.pmt
Binary file not shown.
Binary file modified tests/e2e_regression/damask/rpc-cache/consensus/index.pmt
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/e2e_regression/damask/rpc-cache/consensus/main.pix
Git LFS file not shown
Binary file modified tests/e2e_regression/damask/rpc-cache/emerald/db.pmt
Binary file not shown.
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/e2e_regression/eden/expected/proposal.body
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"epoch": 13402,
"handler": "mainnet-upgrade-2022-04-11",
"id": 2,
"invalid_votes": "0",
"invalid_votes": "1",
"state": "passed",
"submitter": "oasis1qpydpeyjrneq20kh2jz2809lew6d9p64yymutlee",
"target": {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_regression/eden/expected/proposals.body
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"epoch": 13402,
"handler": "mainnet-upgrade-2022-04-11",
"id": 2,
"invalid_votes": "0",
"invalid_votes": "1",
"state": "passed",
"submitter": "oasis1qpydpeyjrneq20kh2jz2809lew6d9p64yymutlee",
"target": {
Expand All @@ -40,7 +40,7 @@
"epoch": 8049,
"handler": "consensus-params-update-2021-08",
"id": 1,
"invalid_votes": "0",
"invalid_votes": "2",
"state": "passed",
"submitter": "oasis1qpydpeyjrneq20kh2jz2809lew6d9p64yymutlee",
"target": {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e_regression/eden/rpc-cache/consensus/00000-1.psg
Git LFS file not shown
Binary file modified tests/e2e_regression/eden/rpc-cache/consensus/00000-1.psg.pmt
Binary file not shown.
Binary file modified tests/e2e_regression/eden/rpc-cache/consensus/index.pmt
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/e2e_regression/eden/rpc-cache/consensus/main.pix
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/e2e_regression/edenfast/rpc-cache/consensus/00000-1.psg
Git LFS file not shown
Binary file not shown.
Binary file modified tests/e2e_regression/edenfast/rpc-cache/consensus/index.pmt
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/e2e_regression/edenfast/rpc-cache/consensus/main.pix
Git LFS file not shown

0 comments on commit 78cc90b

Please sign in to comment.