From 0130091d6c2fe5c802012a861f3db0f907b1bd57 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 10 Dec 2024 15:59:44 +0100 Subject: [PATCH 1/6] clients/stellarcore: Support more parameter from the upgrades endpoint See https://github.com/stellar/stellar-core/blob/0241e79f74dc017f20e190abd3825873222c5ca5/src/main/CommandHandler.cpp#L568 for alll the supported parameters. --- clients/stellarcore/client.go | 49 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 80938da577..93d9b4d9ac 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -56,12 +56,55 @@ func drainReponse(hresp *http.Response, close bool, err *error) (outerror error) } // Upgrade upgrades the protocol version running on the stellar core instance +// +// Deprecated: use UpgradeProtocol instead func (c *Client) Upgrade(ctx context.Context, version int) (err error) { + return c.UpgradeProtocol(ctx, version, time.Unix(int64(0), 0)) +} + +// UpgradeProtocol upgrades the protocol version running on the stellar core instance +func (c *Client) UpgradeProtocol(ctx context.Context, protocolVersion int, at time.Time) (err error) { + queryParams := url.Values{} + queryParams.Add("protocolversion", strconv.Itoa(protocolVersion)) + return c.setUpgradesAt(ctx, at, queryParams) +} + +// UpgradeSorobanConfig upgrades the Soroban configuration to that indicated by the supplied ConfigUpgradeSetKey at the specified time +func (c *Client) UpgradeSorobanConfig(ctx context.Context, configKey xdr.ConfigUpgradeSetKey, at time.Time) error { + keyB64, err := xdr.MarshalBase64(configKey) + if err != nil { + return err + } + queryParams := url.Values{} + queryParams.Add("configupgradesetkey", keyB64) + return c.setUpgradesAt(ctx, at, queryParams) +} + +// UpgradeTxSetSize upgrades the maximum number of transactions per ledger +func (c *Client) UpgradeTxSetSize(ctx context.Context, maxTxSetSize uint32, at time.Time) error { + queryParams := url.Values{} + queryParams.Add("maxtxsetsize", strconv.FormatUint(uint64(maxTxSetSize), 10)) + return c.setUpgradesAt(ctx, at, queryParams) +} + +// UpgradeSorobanTxSetSize upgrades the maximum number of transactions per ledger +func (c *Client) UpgradeSorobanTxSetSize(ctx context.Context, maxTxSetSize uint32, at time.Time) error { queryParams := url.Values{} - queryParams.Add("mode", "set") - queryParams.Add("upgradetime", "1970-01-01T00:00:00Z") - queryParams.Add("protocolversion", strconv.Itoa(version)) + queryParams.Add("maxsorobantxsetsize", strconv.FormatUint(uint64(maxTxSetSize), 10)) + return c.setUpgradesAt(ctx, at, queryParams) +} + +func (c *Client) setUpgradesAt(ctx context.Context, at time.Time, extraQueryParams url.Values) (err error) { + finalQueryParams := url.Values{} + for k, v := range extraQueryParams { + finalQueryParams[k] = v + } + finalQueryParams.Add("mode", "set") + finalQueryParams.Add("upgradetime", at.Format("2006-01-02T15:04:05Z")) + return c.upgrades(ctx, finalQueryParams) +} +func (c *Client) upgrades(ctx context.Context, queryParams url.Values) (err error) { var req *http.Request req, err = c.simpleGet(ctx, "upgrades", queryParams) if err != nil { From defe84fdd0eadbbd6a2a0d23384ce8f57dd91487 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 10 Dec 2024 19:15:03 +0100 Subject: [PATCH 2/6] WIP: add function to generate soroban config upgrade transactions --- clients/stellarcore/client.go | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 93d9b4d9ac..7190c36d41 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -8,11 +8,13 @@ import ( "io" "net/http" "net/url" + "os/exec" "path" "strconv" "strings" "time" + "github.com/stellar/go/keypair" proto "github.com/stellar/go/protocols/stellarcore" "github.com/stellar/go/support/errors" "github.com/stellar/go/xdr" @@ -80,6 +82,56 @@ func (c *Client) UpgradeSorobanConfig(ctx context.Context, configKey xdr.ConfigU return c.setUpgradesAt(ctx, at, queryParams) } +type GenSorobanConfig struct { + BaseSeqNum uint32 + NetworkPassphrase string + // TODO: Should we ask core to sign the tx or should we do it ourselves? + SigningKey keypair.Full + // looks for `stellar-core` in the system PATH if empty + StellarCorePath string +} + +func GenSorobanConfigUpgradeTxAndKey( + config GenSorobanConfig, upgradeConfig xdr.ConfigUpgradeSet) ([]xdr.TransactionEnvelope, xdr.ConfigUpgradeSetKey, error) { + upgradeConfigB64, err := xdr.MarshalBase64(upgradeConfig) + if err != nil { + return nil, xdr.ConfigUpgradeSetKey{}, err + } + corePath := config.StellarCorePath + if corePath == "" { + corePath = "stellar-core" + } + cmd := exec.Command(corePath, "get-settings-upgrade-txs", + config.SigningKey.Address(), + strconv.FormatUint(uint64(config.BaseSeqNum), 10), + config.NetworkPassphrase, + "--xdr", upgradeConfigB64, + "--signtxs") + inputStr := config.SigningKey.Seed() + cmd.Stdin = strings.NewReader(inputStr) + out, err := cmd.CombinedOutput() + if err != nil { + return nil, xdr.ConfigUpgradeSetKey{}, err + } + fields := strings.Fields(string(out)) + if len(fields) < 4 { + return nil, xdr.ConfigUpgradeSetKey{}, fmt.Errorf("get-settings-upgrade-txs: unexpected output: %q", string(out)) + } + txsB64 := fields[0:3] + keyB64 := fields[3] + + txs := make([]xdr.TransactionEnvelope, len(txsB64)) + for i, txB64 := range txsB64 { + err := xdr.SafeUnmarshalBase64(txB64, &txs[i]) + if err != nil { + return nil, xdr.ConfigUpgradeSetKey{}, err + } + } + var key xdr.ConfigUpgradeSetKey + err = xdr.SafeUnmarshalBase64(keyB64, &key) + return txs, key, err +} + // UpgradeTxSetSize upgrades the maximum number of transactions per ledger func (c *Client) UpgradeTxSetSize(ctx context.Context, maxTxSetSize uint32, at time.Time) error { queryParams := url.Values{} From ff4594c82127708951381f136b15c8ba27c56ace Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Wed, 11 Dec 2024 15:26:16 +0100 Subject: [PATCH 3/6] Add test and fix a few bugs --- clients/stellarcore/client.go | 10 ++++---- clients/stellarcore/client_test.go | 39 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 7190c36d41..568f587f33 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -86,7 +86,7 @@ type GenSorobanConfig struct { BaseSeqNum uint32 NetworkPassphrase string // TODO: Should we ask core to sign the tx or should we do it ourselves? - SigningKey keypair.Full + SigningKey *keypair.Full // looks for `stellar-core` in the system PATH if empty StellarCorePath string } @@ -113,12 +113,12 @@ func GenSorobanConfigUpgradeTxAndKey( if err != nil { return nil, xdr.ConfigUpgradeSetKey{}, err } - fields := strings.Fields(string(out)) - if len(fields) < 4 { + lines := strings.Split(string(out), "\n") + if len(lines) < 9 { return nil, xdr.ConfigUpgradeSetKey{}, fmt.Errorf("get-settings-upgrade-txs: unexpected output: %q", string(out)) } - txsB64 := fields[0:3] - keyB64 := fields[3] + txsB64 := []string{lines[0], lines[2], lines[4], lines[6]} + keyB64 := lines[8] txs := make([]xdr.TransactionEnvelope, len(txsB64)) for i, txB64 := range txsB64 { diff --git a/clients/stellarcore/client_test.go b/clients/stellarcore/client_test.go index 47540b2464..88a8180053 100644 --- a/clients/stellarcore/client_test.go +++ b/clients/stellarcore/client_test.go @@ -6,6 +6,8 @@ import ( "io" "net/http" "net/url" + "os" + "os/exec" "testing" "github.com/jarcoal/httpmock" @@ -13,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stellar/go/keypair" + "github.com/stellar/go/network" proto "github.com/stellar/go/protocols/stellarcore" "github.com/stellar/go/support/http/httptest" "github.com/stellar/go/xdr" @@ -135,3 +138,39 @@ func TestGetLedgerEntries(t *testing.T) { require.Equal(t, "pretend this is XDR lol", resp.Entries[0].Entry) require.Equal(t, "pretend this is another XDR lol", resp.Entries[1].Entry) } + +func TestGenSorobanConfigUpgradeTxAndKey(t *testing.T) { + coreBinary := os.Getenv("STELLAR_CORE_BINARY_PATH") + if coreBinary == "" { + var err error + coreBinary, err = exec.LookPath("stellar-core") + if err != nil { + t.Skip("couldn't find stellar core binary") + } + } + key, err := keypair.ParseFull("SB6VZS57IY25334Y6F6SPGFUNESWS7D2OSJHKDPIZ354BK3FN5GBTS6V") + require.NoError(t, err) + funcConfig := GenSorobanConfig{ + BaseSeqNum: 1, + NetworkPassphrase: network.TestNetworkPassphrase, + SigningKey: key, + StellarCorePath: coreBinary, + } + config := xdr.ConfigUpgradeSet{ + UpdatedEntry: []xdr.ConfigSettingEntry{ + { + ConfigSettingId: xdr.ConfigSettingIdConfigSettingContractComputeV0, + ContractCompute: &xdr.ConfigSettingContractComputeV0{ + LedgerMaxInstructions: 1000, + TxMaxInstructions: 100, + FeeRatePerInstructionsIncrement: 1000, + TxMemoryLimit: 10, + }, + }, + }, + } + + txs, _, err := GenSorobanConfigUpgradeTxAndKey(funcConfig, config) + require.NoError(t, err) + require.Len(t, txs, 4) +} From 26101713d51627d2cb75cbe0e93ca3208f83d0fa Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 12 Dec 2024 18:05:01 +0100 Subject: [PATCH 4/6] Appease linter --- clients/stellarcore/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 568f587f33..8e55ac6f6a 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -122,7 +122,7 @@ func GenSorobanConfigUpgradeTxAndKey( txs := make([]xdr.TransactionEnvelope, len(txsB64)) for i, txB64 := range txsB64 { - err := xdr.SafeUnmarshalBase64(txB64, &txs[i]) + err = xdr.SafeUnmarshalBase64(txB64, &txs[i]) if err != nil { return nil, xdr.ConfigUpgradeSetKey{}, err } From 85a37e6b3e1bed11249e19f59edcb9e3648b366b Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Fri, 13 Dec 2024 15:39:14 +0100 Subject: [PATCH 5/6] Address review feedback --- clients/stellarcore/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 8e55ac6f6a..d1512caac6 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -109,7 +109,7 @@ func GenSorobanConfigUpgradeTxAndKey( "--signtxs") inputStr := config.SigningKey.Seed() cmd.Stdin = strings.NewReader(inputStr) - out, err := cmd.CombinedOutput() + out, err := cmd.Output() if err != nil { return nil, xdr.ConfigUpgradeSetKey{}, err } From afad629464155603b77cfcc63526e9ef48fcb8cf Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Fri, 13 Dec 2024 16:43:12 +0100 Subject: [PATCH 6/6] Remove TODO --- clients/stellarcore/client.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index d1512caac6..d479c4df54 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -85,8 +85,7 @@ func (c *Client) UpgradeSorobanConfig(ctx context.Context, configKey xdr.ConfigU type GenSorobanConfig struct { BaseSeqNum uint32 NetworkPassphrase string - // TODO: Should we ask core to sign the tx or should we do it ourselves? - SigningKey *keypair.Full + SigningKey *keypair.Full // looks for `stellar-core` in the system PATH if empty StellarCorePath string }