Skip to content

Commit

Permalink
refactor: Use poktroll ring construction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Jun 26, 2024
1 parent aa4036d commit aabeb2c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 65 deletions.
70 changes: 9 additions & 61 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"fmt"
"slices"

ring_secp256k1 "github.com/athanorlabs/go-dleq/secp256k1"
ringtypes "github.com/athanorlabs/go-dleq/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/pokt-network/poktroll/pkg/crypto/rings"
"github.com/pokt-network/poktroll/x/application/types"
"github.com/pokt-network/ring-go"

"github.com/cosmos/gogoproto/grpc"
"github.com/pokt-network/poktroll/x/application/types"
)

// ApplicationClient is the interface to interact with the on-chain application-module.
Expand Down Expand Up @@ -71,7 +70,7 @@ func (ac *ApplicationClient) GetApplication(
func (ac *ApplicationClient) GetApplicationsDelegatingToGateway(
ctx context.Context,
gatewayAddress string,
queryHeight uint64,
sessionEndHeight uint64,
) ([]string, error) {
allApplications, err := ac.GetAllApplications(ctx)
if err != nil {
Expand All @@ -80,10 +79,9 @@ func (ac *ApplicationClient) GetApplicationsDelegatingToGateway(

gatewayDelegatingApplications := make([]string, 0)
for _, application := range allApplications {
appRing := ApplicationRing{Application: application}
// Get the gateways that are delegated to the application
// at the query height and check if the given gateway address is in the list.
gatewaysDelegatedTo := appRing.ringAddressesAtBlock(queryHeight)
gatewaysDelegatedTo := rings.GetRingAddressesAtSessionEndHeight(&application, sessionEndHeight)
if slices.Contains(gatewaysDelegatedTo, gatewayAddress) {
// The application is delegating to the given gateway address, add it to the list.
gatewayDelegatingApplications = append(gatewayDelegatingApplications, application.Address)
Expand All @@ -105,19 +103,15 @@ type ApplicationRing struct {
// TODO_IMPROVE: use a stateless ring constructor from poktroll once available.
func (a ApplicationRing) GetRing(
ctx context.Context,
queryHeight uint64,
sessionEndHeight uint64,
) (addressRing *ring.Ring, err error) {
if a.PublicKeyFetcher == nil {
return nil, errors.New("GetRing: Public Key Fetcher not set")
}

if queryHeight <= 0 {
return nil, errors.New("GetRing: Query Height should be greater than zero")
}

// Get the gateway addresses that are delegated from the application
// at the query height.
currentGatewayAddresses := a.ringAddressesAtBlock(queryHeight)
currentGatewayAddresses := rings.GetRingAddressesAtSessionEndHeight(&a.Application, sessionEndHeight)

ringAddresses := make([]string, 0)
ringAddresses = append(ringAddresses, a.Application.Address)
Expand All @@ -129,62 +123,16 @@ func (a ApplicationRing) GetRing(
ringAddresses = append(ringAddresses, currentGatewayAddresses...)
}

curve := ring_secp256k1.NewCurve()
ringPoints := make([]ringtypes.Point, 0, len(ringAddresses))

// Create a ring point for each address.
ringPubKeys := make([]cryptotypes.PubKey, 0, len(ringAddresses))
for _, address := range ringAddresses {
pubKey, err := a.PublicKeyFetcher.GetPubKeyFromAddress(ctx, address)
if err != nil {
return nil, err
}

point, err := curve.DecodeToPoint(pubKey.Bytes())
if err != nil {
return nil, err
}

ringPoints = append(ringPoints, point)
}

return ring.NewFixedKeyRingFromPublicKeys(ring_secp256k1.NewCurve(), ringPoints)
}

func (a ApplicationRing) ringAddressesAtBlock(
queryHeight uint64,
) []string {
// Get the current active delegations for the application and use them as a base.
activeDelegationsAtHeight := a.Application.DelegateeGatewayAddresses

// Use a map to keep track of the gateways addresses that have been added to
// the active delegations slice to avoid duplicates.
addedDelegations := make(map[string]struct{})

// Iterate over the pending undelegations recorded at their respective block
// height and check whether to add them back as active delegations.
for pendingUndelegationHeight, undelegatedGateways := range a.Application.PendingUndelegations {
// If the pending undelegation happened BEFORE the target session end height, skip it.
// The gateway is pending undelegation and simply has not been pruned yet.
// It will be pruned in the near future.
// TODO_DISCUSS: should we use the session's ending height instead?
if pendingUndelegationHeight < queryHeight {
continue
}
// Add back any gateway address that was undelegated after the target session
// end height, as we consider it not happening yet relative to the target height.
for _, gatewayAddress := range undelegatedGateways.GatewayAddresses {
if _, ok := addedDelegations[gatewayAddress]; ok {
continue
}

activeDelegationsAtHeight = append(activeDelegationsAtHeight, gatewayAddress)
// Mark the gateway address as added to avoid duplicates.
addedDelegations[gatewayAddress] = struct{}{}
}

ringPubKeys = append(ringPubKeys, pubKey)
}

return activeDelegationsAtHeight
return rings.GetRingFromPubKeys(ringPubKeys)
}

// PublicKeyFetcher specifies an interface that allows getting the public key corresponding to an address.
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cometbft/cometbft v0.38.5
github.com/cosmos/cosmos-sdk v0.50.4
github.com/cosmos/gogoproto v1.4.11
github.com/pokt-network/poktroll v0.0.3-0.20240615000642-ff0456568487
github.com/pokt-network/poktroll v0.0.3-0.20240626124451-bd7b421860ac
github.com/pokt-network/ring-go v0.1.0
github.com/stretchr/testify v1.8.4
google.golang.org/protobuf v1.32.0
Expand Down Expand Up @@ -45,7 +45,7 @@ require (
github.com/cosmos/cosmos-proto v1.0.0-beta.4 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v1.0.1 // indirect
github.com/cosmos/iavl v1.2.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/danieljoos/wincred v1.2.1 // indirect
Expand Down Expand Up @@ -109,7 +109,7 @@ require (
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pokt-network/smt v0.10.2 // indirect
github.com/pokt-network/smt v0.11.1 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.47.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ github.com/cosmos/gogoproto v1.4.11 h1:LZcMHrx4FjUgrqQSWeaGC1v/TeuVFqSLa43CC6aWR
github.com/cosmos/gogoproto v1.4.11/go.mod h1:/g39Mh8m17X8Q/GDEs5zYTSNaNnInBSohtaxzQnYq1Y=
github.com/cosmos/iavl v1.0.1 h1:D+mYbcRO2wptYzOM1Hxl9cpmmHU1ZEt9T2Wv5nZTeUw=
github.com/cosmos/iavl v1.0.1/go.mod h1:8xIUkgVvwvVrBu81scdPty+/Dx9GqwHnAvXz4cwF7RY=
github.com/cosmos/iavl v1.2.0 h1:kVxTmjTh4k0Dh1VNL046v6BXqKziqMDzxo93oh3kOfM=
github.com/cosmos/iavl v1.2.0/go.mod h1:HidWWLVAtODJqFD6Hbne2Y0q3SdxByJepHUOeoH4LiI=
github.com/cosmos/ibc-go/modules/capability v1.0.0 h1:r/l++byFtn7jHYa09zlAdSeevo8ci1mVZNO9+V0xsLE=
github.com/cosmos/ibc-go/modules/capability v1.0.0/go.mod h1:D81ZxzjZAe0ZO5ambnvn1qedsFQ8lOwtqicG6liLBco=
github.com/cosmos/ibc-go/v8 v8.1.0 h1:pf1106wl0Cf+p1+FjXzV6odlS9DnqVunPVWCH1Uz+lQ=
Expand Down Expand Up @@ -641,10 +643,14 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pokt-network/poktroll v0.0.3-0.20240615000642-ff0456568487 h1:mOdXRweVWUYpGyaroiZeOGfL5CVuscLRrRgXi7EC08E=
github.com/pokt-network/poktroll v0.0.3-0.20240615000642-ff0456568487/go.mod h1:jltwJuL/Tw8gOMRr2XasiMUwFLxpqzLF18zY697hlYw=
github.com/pokt-network/poktroll v0.0.3-0.20240626124451-bd7b421860ac h1:l/kINNXzY84wQCqmzC3UwJI4PJebWuG686Z8EuPuL/Y=
github.com/pokt-network/poktroll v0.0.3-0.20240626124451-bd7b421860ac/go.mod h1:+O0y3mT8L9C6igO3EZHjLgBHTO7z+3VJWIxpZjqNsA4=
github.com/pokt-network/ring-go v0.1.0 h1:hF7mDR4VVCIqqDAsrloP8azM9y1mprc99YgnTjKSSwk=
github.com/pokt-network/ring-go v0.1.0/go.mod h1:8NHPH7H3EwrPX3XHfpyRI6bz4gApkE3+fd0XZRbMWP0=
github.com/pokt-network/smt v0.10.2 h1:7OCimi2qN9kPwv+UDbUqaFYLaVMed3DYO8AbY8R0rNo=
github.com/pokt-network/smt v0.10.2/go.mod h1:S4Ho4OPkK2v2vUCHNtA49XDjqUC/OFYpBbynRVYmxvA=
github.com/pokt-network/smt v0.11.1 h1:ySN8PjrPDKyvzLcX0qTHR2s5ReaZnjq25z0B7p6AWl0=
github.com/pokt-network/smt v0.11.1/go.mod h1:S4Ho4OPkK2v2vUCHNtA49XDjqUC/OFYpBbynRVYmxvA=
github.com/pokt-network/smt/kvstore/badger v0.0.0-20240109205447-868237978c0b h1:TjfgV3vgW0zW47Br/OgUXD4M8iyR74EYanbFfN4ed8o=
github.com/pokt-network/smt/kvstore/badger v0.0.0-20240109205447-868237978c0b/go.mod h1:GbzcG5ebj8twKmBL1VzdPM4NS44okwYXBfQaVXT+6yU=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down
2 changes: 1 addition & 1 deletion signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *Signer) Sign(
app ApplicationRing,
queryHeight uint64,
) (*servicetypes.RelayRequest, error) {
appRing, err := app.GetRing(ctx, queryHeight)
appRing, err := app.GetRing(ctx, uint64(relayRequest.Meta.SessionHeader.SessionEndBlockHeight))
if err != nil {
return nil, fmt.Errorf("Sign: error getting a ring for application address %s: %w", app.Address, err)

Check failure on line 32 in signer.go

View workflow job for this annotation

GitHub Actions / go-test

app.Address undefined (type ApplicationRing has no field or method Address) (typecheck)
}
Expand Down

0 comments on commit aabeb2c

Please sign in to comment.