From 97465bc0c5e2b77efb8ca18375a4c6b040957e54 Mon Sep 17 00:00:00 2001 From: Brian Meek Date: Mon, 20 May 2024 15:37:56 -0700 Subject: [PATCH] Revert "Enforce rules entitlement checks on isEntitledToSpace" (#9) Reverts river-build/river#7 There's an issue in this change running go mod tidy I missed by not building the docker image. Will resubmit after I fix it. --- core/Dockerfile | 2 +- core/node/auth/auth_impl.go | 105 +++++------------- core/node/auth/auth_impl_cache.go | 5 +- core/node/auth/auth_impl_cache_test.go | 8 +- core/node/auth/fake_auth.go | 4 +- core/node/auth/space_contract.go | 5 +- core/node/auth/space_contract_v3.go | 5 +- core/node/cmd/root_cmd.go | 1 - core/node/config/config.go | 69 +----------- core/node/default_config.yaml | 16 --- core/node/rpc/add_event.go | 2 +- core/node/rpc/create_stream.go | 2 +- core/node/run_impl.sh | 3 - .../client_simulator/client_simulator.go | 37 +++--- core/xchain/cmd/root_cmd.go | 16 +-- core/xchain/config/config.go | 88 +++++++++++++++ core/xchain/contracts/contracts.go | 3 +- core/xchain/create_multi.sh | 35 +++--- core/xchain/default_config.yaml | 34 ++++++ core/xchain/entitlement/check_operation.go | 3 +- core/xchain/entitlement/client_pool.go | 3 +- core/xchain/entitlement/entitlement.go | 7 +- core/xchain/entitlement/entitlement_test.go | 3 +- core/xchain/server/server.go | 6 +- core/xchain/server/server_test.go | 60 +++++----- 25 files changed, 240 insertions(+), 282 deletions(-) create mode 100644 core/xchain/config/config.go create mode 100644 core/xchain/default_config.yaml diff --git a/core/Dockerfile b/core/Dockerfile index 09c639171..a524628df 100644 --- a/core/Dockerfile +++ b/core/Dockerfile @@ -48,7 +48,7 @@ COPY --from=builder /bin/xchain_node /usr/bin/xchain_node RUN setcap 'cap_net_bind_service=+ep' /usr/bin/stream_node COPY --from=builder /build/node/node/default_config.yaml /riveruser/stream_node/config/config.yaml -COPY --from=builder /build/node/node/default_config.yaml /riveruser/xchain_node/config/config.yaml +COPY --from=builder /build/xchain/xchain/default_config.yaml /riveruser/xchain_node/config/config.yaml RUN mkdir -p /riveruser/stream_node/logs RUN mkdir -p /riveruser/xchain_node/logs diff --git a/core/node/auth/auth_impl.go b/core/node/auth/auth_impl.go index a86ad68c1..2bff4af46 100644 --- a/core/node/auth/auth_impl.go +++ b/core/node/auth/auth_impl.go @@ -3,7 +3,6 @@ package auth import ( "context" "fmt" - "strings" "sync" "time" @@ -14,13 +13,12 @@ import ( "github.com/river-build/river/core/node/infra" . "github.com/river-build/river/core/node/protocol" "github.com/river-build/river/core/node/shared" - "github.com/river-build/river/core/xchain/entitlement" "github.com/ethereum/go-ethereum/common" ) type ChainAuth interface { - IsEntitled(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) error + IsEntitled(ctx context.Context, args *ChainAuthArgs) error } var everyone = common.HexToAddress("0x1") // This represents an Ethereum address of "0x1" @@ -59,12 +57,11 @@ const ( ) type ChainAuthArgs struct { - kind chainAuthKind - spaceId shared.StreamId - channelId shared.StreamId - principal common.Address - permission Permission - linkedWallets string // a serialized list of linked wallets to comply with the cache key constraints + kind chainAuthKind + spaceId shared.StreamId + channelId shared.StreamId + principal common.Address + permission Permission } // Replaces principal with given wallet and returns new copy of args. @@ -74,19 +71,6 @@ func (args *ChainAuthArgs) withWallet(wallet common.Address) *ChainAuthArgs { return &ret } -func (args *ChainAuthArgs) withLinkedWallets(linkedWallets []common.Address) *ChainAuthArgs { - ret := *args - var builder strings.Builder - for i, addr := range linkedWallets { - if i > 0 { - builder.WriteString(",") - } - builder.WriteString(addr.Hex()) - } - ret.linkedWallets = builder.String() - return &ret -} - func newArgsForEnabledSpace(spaceId shared.StreamId) *ChainAuthArgs { return &ChainAuthArgs{ kind: chainAuthKindSpaceEnabled, @@ -179,11 +163,10 @@ func NewChainAuth( }, nil } -func (ca *chainAuth) IsEntitled(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) error { +func (ca *chainAuth) IsEntitled(ctx context.Context, args *ChainAuthArgs) error { // TODO: counter for cache hits here? result, _, err := ca.entitlementCache.executeUsingCache( ctx, - cfg, args, ca.checkEntitlement, ) @@ -207,29 +190,28 @@ func (ca *chainAuth) IsEntitled(ctx context.Context, cfg *config.Config, args *C return nil } -func (ca *chainAuth) isWalletEntitled(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (bool, error) { +func (ca *chainAuth) isWalletEntitled(ctx context.Context, args *ChainAuthArgs) (bool, error) { log := dlog.FromCtx(ctx) if args.kind == chainAuthKindSpace { log.Debug("isWalletEntitled", "kind", "space", "args", args) - return ca.isEntitledToSpace(ctx, cfg, args) + return ca.isEntitledToSpace(ctx, args) } else if args.kind == chainAuthKindChannel { log.Debug("isWalletEntitled", "kind", "channel", "args", args) - return ca.isEntitledToChannel(ctx, cfg, args) + return ca.isEntitledToChannel(ctx, args) } else { return false, RiverError(Err_INTERNAL, "Unknown chain auth kind").Func("isWalletEntitled") } } -func (ca *chainAuth) isSpaceEnabledUncached(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (CacheResult, error) { +func (ca *chainAuth) isSpaceEnabledUncached(ctx context.Context, args *ChainAuthArgs) (CacheResult, error) { // This is awkward as we want enabled to be cached for 15 minutes, but the API returns the inverse isDisabled, err := ca.spaceContract.IsSpaceDisabled(ctx, args.spaceId) return &boolCacheResult{allowed: !isDisabled}, err } -func (ca *chainAuth) checkSpaceEnabled(ctx context.Context, cfg *config.Config, spaceId shared.StreamId) error { +func (ca *chainAuth) checkSpaceEnabled(ctx context.Context, spaceId shared.StreamId) error { isEnabled, cacheHit, err := ca.entitlementCache.executeUsingCache( ctx, - cfg, newArgsForEnabledSpace(spaceId), ca.isSpaceEnabledUncached, ) @@ -249,7 +231,7 @@ func (ca *chainAuth) checkSpaceEnabled(ctx context.Context, cfg *config.Config, } } -func (ca *chainAuth) isChannelEnabledUncached(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (CacheResult, error) { +func (ca *chainAuth) isChannelEnabledUncached(ctx context.Context, args *ChainAuthArgs) (CacheResult, error) { // This is awkward as we want enabled to be cached for 15 minutes, but the API returns the inverse isDisabled, err := ca.spaceContract.IsChannelDisabled(ctx, args.spaceId, args.channelId) return &boolCacheResult{allowed: !isDisabled}, err @@ -257,13 +239,11 @@ func (ca *chainAuth) isChannelEnabledUncached(ctx context.Context, cfg *config.C func (ca *chainAuth) checkChannelEnabled( ctx context.Context, - cfg *config.Config, spaceId shared.StreamId, channelId shared.StreamId, ) error { isEnabled, cacheHit, err := ca.entitlementCache.executeUsingCache( ctx, - cfg, newArgsForEnabledChannel(spaceId, channelId), ca.isChannelEnabledUncached, ) @@ -300,7 +280,6 @@ func (scr *entitlementCacheResult) IsAllowed() bool { // If the call fails or the space is not found, the allowed flag is set to false so the negative caching time applies. func (ca *chainAuth) getSpaceEntitlementsForPermissionUncached( ctx context.Context, - cfg *config.Config, args *ChainAuthArgs, ) (CacheResult, error) { log := dlog.FromCtx(ctx) @@ -322,21 +301,11 @@ func (ca *chainAuth) getSpaceEntitlementsForPermissionUncached( return &entitlementCacheResult{allowed: true, entitlementData: entitlementData, owner: owner}, nil } -func deserializeWallets(serialized string) []common.Address { - addressStrings := strings.Split(serialized, ",") - linkedWallets := make([]common.Address, len(addressStrings)) - for i, addrStr := range addressStrings { - linkedWallets[i] = common.HexToAddress(addrStr) - } - return linkedWallets -} - -func (ca *chainAuth) isEntitledToSpaceUncached(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (CacheResult, error) { +func (ca *chainAuth) isEntitledToSpaceUncached(ctx context.Context, args *ChainAuthArgs) (CacheResult, error) { log := dlog.FromCtx(ctx) log.Debug("isEntitledToSpaceUncached", "args", args) result, cacheHit, err := ca.entitlementManagerCache.executeUsingCache( ctx, - cfg, args, ca.getSpaceEntitlementsForPermissionUncached, ) @@ -364,25 +333,12 @@ func (ca *chainAuth) isEntitledToSpaceUncached(ctx context.Context, cfg *config. entitlementData := temp.(*entitlementCacheResult) // Assuming result is of *entitlementCacheResult type log.Debug("entitlementData", "args", args, "entitlementData", entitlementData) - for _, ent := range entitlementData.entitlementData { - log.Debug("entitlement", "entitlement", ent) - if ent.entitlementType == "RuleEntitlement" { - re := ent.ruleEntitlement - log.Debug("RuleEntitlement", "ruleEntitlement", re) - result, err := entitlement.EvaluateRuleData(ctx, cfg, deserializeWallets(args.linkedWallets), re) - - if err != nil { - return &boolCacheResult{allowed: false}, AsRiverError(err).Func("isEntitledToSpace") - } - if result { - log.Debug("rule entitlement is true", "spaceId", args.spaceId) - return &boolCacheResult{allowed: true}, nil - } else { - log.Debug("rule entitlement is false", "spaceId", args.spaceId) - return &boolCacheResult{allowed: false}, nil - } - } else if ent.entitlementType == "UserEntitlement" { - for _, user := range ent.userEntitlement { + for _, entitlement := range entitlementData.entitlementData { + log.Debug("entitlement", "entitlement", entitlement) + if entitlement.entitlementType == "RuleEntitlement" { + // TODO implement rule entitlment + } else if entitlement.entitlementType == "UserEntitlement" { + for _, user := range entitlement.userEntitlement { if user == everyone { log.Debug("everyone is entitled to space", "spaceId", args.spaceId) return &boolCacheResult{allowed: true}, nil @@ -392,19 +348,19 @@ func (ca *chainAuth) isEntitledToSpaceUncached(ctx context.Context, cfg *config. } } } else { - log.Warn("Invalid entitlement type", "entitlement", ent) + log.Warn("Invalid entitlement type", "entitlement", entitlement) } } return &boolCacheResult{allowed: false}, nil } -func (ca *chainAuth) isEntitledToSpace(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (bool, error) { +func (ca *chainAuth) isEntitledToSpace(ctx context.Context, args *ChainAuthArgs) (bool, error) { if args.kind != chainAuthKindSpace { return false, RiverError(Err_INTERNAL, "Wrong chain auth kind") } - isEntitled, cacheHit, err := ca.entitlementCache.executeUsingCache(ctx, cfg, args, ca.isEntitledToSpaceUncached) + isEntitled, cacheHit, err := ca.entitlementCache.executeUsingCache(ctx, args, ca.isEntitledToSpaceUncached) if err != nil { return false, err } @@ -417,7 +373,7 @@ func (ca *chainAuth) isEntitledToSpace(ctx context.Context, cfg *config.Config, return isEntitled.IsAllowed(), nil } -func (ca *chainAuth) isEntitledToChannelUncached(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (CacheResult, error) { +func (ca *chainAuth) isEntitledToChannelUncached(ctx context.Context, args *ChainAuthArgs) (CacheResult, error) { allowed, err := ca.spaceContract.IsEntitledToChannel( ctx, args.spaceId, @@ -428,12 +384,12 @@ func (ca *chainAuth) isEntitledToChannelUncached(ctx context.Context, cfg *confi return &boolCacheResult{allowed: allowed}, err } -func (ca *chainAuth) isEntitledToChannel(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (bool, error) { +func (ca *chainAuth) isEntitledToChannel(ctx context.Context, args *ChainAuthArgs) (bool, error) { if args.kind != chainAuthKindChannel { return false, RiverError(Err_INTERNAL, "Wrong chain auth kind") } - isEntitled, cacheHit, err := ca.entitlementCache.executeUsingCache(ctx, cfg, args, ca.isEntitledToChannelUncached) + isEntitled, cacheHit, err := ca.entitlementCache.executeUsingCache(ctx, args, ca.isEntitledToChannelUncached) if err != nil { return false, err } @@ -496,19 +452,19 @@ func (ca *chainAuth) checkMembership( * If any of the operations fail before getting positive result, the whole operation fails. * A prerequisite for this function is that one of the linked wallets is a member of the space. */ -func (ca *chainAuth) checkEntitlement(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) (CacheResult, error) { +func (ca *chainAuth) checkEntitlement(ctx context.Context, args *ChainAuthArgs) (CacheResult, error) { log := dlog.FromCtx(ctx) ctx, cancel := context.WithTimeout(ctx, time.Millisecond*time.Duration(ca.contractCallsTimeoutMs)) defer cancel() if args.kind == chainAuthKindSpace { - err := ca.checkSpaceEnabled(ctx, cfg, args.spaceId) + err := ca.checkSpaceEnabled(ctx, args.spaceId) if err != nil { return &boolCacheResult{allowed: false}, nil } } else if args.kind == chainAuthKindChannel { - err := ca.checkChannelEnabled(ctx, cfg, args.spaceId, args.channelId) + err := ca.checkChannelEnabled(ctx, args.spaceId, args.channelId) if err != nil { return &boolCacheResult{allowed: false}, nil } @@ -524,7 +480,6 @@ func (ca *chainAuth) checkEntitlement(ctx context.Context, cfg *config.Config, a // Add the root key to the list of wallets. wallets = append(wallets, args.principal) - args = args.withLinkedWallets(wallets) isMemberCtx, isMemberCancel := context.WithCancel(ctx) defer isMemberCancel() @@ -579,7 +534,7 @@ func (ca *chainAuth) checkEntitlement(ctx context.Context, cfg *config.Config, a wg.Add(1) go func(address common.Address) { defer wg.Done() - result, err := ca.isWalletEntitled(ctx, cfg, args.withWallet(address)) + result, err := ca.isWalletEntitled(ctx, args.withWallet(address)) resultsChan <- entitlementCheckResult{allowed: result, err: err} }(wallet) } diff --git a/core/node/auth/auth_impl_cache.go b/core/node/auth/auth_impl_cache.go index d9771fbb9..86eb51172 100644 --- a/core/node/auth/auth_impl_cache.go +++ b/core/node/auth/auth_impl_cache.go @@ -141,9 +141,8 @@ func newEntitlementManagerCache(ctx context.Context, cfg *config.ChainConfig) (* func (ec *entitlementCache) executeUsingCache( ctx context.Context, - cfg *config.Config, key *ChainAuthArgs, - onMiss func(context.Context, *config.Config, *ChainAuthArgs) (CacheResult, error), + onMiss func(context.Context, *ChainAuthArgs) (CacheResult, error), ) (CacheResult, bool, error) { // Check positive cache first if val, ok := ec.positiveCache.Get(*key); ok { @@ -168,7 +167,7 @@ func (ec *entitlementCache) executeUsingCache( } // Cache miss, execute the closure - result, err := onMiss(ctx, cfg, key) + result, err := onMiss(ctx, key) if err != nil { return nil, false, err } diff --git a/core/node/auth/auth_impl_cache_test.go b/core/node/auth/auth_impl_cache_test.go index 51016c8b0..6a0f53a66 100644 --- a/core/node/auth/auth_impl_cache_test.go +++ b/core/node/auth/auth_impl_cache_test.go @@ -25,8 +25,6 @@ func TestCache(t *testing.T) { ctx, cancel := test.NewTestContext() defer cancel() - cfg := &config.Config{} - c, err := newEntitlementCache( ctx, &config.ChainConfig{ @@ -43,9 +41,8 @@ func TestCache(t *testing.T) { var cacheMissForReal bool result, cacheHit, err := c.executeUsingCache( ctx, - cfg, NewChainAuthArgsForChannel(spaceId, channelId, "3", PermissionWrite), - func(context.Context, *config.Config, *ChainAuthArgs) (CacheResult, error) { + func(context.Context, *ChainAuthArgs) (CacheResult, error) { cacheMissForReal = true return &simpleCacheResult{allowed: true}, nil }, @@ -58,9 +55,8 @@ func TestCache(t *testing.T) { cacheMissForReal = false result, cacheHit, err = c.executeUsingCache( ctx, - cfg, NewChainAuthArgsForChannel(spaceId, channelId, "3", PermissionWrite), - func(context.Context, *config.Config, *ChainAuthArgs) (CacheResult, error) { + func(context.Context, *ChainAuthArgs) (CacheResult, error) { cacheMissForReal = true return &simpleCacheResult{allowed: false}, nil }, diff --git a/core/node/auth/fake_auth.go b/core/node/auth/fake_auth.go index fd030fcec..83e158dbd 100644 --- a/core/node/auth/fake_auth.go +++ b/core/node/auth/fake_auth.go @@ -2,8 +2,6 @@ package auth import ( "context" - - "github.com/river-build/river/core/node/config" ) // This checkers always returns true, used for some testing scenarios. @@ -15,6 +13,6 @@ type fakeChainAuth struct{} var _ ChainAuth = (*fakeChainAuth)(nil) -func (a *fakeChainAuth) IsEntitled(ctx context.Context, cfg *config.Config, args *ChainAuthArgs) error { +func (a *fakeChainAuth) IsEntitled(ctx context.Context, args *ChainAuthArgs) error { return nil } diff --git a/core/node/auth/space_contract.go b/core/node/auth/space_contract.go index fa7526367..07d3c68aa 100644 --- a/core/node/auth/space_contract.go +++ b/core/node/auth/space_contract.go @@ -3,14 +3,15 @@ package auth import ( "context" + v3 "github.com/river-build/river/core/xchain/contracts/v3" + "github.com/ethereum/go-ethereum/common" "github.com/river-build/river/core/node/shared" - "github.com/river-build/river/core/xchain/contracts" ) type SpaceEntitlements struct { entitlementType string - ruleEntitlement *contracts.IRuleData + ruleEntitlement v3.IRuleEntitlementRuleData userEntitlement []common.Address } diff --git a/core/node/auth/space_contract_v3.go b/core/node/auth/space_contract_v3.go index d3e1a3c64..8d55fffea 100644 --- a/core/node/auth/space_contract_v3.go +++ b/core/node/auth/space_contract_v3.go @@ -16,7 +16,6 @@ import ( "github.com/river-build/river/core/node/shared" "github.com/river-build/river/core/xchain/bindings/erc721" "github.com/river-build/river/core/xchain/bindings/ierc5313" - "github.com/river-build/river/core/xchain/contracts" v3 "github.com/river-build/river/core/xchain/contracts/v3" "github.com/ethereum/go-ethereum/accounts/abi" @@ -184,7 +183,7 @@ func (sc *SpaceContractV3) GetSpaceEntitlementsForPermission( return nil, EMPTY_ADDRESS, err } - var ruleData contracts.IRuleData + var ruleData v3.IRuleEntitlementRuleData unpackedData, err := parsedABI.Unpack("getRuleData", entitlement.EntitlementData) if err != nil { @@ -224,7 +223,7 @@ func (sc *SpaceContractV3) GetSpaceEntitlementsForPermission( log.Warn("No data unpacked", "unpackedData", unpackedData) } - entitlements[i].ruleEntitlement = &ruleData + entitlements[i].ruleEntitlement = ruleData } else if entitlement.EntitlementType == "UserEntitlement" { entitlements[i].entitlementType = entitlement.EntitlementType diff --git a/core/node/cmd/root_cmd.go b/core/node/cmd/root_cmd.go index 65ff5cf14..5ff040f59 100644 --- a/core/node/cmd/root_cmd.go +++ b/core/node/cmd/root_cmd.go @@ -88,7 +88,6 @@ func initConfigAndLog() { if logNoColor { configStruct.Log.NoColor = true } - configStruct.Init() // If loaded successfully, set the global config cmdConfig = &configStruct diff --git a/core/node/config/config.go b/core/node/config/config.go index 4ce4d091c..12c8baf3d 100644 --- a/core/node/config/config.go +++ b/core/node/config/config.go @@ -2,9 +2,6 @@ package config import ( "encoding/hex" - "fmt" - "strconv" - "strings" "time" "github.com/ethereum/go-ethereum/common" @@ -12,13 +9,6 @@ import ( "github.com/river-build/river/core/node/shared" ) -type ContractVersion string - -const ( - VersionDev ContractVersion = "dev" - VersionV3 ContractVersion = "v3" -) - type TLSConfig struct { Cert string // Path to certificate file or BASE64 encoded certificate Key string `dlog:"omit" json:"-"` // Path to key file or BASE64 encoded key. Sensitive data, omitted from logging. @@ -86,19 +76,7 @@ type Config struct { // Disable base chain contract usage. DisableBaseChain bool - - // xChain configuration - ChainsString string `mapstructure:"chains"` - Chains map[uint64]string `mapstructure:"-"` // This is a derived field - EntitlementContract ContractConfig `mapstructure:"entitlement_contract"` - WalletLinkContract ContractConfig `mapstructure:"wallet_link_contract"` - contractVersion ContractVersion `mapstructure:"contract_version"` - TestEntitlementContract ContractConfig `mapstructure:"test_contract"` - TestCustomEntitlementContract ContractConfig `mapstructure:"test_custom_entitlement_contract"` - - // History indicates how far back xchain must look for entitlement check requests after start - History time.Duration - EnableTestAPIs bool + EnableTestAPIs bool } type NetworkConfig struct { @@ -261,48 +239,3 @@ func (c *Config) GetGraffiti() string { } return c.Graffiti } - -func (c *Config) GetContractVersion() ContractVersion { - if c.contractVersion == VersionV3 { - return VersionV3 - } else { - return VersionDev - } -} - -func (c *Config) GetEntitlementContractAddress() common.Address { - return c.EntitlementContract.Address -} - -func (c *Config) GetWalletLinkContractAddress() common.Address { - return c.WalletLinkContract.Address -} - -func (c *Config) GetTestEntitlementContractAddress() common.Address { - return c.TestEntitlementContract.Address -} - -func (c *Config) GetTestCustomEntitlementContractAddress() common.Address { - return c.TestCustomEntitlementContract.Address -} - -func (c *Config) Init() { - c.parseChains() -} - -func (c *Config) parseChains() { - chainUrls := make(map[uint64]string) - chainPairs := strings.Split(c.ChainsString, ",") - for _, pair := range chainPairs { - parts := strings.SplitN(pair, ":", 2) // Use SplitN to split into exactly two parts - if len(parts) == 2 { - chainID, err := strconv.Atoi(parts[0]) - if err != nil { - fmt.Printf("Error converting chainID to int: %v\n", err) - continue - } - chainUrls[uint64(chainID)] = parts[1] - } - } - c.Chains = chainUrls -} diff --git a/core/node/default_config.yaml b/core/node/default_config.yaml index a580202af..8ad40d95a 100644 --- a/core/node/default_config.yaml +++ b/core/node/default_config.yaml @@ -110,19 +110,3 @@ graffiti: '' # Debug feature flags. disableBaseChain: false - -chains: '31337:http://localhost:8545,31338:http://localhost:8546,84532:https://sepolia.base.org,11155111:https://ethereum-sepolia-rpc.publicnode.com' - -history: 30s - -entitlement_contract: - address: '' - version: '3' - -test_contract: - address: '' - version: '3' - -wallet_link_contract: - address: '' - version: '3' diff --git a/core/node/rpc/add_event.go b/core/node/rpc/add_event.go index b391088d5..5ad181436 100644 --- a/core/node/rpc/add_event.go +++ b/core/node/rpc/add_event.go @@ -74,7 +74,7 @@ func (s *Service) addParsedEvent( } if chainAuthArgs != nil { - err := s.chainAuth.IsEntitled(ctx, s.config, chainAuthArgs) + err := s.chainAuth.IsEntitled(ctx, chainAuthArgs) if err != nil { return err } diff --git a/core/node/rpc/create_stream.go b/core/node/rpc/create_stream.go index 9ce8bff76..f59cf45d1 100644 --- a/core/node/rpc/create_stream.go +++ b/core/node/rpc/create_stream.go @@ -107,7 +107,7 @@ func (s *Service) createStream(ctx context.Context, req *CreateStreamRequest) (* // check entitlements if csRules.ChainAuth != nil { - err := s.chainAuth.IsEntitled(ctx, s.config, csRules.ChainAuth) + err := s.chainAuth.IsEntitled(ctx, csRules.ChainAuth) if err != nil { return nil, err } diff --git a/core/node/run_impl.sh b/core/node/run_impl.sh index 17aabb19c..c7c8cbb81 100755 --- a/core/node/run_impl.sh +++ b/core/node/run_impl.sh @@ -59,12 +59,9 @@ if [ "$CONFIG" == "true" ]; then SPACE_FACTORY_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/spaceFactory.json) WALLET_LINK_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/walletLink.json) - BASE_REGISTRY_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/baseRegistry.json) RIVER_REGISTRY_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/river/addresses/riverRegistry.json) - export SPACE_FACTORY_ADDRESS export WALLET_LINK_ADDRESS - export BASE_REGISTRY_ADDRESS export RIVER_REGISTRY_ADDRESS source ../../contracts/.env.localhost diff --git a/core/xchain/client_simulator/client_simulator.go b/core/xchain/client_simulator/client_simulator.go index d8a5e1722..a8378a998 100644 --- a/core/xchain/client_simulator/client_simulator.go +++ b/core/xchain/client_simulator/client_simulator.go @@ -2,6 +2,8 @@ package client_simulator import ( "context" + "core/xchain/config" + "core/xchain/contracts" "core/xchain/entitlement" "core/xchain/examples" "crypto/ecdsa" @@ -9,17 +11,13 @@ import ( "math/big" "time" - "github.com/river-build/river/core/xchain/contracts" - - "github.com/river-build/river/core/node/config" - node_contracts "github.com/river-build/river/core/node/contracts" node_crypto "github.com/river-build/river/core/node/crypto" "github.com/river-build/river/core/node/dlog" xc "core/xchain/common" - e "github.com/river-build/river/core/xchain/contracts" + e "core/xchain/contracts" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -179,7 +177,7 @@ type postResult struct { type ClientSimulator interface { Start(ctx context.Context) Stop() - EvaluateRuleData(ctx context.Context, cfg *config.Config, ruleData e.IRuleData) (bool, error) + EvaluateRuleData(ctx context.Context, ruleData e.IRuleData) (bool, error) Wallet() *node_crypto.Wallet } @@ -215,7 +213,7 @@ func New( wallet *node_crypto.Wallet, ) (ClientSimulator, error) { entitlementGated, err := e.NewMockEntitlementGated( - cfg.GetTestEntitlementContractAddress(), + cfg.GetMockEntitlementContractAddress(), nil, cfg.GetContractVersion(), ) @@ -230,7 +228,7 @@ func New( var ( entitlementGatedABI = entitlementGated.GetAbi() entitlementGatedContract = bind.NewBoundContract( - cfg.GetTestEntitlementContractAddress(), + cfg.GetMockEntitlementContractAddress(), *entitlementGated.GetAbi(), nil, nil, @@ -284,7 +282,7 @@ func (cs *clientSimulator) Stop() { func (cs *clientSimulator) Start(ctx context.Context) { cs.baseChain.ChainMonitor.OnContractWithTopicsEvent( - cs.cfg.GetTestEntitlementContractAddress(), + cs.cfg.GetMockEntitlementContractAddress(), [][]common.Hash{{cs.entitlementGatedABI.Events["EntitlementCheckResultPosted"].ID}}, func(ctx context.Context, event types.Log) { cs.onEntitlementCheckResultPosted(ctx, event, cs.resultPosted) @@ -306,26 +304,20 @@ func (cs *clientSimulator) Start(ctx context.Context) { func (cs *clientSimulator) executeCheck(ctx context.Context, ruleData *e.IRuleData) error { log := dlog.FromCtx(ctx).With("application", "clientSimulator") - log.Info("ClientSimulator executing check", "ruleData", ruleData, "cfg", cs.cfg) pendingTx, err := cs.baseChain.TxPool.Submit( ctx, "RequestEntitlementCheck", func(opts *bind.TransactOpts) (*types.Transaction, error) { - log.Info("Calling RequestEntitlementCheck", "opts", opts, "ruleData", ruleData) gated, err := contracts.NewMockEntitlementGated( - cs.cfg.GetTestEntitlementContractAddress(), + cs.cfg.GetMockEntitlementContractAddress(), cs.baseChain.Client, cs.cfg.GetContractVersion(), ) if err != nil { - log.Error("Failed to get NewMockEntitlementGated", "err", err) return nil, err } - log.Info("NewMockEntitlementGated", "gated", gated.RequestEntitlementCheck, "err", err) - tx, err := gated.RequestEntitlementCheck(opts, big.NewInt(0), *ruleData) - log.Info("RequestEntitlementCheck called", "tx", tx, "err", err) - return tx, err + return gated.RequestEntitlementCheck(opts, big.NewInt(0), *ruleData) }, ) @@ -334,13 +326,13 @@ func (cs *clientSimulator) executeCheck(ctx context.Context, ruleData *e.IRuleDa customErr, stringErr, err := cs.decoder.DecodeEVMError(err) switch { case customErr != nil: - log.Error("Failed to submit entitlement check", "type", "customErr", "err", customErr) + log.Error("Failed to submit entitlement check", "err", customErr) return err case stringErr != nil: - log.Error("Failed to submit entitlement check", "type", "stringErr", "err", stringErr) + log.Error("Failed to submit entitlement check", "err", stringErr) return err case err != nil: - log.Error("Failed to submit entitlement check", "type", "err", "err", err) + log.Error("Failed to submit entitlement check", "err", err) return err } @@ -472,9 +464,8 @@ func (cs *clientSimulator) Wallet() *node_crypto.Wallet { return cs.wallet } -func (cs *clientSimulator) EvaluateRuleData(ctx context.Context, cfg *config.Config, ruleData e.IRuleData) (bool, error) { +func (cs *clientSimulator) EvaluateRuleData(ctx context.Context, ruleData e.IRuleData) (bool, error) { log := dlog.FromCtx(ctx).With("application", "clientSimulator") - log.Info("ClientSimulator evaluating rule data", "ruleData", ruleData) err := cs.executeCheck(ctx, &ruleData) if err != nil { @@ -536,7 +527,7 @@ func RunClientSimulator(ctx context.Context, cfg *config.Config, wallet *node_cr return } - cs.EvaluateRuleData(ctx, cfg, ruleData) + cs.EvaluateRuleData(ctx, ruleData) } func ToggleEntitlement(ctx context.Context, cfg *config.Config, wallet *node_crypto.Wallet) { diff --git a/core/xchain/cmd/root_cmd.go b/core/xchain/cmd/root_cmd.go index 3c8659bc2..1559d69cf 100644 --- a/core/xchain/cmd/root_cmd.go +++ b/core/xchain/cmd/root_cmd.go @@ -1,13 +1,12 @@ package cmd import ( + "core/xchain/config" "fmt" "os" "strings" - "github.com/mitchellh/mapstructure" - "github.com/river-build/river/core/node/config" - + sconfig "github.com/river-build/river/core/node/config" "github.com/river-build/river/core/node/infra" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -49,15 +48,8 @@ func initConfigAndLog() { fmt.Printf("Failed to read config file, file=%v, error=%v\n", configFile, err) } - var ( - configStruct config.Config - decodeHooks = mapstructure.ComposeDecodeHookFunc( - config.DecodeAddressOrAddressFileHook(), - config.DecodeDurationHook(), - ) - ) - - if err := viper.Unmarshal(&configStruct, viper.DecodeHook(decodeHooks)); err != nil { + var configStruct config.Config + if err := viper.Unmarshal(&configStruct, viper.DecodeHook(sconfig.DecodeDurationHook())); err != nil { fmt.Printf("Failed to unmarshal config, error=%v\n", err) } diff --git a/core/xchain/config/config.go b/core/xchain/config/config.go new file mode 100644 index 000000000..33c0f9d8b --- /dev/null +++ b/core/xchain/config/config.go @@ -0,0 +1,88 @@ +package config + +import ( + "fmt" + "strconv" + "strings" + "time" + + "github.com/ethereum/go-ethereum/common" + node_config "github.com/river-build/river/core/node/config" + infra "github.com/river-build/river/core/node/infra/config" +) + +type ContractVersion string + +const ( + VersionDev ContractVersion = "dev" + VersionV3 ContractVersion = "v3" +) + +// Viper uses mapstructure module to marshal settings into config struct. +type Config struct { + Metrics infra.MetricsConfig `mapstructure:"metrics"` + Log infra.LogConfig `mapstructure:"log"` + ChainsString string `mapstructure:"chains"` + Chains map[uint64]string `mapstructure:"-"` // This is a derived field + EntitlementContract ContractConfig `mapstructure:"entitlement_contract"` + WalletLinkContract ContractConfig `mapstructure:"wallet_link_contract"` + TestingContract ContractConfig `mapstructure:"test_contract"` + contractVersion ContractVersion `mapstructure:"contract_version"` + TestCustomEntitlementContract ContractConfig `mapstructure:"test_custom_entitlement_contract"` + + // History indicates how far back xchain must look for entitlement check requests after start + History time.Duration + + // Blockchain configuration + BaseChain node_config.ChainConfig + RiverChain node_config.ChainConfig +} + +type ContractConfig struct { + Address string +} + +func (c *Config) GetContractVersion() ContractVersion { + if c.contractVersion == VersionV3 { + return VersionV3 + } else { + return VersionDev + } +} + +func (c *Config) GetEntitlementContractAddress() common.Address { + return common.HexToAddress(c.EntitlementContract.Address) +} + +func (c *Config) GetWalletLinkContractAddress() common.Address { + return common.HexToAddress(c.WalletLinkContract.Address) +} + +func (c *Config) GetMockEntitlementContractAddress() common.Address { + return common.HexToAddress(c.TestingContract.Address) +} + +func (c *Config) GetTestCustomEntitlementContractAddress() common.Address { + return common.HexToAddress(c.TestCustomEntitlementContract.Address) +} + +func (c *Config) Init() { + c.parseChains() +} + +func (c *Config) parseChains() { + chainUrls := make(map[uint64]string) + chainPairs := strings.Split(c.ChainsString, ",") + for _, pair := range chainPairs { + parts := strings.SplitN(pair, ":", 2) // Use SplitN to split into exactly two parts + if len(parts) == 2 { + chainID, err := strconv.Atoi(parts[0]) + if err != nil { + fmt.Printf("Error converting chainID to int: %v\n", err) + continue + } + chainUrls[uint64(chainID)] = parts[1] + } + } + c.Chains = chainUrls +} diff --git a/core/xchain/contracts/contracts.go b/core/xchain/contracts/contracts.go index ccf6943fb..f6e9ae72c 100644 --- a/core/xchain/contracts/contracts.go +++ b/core/xchain/contracts/contracts.go @@ -2,12 +2,11 @@ package contracts import ( "context" + "core/xchain/config" dev "core/xchain/contracts/dev" v3 "core/xchain/contracts/v3" "math/big" - "github.com/river-build/river/core/node/config" - "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" diff --git a/core/xchain/create_multi.sh b/core/xchain/create_multi.sh index ea7191808..381d4b524 100755 --- a/core/xchain/create_multi.sh +++ b/core/xchain/create_multi.sh @@ -42,13 +42,12 @@ cd "$(dirname "$0")" : ${RUN_ENV:?} : ${RIVER_ENV:?} -: ${SPACE_FACTORY_ADDRESS:?} -: ${WALLET_LINK_ADDRESS:?} -: ${BASE_REGISTRY_ADDRESS:?} -: ${RIVER_REGISTRY_ADDRESS:?} - -export ENTITLEMENT_TEST_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/entitlementGatedExample.json) -export CUSTOM_ENTITLEMENT_TEST_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/customEntitlementExample.json) +BASE_CHAIN_URL="ws://localhost:8545" +BASE_REGISTRY_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/baseRegistry.json) +SPACE_FACTORY_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/spaceFactory.json) +ENTITLEMENT_TEST_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/entitlementGatedExample.json) +CUSTOM_ENTITLEMENT_TEST_ADDRESS=$(jq -r '.address' ../../packages/generated/deployments/${RIVER_ENV}/base/addresses/customEntitlementExample.json) +BASE_CHAIN_ID=31337 make @@ -58,6 +57,8 @@ N=5 # Base directory for the instances BASE_DIR="./run_files/${RUN_ENV}" + + mkdir -p "${BASE_DIR}" # Loop to create N instances in parallel @@ -75,8 +76,7 @@ do # Copy node binary and config template cp "./bin/xchain_node" "${INSTANCE_DIR}/bin" - # Using the shared default_config.yaml with the node - cp ../node/default_config.yaml "${INSTANCE_DIR}/config/config.yaml" + cp default_config.yaml "${INSTANCE_DIR}/config/config.yaml" # Substitute METRIC_PORT and create config.yaml METRICS_PORT=$((9080 + i)) @@ -84,11 +84,18 @@ do echo "Creating instance_${i}" yq eval ".metrics.port = \"$METRICS_PORT\"" -i "${INSTANCE_DIR}/config/config.yaml" - yq eval ".entitlement_contract.address = strenv(BASE_REGISTRY_ADDRESS)" -i "${INSTANCE_DIR}/config/config.yaml" - yq eval ".wallet_link_contract.address = strenv(WALLET_LINK_ADDRESS)" -i "${INSTANCE_DIR}/config/config.yaml" - yq eval ".test_contract.address = strenv(ENTITLEMENT_TEST_ADDRESS)" -i "${INSTANCE_DIR}/config/config.yaml" - yq eval ".architectContract.address = strenv(SPACE_FACTORY_ADDRESS)" -i "${INSTANCE_DIR}/config/config.yaml" - yq eval ".registryContract.address = strenv(RIVER_REGISTRY_ADDRESS)" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".entitlement_contract.url = \"$BASE_CHAIN_URL\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".entitlement_contract.address = \"$BASE_REGISTRY_ADDRESS\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".entitlement_contract.chainId = \"$BASE_CHAIN_ID\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".wallet_link_contract.url = \"$BASE_CHAIN_URL\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".wallet_link_contract.address = \"$SPACE_FACTORY_ADDRESS\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".wallet_link_contract.chainId = \"$BASE_CHAIN_ID\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_contract.url = \"$BASE_CHAIN_URL\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_contract.address = \"$ENTITLEMENT_TEST_ADDRESS\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_contract.chainId = \"$BASE_CHAIN_ID\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_custom_entitlement_contract.url = \"$BASE_CHAIN_URL\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_custom_entitlement_contract.address = \"$CUSTOM_ENTITLEMENT_TEST_ADDRESS\"" -i "${INSTANCE_DIR}/config/config.yaml" + yq eval ".test_custom_entitlement_contract.chainId = \"$BASE_CHAIN_ID\"" -i "${INSTANCE_DIR}/config/config.yaml" yq eval ".log.level = \"debug\"" -i "${INSTANCE_DIR}/config/config.yaml" diff --git a/core/xchain/default_config.yaml b/core/xchain/default_config.yaml new file mode 100644 index 000000000..7f9f9f5f8 --- /dev/null +++ b/core/xchain/default_config.yaml @@ -0,0 +1,34 @@ +chains: '31337:http://localhost:8545,31338:http://localhost:8546,84532:https://sepolia.base.org,11155111:https://ethereum-sepolia-rpc.publicnode.com' +history: 30s +entitlement_contract: + url: '' + chainId: '' + address: '' +test_contract: + url: '' + chainId: '' + address: '' +architectContract: + address: '' + version: '' +wallet_link_contract: + address: '' + version: '' +log: + file: logs/dev.log + level: info + console: true + noColor: false + format: text +metrics: + enabled: true + port: '9081' +# Blockchain configuration +baseChain: + chainId: 31337 + networkUrl: 'http://127.0.0.1:8545' + blockTimeMs: 2000 +riverChain: + chainId: 31338 + networkUrl: 'http://127.0.0.1:8546' + blockTimeMs: 2000 diff --git a/core/xchain/entitlement/check_operation.go b/core/xchain/entitlement/check_operation.go index aef3dec13..17982ec63 100644 --- a/core/xchain/entitlement/check_operation.go +++ b/core/xchain/entitlement/check_operation.go @@ -4,14 +4,13 @@ import ( "context" "core/xchain/bindings/erc20" "core/xchain/bindings/erc721" + "core/xchain/config" "core/xchain/contracts" "fmt" "math/big" "sync" "time" - "github.com/river-build/river/core/node/config" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/river-build/river/core/node/dlog" diff --git a/core/xchain/entitlement/client_pool.go b/core/xchain/entitlement/client_pool.go index 17e7ea809..75e25ead0 100644 --- a/core/xchain/entitlement/client_pool.go +++ b/core/xchain/entitlement/client_pool.go @@ -2,8 +2,7 @@ package entitlement import ( "context" - - "github.com/river-build/river/core/node/config" + "core/xchain/config" "github.com/ethereum/go-ethereum/ethclient" . "github.com/river-build/river/core/node/base" diff --git a/core/xchain/entitlement/entitlement.go b/core/xchain/entitlement/entitlement.go index 8b37fdcb8..47132c750 100644 --- a/core/xchain/entitlement/entitlement.go +++ b/core/xchain/entitlement/entitlement.go @@ -2,14 +2,13 @@ package entitlement import ( "context" + "core/xchain/config" "errors" "fmt" "math/big" "sync" - "github.com/river-build/river/core/node/config" - - er "github.com/river-build/river/core/xchain/contracts" + er "core/xchain/contracts" "github.com/ethereum/go-ethereum/common" "github.com/river-build/river/core/node/dlog" @@ -22,7 +21,7 @@ func EvaluateRuleData( ruleData *er.IRuleData, ) (bool, error) { log := dlog.FromCtx(ctx) - log.Info("Evaluating rule data", "ruleData", ruleData) + log.Debug("Evaluating rule data", "ruleData", ruleData) opTree, err := getOperationTree(ctx, ruleData) if err != nil { return false, err diff --git a/core/xchain/entitlement/entitlement_test.go b/core/xchain/entitlement/entitlement_test.go index 80322dfc9..1ca6fb19f 100644 --- a/core/xchain/entitlement/entitlement_test.go +++ b/core/xchain/entitlement/entitlement_test.go @@ -2,13 +2,12 @@ package entitlement import ( "context" + "core/xchain/config" "core/xchain/examples" "math/big" "testing" "time" - "github.com/river-build/river/core/node/config" - "github.com/stretchr/testify/require" "github.com/ethereum/go-ethereum/common" diff --git a/core/xchain/server/server.go b/core/xchain/server/server.go index 6abcb699d..10abb5b7f 100644 --- a/core/xchain/server/server.go +++ b/core/xchain/server/server.go @@ -2,16 +2,14 @@ package server import ( "context" + "core/xchain/config" + "core/xchain/contracts" "core/xchain/entitlement" "core/xchain/util" "log/slog" "math/big" "time" - "github.com/river-build/river/core/xchain/contracts" - - "github.com/river-build/river/core/node/config" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" diff --git a/core/xchain/server/server_test.go b/core/xchain/server/server_test.go index a0f9dfa35..4604d917c 100644 --- a/core/xchain/server/server_test.go +++ b/core/xchain/server/server_test.go @@ -7,6 +7,9 @@ import ( "context" "core/xchain/client_simulator" xc_common "core/xchain/common" + "core/xchain/config" + "core/xchain/contracts" + test_contracts "core/xchain/contracts/test" "core/xchain/entitlement" "core/xchain/server" "fmt" @@ -16,11 +19,6 @@ import ( "testing" "time" - "github.com/river-build/river/core/xchain/contracts" - test_contracts "github.com/river-build/river/core/xchain/contracts/test" - - "github.com/river-build/river/core/node/config" - "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -259,17 +257,17 @@ func (st *serviceTester) Config() *config.Config { BaseChain: node_config.ChainConfig{}, RiverChain: node_config.ChainConfig{}, ChainsString: fmt.Sprintf("%d:%s", ChainID, BaseRpcEndpoint), - TestEntitlementContract: config.ContractConfig{ - Address: st.mockEntitlementGatedAddress, + TestingContract: config.ContractConfig{ + Address: st.mockEntitlementGatedAddress.String(), }, EntitlementContract: config.ContractConfig{ - Address: st.entitlementCheckerAddress, + Address: st.entitlementCheckerAddress.String(), }, WalletLinkContract: config.ContractConfig{ - Address: st.walletLinkingAddress, + Address: st.walletLinkingAddress.String(), }, TestCustomEntitlementContract: config.ContractConfig{ - Address: st.mockCustomEntitlementAddress, + Address: st.mockCustomEntitlementAddress.String(), }, Log: infra.LogConfig{ NoColor: true, @@ -448,11 +446,10 @@ func expectEntitlementCheckResult( require *require.Assertions, cs client_simulator.ClientSimulator, ctx context.Context, - cfg *config.Config, data contracts.IRuleData, expected bool, ) { - result, err := cs.EvaluateRuleData(ctx, cfg, data) + result, err := cs.EvaluateRuleData(ctx, data) require.NoError(err) require.Equal(expected, result) } @@ -529,8 +526,7 @@ func TestErc721Entitlements(t *testing.T) { st.Start(t) bc := st.ClientSimulatorBlockchain() - cfg := st.Config() - cs, err := client_simulator.New(ctx, cfg, bc, bc.Wallet) + cs, err := client_simulator.New(ctx, st.Config(), bc, bc.Wallet) require.NoError(err) cs.Start(ctx) defer cs.Stop() @@ -539,22 +535,22 @@ func TestErc721Entitlements(t *testing.T) { auth, contractAddress, erc721 := deployMockErc721Contract(require, st) // Expect no NFT minted for the client simulator wallet - expectEntitlementCheckResult(require, cs, ctx, cfg, erc721Check(ChainID, contractAddress, 1), false) + expectEntitlementCheckResult(require, cs, ctx, erc721Check(ChainID, contractAddress, 1), false) // Mint an NFT for client simulator wallet. mintTokenForWallet(require, auth, st, erc721, cs.Wallet(), 1) // Check if the wallet a 1 balance of the NFT - should pass - expectEntitlementCheckResult(require, cs, ctx, cfg, erc721Check(ChainID, contractAddress, 1), true) + expectEntitlementCheckResult(require, cs, ctx, erc721Check(ChainID, contractAddress, 1), true) // Checking for balance of 2 should fail - expectEntitlementCheckResult(require, cs, ctx, cfg, erc721Check(ChainID, contractAddress, 2), false) + expectEntitlementCheckResult(require, cs, ctx, erc721Check(ChainID, contractAddress, 2), false) // Create a set of 3 linked wallets using client simulator address. _, wallet1, wallet2, _ := generateLinkedWallets(ctx, require, tc.sentByRootKeyWallet, st, cs.Wallet()) // Sanity check: balance of 4 across all 3 wallets should fail - expectEntitlementCheckResult(require, cs, ctx, cfg, erc721Check(ChainID, contractAddress, 4), false) + expectEntitlementCheckResult(require, cs, ctx, erc721Check(ChainID, contractAddress, 4), false) // Mint 2 NFTs for wallet1. mintTokenForWallet(require, auth, st, erc721, wallet1, 2) @@ -563,7 +559,7 @@ func TestErc721Entitlements(t *testing.T) { mintTokenForWallet(require, auth, st, erc721, wallet2, 1) // Accumulated balance of 4 across all 3 wallets should now pass - expectEntitlementCheckResult(require, cs, ctx, cfg, erc721Check(ChainID, contractAddress, 4), true) + expectEntitlementCheckResult(require, cs, ctx, erc721Check(ChainID, contractAddress, 4), true) }) } } @@ -620,9 +616,8 @@ func TestErc20Entitlements(t *testing.T) { defer st.Close() st.Start(t) - cfg := st.Config() bc := st.ClientSimulatorBlockchain() - cs, err := client_simulator.New(ctx, cfg, bc, bc.Wallet) + cs, err := client_simulator.New(ctx, st.Config(), bc, bc.Wallet) require.NoError(err) cs.Start(ctx) defer cs.Stop() @@ -631,22 +626,22 @@ func TestErc20Entitlements(t *testing.T) { auth, contractAddress, erc20 := deployMockErc20Contract(require, st) // Check for balance of 1 should fail, as this wallet has no coins. - expectEntitlementCheckResult(require, cs, ctx, cfg, erc20Check(ChainID, contractAddress, 1), false) + expectEntitlementCheckResult(require, cs, ctx, erc20Check(ChainID, contractAddress, 1), false) // Mint 10 tokens for the client simulator wallet. mintErc20TokensForWallet(require, auth, st, erc20, cs.Wallet(), 10) // Check for balance of 10 should pass. - expectEntitlementCheckResult(require, cs, ctx, cfg, erc20Check(ChainID, contractAddress, 10), true) + expectEntitlementCheckResult(require, cs, ctx, erc20Check(ChainID, contractAddress, 10), true) // Checking for balance of 20 should fail - expectEntitlementCheckResult(require, cs, ctx, cfg, erc20Check(ChainID, contractAddress, 20), false) + expectEntitlementCheckResult(require, cs, ctx, erc20Check(ChainID, contractAddress, 20), false) // Create a set of 3 linked wallets using client simulator address. _, wallet1, wallet2, _ := generateLinkedWallets(ctx, require, tc.sentByRootKeyWallet, st, cs.Wallet()) // Sanity check: balance of 30 across all 3 wallets should fail - expectEntitlementCheckResult(require, cs, ctx, cfg, erc20Check(ChainID, contractAddress, 30), false) + expectEntitlementCheckResult(require, cs, ctx, erc20Check(ChainID, contractAddress, 30), false) // Mint 19 tokens for wallet1. mintErc20TokensForWallet(require, auth, st, erc20, wallet1, 19) @@ -654,7 +649,7 @@ func TestErc20Entitlements(t *testing.T) { mintErc20TokensForWallet(require, auth, st, erc20, wallet2, 1) // Accumulated balance of 30 across all 3 wallets should now pass - expectEntitlementCheckResult(require, cs, ctx, cfg, erc20Check(ChainID, contractAddress, 30), true) + expectEntitlementCheckResult(require, cs, ctx, erc20Check(ChainID, contractAddress, 30), true) }) } } @@ -721,26 +716,23 @@ func TestCustomEntitlements(t *testing.T) { defer st.Close() st.Start(t) - cfg := st.Config() bc := st.ClientSimulatorBlockchain() - cs, err := client_simulator.New(ctx, cfg, bc, bc.Wallet) + cs, err := client_simulator.New(ctx, st.Config(), bc, bc.Wallet) require.NoError(err) cs.Start(ctx) defer cs.Stop() // Deploy mock custom entitlement contract to anvil chain auth, contractAddress, customEntitlement := deployMockCustomEntitlement(require, st) - t.Log("Deployed custom entitlement contract", contractAddress.Hex(), ChainID) // Initially the check should fail. customCheck := customEntitlementCheck(ChainID, contractAddress) - t.Log("Checking entitlement for client simulator wallet", customCheck) - expectEntitlementCheckResult(require, cs, ctx, cfg, customCheck, false) + expectEntitlementCheckResult(require, cs, ctx, customCheck, false) toggleEntitlement(require, auth, customEntitlement, cs.Wallet(), true) // Check should now succeed. - expectEntitlementCheckResult(require, cs, ctx, cfg, customCheck, true) + expectEntitlementCheckResult(require, cs, ctx, customCheck, true) // Untoggle entitlement for client simulator wallet toggleEntitlement(require, auth, customEntitlement, cs.Wallet(), false) @@ -750,13 +742,13 @@ func TestCustomEntitlements(t *testing.T) { for _, wallet := range []*node_crypto.Wallet{wallet1, wallet2, wallet3} { // Check should fail for all wallets. - expectEntitlementCheckResult(require, cs, ctx, cfg, customCheck, false) + expectEntitlementCheckResult(require, cs, ctx, customCheck, false) // Toggle entitlement for a particular linked wallet toggleEntitlement(require, auth, customEntitlement, wallet, true) // Check should now succeed for the wallet. - expectEntitlementCheckResult(require, cs, ctx, cfg, customCheck, true) + expectEntitlementCheckResult(require, cs, ctx, customCheck, true) // Untoggle entitlement for the wallet toggleEntitlement(require, auth, customEntitlement, wallet, false)