From 00f77d9fd7495acdb4635b85142aa705ed38422c Mon Sep 17 00:00:00 2001 From: Josh Allmann Date: Wed, 11 Nov 2026 23:59:41 -0800 Subject: [PATCH] Add TicketBroker recipient funding and deposit options Allow funding a separate account with --recipient. Use --deposit and --reserve with zero defaults, reject zero totals, and retain --amount as a silent compatibility alias tracked for removal in #72. --- chain/actions.go | 18 +++++++++++++----- chain/cli.go | 17 ++++++++++++++++- chain/cli_test.go | 25 ++++++++++++++++++------- chain/management_test.go | 7 +++++++ chain/params.go | 6 ++++-- chain/transactions_test.go | 10 +++++++++- configs/chain/config.example.toml | 2 +- docs/chain.md | 12 ++++++++---- go.mod | 2 +- 9 files changed, 77 insertions(+), 22 deletions(-) diff --git a/chain/actions.go b/chain/actions.go index 4549a01..7804d1a 100644 --- a/chain/actions.go +++ b/chain/actions.go @@ -359,9 +359,9 @@ func (p ClaimParams) actions() ([]action, error) { }), nil } func (p FundParams) actions() ([]action, error) { - deposit, err := parseAmount(p.Amount, p.BaseUnits, false, false) + deposit, err := parseAmount(p.Deposit, p.BaseUnits, false, false) if err != nil { - return nil, fmt.Errorf("invalid --amount (interpreted as wei when --base-units is set): %w", err) + return nil, fmt.Errorf("invalid --deposit (interpreted as wei when --base-units is set): %w", err) } reserve, err := parseAmount(p.Reserve, p.BaseUnits, false, false) if err != nil { @@ -369,12 +369,20 @@ func (p FundParams) actions() ([]action, error) { } value := new(big.Int).Add(deposit, reserve) if value.BitLen() > 256 { - return nil, errors.New("--amount and --reserve total exceeds uint256") + return nil, errors.New("--deposit and --reserve total exceeds uint256") } if value.Sign() == 0 { - return nil, errors.New("--amount and --reserve cannot both be zero") + return nil, errors.New("--deposit and --reserve cannot both be zero") } - return []action{{Contract: "ticketBroker", Method: "fundDepositAndReserve", Args: []any{deposit, reserve}, Value: value}}, nil + fund := action{Contract: "ticketBroker", Method: "fundDepositAndReserve", Args: []any{deposit, reserve}, Value: value} + if p.Recipient != nil { + if *p.Recipient == (common.Address{}) { + return nil, errors.New("recipient must be nonempty") + } + fund.Method = "fundDepositAndReserveFor" + fund.Args = []any{*p.Recipient, deposit, reserve} + } + return []action{fund}, nil } func (p RewardParams) actions() ([]action, error) { return resolvedActions(func(ctx context.Context, c *eth.Contracts, from common.Address) ([]action, error) { diff --git a/chain/cli.go b/chain/cli.go index 4ee2ce7..5bb28c5 100644 --- a/chain/cli.go +++ b/chain/cli.go @@ -13,6 +13,7 @@ import ( "github.com/livepeer/node/eth" "github.com/livepeer/node/nodeconfig" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type transactionParams interface { @@ -72,6 +73,20 @@ func transactionCommand[T transactionParams](root *RootParams, command string, b }}) } +func fundCommand(root *RootParams) boa.Cmd[FundParams] { + command := transactionCommand(root, "ticketbroker fund", FundParams.actions) + command.PostCreateFuncCtx = func(_ *boa.HookContext, _ *FundParams, cmd *cobra.Command) error { + cmd.Flags().SetNormalizeFunc(func(_ *pflag.FlagSet, name string) pflag.NormalizedName { + if name == "amount" { + name = "deposit" + } + return pflag.NormalizedName(name) + }) + return nil + } + return command +} + func signingCommand[T any](root *RootParams, use, short string, path func(T) string, typed bool) boa.Cmd[T] { return invocationCommand(boa.Cmd[T]{Use: use, Short: short, Args: cobra.NoArgs, RunFuncE: func(p *T, cmd *cobra.Command, _ []string) error { if root.PrintConfig { @@ -195,7 +210,7 @@ func Root(out, errOut io.Writer) *cobra.Command { )}), groupCommand(params, boa.Cmd[boa.NoParams]{Use: "ticketbroker", Short: "Manage ticket broker funds", SubCmds: boa.SubCmds( readCommand(params, "get", "Read TicketBroker funding and withdrawal status", ticketBrokerGet), - transactionCommand(params, "ticketbroker fund", FundParams.actions), + fundCommand(params), staticTransaction(params, "ticketbroker unlock", "ticketBroker", "unlock"), staticTransaction(params, "ticketbroker cancel-unlock", "ticketBroker", "cancelUnlock"), staticTransaction(params, "ticketbroker withdraw", "ticketBroker", "withdraw"), diff --git a/chain/cli_test.go b/chain/cli_test.go index e5f4f93..5faab77 100644 --- a/chain/cli_test.go +++ b/chain/cli_test.go @@ -34,7 +34,7 @@ func TestLeafHelpAndCompletion(t *testing.T) { {"account create", []string{"--keystore-file", "--keystore-password-file", "--output"}, []string{"--submit", "--amount"}}, {"account get", []string{"--account", "--output"}, []string{"--submit", "--amount"}}, {"stake bond", []string{"--amount", "--submit", "--wait", "--quiet"}, []string{"--delegate", "--reserve", "--lock-id"}}, - {"ticketbroker fund", []string{"--amount", "--reserve", "--quiet"}, []string{"--lock-id", "--fee-cut"}}, + {"ticketbroker fund", []string{"--deposit", "--reserve", "--base-units", "--recipient", "--quiet"}, []string{"--amount", "--lock-id", "--fee-cut"}}, {"orchestrator register", []string{"--amount", "--redelegate", "--lock-id", "--reward-cut", "--fee-cut", "--service-uri"}, []string{"--fee-share", "--recipient"}}, {"stake locks", []string{"--from-id", "--limit", "--withdrawable", "--locked"}, []string{"--submit", "--amount"}}, {"governance proposal vote", []string{"--reason", "--gas-limit", "--max-priority-fee-per-gas", "--max-transaction-replacements"}, []string{"--amount", "--lock-id"}}, @@ -93,7 +93,7 @@ func TestActionValidationBeforeRPC(t *testing.T) { {[]string{"orchestrator", "reward"}, "unknown command"}, {[]string{"ticketbroker", "unlock", "--transaction-timeout", "0s"}, "must be positive"}, {[]string{"ticketbroker", "unlock", "--gas-limit", "0"}, "below min"}, - {[]string{"ticketbroker", "fund", "--amount", "all", "--reserve", "0"}, "not supported"}, + {[]string{"ticketbroker", "fund", "--deposit", "all"}, "not supported"}, {[]string{"governance", "poll", "vote", testAccount, "maybe"}, "allowed values"}, {[]string{"stake", "locks", "--limit", "1001"}, "exceeds max"}, {[]string{"stake", "locks", "--withdrawable", "--locked"}, "mutually exclusive"}, @@ -113,9 +113,14 @@ func TestActionValidationBeforeRPC(t *testing.T) { {[]string{"orchestrator", "set-config", "--service-uri", "ftp://example.com"}, "absolute HTTP"}, {[]string{"orchestrator", "set-config", "--service-uri", "https://example.com?key=secret"}, "absolute HTTP"}, {[]string{"orchestrator", "set-config", "--service-uri", "https://example.com#fragment"}, "absolute HTTP"}, - {[]string{"ticketbroker", "fund", "--amount", "1"}, "reserve"}, - {[]string{"ticketbroker", "fund", "--amount", "0", "--reserve", "0"}, "cannot both be zero"}, - {[]string{"ticketbroker", "fund", "--amount", "115792089237316195423570985008687907853269984665640564039457584007913129639935", "--reserve", "1", "--base-units"}, "total exceeds uint256"}, + {[]string{"ticketbroker", "fund"}, "cannot both be zero"}, + {[]string{"ticketbroker", "fund", "--deposit", "0"}, "cannot both be zero"}, + {[]string{"ticketbroker", "fund", "--reserve", "0"}, "cannot both be zero"}, + {[]string{"ticketbroker", "fund", "--deposit", "0", "--reserve", "0"}, "cannot both be zero"}, + {[]string{"ticketbroker", "fund", "--amount", "0"}, "cannot both be zero"}, + {[]string{"ticketbroker", "fund", "--deposit", "1", "--recipient", "not-an-address"}, "invalid"}, + {[]string{"ticketbroker", "fund", "--deposit", "1", "--recipient", "0x0000000000000000000000000000000000000000"}, "recipient must be nonempty"}, + {[]string{"ticketbroker", "fund", "--deposit", "115792089237316195423570985008687907853269984665640564039457584007913129639935", "--reserve", "1", "--base-units"}, "total exceeds uint256"}, {[]string{"ticketbroker", "unlock", "--no-wait", "--max-transaction-replacements", "1"}, "no-wait"}, {[]string{"ticketbroker", "unlock", "--submit"}, "keystore-password"}, {[]string{"ticketbroker", "unlock", "--submit", "--keystore-file", path}, "keystore-password-file"}, @@ -163,7 +168,7 @@ func TestConfigRejectsInvocationInputs(t *testing.T) { load func([]byte) error entries []string }{ - {"ticketbroker fund", loadInvocationConfig[FundParams], []string{`Amount = "1"`, `Reserve = "1"`, `BaseUnits = true`}}, + {"ticketbroker fund", loadInvocationConfig[FundParams], []string{`Deposit = "1"`, `Reserve = "1"`, `BaseUnits = true`, `Recipient = "` + testAccount + `"`}}, {"stake bond", loadInvocationConfig[BondParams], []string{`Orchestrator = "` + testAccount + `"`, `Redelegate = true`}}, {"stake cancel-unbond", loadInvocationConfig[CancelUnbondParams], []string{`Delegate = "` + testAccount + `"`, `LockID = "0"`}}, {"earnings withdraw-fees", loadInvocationConfig[WithdrawFeesParams], []string{`Recipient = "` + testAccount + `"`}}, @@ -227,7 +232,7 @@ func TestDataDirectoryAndConfigDiscovery(t *testing.T) { } func TestActionEnvironmentCannotSupplyInputs(t *testing.T) { - for _, name := range []string{"AMOUNT", "RESERVE", "DELEGATE", "ORCHESTRATOR", "RECIPIENT", "LOCK_ID", "END_ROUND", "REWARD_CUT", "FEE_CUT", "MAX_PRIORITY_FEE_PER_GAS", "TRANSACTION_TIMEOUT", "MAX_TRANSACTION_REPLACEMENTS", "GAS_LIMIT", "SERVICE_URI", "OUTPUT"} { + for _, name := range []string{"AMOUNT", "DEPOSIT", "RESERVE", "DELEGATE", "ORCHESTRATOR", "RECIPIENT", "LOCK_ID", "END_ROUND", "REWARD_CUT", "FEE_CUT", "MAX_PRIORITY_FEE_PER_GAS", "TRANSACTION_TIMEOUT", "MAX_TRANSACTION_REPLACEMENTS", "GAS_LIMIT", "SERVICE_URI", "OUTPUT"} { t.Setenv("LIVEPEER_CHAIN_"+name, "invalid") } for _, name := range []string{"SUBMIT", "WAIT", "QUIET", "PRINT_CONFIG"} { @@ -239,6 +244,12 @@ func TestActionEnvironmentCannotSupplyInputs(t *testing.T) { root.SetArgs([]string{"stake", "unbond"}) require.ErrorContains(t, root.Execute(), "missing required param 'amount'") require.Empty(t, output.String()) + t.Setenv("LIVEPEER_CHAIN_DEPOSIT", "1") + t.Setenv("LIVEPEER_CHAIN_RESERVE", "1") + root = Root(&output, &output) + root.SetArgs([]string{"ticketbroker", "fund"}) + require.ErrorContains(t, root.Execute(), "cannot both be zero") + require.Empty(t, output.String()) root = Root(&output, &output) root.SetArgs([]string{"orchestrator", "set-config", "--print-config"}) require.NoError(t, root.Execute()) diff --git a/chain/management_test.go b/chain/management_test.go index 864931a..6c0f41f 100644 --- a/chain/management_test.go +++ b/chain/management_test.go @@ -405,7 +405,14 @@ func TestResolvedAmountsAndCalldata(t *testing.T) { {"governance poll vote 0x0000000000000000000000000000000000000009 no", "vote(uint256)", []string{uintWord(1)}}, {"governance proposal vote 42 abstain", "castVote(uint256,uint8)", []string{uintWord(42), uintWord(2)}}, {"stake withdraw --lock-id 0", "withdrawStake(uint256)", []string{uintWord(0)}}, + {"ticketbroker fund --deposit 0.000000000000000001 --reserve 0.000000000000000002", "fundDepositAndReserve(uint256,uint256)", []string{uintWord(1), uintWord(2)}}, {"ticketbroker fund --amount 0.000000000000000001 --reserve 0.000000000000000002", "fundDepositAndReserve(uint256,uint256)", []string{uintWord(1), uintWord(2)}}, + {"ticketbroker fund --amount 1 --deposit 2 --base-units", "fundDepositAndReserve(uint256,uint256)", []string{uintWord(2), uintWord(0)}}, + {"ticketbroker fund --deposit 1 --amount 2 --base-units --recipient " + testAccount, "fundDepositAndReserveFor(address,uint256,uint256)", []string{addressWord(testAccount), uintWord(2), uintWord(0)}}, + {"ticketbroker fund --deposit 1 --base-units", "fundDepositAndReserve(uint256,uint256)", []string{uintWord(1), uintWord(0)}}, + {"ticketbroker fund --reserve 2 --base-units", "fundDepositAndReserve(uint256,uint256)", []string{uintWord(0), uintWord(2)}}, + {"ticketbroker fund --deposit 1 --base-units --recipient " + testAccount, "fundDepositAndReserveFor(address,uint256,uint256)", []string{addressWord(testAccount), uintWord(1), uintWord(0)}}, + {"ticketbroker fund --reserve 2 --base-units --recipient " + testAccount, "fundDepositAndReserveFor(address,uint256,uint256)", []string{addressWord(testAccount), uintWord(0), uintWord(2)}}, } { t.Run(tc.command, func(t *testing.T) { f := newManagementFixture(t) diff --git a/chain/params.go b/chain/params.go index fcf90f1..3e73186 100644 --- a/chain/params.go +++ b/chain/params.go @@ -143,8 +143,10 @@ type WithdrawFeesParams struct { } type FundParams struct { TransactionOptions - AmountOptions - Reserve string `required:"true" boa:"noconfig,noenv" descr:"Reserve amount in human ETH units, or wei with --base-units"` + Deposit string `default:"0" boa:"noconfig,noenv" descr:"Deposit amount in human ETH units, or wei with --base-units"` + Reserve string `default:"0" boa:"noconfig,noenv" descr:"Reserve amount in human ETH units, or wei with --base-units"` + BaseUnits bool `optional:"true" boa:"noconfig,noenv" descr:"Interpret deposit and reserve as integer wei"` + Recipient *ethcommon.Address `boa:"noconfig,noenv" descr:"Account to fund; default: configured account"` } type TransferParams struct { TransactionOptions diff --git a/chain/transactions_test.go b/chain/transactions_test.go index 1fd1cce..606962a 100644 --- a/chain/transactions_test.go +++ b/chain/transactions_test.go @@ -26,6 +26,7 @@ func TestTransactionCommands(t *testing.T) { name string keySource int flags []string + recipient string broadcastErr bool receiptErr bool receiptRevert bool @@ -39,6 +40,7 @@ func TestTransactionCommands(t *testing.T) { {name: "simulate with unavailable password file", flags: []string{"--keystore-password-file", "/missing/password"}, wantErr: "secret file"}, {name: "env password submission", keySource: 3, flags: []string{"--submit"}, wantSent: 1, wantReceipt: true}, {name: "explicit submission", flags: []string{"--submit"}, wantSent: 1, wantReceipt: true}, + {name: "fund another account", flags: []string{"--submit"}, recipient: testAccount, wantSent: 1, wantReceipt: true}, {name: "keystore environment overrides config", keySource: 1, flags: []string{"--submit"}, wantSent: 1, wantReceipt: true}, {name: "keystore CLI overrides environment", keySource: 2, flags: []string{"--submit"}, wantSent: 1, wantReceipt: true}, {name: "receipt failure", flags: []string{"--submit", "--wait"}, receiptErr: true, wantReceipt: true, wantErr: "receipt", wantSent: 1}, @@ -61,6 +63,12 @@ func TestTransactionCommands(t *testing.T) { value := new(big.Int).Add(deposit, reserve) data := append(crypto.Keccak256([]byte("fundDepositAndReserve(uint256,uint256)"))[:4], ethcommon.LeftPadBytes(deposit.Bytes(), 32)...) data = append(data, ethcommon.LeftPadBytes(reserve.Bytes(), 32)...) + if tc.recipient != "" { + data = append(crypto.Keccak256([]byte("fundDepositAndReserveFor(address,uint256,uint256)"))[:4], ethcommon.LeftPadBytes(ethcommon.HexToAddress(tc.recipient).Bytes(), 32)...) + data = append(data, ethcommon.LeftPadBytes(deposit.Bytes(), 32)...) + data = append(data, ethcommon.LeftPadBytes(reserve.Bytes(), 32)...) + tc.flags = append(tc.flags, "--recipient", tc.recipient) + } f.simulationHook = func(from, to ethcommon.Address, input []byte, amount string) { require.Equal(t, key.Address(), from) require.Equal(t, broker, to) @@ -103,7 +111,7 @@ func TestTransactionCommands(t *testing.T) { require.NoError(t, os.WriteFile(configFile, []byte(config), 0600)) var output bytes.Buffer root := Root(&output, &output) - args := append([]string{"ticketbroker", "fund", "--config", configFile, "--amount", deposit.String(), "--reserve", reserve.String(), "--base-units", "--output", "json"}, tc.flags...) + args := append([]string{"ticketbroker", "fund", "--config", configFile, "--deposit", deposit.String(), "--reserve", reserve.String(), "--base-units", "--output", "json"}, tc.flags...) root.SetArgs(args) err = root.Execute() if tc.wantErr == "" { diff --git a/configs/chain/config.example.toml b/configs/chain/config.example.toml index f983756..1b5d2fa 100644 --- a/configs/chain/config.example.toml +++ b/configs/chain/config.example.toml @@ -15,7 +15,7 @@ RPCURLFile = "/run/secrets/livepeer-chain-rpc-url" # Action inputs and output controls must be provided on the command line. # Example dry run: -# livepeer chain --config /path/to/chain.toml --output json ticketbroker fund --amount 1 --reserve 0 +# livepeer chain --config /path/to/chain.toml --output json ticketbroker fund --deposit 1 # Add --submit to broadcast and wait for inclusion, or --quiet to suppress output. # --no-wait broadcasts a single transaction without waiting. # Amounts use human ETH/LPT units; --base-units selects integer wei/token units. diff --git a/docs/chain.md b/docs/chain.md index d32543b..719effe 100644 --- a/docs/chain.md +++ b/docs/chain.md @@ -96,15 +96,19 @@ rewards for the orchestrator; `unset` clears that authorization. ```text ticketbroker get -ticketbroker fund --amount DEPOSIT --reserve RESERVE +ticketbroker fund [--deposit ETH] [--reserve ETH] [--recipient ADDRESS] ticketbroker unlock ticketbroker cancel-unlock ticketbroker withdraw ``` -Funding sends the sum of deposit and reserve, which must be positive. Unlock -starts the withdrawal period; cancel-unlock cancels it. Check `ticketbroker get` -for balances and the withdrawal round, then withdraw once that round is reached. +Set `--deposit`, `--reserve`, or both to fund them in one transaction. Omitted +amounts default to zero; at least one must be positive. The configured wallet +pays; use `--recipient ADDRESS` to fund another account. + +Unlock starts the withdrawal period; cancel-unlock cancels it. Check +`ticketbroker get` for balances and the withdrawal round, then withdraw once that +round is reached. Orchestrators act as redeemers, submitting winning tickets against the payer's funds. See [payment recovery](payment-recovery.md#orchestrator-transaction-recovery) for diff --git a/go.mod b/go.mod index 6a8c45d..645cf31 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 github.com/segmentio/kafka-go v0.4.51 github.com/spf13/cobra v1.10.2 + github.com/spf13/pflag v1.0.10 github.com/stretchr/testify v1.12.1 google.golang.org/protobuf v1.36.12 modernc.org/sqlite v1.61.0 @@ -49,7 +50,6 @@ require ( github.com/prometheus/procfs v0.22.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect - github.com/spf13/pflag v1.0.10 // indirect github.com/supranational/blst v0.3.16 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect