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

Expose protobuf converters #384

Merged
merged 1 commit into from Mar 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions wire/protobuf/proposalmsgs.go
Expand Up @@ -150,7 +150,7 @@ func toBaseChannelProposal(protoProp *BaseChannelProposal) (prop client.BaseChan
if err != nil {
return prop, errors.WithMessage(err, "init bals")
}
prop.FundingAgreement = toBalances(protoProp.FundingAgreement)
prop.FundingAgreement = ToBalances(protoProp.FundingAgreement)
if err != nil {
return prop, errors.WithMessage(err, "funding agreement")
}
Expand Down Expand Up @@ -214,11 +214,12 @@ func toAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error)
return nil, errors.WithMessagef(err, "%d'th sub alloc", i)
}
}
alloc.Balances = toBalances(protoAlloc.Balances)
alloc.Balances = ToBalances(protoAlloc.Balances)
return alloc, nil
}

func toBalances(protoBalances *Balances) (balances channel.Balances) {
// ToBalances parses protobuf balances.
func ToBalances(protoBalances *Balances) (balances channel.Balances) {
balances = make([][]channel.Bal, len(protoBalances.Balances))
for i := range protoBalances.Balances {
balances[i] = toBalance(protoBalances.Balances[i])
Expand Down
2 changes: 1 addition & 1 deletion wire/protobuf/syncmsgs.go
Expand Up @@ -29,7 +29,7 @@ func toChannelSyncMsg(protoEnvMsg *Envelope_ChannelSyncMsg) (msg *client.Channel
msg.CurrentTX.Sigs[i] = make([]byte, len(protoMsg.CurrentTx.Sigs[i]))
copy(msg.CurrentTX.Sigs[i], protoMsg.CurrentTx.Sigs[i])
}
msg.CurrentTX.State, err = toState(protoMsg.CurrentTx.State)
msg.CurrentTX.State, err = ToState(protoMsg.CurrentTx.State)
return msg, err
}

Expand Down
19 changes: 11 additions & 8 deletions wire/protobuf/updatemsgs.go
Expand Up @@ -35,7 +35,7 @@ func toVirtualChannelFundingProposalMsg(protoEnvMsg *Envelope_VirtualChannelFund
protoMsg := protoEnvMsg.VirtualChannelFundingProposalMsg

msg = &client.VirtualChannelFundingProposalMsg{}
msg.Initial, err = toSignedState(protoMsg.Initial)
msg.Initial, err = ToSignedState(protoMsg.Initial)
if err != nil {
return nil, errors.WithMessage(err, "initial state")
}
Expand All @@ -54,7 +54,7 @@ func toVirtualChannelSettlementProposalMsg(protoEnvMsg *Envelope_VirtualChannelS
protoMsg := protoEnvMsg.VirtualChannelSettlementProposalMsg

msg = &client.VirtualChannelSettlementProposalMsg{}
msg.Final, err = toSignedState(protoMsg.Final)
msg.Final, err = ToSignedState(protoMsg.Final)
if err != nil {
return nil, errors.WithMessage(err, "final state")
}
Expand Down Expand Up @@ -90,12 +90,13 @@ func toChannelUpdate(protoUpdate *ChannelUpdateMsg) (update client.ChannelUpdate
update.ActorIdx = channel.Index(protoUpdate.ChannelUpdate.ActorIdx)
update.Sig = make([]byte, len(protoUpdate.Sig))
copy(update.Sig, protoUpdate.Sig)
update.State, err = toState(protoUpdate.ChannelUpdate.State)
update.State, err = ToState(protoUpdate.ChannelUpdate.State)
return update, err
}

func toSignedState(protoSignedState *SignedState) (signedState channel.SignedState, err error) {
signedState.Params, err = toParams(protoSignedState.Params)
// ToSignedState parses protobuf signed states.
func ToSignedState(protoSignedState *SignedState) (signedState channel.SignedState, err error) {
signedState.Params, err = ToParams(protoSignedState.Params)
if err != nil {
return signedState, err
}
Expand All @@ -104,11 +105,12 @@ func toSignedState(protoSignedState *SignedState) (signedState channel.SignedSta
signedState.Sigs[i] = make([]byte, len(protoSignedState.Sigs[i]))
copy(signedState.Sigs[i], protoSignedState.Sigs[i])
}
signedState.State, err = toState(protoSignedState.State)
signedState.State, err = ToState(protoSignedState.State)
return signedState, err
}

func toParams(protoParams *Params) (*channel.Params, error) {
// ToParams parses protobuf params.
func ToParams(protoParams *Params) (*channel.Params, error) {
app, err := toApp(protoParams.App)
if err != nil {
return nil, err
Expand All @@ -128,7 +130,8 @@ func toParams(protoParams *Params) (*channel.Params, error) {
return params, nil
}

func toState(protoState *State) (state *channel.State, err error) {
// ToState parses protobuf states.
func ToState(protoState *State) (state *channel.State, err error) {
state = &channel.State{}
copy(state.ID[:], protoState.Id)
state.Version = protoState.Version
Expand Down