Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ring] Poktroll ring construction #17

Merged
merged 27 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
63334fd
refactor: remove redundant application client
adshmh Jun 13, 2024
6200a93
refactor: move the BlockClient to top level
adshmh Jun 13, 2024
a40a069
remove the redundant clients/block.go
adshmh Jun 13, 2024
f37f72d
Merge remote-tracking branch 'origin/main' into refactor-move-blocks-…
adshmh Jun 13, 2024
6560f57
fix spacing in the comment
adshmh Jun 13, 2024
7b18057
Merge remote-tracking branch 'origin/main' into refactor-move-blocks-…
adshmh Jun 13, 2024
46d51b2
Refactor: move session client to top level
adshmh Jun 13, 2024
8fad775
refactor: use poktroll exported types in GetSessionSupplierEndpoints
adshmh Jun 15, 2024
fedaf8e
Use PoktNodeSessionFetcher as the interface consumed by SessionClinet
adshmh Jun 17, 2024
88b8f64
Merge remote-tracking branch 'origin/main' into refactor-move-session…
adshmh Jun 21, 2024
dc2b742
feat: introduce a wrapper for Sessions to support endpoint selection
adshmh Jun 21, 2024
450fac4
Draft: overall SDK structure
adshmh Jun 24, 2024
24daa09
Fix the send relay example
adshmh Jun 24, 2024
30b890f
fix typo
adshmh Jun 24, 2024
54a4287
Merge remote-tracking branch 'origin/dev' into refactor-ring-functions
adshmh Jun 25, 2024
7021d10
address review comments
adshmh Jun 26, 2024
f5c4255
fix linter warnings
adshmh Jun 26, 2024
e44fc15
address review comment: make SupplierAddress an input for Validating …
adshmh Jun 26, 2024
303d856
address review comment: use query height to get app ring
adshmh Jun 26, 2024
aa4036d
address review: TODO for stateless ring constructor
adshmh Jun 26, 2024
aabeb2c
refactor: Use poktroll ring construction functions
red-0ne Jun 26, 2024
eb02547
fix: ring naming and linting
red-0ne Jun 26, 2024
fc32455
fix: Remove unused argument
red-0ne Jun 26, 2024
36df96d
Merge remote-tracking branch 'origin/dev' into poktroll-ring-construc…
red-0ne Jun 26, 2024
73679b6
fix: Application address logging
red-0ne Jun 26, 2024
7df99f8
fix: Relay test example and go.mod poktroll dep
red-0ne Jun 27, 2024
7ac6bb4
chore: Update dependencies
red-0ne Jun 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ module github.com/pokt-network/shannon-sdk
go 1.22.2

require (
github.com/athanorlabs/go-dleq v0.1.0
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.20240627055047-4208589342d7
github.com/pokt-network/ring-go v0.1.0
github.com/stretchr/testify v1.8.4
google.golang.org/protobuf v1.32.0
google.golang.org/grpc v1.60.1
)

require (
Expand All @@ -28,6 +26,7 @@ require (
github.com/99designs/keyring v1.2.1 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/athanorlabs/go-dleq v0.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
Expand Down Expand Up @@ -109,7 +108,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 All @@ -127,6 +126,7 @@ require (
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand All @@ -145,7 +145,7 @@ require (
google.golang.org/genproto v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
Expand Down
10 changes: 10 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,18 @@ 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/poktroll v0.0.3-0.20240627050729-d3d73d95631a h1:iIx02alw6wEfytWR614Wbq0Ab+EjBmc/ImG9bMMyRuc=
github.com/pokt-network/poktroll v0.0.3-0.20240627050729-d3d73d95631a/go.mod h1:PP8lLCUgvV1Bq/1tHcw22fpkJkfXNbP4qNN+/PIE9fo=
github.com/pokt-network/poktroll v0.0.3-0.20240627055047-4208589342d7 h1:KvGtE+nPcHalIjjSkKByFPp5XSPjGISOkmyghiBUglk=
github.com/pokt-network/poktroll v0.0.3-0.20240627055047-4208589342d7/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
3 changes: 1 addition & 2 deletions relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func ExampleSigner() {

ctx := context.Background()
// 4.e. Sign the Relay Request
queryHeight := uint64(req.Meta.SessionHeader.SessionEndBlockHeight)
req, err = signer.Sign(ctx, req, ring, queryHeight)
req, err = signer.Sign(ctx, req, ring)
if err != nil {
fmt.Printf("error signing relay: %v", err)
return
Expand Down
13 changes: 6 additions & 7 deletions signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ func (s *Signer) Sign(
ctx context.Context,
relayRequest *servicetypes.RelayRequest,
// TODO_IMPROVE: this input argument should be changed to an interface.
app ApplicationRing,
queryHeight uint64,
appRing ApplicationRing,
adshmh marked this conversation as resolved.
Show resolved Hide resolved
) (*servicetypes.RelayRequest, error) {
appRing, err := app.GetRing(ctx, queryHeight)
sessionRing, err := appRing.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)
return nil, fmt.Errorf("Sign: error getting a ring for application address %s: %w", appRing.Application.Address, err)
}

signableBz, err := relayRequest.GetSignableBytesHash()
Expand All @@ -49,14 +48,14 @@ func (s *Signer) Sign(
return nil, fmt.Errorf("Sign: error decoding private key to a scalar: %w", err)
}

ringSig, err := appRing.Sign(signableBz, signerPrivKey)
ringSig, err := sessionRing.Sign(signableBz, signerPrivKey)
if err != nil {
return nil, fmt.Errorf("Sign: error signing using the ring of application with address %s: %w", app.Address, err)
return nil, fmt.Errorf("Sign: error signing using the ring of application with address %s: %w", appRing.Application.Address, err)
}

signature, err := ringSig.Serialize()
if err != nil {
return nil, fmt.Errorf("Sign: error serializing the signature of application with address %s: %w", app.Address, err)
return nil, fmt.Errorf("Sign: error serializing the signature of application with address %s: %w", appRing.Application.Address, err)
}

relayRequest.Meta.Signature = signature
Expand Down
Loading