Skip to content
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

MetadataUriWrapper #138

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
72 changes: 71 additions & 1 deletion app/abci_vote_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,33 @@ import (
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
blocksdkabci "github.com/skip-mev/block-sdk/v2/abci"
"github.com/spf13/cast"

"github.com/sunriselayer/sunrise/x/da/keeper"
damodulekeeper "github.com/sunriselayer/sunrise/x/da/keeper"
"github.com/sunriselayer/sunrise/x/da/types"
"github.com/sunriselayer/sunrise/x/da/zkp"
)

const flagDAShardHashesAPI = "da.shard_hashes_api"
const flagDAMetadataURI = "da.metadata_uri"

type DAConfig struct {
MetadataURI string `mapstructure:"metadata_uri"`
ShardHashesAPI string `mapstructure:"shard_hashes_api"`
}

type DAShardHashesResponse struct {
ShardHashes []string `json:"shard_hashes"`
}

type DAMetadataResponse struct {
RecoveredDataHash []string `json:"recovered_data_hash"`
RecoveredDataSize uint64 `json:"recovered_data_size"`
ShardSize uint64 `json:"shard_size"`
ShardUris []string `json:"shard_uris"`
ParityShardCount uint64 `json:"parity_shard_count"`
}

// ReadOracleConfig reads the wasm specifig configuration
func ReadDAConfig(opts servertypes.AppOptions) (DAConfig, error) {
cfg := DAConfig{}
Expand All @@ -47,10 +58,37 @@ func ReadDAConfig(opts servertypes.AppOptions) (DAConfig, error) {
return cfg, err
}
}
if v := opts.Get(flagDAMetadataURI); v != nil {
if cfg.MetadataURI, err = cast.ToStringE(v); err != nil {
return cfg, err
}
}

return cfg, nil
}

func GetDAMetadata(daConfig DAConfig, metadataUri string) (DAMetadataResponse, error) {
url := daConfig.MetadataURI + "?metadata_uri=" + metadataUri
res, err := http.Get(url)
if err != nil {
return DAMetadataResponse{}, err
}
defer res.Body.Close()

resBody, err := io.ReadAll(res.Body)
if err != nil {
return DAMetadataResponse{}, err
}

daMetadata := DAMetadataResponse{}
err = json.Unmarshal(resBody, &daMetadata)
if err != nil {
return DAMetadataResponse{}, err
}

return daMetadata, nil
}

func GetDataShardHashes(daConfig DAConfig, metadataUri string, n, threshold int64, valAddr sdk.ValAddress) ([]int64, [][]byte, error) {
indices := types.ShardIndicesForValidator(valAddr, n, threshold)
url := daConfig.ShardHashesAPI + "?metadata_uri=" + metadataUri + "&indices=" + strings.Trim(strings.Replace(fmt.Sprint(indices), " ", ",", -1), "[]")
Expand Down Expand Up @@ -301,7 +339,7 @@ func (h *ProposalHandler) PrepareProposal() sdk.PrepareProposalHandler {
}
}

func (h *ProposalHandler) ProcessProposal() sdk.ProcessProposalHandler {
func (h *ProposalHandler) ProcessProposal(daConfig DAConfig) sdk.ProcessProposalHandler {
return func(ctx sdk.Context, req *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) {
txs := [][]byte{}
var voteExtTx VoteExtensionTx
Expand All @@ -324,6 +362,38 @@ func (h *ProposalHandler) ProcessProposal() sdk.ProcessProposalHandler {
if err := ConfirmFaultValidators(voteExtTx.FaultValidators, faultValidators); err != nil {
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
}

// Create and append metadata for each successfully voted blob
for _, data := range votedData {
daMetadata, err := GetDAMetadata(daConfig, data.MetadataUri)
if err != nil {
continue
}

dataHashes := []byte{}
for _, hashEncoded := range daMetadata.RecoveredDataHash {
hash, err := base64.StdEncoding.DecodeString(hashEncoded)
if err != nil {
continue
}
dataHashes = append(dataHashes, hash...)
}

metadata := &types.Metadata{
RecoveredDataHash: dataHashes,
RecoveredDataSize: daMetadata.RecoveredDataSize,
ShardSize: daMetadata.ShardSize,
ParityShardCount: daMetadata.ParityShardCount,
ShardUris: daMetadata.ShardUris,
}

metadataBz, err := metadata.Marshal()
if err != nil {
return nil, fmt.Errorf("failed to marshal metadata: %w", err)
}

txs = append(txs, metadataBz)
}
} else {
txs = append(txs, tx)
}
Expand Down
9 changes: 1 addition & 8 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"cosmossdk.io/x/tx/signing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
ibcante "github.com/cosmos/ibc-go/v8/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

Expand All @@ -20,7 +19,7 @@ import (

func NewAnteHandler(
accountKeeper ante.AccountKeeper,
bankKeeper bankkeeper.Keeper,
bankKeeper feeante.BankKeeper,
feegrantKeeper ante.FeegrantKeeper,
// blobKeeper blob.Keeper,
feeKeeper fee.Keeper,
Expand Down Expand Up @@ -69,12 +68,6 @@ func NewAnteHandler(
// account sequence number of the signer.
// Note: does not consume gas from the gas meter.
ante.NewSigVerificationDecorator(accountKeeper, signModeHandler),
// Ensure that the tx's gas limit is > the gas consumed based on the blob size(s).
// Contract: must be called after all decorators that consume gas.
// Note: does not consume gas from the gas meter.
// blobante.NewMinGasPFBDecorator(blobKeeper),
// Ensure that the tx's total blob size is <= the max blob size.
// blobante.NewMaxBlobSizeDecorator(blobKeeper),
// Side effect: increment the nonce for all tx signers.
ante.NewIncrementSequenceDecorator(accountKeeper),
// Ensure that the tx is not a IBC packet or update message that has already been processed.
Expand Down
34 changes: 15 additions & 19 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
// this line is used by starport scaffolding # stargate/app/moduleImport

"cosmossdk.io/depinject"
"cosmossdk.io/log"
Expand Down Expand Up @@ -32,8 +33,10 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
crisiskeeper "github.com/cosmos/cosmos-sdk/x/crisis/keeper"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
Expand All @@ -43,52 +46,44 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
icacontrollerkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/controller/keeper"
icahostkeeper "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/keeper"
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

"github.com/skip-mev/block-sdk/v2/abci"
"github.com/skip-mev/block-sdk/v2/abci/checktx"
"github.com/skip-mev/block-sdk/v2/block"
"github.com/skip-mev/block-sdk/v2/block/base"
"github.com/skip-mev/block-sdk/v2/block/service"
mevlane "github.com/skip-mev/block-sdk/v2/lanes/mev"
auctionkeeper "github.com/skip-mev/block-sdk/v2/x/auction/keeper"

"github.com/sunriselayer/sunrise/app/ante"
defaultoverrides "github.com/sunriselayer/sunrise/app/defaultoverrides"
"github.com/sunriselayer/sunrise/app/keepers"
"github.com/sunriselayer/sunrise/app/upgrades"

v0_2_1_test "github.com/sunriselayer/sunrise/app/upgrades/v0.2.1-test"
v0_2_2_test "github.com/sunriselayer/sunrise/app/upgrades/v0.2.2-test"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
defaultoverrides "github.com/sunriselayer/sunrise/app/defaultoverrides"
feetypes "github.com/sunriselayer/sunrise/x/fee/types"
tokenconvertertypes "github.com/sunriselayer/sunrise/x/tokenconverter/types"

// blobmodulekeeper "github.com/sunriselayer/sunrise/x/blob/keeper"
// streammodulekeeper "github.com/sunriselayer/sunrise/x/blobstream/keeper"
"github.com/sunriselayer/sunrise/docs"
damodulekeeper "github.com/sunriselayer/sunrise/x/da/keeper"
feemodulekeeper "github.com/sunriselayer/sunrise/x/fee/keeper"
liquidityincentivemodulekeeper "github.com/sunriselayer/sunrise/x/liquidityincentive/keeper"
liquiditypoolmodulekeeper "github.com/sunriselayer/sunrise/x/liquiditypool/keeper"
swapmodulekeeper "github.com/sunriselayer/sunrise/x/swap/keeper"
tokenconvertermodulekeeper "github.com/sunriselayer/sunrise/x/tokenconverter/keeper"

// this line is used by starport scaffolding # stargate/app/moduleImport

"github.com/sunriselayer/sunrise/docs"
)

const (
Expand Down Expand Up @@ -459,8 +454,14 @@ func New(
app.ModuleManager,
blockSdkProposalHandler,
)

daConfig, err := ReadDAConfig(appOpts)
if err != nil {
return nil, err
}

app.BaseApp.SetPrepareProposal(propHandler.PrepareProposal())
app.BaseApp.SetProcessProposal(propHandler.ProcessProposal())
app.BaseApp.SetProcessProposal(propHandler.ProcessProposal(daConfig))
app.BaseApp.SetPreBlocker(propHandler.PreBlocker)

// Step 7: Set the custom CheckTx handler on BaseApp. This is only required if you
Expand Down Expand Up @@ -495,11 +496,6 @@ func New(
// Vote extension
voteExtHandler := NewVoteExtHandler(app.DaKeeper, app.StakingKeeper)

daConfig, err := ReadDAConfig(appOpts)
if err != nil {
return nil, err
}

app.App.BaseApp.SetExtendVoteHandler(voteExtHandler.ExtendVoteHandler(daConfig, app.txConfig.TxDecoder(), anteHandler, app.DaKeeper))
app.App.BaseApp.SetVerifyVoteExtensionHandler(voteExtHandler.VerifyVoteExtensionHandler(daConfig, app.DaKeeper))

Expand Down
8 changes: 6 additions & 2 deletions cmd/sunrised/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sunriselayer/sunrise/app"

defaultoverrides "github.com/sunriselayer/sunrise/app/defaultoverrides"
)

Expand Down Expand Up @@ -51,7 +50,11 @@ var ConfigTemplate = serverconfig.DefaultConfigTemplate + `
[da]
# API to query DA v2 uploaded data shard hashes
shard_hashes_api = {{ .DA.ShardHashesAPI }}`
shard_hashes_api = {{ .DA.ShardHashesAPI }}
# API to query DA v2 metadata
metadata_uri = {{ .DA.MetadataURI }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be metadata_api

`

// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
Expand Down Expand Up @@ -79,6 +82,7 @@ func InitAppConfig() (string, CustomAppConfig) {
Config: *srvCfg,
DA: app.DAConfig{
ShardHashesAPI: "http://localhost:8000/api/shard_hashes",
MetadataURI: "http://localhost:8000/api/metadata",
},
}

Expand Down
Loading