Skip to content

Commit

Permalink
metdata refactor wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodek committed Dec 19, 2024
1 parent 4bab1a2 commit 1d5db83
Show file tree
Hide file tree
Showing 29 changed files with 1,895 additions and 454 deletions.
2 changes: 1 addition & 1 deletion example/access_ticket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func runDemo(chainId string, nodeAddr string, validatorKeyName string) {
record := registerObject(ctx, client, &txBuilder, txSigner, policy.Id)

log.Printf("Evaluating Access Request to read file:readme")
decision := checkAccess(ctx, client, &txBuilder, txSigner, policy.Id, record.OwnerDid, []*acptypes.Operation{
decision := checkAccess(ctx, client, &txBuilder, txSigner, policy.Id, record.Metadata.Creator.Identifier, []*acptypes.Operation{
{
Object: coretypes.NewObject("file", "readme"),
Permission: "read",
Expand Down
9 changes: 5 additions & 4 deletions proto/sourcehub/acp/policy_cmd.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "google/protobuf/timestamp.proto";
import "sourcenetwork/acp_core/relationship.proto";
import "sourcehub/acp/commitment.proto";
import "sourcehub/acp/registration.proto";
import "sourcehub/acp/record.proto";

option go_package = "github.com/sourcenetwork/sourcehub/x/acp/types";

Expand Down Expand Up @@ -104,7 +105,7 @@ enum RegistrationResultStatus {

message RevealRegistrationCmdResult {
RegistrationResultStatus result = 1;
sourcenetwork.acp_core.RelationshipRecord record = 2;
RelationshipRecord record = 2;
ObjectRegistrationEvent event = 3;
}

Expand All @@ -121,7 +122,7 @@ message UnarchiveObjectCmd{
}

message UnarchiveObjectCmdResult{
sourcenetwork.acp_core.RelationshipRecord record = 1;
RelationshipRecord record = 1;
bool relationship_modified = 2;
}

Expand All @@ -145,7 +146,7 @@ message ArchiveObjectCmd {
message SetRelationshipCmdResult {
// Indicates whether the given Relationship previously existed, ie the Tx was a no op
bool record_existed = 1;
sourcenetwork.acp_core.RelationshipRecord record = 2;
RelationshipRecord record = 2;
}

// DeleteRelationshipCmdResult removes a Relationship in a Policy
Expand All @@ -155,7 +156,7 @@ message DeleteRelationshipCmdResult {

// RegisterObjectCmdResult registers an Object in a Policy
message RegisterObjectCmdResult {
sourcenetwork.acp_core.RelationshipRecord record = 1;
RelationshipRecord record = 1;
}

// ArchiveObjectCmdResult unregisters an Object in a Policy
Expand Down
14 changes: 3 additions & 11 deletions proto/sourcehub/acp/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "google/api/annotations.proto";
import "sourcehub/acp/params.proto";
import "sourcehub/acp/access_decision.proto";
import "sourcehub/acp/registration.proto";
import "sourcehub/acp/record.proto";
import "sourcehub/acp/commitment.proto";
import "sourcenetwork/acp_core/policy.proto";
import "sourcenetwork/acp_core/policy_short.proto";
Expand Down Expand Up @@ -114,23 +115,14 @@ message QueryPolicyRequest {
}

message QueryPolicyResponse {
sourcenetwork.acp_core.Policy policy = 1;

// policy_raw contains the raw policy document the user submitted to create
// the policy
string policy_raw = 2;

// marshal_type flags the format of policy_raw
sourcenetwork.acp_core.PolicyMarshalingType marshal_type = 3;
PolicyRecord record = 1;
}

message QueryPolicyIdsRequest {

//cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryPolicyIdsResponse {

//cosmos.base.query.v1beta1.PageResponse pagination = 1;
repeated string ids = 1;
}
Expand All @@ -141,7 +133,7 @@ message QueryFilterRelationshipsRequest {
}

message QueryFilterRelationshipsResponse {
repeated sourcenetwork.acp_core.RelationshipRecord records = 1;
repeated RelationshipRecord records = 1;
}

message QueryVerifyAccessRequestRequest {
Expand Down
36 changes: 36 additions & 0 deletions proto/sourcehub/acp/record.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
syntax = "proto3";

package sourcehub.acp;

import "amino/amino.proto";
import "cosmos/msg/v1/msg.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "sourcehub/acp/access_decision.proto";
import "sourcehub/acp/time.proto";
import "sourcenetwork/acp_core/policy_short.proto";
import "sourcenetwork/acp_core/policy.proto";
import "sourcenetwork/acp_core/relationship.proto";

option go_package = "github.com/sourcenetwork/sourcehub/x/acp/types";

message RecordMetadata {
Timestamp creation_ts = 1;
bytes tx_hash = 2;
string tx_signer = 3;
string owner = 4;
}

message PolicyRecord {
sourcenetwork.acp_core.Policy policy = 1;
RecordMetadata metadata = 2;
string raw_policy = 3;
// marshal_type flags the format of policy_raw
sourcenetwork.acp_core.PolicyMarshalingType marshal_type = 4;
}

message RelationshipRecord {
sourcenetwork.acp_core.Relationship relationship = 1;
RecordMetadata metadata = 2;
}
3 changes: 2 additions & 1 deletion proto/sourcehub/acp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "google/protobuf/timestamp.proto";
import "sourcehub/acp/access_decision.proto";
import "sourcehub/acp/params.proto";
import "sourcehub/acp/policy_cmd.proto";
import "sourcehub/acp/record.proto";
import "sourcenetwork/acp_core/policy_short.proto";
import "sourcenetwork/acp_core/policy.proto";

Expand Down Expand Up @@ -79,7 +80,7 @@ message MsgCreatePolicy {
}

message MsgCreatePolicyResponse {
sourcenetwork.acp_core.Policy policy = 1;
PolicyRecord record = 1;
}

message MsgCheckAccess {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/acp/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (a *CommitRegistrationsAction) Run(ctx *TestCtx) *types.RegistrationsCommit

type RevealRegistrationAction struct {
PolicyId string
CommitmentId string
CommitmentId uint64
Objects []*coretypes.Object
Index int
Actor *TestActor
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/acp/suite/object/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/sourcenetwork/acp_core/pkg/errors"
coretypes "github.com/sourcenetwork/acp_core/pkg/types"

prototypes "github.com/cosmos/gogoproto/types"

test "github.com/sourcenetwork/sourcehub/tests/integration/acp"
"github.com/sourcenetwork/sourcehub/x/acp/types"
)
Expand Down Expand Up @@ -41,12 +39,14 @@ func TestRegisterObject_RegisteringNewObjectIsSucessful(t *testing.T) {
Expected: &types.RegisterObjectCmdResult{
Record: &coretypes.RelationshipRecord{
PolicyId: ctx.State.PolicyId,
OwnerDid: bob.DID,
Relationship: coretypes.NewActorRelationship("resource", "foo", "owner", bob.DID),
Archived: false,
CreationTime: &prototypes.Timestamp{
Seconds: -62135596800,
Nanos: 0,
Metadata: &coretypes.RecordMetadata{
Creator: &coretypes.Principal{
Kind: coretypes.PrincipalKind_DID,
Identifier: bob.DID,
},
CreationTs: test.TimeToProto(ctx.Timestamp),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestRevealRegistration_ObjectRegisteredAfterCommitment_RegistrationAmended(
result := a.Run(ctx)

require.Equal(ctx.T, result.Event.Type, types.ObjectRegistrationEventType_AMENDMENT)
require.Equal(ctx.T, result.Record.OwnerDid, ctx.GetActor("bob").DID)
require.Equal(ctx.T, result.Record.Metadata.Creator, ctx.GetActor("bob").DID)
require.Equal(ctx.T, result.Record.Relationship, coretypes.NewActorRelationship("file", "foo.txt", "owner", ctx.GetActor("bob").DID))
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestRevealRegistration_ObjectRegisteredThroughNewerCommitment_RegistrationI

// Then Bob is the owner of foo.txt
require.Equal(ctx.T, result.Event.Type, types.ObjectRegistrationEventType_AMENDMENT)
require.Equal(ctx.T, result.Record.OwnerDid, ctx.GetActor("bob").DID)
require.Equal(ctx.T, result.Record.Metadata.Creator, ctx.GetActor("bob").DID)
require.Equal(ctx.T, result.Record.Relationship, coretypes.NewActorRelationship("file", "foo.txt", "owner", ctx.GetActor("bob").DID))
}

Expand Down
7 changes: 3 additions & 4 deletions tests/integration/acp/suite/policy/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ actor:
doc: my actor
`
want := &coretypes.Policy{
Id: "d011372c7e2cd34fd63777c513bb5eb16713834b855f424158474b77c1800410",
Name: "policy",
Description: "ok",
CreationTime: test.TimeToProto(ctx.Timestamp),
Id: "d011372c7e2cd34fd63777c513bb5eb16713834b855f424158474b77c1800410",
Name: "policy",
Description: "ok",
Resources: []*coretypes.Resource{
&coretypes.Resource{
Name: "file",
Expand Down
19 changes: 14 additions & 5 deletions tests/integration/acp/suite/relationship/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func setupSetRel(t *testing.T) *test.TestCtx {
},
}
action.Run(ctx)

return ctx
}

Expand All @@ -57,8 +56,13 @@ func TestSetRelationship_OwnerCanShareObjectTheyOwn(t *testing.T) {
Expected: &types.SetRelationshipCmdResult{
RecordExisted: false,
Record: &coretypes.RelationshipRecord{
OwnerDid: ctx.GetActor("alice").DID,
CreationTime: test.TimeToProto(ctx.Timestamp),
Metadata: &coretypes.RecordMetadata{
Creator: &coretypes.Principal{
Kind: coretypes.PrincipalKind_DID,
Identifier: ctx.GetActor("alice").DID,
},
CreationTs: test.TimeToProto(ctx.Timestamp),
},
PolicyId: ctx.State.PolicyId,
Relationship: coretypes.NewActorRelationship("file", "foo", "reader", bob),
Archived: false,
Expand Down Expand Up @@ -118,8 +122,13 @@ func TestSetRelationship_ManagerActorCanDelegateAccessToAnotherActor(t *testing.
Expected: &types.SetRelationshipCmdResult{
RecordExisted: false,
Record: &coretypes.RelationshipRecord{
OwnerDid: bob,
CreationTime: test.TimeToProto(ctx.Timestamp),
Metadata: &coretypes.RecordMetadata{
Creator: &coretypes.Principal{
Kind: coretypes.PrincipalKind_DID,
Identifier: bob,
},
CreationTs: test.TimeToProto(ctx.Timestamp),
},
PolicyId: ctx.State.PolicyId,
Relationship: coretypes.NewActorRelationship("file", "foo", "reader", charlie),
Archived: false,
Expand Down
11 changes: 9 additions & 2 deletions x/acp/client/cli/policy_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"encoding/hex"
"fmt"
"strconv"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/gogoproto/jsonpb"
Expand Down Expand Up @@ -112,7 +113,10 @@ func CmdRevealRegistration(dispatcher dispatcher) *cobra.Command {
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
policyId := args[0]
commitId := args[1]
commitId, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("invalid commitId: %w", err)
}
proofJson := args[2]

proof := &types.RegistrationProof{}
Expand Down Expand Up @@ -167,7 +171,10 @@ func CmdFlagHijack(dispatcher dispatcher) *cobra.Command {
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
polId := args[0]
eventId := args[1]
eventId, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("invalid event id: %w", err)
}

polCmd := types.NewFlagHijackAttemptCmd(eventId)

Expand Down
23 changes: 23 additions & 0 deletions x/acp/keeper/acp_core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
prototypes "github.com/cosmos/gogoproto/types"
"github.com/sourcenetwork/acp_core/pkg/runtime"
)

var _ runtime.TimeService = (*SourcehubTimeProvider)(nil)

type SourcehubTimeProvider struct{}

func (p *SourcehubTimeProvider) GetNow(goCtx context.Context) (*prototypes.Timestamp, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
time := ctx.BlockTime()
ts, err := prototypes.TimestampProto(time)
if err != nil {
return nil, err
}
return ts, nil
}
29 changes: 19 additions & 10 deletions x/acp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
acpruntime "github.com/sourcenetwork/acp_core/pkg/runtime"
"github.com/sourcenetwork/acp_core/pkg/services"
coretypes "github.com/sourcenetwork/acp_core/pkg/types"
"github.com/sourcenetwork/raccoondb/v2/primitives"

"github.com/sourcenetwork/sourcehub/x/acp/access_decision"
"github.com/sourcenetwork/sourcehub/x/acp/registration"
Expand Down Expand Up @@ -79,6 +80,7 @@ func (k *Keeper) GetACPEngine(ctx sdk.Context) (coretypes.ACPEngineServer, error
raccoonAdapted := stores.RaccoonKVFromCosmos(adapted)
runtime, err := acpruntime.NewRuntimeManager(
acpruntime.WithKVStore(raccoonAdapted),
acpruntime.WithTimeService(&SourcehubTimeProvider{}),
)
if err != nil {
return nil, err
Expand All @@ -88,17 +90,24 @@ func (k *Keeper) GetACPEngine(ctx sdk.Context) (coretypes.ACPEngineServer, error
}

func (k *Keeper) GetRegistrationsCommitmentRepository(ctx sdk.Context) registration.CommitmentRepository {
kv := k.storeService.OpenKVStore(ctx)
prefixKey := []byte(types.RegistrationsCommitmentPrefix)
adapted := runtime.KVStoreAdapter(kv)
adapted = prefix.NewStore(adapted, prefixKey)
return registration.NewKVRegistrationRepository(adapted)
cmtkv := k.storeService.OpenKVStore(ctx)
kv := stores.NewRaccoonKV(cmtkv)
kv = primitives.NewPrefixedKV(kv, []byte(types.RegistrationsCommitmentPrefix))
repo, err := registration.NewKVRegistrationRepository(kv)
if err != nil {
panic(err)
}
return repo
}

func (k *Keeper) GetObjectEventRepository(ctx sdk.Context) registration.RegistrationEventRepository {
kv := k.storeService.OpenKVStore(ctx)
prefixKey := []byte(types.ObjectEventsPreix)
adapted := runtime.KVStoreAdapter(kv)
adapted = prefix.NewStore(adapted, prefixKey)
return registration.NewObjectEventRepository(adapted)
cmtkv := k.storeService.OpenKVStore(ctx)
kv := stores.NewRaccoonKV(cmtkv)
kv = primitives.NewPrefixedKV(kv, []byte(types.ObjectEventsPrefix))
repo, err := registration.NewObjectEventRepository(kv)
if err != nil {
panic(err)
// TODO not sure how to best handle this, don't think the chain should continue
}
return repo
}
2 changes: 1 addition & 1 deletion x/acp/keeper/msg_server_check_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k msgServer) CheckAccess(goCtx context.Context, msg *types.MsgCheckAccess)
}

cmd := access_decision.EvaluateAccessRequestsCommand{
Policy: record.Policy,
Policy: record.Record.Policy,
Operations: msg.AccessRequest.Operations,
Actor: msg.AccessRequest.Actor.Id,
CreationTime: msg.CreationTime,
Expand Down
Loading

0 comments on commit 1d5db83

Please sign in to comment.