feature/1125 offload sync service worker (#1144)

- **feat: provide access to block time**
- **refactor: move block expiry calculation to helper function**
- **feat: register decentralized web node HTMX views**
- **feat: Reorganize methods in layout.templ file alphabetically**
- **feat: add support for layout variants**
- **feat: update Allocate RPC to use GET request with path parameters**
- **feat: add gRPC Gateway endpoint for Allocate**
- **refactor: rename SyncCurrent to Sync**
- **feat: improve code organization by making vault assembly private**
- **feat: add a new method for syncing DID documents**
This commit is contained in:
Prad Nukala
2024-10-18 13:07:52 -04:00
committed by GitHub
parent c2f7a53017
commit 2cd44c0b66
23 changed files with 571 additions and 4641 deletions
+10 -6
View File
@@ -5,13 +5,12 @@ import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onsonr/sonr/internal/ctx"
dwngen "github.com/onsonr/sonr/internal/dwn/gen"
"github.com/onsonr/sonr/x/vault/types"
)
// assembleVault assembles the initial vault
func (k Keeper) AssembleVault(cotx sdk.Context) (string, int64, error) {
func (k Keeper) assembleVault(cotx sdk.Context) (string, int64, error) {
_, con, err := k.DIDKeeper.NewController(cotx)
if err != nil {
return "", 0, err
@@ -20,7 +19,7 @@ func (k Keeper) AssembleVault(cotx sdk.Context) (string, int64, error) {
if err != nil {
return "", 0, err
}
sch, err := k.CurrentSchema(cotx)
sch, err := k.currentSchema(cotx)
if err != nil {
return "", 0, err
}
@@ -32,12 +31,11 @@ func (k Keeper) AssembleVault(cotx sdk.Context) (string, int64, error) {
if err != nil {
return "", 0, err
}
sctx := ctx.GetSonrCTX(cotx)
return cid.String(), sctx.GetBlockExpiration(time.Second * 30), nil
return cid.String(), calculateBlockExpiry(cotx, time.Second*30), nil
}
// currentSchema returns the current schema
func (k Keeper) CurrentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
func (k Keeper) currentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
p, err := k.Params.Get(ctx)
if err != nil {
return nil, err
@@ -55,3 +53,9 @@ func (k Keeper) CurrentSchema(ctx sdk.Context) (*dwngen.Schema, error) {
Profile: schema.Profile,
}, nil
}
func calculateBlockExpiry(sdkctx sdk.Context, duration time.Duration) int64 {
blockTime := sdkctx.BlockTime()
avgBlockTime := float64(blockTime.Sub(blockTime).Seconds())
return int64(duration.Seconds() / avgBlockTime)
}
+37 -32
View File
@@ -10,14 +10,17 @@ import (
var _ types.QueryServer = Querier{}
type Querier struct {
Keeper
}
type Querier struct{ Keeper }
func NewQuerier(keeper Keeper) Querier {
return Querier{Keeper: keeper}
}
// ╭───────────────────────────────────────────────────────────╮
// │ Fixed Query Methods │
// ╰───────────────────────────────────────────────────────────╯
// Params implements types.QueryServer.
func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
@@ -42,41 +45,16 @@ func (k Querier) Schema(goCtx context.Context, req *types.QuerySchemaRequest) (*
}, nil
}
// SyncInitial implements types.QueryServer.
func (k Querier) SyncInitial(goCtx context.Context, req *types.SyncInitialRequest) (*types.SyncInitialResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
p, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}
c, _ := k.DIDKeeper.ResolveController(ctx, req.Did)
if c == nil {
return &types.SyncInitialResponse{
Success: false,
Schema: p.Schema,
ChainID: ctx.ChainID(),
}, nil
}
return &types.SyncInitialResponse{
Success: true,
Schema: p.Schema,
ChainID: ctx.ChainID(),
Address: c.SonrAddress(),
}, nil
}
// SyncCurrent implements types.QueryServer.
func (k Querier) SyncCurrent(goCtx context.Context, req *types.SyncCurrentRequest) (*types.SyncCurrentResponse, error) {
// ctx := sdk.UnwrapSDKContext(goCtx)
return &types.SyncCurrentResponse{}, nil
}
// ╭───────────────────────────────────────────────────────────╮
// │ Pre-Authenticated Queries │
// ╰───────────────────────────────────────────────────────────╯
// Allocate implements types.QueryServer.
func (k Querier) Allocate(goCtx context.Context, req *types.AllocateRequest) (*types.AllocateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// 2.Allocate the vault msg.GetSubject(), msg.GetOrigin()
cid, expiryBlock, err := k.AssembleVault(ctx)
cid, expiryBlock, err := k.assembleVault(ctx)
if err != nil {
return nil, err
}
@@ -87,3 +65,30 @@ func (k Querier) Allocate(goCtx context.Context, req *types.AllocateRequest) (*t
ExpiryBlock: expiryBlock,
}, nil
}
// ╭───────────────────────────────────────────────────────────╮
// │ Authenticated Endpoints │
// ╰───────────────────────────────────────────────────────────╯
// Sync implements types.QueryServer.
func (k Querier) Sync(goCtx context.Context, req *types.SyncRequest) (*types.SyncResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
p, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err
}
c, _ := k.DIDKeeper.ResolveController(ctx, req.Did)
if c == nil {
return &types.SyncResponse{
Success: false,
Schema: p.Schema,
ChainID: ctx.ChainID(),
}, nil
}
return &types.SyncResponse{
Success: true,
Schema: p.Schema,
ChainID: ctx.ChainID(),
Address: c.SonrAddress(),
}, nil
}
-19
View File
@@ -3,7 +3,6 @@ package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"cosmossdk.io/errors"
@@ -28,21 +27,3 @@ func (ms msgServer) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams
return nil, ms.k.Params.Set(ctx, msg.Params)
}
// AllocateVault implements types.MsgServer.
func (ms msgServer) AllocateVault(goCtx context.Context, msg *types.MsgAllocateVault) (*types.MsgAllocateVaultResponse, error) {
// 1.Check if the service origin is valid
ctx := sdk.UnwrapSDKContext(goCtx)
// 2.Allocate the vault msg.GetSubject(), msg.GetOrigin()
cid, expiryBlock, err := ms.k.AssembleVault(ctx)
if err != nil {
return nil, err
}
// 3.Return the response
return &types.MsgAllocateVaultResponse{
ExpiryBlock: expiryBlock,
Cid: cid,
}, nil
}