Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 13 additions & 5 deletions chain/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,30 @@ 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 {
return nil, fmt.Errorf("invalid --reserve (interpreted as wei when --base-units is set): %w", err)
}
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) {
Expand Down
17 changes: 16 additions & 1 deletion chain/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"),
Expand Down
25 changes: 18 additions & 7 deletions chain/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down Expand Up @@ -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"},
Expand All @@ -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"},
Expand Down Expand Up @@ -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 + `"`}},
Expand Down Expand Up @@ -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"} {
Expand All @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions chain/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion chain/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestTransactionCommands(t *testing.T) {
name string
keySource int
flags []string
recipient string
broadcastErr bool
receiptErr bool
receiptRevert bool
Expand All @@ -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},
Expand All @@ -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)
Expand Down Expand Up @@ -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 == "" {
Expand Down
2 changes: 1 addition & 1 deletion configs/chain/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions docs/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading