Skip to content

Commit 252a926

Browse files
committed
refactor(pm): colocate payment wire encoding under pm/wire
1 parent dec57ed commit 252a926

15 files changed

Lines changed: 80 additions & 80 deletions

File tree

‎architecture/imports_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func forbiddenImport(file, dep string) string {
5959
return "legacy, media or gRPC dependency"
6060
}
6161
}
62-
if strings.HasPrefix(dep, "google.golang.org/protobuf") && !strings.HasPrefix(file, "signercompat/") {
63-
return "Protobuf runtime outside signercompat"
62+
if strings.HasPrefix(dep, "google.golang.org/protobuf") && !strings.HasPrefix(file, "pm/wire/") {
63+
return "Protobuf runtime outside pm/wire"
6464
}
6565
if !strings.HasPrefix(dep, module) {
6666
return ""
@@ -82,8 +82,8 @@ func forbiddenImport(file, dep string) string {
8282
return "cross-component or shared-to-component import"
8383
}
8484
}
85-
if strings.HasPrefix(local, "signercompat") && !strings.HasPrefix(file, "signer/") && !strings.HasPrefix(file, "pm/") {
86-
return "signer compatibility import outside signer"
85+
if strings.HasPrefix(local, "pm/wire") && !strings.HasPrefix(file, "signer/") && !strings.HasPrefix(file, "pm/") {
86+
return "payment wire import outside signer or pm"
8787
}
8888
if (local == "pm" || strings.HasPrefix(local, "pm/")) && (strings.HasPrefix(file, "chain/") || strings.HasPrefix(file, "eth/")) {
8989
return "chain or Ethereum package imports payment implementation"

‎pm/engine.go‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
ethcommon "github.com/ethereum/go-ethereum/common"
1616
"github.com/ethereum/go-ethereum/crypto"
1717
"github.com/livepeer/node/eth"
18-
"github.com/livepeer/node/signercompat"
18+
"github.com/livepeer/node/pm/wire"
1919
)
2020

2121
type ChainSnapshot = eth.ChainSnapshot
@@ -94,10 +94,10 @@ func (e *Engine) MakeChallenge(ctx context.Context, runner, manifest string, sen
9494
return Challenge{}, err
9595
}
9696
randHash := crypto.Keccak256Hash(random)
97-
info := signercompat.OrchestratorInfo{Transcoder: service, Address: e.recipient.Bytes(), Price: signercompat.PriceInfo{PricePerUnit: price, UnitsPerPrice: 1},
98-
TicketParams: signercompat.TicketParams{Recipient: e.recipient.Bytes(), FaceValue: e.faceValue.Bytes(), WinProb: e.winProb.Bytes(), RecipientRandHash: randHash.Bytes(), Seed: seed, ExpirationBlock: new(big.Int).Add(snapshot.Block, big.NewInt(40)).Bytes(), Expiration: signercompat.ExpirationParams{CreationRound: snapshot.Round.Int64(), CreationRoundBlockHash: snapshot.RoundHash.Bytes()}},
99-
Auth: signercompat.AuthToken{Token: token, SessionID: manifest, Expiration: time.Now().Add(time.Hour).Unix()}}
100-
encoded := signercompat.EncodeOrchestratorInfo(info)
97+
info := wire.OrchestratorInfo{Transcoder: service, Address: e.recipient.Bytes(), Price: wire.PriceInfo{PricePerUnit: price, UnitsPerPrice: 1},
98+
TicketParams: wire.TicketParams{Recipient: e.recipient.Bytes(), FaceValue: e.faceValue.Bytes(), WinProb: e.winProb.Bytes(), RecipientRandHash: randHash.Bytes(), Seed: seed, ExpirationBlock: new(big.Int).Add(snapshot.Block, big.NewInt(40)).Bytes(), Expiration: wire.ExpirationParams{CreationRound: snapshot.Round.Int64(), CreationRoundBlockHash: snapshot.RoundHash.Bytes()}},
99+
Auth: wire.AuthToken{Token: token, SessionID: manifest, Expiration: time.Now().Add(time.Hour).Unix()}}
100+
encoded := wire.EncodeOrchestratorInfo(info)
101101
e.mu.Lock()
102102
defer e.mu.Unlock()
103103
tx, err := e.store.db.BeginTx(ctx, nil)
@@ -125,20 +125,20 @@ func (e *Engine) MakeChallenge(ctx context.Context, runner, manifest string, sen
125125
return Challenge{PaymentParams: base64.StdEncoding.EncodeToString(encoded), Orchestrator: service, ManifestID: manifest, PaymentURL: service + "/apps/" + runner + "/session/" + manifest + "/payment"}, nil
126126
}
127127

128-
func (e *Engine) ChallengeInfo(manifest string) (signercompat.OrchestratorInfo, error) {
128+
func (e *Engine) ChallengeInfo(manifest string) (wire.OrchestratorInfo, error) {
129129
var encoded []byte
130130
if err := e.store.db.QueryRow("SELECT info FROM payment_challenges WHERE manifest=?", manifest).Scan(&encoded); err != nil {
131-
return signercompat.OrchestratorInfo{}, err
131+
return wire.OrchestratorInfo{}, err
132132
}
133-
return signercompat.DecodeOrchestratorInfo(encoded)
133+
return wire.DecodeOrchestratorInfo(encoded)
134134
}
135135

136136
func (e *Engine) ChallengeForManifest(runner, manifest, service string) (Challenge, error) {
137137
info, err := e.ChallengeInfo(manifest)
138138
if err != nil {
139139
return Challenge{}, err
140140
}
141-
return Challenge{PaymentParams: base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), Orchestrator: service, ManifestID: manifest, PaymentURL: service + "/apps/" + runner + "/session/" + manifest + "/payment"}, nil
141+
return Challenge{PaymentParams: base64.StdEncoding.EncodeToString(wire.EncodeOrchestratorInfo(info)), Orchestrator: service, ManifestID: manifest, PaymentURL: service + "/apps/" + runner + "/session/" + manifest + "/payment"}, nil
142142
}
143143

144144
func (e *Engine) RefreshChallenge(ctx context.Context, manifest string, sender ethcommon.Address, service string) (Challenge, error) {
@@ -150,7 +150,7 @@ func (e *Engine) RefreshChallenge(ctx context.Context, manifest string, sender e
150150
if sender.Hex() != senderString {
151151
return Challenge{}, ErrInvalidPayment
152152
}
153-
info, err := signercompat.DecodeOrchestratorInfo(encoded)
153+
info, err := wire.DecodeOrchestratorInfo(encoded)
154154
if err != nil {
155155
return Challenge{}, err
156156
}
@@ -162,7 +162,7 @@ func ManifestFromSegment(header string) (string, error) {
162162
if err != nil {
163163
return "", ErrInvalidPayment
164164
}
165-
segment, err := signercompat.DecodeSegData(data)
165+
segment, err := wire.DecodeSegData(data)
166166
if err != nil || segment.Auth.SessionID == "" || string(segment.ManifestID) != segment.Auth.SessionID {
167167
return "", ErrInvalidPayment
168168
}
@@ -174,22 +174,22 @@ var ErrInsufficientBalance = errors.New("insufficient payment balance")
174174
var ErrMissingChallenge = errors.New("payment challenge not found")
175175
var ErrFixedAlreadyCharged = errors.New("fixed-price session already charged")
176176

177-
func decodeHeaders(paymentHeader, segmentHeader string) (signercompat.Payment, signercompat.SegData, error) {
177+
func decodeHeaders(paymentHeader, segmentHeader string) (wire.Payment, wire.SegData, error) {
178178
paymentBytes, err := base64.StdEncoding.DecodeString(paymentHeader)
179179
if err != nil {
180-
return signercompat.Payment{}, signercompat.SegData{}, ErrInvalidPayment
180+
return wire.Payment{}, wire.SegData{}, ErrInvalidPayment
181181
}
182182
segmentBytes, err := base64.StdEncoding.DecodeString(segmentHeader)
183183
if err != nil {
184-
return signercompat.Payment{}, signercompat.SegData{}, ErrInvalidPayment
184+
return wire.Payment{}, wire.SegData{}, ErrInvalidPayment
185185
}
186-
payment, err := signercompat.DecodePayment(paymentBytes)
186+
payment, err := wire.DecodePayment(paymentBytes)
187187
if err != nil {
188-
return signercompat.Payment{}, signercompat.SegData{}, ErrInvalidPayment
188+
return wire.Payment{}, wire.SegData{}, ErrInvalidPayment
189189
}
190-
segment, err := signercompat.DecodeSegData(segmentBytes)
190+
segment, err := wire.DecodeSegData(segmentBytes)
191191
if err != nil {
192-
return signercompat.Payment{}, signercompat.SegData{}, ErrInvalidPayment
192+
return wire.Payment{}, wire.SegData{}, ErrInvalidPayment
193193
}
194194
return payment, segment, nil
195195
}
@@ -211,13 +211,13 @@ func (e *Engine) Receive(ctx context.Context, runner, manifest, paymentHeader, s
211211
if err != nil {
212212
return ethcommon.Address{}, nil, err
213213
}
214-
info, err := signercompat.DecodeOrchestratorInfo(encoded)
214+
info, err := wire.DecodeOrchestratorInfo(encoded)
215215
if err != nil {
216216
return ethcommon.Address{}, nil, err
217217
}
218218
sender := ethcommon.BytesToAddress(payment.Sender)
219219
if storedRunner != runner || sender.Hex() != senderString || !bytes.Equal(segment.Auth.Token, info.Auth.Token) || segment.Auth.Expiration != info.Auth.Expiration || time.Now().Unix() > info.Auth.Expiration ||
220-
!bytes.Equal(signercompat.EncodeTicketParams(payment.TicketParams), signercompat.EncodeTicketParams(info.TicketParams)) ||
220+
!bytes.Equal(wire.EncodeTicketParams(payment.TicketParams), wire.EncodeTicketParams(info.TicketParams)) ||
221221
payment.Expiration.CreationRound != info.TicketParams.Expiration.CreationRound || !bytes.Equal(payment.Expiration.CreationRoundBlockHash, info.TicketParams.Expiration.CreationRoundBlockHash) ||
222222
payment.ExpectedPrice != info.Price || len(payment.SenderParams) == 0 || len(payment.SenderParams) > 100 {
223223
return ethcommon.Address{}, nil, ErrInvalidPayment
@@ -357,7 +357,7 @@ func (e *Engine) Charge(ctx context.Context, manifest string, now time.Time) err
357357
}
358358
return err
359359
}
360-
info, err := signercompat.DecodeOrchestratorInfo(encoded)
360+
info, err := wire.DecodeOrchestratorInfo(encoded)
361361
if err != nil {
362362
return err
363363
}
@@ -428,6 +428,6 @@ func (e *Engine) ChallengePrice(runner, manifest string) (int64, string, error)
428428
if err := e.store.db.QueryRow("SELECT info,unit FROM payment_challenges WHERE runner=? AND manifest=?", runner, manifest).Scan(&raw, &unit); err != nil {
429429
return 0, "", err
430430
}
431-
info, err := signercompat.DecodeOrchestratorInfo(raw)
431+
info, err := wire.DecodeOrchestratorInfo(raw)
432432
return info.Price.PricePerUnit, unit, err
433433
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package signercompat
1+
package wire
22

33
import (
44
"testing"
File renamed without changes.

signercompat/testdata/fuzz/FuzzDecodeEnvelopes/short-field renamed to pm/wire/testdata/fuzz/FuzzDecodeEnvelopes/short-field

File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Package wire preserves the retained fields and field numbers of the
22
// legacy unversioned remote-signer Protobuf envelopes. Unknown fields are
33
// skipped so current Go and Python runner clients can send their full messages.
4-
package signercompat
4+
package wire
55

66
import (
77
"errors"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package signercompat
1+
package wire
22

33
import (
44
"encoding/hex"

‎signer/auth.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/livepeer/node/destination"
14-
"github.com/livepeer/node/signercompat"
14+
"github.com/livepeer/node/pm/wire"
1515
)
1616

1717
type maxPrice struct {
@@ -50,7 +50,7 @@ func (s *Service) SetAuthWebhook(endpoint string, grants []string, caFile string
5050
return nil
5151
}
5252

53-
func (s *Service) authorizePayment(r *http.Request, req paymentRequest, price signercompat.PriceInfo, state *paymentState) error {
53+
func (s *Service) authorizePayment(r *http.Request, req paymentRequest, price wire.PriceInfo, state *paymentState) error {
5454
identity := r.Header.Get("Signer-Auth-Id")
5555
if identity != "" && state.AuthID != "" && state.AuthID != identity {
5656
return paymentFailure{403, "signer auth ID changed"}

‎signer/auth_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/livepeer/node/signercompat"
13+
"github.com/livepeer/node/pm/wire"
1414
"github.com/stretchr/testify/require"
1515
)
1616

@@ -45,7 +45,7 @@ func TestAuthWebhookCachePriceAndIdentity(t *testing.T) {
4545
}))
4646
defer webhook.Close()
4747
require.NoError(t, s.SetAuthWebhook(webhook.URL, []string{strings.TrimPrefix(webhook.URL, "http://")}, "", map[string]string{"X-Webhook-Secret": "configured"}))
48-
req := map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"}
48+
req := map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(wire.EncodeOrchestratorInfo(info)), "type": "fixed"}
4949
w := postPayment(t, s, req)
5050
require.Equal(t, 200, w.Code, w.Body.String())
5151
var first paymentResponse
@@ -86,7 +86,7 @@ func TestAuthWebhookPriceRejectsBeforeResponse(t *testing.T) {
8686
}))
8787
defer webhook.Close()
8888
require.NoError(t, s.SetAuthWebhook(webhook.URL, []string{strings.TrimPrefix(webhook.URL, "http://")}, "", nil))
89-
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"})
89+
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(wire.EncodeOrchestratorInfo(info)), "type": "fixed"})
9090
require.Equal(t, 481, w.Code, w.Body.String())
9191
require.NotContains(t, w.Body.String(), "segCreds")
9292
require.NotContains(t, w.Body.String(), "payment")
@@ -102,7 +102,7 @@ func TestExpiredParamsRequestRefresh(t *testing.T) {
102102
} else {
103103
info.TicketParams.ExpirationBlock = []byte{51}
104104
}
105-
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(signercompat.EncodeOrchestratorInfo(info)), "type": "fixed"})
105+
w := postPayment(t, s, map[string]any{"orchestrator": base64.StdEncoding.EncodeToString(wire.EncodeOrchestratorInfo(info)), "type": "fixed"})
106106
require.Equal(t, 480, w.Code, w.Body.String())
107107
require.Equal(t, info.Transcoder, w.Header().Get("Livepeer-Orchestrator-URL"))
108108
})

‎signer/payment.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
ethcommon "github.com/ethereum/go-ethereum/common"
1313
"github.com/ethereum/go-ethereum/crypto"
1414
"github.com/livepeer/node/pm"
15-
"github.com/livepeer/node/signercompat"
15+
"github.com/livepeer/node/pm/wire"
1616
)
1717

1818
type remoteSender struct {
@@ -44,7 +44,7 @@ type paymentState struct {
4444
ManifestID string
4545
}
4646

47-
func (s remoteSender) Generate(ctx context.Context, paymentType, manifest string, info signercompat.OrchestratorInfo, state paymentState, oldSequence int64) (paymentDraft, error) {
47+
func (s remoteSender) Generate(ctx context.Context, paymentType, manifest string, info wire.OrchestratorInfo, state paymentState, oldSequence int64) (paymentDraft, error) {
4848
if s.Signer == nil || (paymentType != "live" && paymentType != "fixed") || manifest == "" || oldSequence < -1 || oldSequence == math.MaxInt64 || info.Price.PricePerUnit <= 0 || info.Price.UnitsPerPrice <= 0 {
4949
return paymentDraft{}, errors.New("invalid remote payment request")
5050
}
@@ -96,22 +96,22 @@ func (s remoteSender) Generate(ctx context.Context, paymentType, manifest string
9696
if err != nil {
9797
return paymentDraft{}, err
9898
}
99-
senderParams := make([]signercompat.TicketSenderParams, 0, len(batch.SenderParams))
99+
senderParams := make([]wire.TicketSenderParams, 0, len(batch.SenderParams))
100100
for _, sp := range batch.SenderParams {
101-
senderParams = append(senderParams, signercompat.TicketSenderParams{SenderNonce: sp.SenderNonce, Sig: sp.Sig})
101+
senderParams = append(senderParams, wire.TicketSenderParams{SenderNonce: sp.SenderNonce, Sig: sp.Sig})
102102
}
103-
message := signercompat.Payment{TicketParams: info.TicketParams, Sender: s.Signer.Address().Bytes(), Expiration: info.TicketParams.Expiration, SenderParams: senderParams, ExpectedPrice: info.Price}
103+
message := wire.Payment{TicketParams: info.TicketParams, Sender: s.Signer.Address().Bytes(), Expiration: info.TicketParams.Expiration, SenderParams: senderParams, ExpectedPrice: info.Price}
104104
segHash := crypto.Keccak256(nil)
105105
flatten := append([]byte(manifest), make([]byte, 32)...)
106106
flatten = append(flatten, segHash...)
107107
sig, err := s.Signer.SignMessage(flatten)
108108
if err != nil {
109109
return paymentDraft{}, err
110110
}
111-
segment := signercompat.SegData{ManifestID: []byte(manifest), Hash: segHash, Signature: sig, Auth: info.Auth}
111+
segment := wire.SegData{ManifestID: []byte(manifest), Hash: segHash, Signature: sig, Auth: info.Auth}
112112
state.SenderNonce = batch.SenderParams[len(batch.SenderParams)-1].SenderNonce
113113
state.Balance = remaining.RatString()
114114
state.LastUpdate = now
115115
state.SequenceNumber = uint64(oldSequence + 1)
116-
return paymentDraft{Payment: base64.StdEncoding.EncodeToString(signercompat.EncodePayment(message)), SegCreds: base64.StdEncoding.EncodeToString(signercompat.EncodeSegData(segment)), State: state}, nil
116+
return paymentDraft{Payment: base64.StdEncoding.EncodeToString(wire.EncodePayment(message)), SegCreds: base64.StdEncoding.EncodeToString(wire.EncodeSegData(segment)), State: state}, nil
117117
}

0 commit comments

Comments
 (0)