Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodek committed Jan 28, 2025
1 parent ab524bc commit ae74d5d
Show file tree
Hide file tree
Showing 56 changed files with 842 additions and 1,200 deletions.
12 changes: 6 additions & 6 deletions 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.Metadata.Creator.Identifier, []*acptypes.Operation{
decision := checkAccess(ctx, client, &txBuilder, txSigner, policy.Id, record.Metadata.OwnerDid, []*acptypes.Operation{
{
Object: coretypes.NewObject("file", "readme"),
Permission: "read",
Expand Down Expand Up @@ -133,7 +133,7 @@ func getSigner(accAddr string) sdk.TxSigner {

func createPolicy(ctx context.Context, client *sdk.Client, txBuilder *sdk.TxBuilder, txSigner sdk.TxSigner) *coretypes.Policy {
msgSet := sdk.MsgSet{}
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicyNow(txSigner.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicy(txSigner.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
tx, err := txBuilder.Build(ctx, txSigner, &msgSet)
if err != nil {
log.Fatal(err)
Expand All @@ -157,14 +157,14 @@ func createPolicy(ctx context.Context, client *sdk.Client, txBuilder *sdk.TxBuil
log.Fatal(err)
}

log.Printf("policy created: %v", policyResponse.Policy.Id)
return policyResponse.Policy
log.Printf("policy created: %v", policyResponse.Record.Policy.Id)
return policyResponse.Record.Policy
}

func registerObject(ctx context.Context, client *sdk.Client, txBuilder *sdk.TxBuilder, txSigner sdk.TxSigner, policyId string) *coretypes.RelationshipRecord {
func registerObject(ctx context.Context, client *sdk.Client, txBuilder *sdk.TxBuilder, txSigner sdk.TxSigner, policyId string) *acptypes.RelationshipRecord {
msgSet := sdk.MsgSet{}
mapper := msgSet.WithDirectPolicyCmd(
acptypes.NewMsgDirectPolicyCmdNow(
acptypes.NewMsgDirectPolicyCmd(
txSigner.GetAccAddress(),
policyId,
acptypes.NewRegisterObjectCmd(coretypes.NewObject("file", "readme")),
Expand Down
14 changes: 6 additions & 8 deletions example/bearer_auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocdc "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
prototypes "github.com/cosmos/gogoproto/types"
coretypes "github.com/sourcenetwork/acp_core/pkg/types"

"github.com/sourcenetwork/sourcehub/sdk"
Expand Down Expand Up @@ -57,7 +56,7 @@ resources:
`

msgSet := sdk.MsgSet{}
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicyNow(txSigner.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicy(txSigner.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
tx, err := txBuilder.Build(ctx, txSigner, &msgSet)
if err != nil {
log.Fatal(err)
Expand All @@ -81,7 +80,7 @@ resources:
log.Fatal(err)
}

log.Printf("policy created: %v", policyResponse.Policy.Id)
log.Printf("policy created: %v", policyResponse.Record.Policy.Id)

alice, signer, err := did.ProduceDID()
if err != nil {
Expand All @@ -99,11 +98,10 @@ resources:
log.Printf("alice's JWS: %v", jws)

bearerCmd := acptypes.MsgBearerPolicyCmd{
Creator: txSigner.GetAccAddress(),
BearerToken: jws,
PolicyId: policyResponse.Policy.Id,
Cmd: acptypes.NewRegisterObjectCmd(coretypes.NewObject("resource", "foo")),
CreationTime: prototypes.TimestampNow(),
Creator: txSigner.GetAccAddress(),
BearerToken: jws,
PolicyId: policyResponse.Record.Policy.Id,
Cmd: acptypes.NewRegisterObjectCmd(coretypes.NewObject("resource", "foo")),
}

log.Printf("Bearer Cmd: %v", bearerCmd)
Expand Down
4 changes: 2 additions & 2 deletions example/sdk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resources:
`

msgSet := sdk.MsgSet{}
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicyNow(signer.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
policyMapper := msgSet.WithCreatePolicy(acptypes.NewMsgCreatePolicy(signer.GetAccAddress(), policy, coretypes.PolicyMarshalingType_SHORT_YAML))
tx, err := txBuilder.Build(ctx, signer, &msgSet)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -92,7 +92,7 @@ resources:
log.Fatal(err)
}
cmdBuilder.PolicyCmd(acptypes.NewRegisterObjectCmd(coretypes.NewObject("resource", "readme.txt")))
cmdBuilder.PolicyID(policyResponse.Policy.Id)
cmdBuilder.PolicyID(policyResponse.Record.Policy.Id)
cmdBuilder.Actor(aliceDid)
cmdBuilder.SetSigner(aliceSigner)
jws, err := cmdBuilder.BuildJWS(ctx)
Expand Down
3 changes: 2 additions & 1 deletion proto/sourcehub/acp/access_decision.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sourcehub.acp;
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "sourcenetwork/acp_core/relationship.proto";
import "sourcehub/acp/time.proto";

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

Expand All @@ -23,7 +24,7 @@ message AccessDecision {

DecisionParams params = 7; // used as part of id generation

google.protobuf.Timestamp creation_time = 8;
Timestamp creation_time = 8;

// issued_height stores the block height when the Decision was evaluated
uint64 issued_height = 9;
Expand Down
11 changes: 2 additions & 9 deletions proto/sourcehub/acp/policy_cmd.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,9 @@ message RevealRegistrationCmd {
RegistrationProof proof = 2;
}

enum RevealRegistrationResult {
NO_OP = 0;
CREATED = 1;
AMENDED = 2;
}

message RevealRegistrationCmdResult {
RevealRegistrationResult result = 1;
RelationshipRecord record = 2;
ObjectRegistrationEvent event = 3;
RelationshipRecord record = 1;
ObjectRegistrationEvent event = 2;
}

message FlagHijackAttemptCmd {
Expand Down
6 changes: 1 addition & 5 deletions proto/sourcehub/acp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ message MsgCreatePolicy {
string creator = 1;
string policy = 2;
sourcenetwork.acp_core.PolicyMarshalingType marshal_type = 3;
google.protobuf.Timestamp creation_time = 4;
}

message MsgCreatePolicyResponse {
Expand All @@ -87,8 +86,7 @@ message MsgCheckAccess {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
string policy_id = 2;
google.protobuf.Timestamp creation_time = 3;
AccessRequest access_request = 4;
AccessRequest access_request = 3;
}

message MsgCheckAccessResponse {
Expand Down Expand Up @@ -116,7 +114,6 @@ message MsgBearerPolicyCmd {
string bearer_token = 2;
string policy_id = 3;
PolicyCmd cmd = 4;
google.protobuf.Timestamp creation_time = 5;
}

message MsgBearerPolicyCmdResponse {
Expand All @@ -128,7 +125,6 @@ message MsgDirectPolicyCmd {
string creator = 1;
string policy_id = 2;
PolicyCmd cmd = 3;
google.protobuf.Timestamp creation_time = 4;
}

message MsgDirectPolicyCmdResponse {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ name: test policy
`
msgSet := sdk.MsgSet{}
mapper := msgSet.WithCreatePolicy(
types.NewMsgCreatePolicyNow(
types.NewMsgCreatePolicy(
network.GetValidatorAddr(),
policy,
coretypes.PolicyMarshalingType_SHORT_YAML,
Expand Down
33 changes: 14 additions & 19 deletions tests/integration/acp/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
coretypes "github.com/sourcenetwork/acp_core/pkg/types"
"github.com/stretchr/testify/require"

"github.com/sourcenetwork/sourcehub/x/acp/registration"
"github.com/sourcenetwork/sourcehub/x/acp/commitment"
"github.com/sourcenetwork/sourcehub/x/acp/types"
)

Expand All @@ -17,24 +17,18 @@ type CreatePolicyAction struct {

func (a *CreatePolicyAction) Run(ctx *TestCtx) *coretypes.Policy {
msg := &types.MsgCreatePolicy{
Policy: a.Policy,
Creator: a.Creator.SourceHubAddr,
MarshalType: coretypes.PolicyMarshalingType_SHORT_YAML,
CreationTime: TimeToProto(ctx.Timestamp),
Policy: a.Policy,
Creator: a.Creator.SourceHubAddr,
MarshalType: coretypes.PolicyMarshalingType_SHORT_YAML,
}
response, err := ctx.Executor.CreatePolicy(ctx, msg)

var expected any = nil
if a.Expected != nil {
expected = &types.MsgCreatePolicyResponse{
Policy: a.Expected,
}
}
AssertResults(ctx, response, expected, err, a.ExpectedErr)
AssertError(ctx, err, a.ExpectedErr)
if response != nil {
require.Equal(ctx.T, a.Expected, response.Record.Policy)
ctx.State.PolicyCreator = a.Creator.SourceHubAddr
ctx.State.PolicyId = response.Policy.Id
return response.Policy
ctx.State.PolicyId = response.Record.Policy.Id
return response.Record.Policy
}
return nil
}
Expand All @@ -47,13 +41,14 @@ type SetRelationshipAction struct {
ExpectedErr error
}

func (a *SetRelationshipAction) Run(ctx *TestCtx) *coretypes.RelationshipRecord {
func (a *SetRelationshipAction) Run(ctx *TestCtx) *types.RelationshipRecord {
cmd := types.NewSetRelationshipCmd(a.Relationship)
result, err := dispatchPolicyCmd(ctx, a.PolicyId, a.Actor, cmd)
got := (*types.SetRelationshipCmdResult)(nil)
if result != nil {
got = result.GetSetRelationshipResult()
}

AssertResults(ctx, got, a.Expected, err, a.ExpectedErr)
if got != nil {
return got.Record
Expand All @@ -69,7 +64,7 @@ type RegisterObjectAction struct {
ExpectedErr error
}

func (a *RegisterObjectAction) Run(ctx *TestCtx) *coretypes.RelationshipRecord {
func (a *RegisterObjectAction) Run(ctx *TestCtx) *types.RelationshipRecord {
cmd := types.NewRegisterObjectCmd(a.Object)
result, err := dispatchPolicyCmd(ctx, a.PolicyId, a.Actor, cmd)
got := (*types.RegisterObjectCmdResult)(nil)
Expand Down Expand Up @@ -209,7 +204,7 @@ func (a *PolicySetupAction) Run(ctx *TestCtx) {

type GetPolicyAction struct {
Id string
Expected *types.QueryPolicyResponse
Expected *types.PolicyRecord
ExpectedErr error
}

Expand All @@ -233,7 +228,7 @@ type CommitRegistrationsAction struct {

func (a *CommitRegistrationsAction) Run(ctx *TestCtx) *types.RegistrationsCommitment {
actor := coretypes.NewActor(a.Actor.DID)
commitment, err := registration.GenerateCommitmentWithoutValidation(a.PolicyId, actor, a.Objects)
commitment, err := commitment.GenerateCommitmentWithoutValidation(a.PolicyId, actor, a.Objects)
require.NoError(ctx.T, err)
cmd := types.NewCommitRegistrationCmd(commitment)
result, err := dispatchPolicyCmd(ctx, a.PolicyId, a.Actor, cmd)
Expand All @@ -260,7 +255,7 @@ type RevealRegistrationAction struct {

func (a *RevealRegistrationAction) Run(ctx *TestCtx) *types.RevealRegistrationCmdResult {
actor := coretypes.NewActor(a.Actor.DID)
proof, err := registration.ProofForObject(a.PolicyId, actor, a.Index, a.Objects)
proof, err := commitment.ProofForObject(a.PolicyId, actor, a.Index, a.Objects)
require.NoError(ctx.T, err)
cmd := types.NewRevealRegistrationCmd(a.CommitmentId, proof)
result, err := dispatchPolicyCmd(ctx, a.PolicyId, a.Actor, cmd)
Expand Down
68 changes: 50 additions & 18 deletions tests/integration/acp/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"testing"
"time"

prototypes "github.com/cosmos/gogoproto/types"
"github.com/stretchr/testify/require"

"github.com/sourcenetwork/sourcehub/x/acp/signed_policy_cmd"
"github.com/sourcenetwork/sourcehub/x/acp/testutil"
"github.com/sourcenetwork/sourcehub/x/acp/types"
"github.com/sourcenetwork/sourcehub/x/acp/utils"
)

var DefaultTs = MustDateTimeToProto("2024-01-01 00:00:00")
Expand All @@ -29,21 +31,32 @@ func NewTestCtxFromConfig(t *testing.T, config TestConfig) *TestCtx {
}
executor := NewExecutor(t, config.ExecutorStrategy, params)

logicalClk := &logicalClockImpl{}
height, err := logicalClk.GetTimestampNow(context.TODO())
require.NoError(t, err)

tx := GenRandomTx(t)
root := MustNewSourceHubActorFromName("root")
ctx := &TestCtx{
Ctx: context.TODO(),
T: t,
TxSigner: root,
Timestamp: time.Now(),
TokenIssueTs: time.Now(),
Ctx: context.TODO(),
T: t,
TxSigner: root,
TokenIssueTs: time.Now(),
TokenIssueProtoTs: prototypes.TimestampNow(),
ExecutionTs: &types.Timestamp{
ProtoTs: prototypes.TimestampNow(),
BlockHeight: height,
},
Executor: executor,
Strategy: config.AuthStrategy,
ActorType: config.ActorType,
LogicalClock: &logicalClockImpl{},
LogicalClock: logicalClk,
Params: params,
Tx: tx,
TxHash: utils.HashTx(tx),
}

_, err := executor.GetOrCreateAccountFromActor(ctx, root)
_, err = executor.GetOrCreateAccountFromActor(ctx, root)
require.NoError(t, err)

return ctx
Expand All @@ -54,17 +67,18 @@ type TestCtx struct {
T *testing.T
State TestState
// Signer for Txs while running tests under Bearer or Signed Auth modes
TxSigner *TestActor
// Timestamp used to generate Msgs in Test
Timestamp time.Time
TokenIssueTs time.Time
Executor MsgExecutor
Strategy AuthenticationStrategy
AccountKeeper *testutil.AccountKeeperStub
ActorType ActorKeyType
LogicalClock signed_policy_cmd.LogicalClock
TxHash string
Params types.Params
TxSigner *TestActor
ExecutionTs *types.Timestamp
TokenIssueTs time.Time
TokenIssueProtoTs *prototypes.Timestamp
Executor MsgExecutor
Strategy AuthenticationStrategy
AccountKeeper *testutil.AccountKeeperStub
ActorType ActorKeyType
LogicalClock signed_policy_cmd.LogicalClock
Tx []byte
TxHash []byte
Params types.Params
}

func NewTestCtx(t *testing.T) *TestCtx {
Expand Down Expand Up @@ -100,6 +114,24 @@ func (c *TestCtx) GetSourceHubAccount(alias string) *TestActor {
return acc
}

func (c *TestCtx) GetRecordMetadataForActor(actor string) *types.RecordMetadata {
return &types.RecordMetadata{
CreationTs: c.ExecutionTs,
TxHash: c.TxHash,
TxSigner: c.TxSigner.SourceHubAddr,
OwnerDid: c.GetActor(actor).DID,
}
}

func (c *TestCtx) GetRootRecordMetadata() *types.RecordMetadata {
return &types.RecordMetadata{
CreationTs: c.ExecutionTs,
TxHash: c.TxHash,
TxSigner: c.TxSigner.SourceHubAddr,
OwnerDid: c.TxSigner.DID,
}
}

func (c *TestCtx) GetParams() types.Params {
return types.NewParams()
}
Expand Down
Loading

0 comments on commit ae74d5d

Please sign in to comment.