cnct: add new from reader resolver initializers
This commit is contained in:
parent
ff63a680a1
commit
1e5eec990e
@ -469,54 +469,38 @@ func (b *boltArbitratorLog) FetchUnresolvedContracts() ([]ContractResolver, erro
|
||||
|
||||
switch resType {
|
||||
case resolverTimeout:
|
||||
timeoutRes := &htlcTimeoutResolver{}
|
||||
if err := timeoutRes.Decode(resReader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res = timeoutRes
|
||||
res, err = newTimeoutResolverFromReader(
|
||||
resReader, resolverCfg,
|
||||
)
|
||||
|
||||
case resolverSuccess:
|
||||
successRes := &htlcSuccessResolver{}
|
||||
if err := successRes.Decode(resReader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res = successRes
|
||||
res, err = newSuccessResolverFromReader(
|
||||
resReader, resolverCfg,
|
||||
)
|
||||
|
||||
case resolverOutgoingContest:
|
||||
outContestRes := &htlcOutgoingContestResolver{
|
||||
htlcTimeoutResolver: htlcTimeoutResolver{},
|
||||
}
|
||||
if err := outContestRes.Decode(resReader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res = outContestRes
|
||||
res, err = newOutgoingContestResolverFromReader(
|
||||
resReader, resolverCfg,
|
||||
)
|
||||
|
||||
case resolverIncomingContest:
|
||||
inContestRes := &htlcIncomingContestResolver{
|
||||
htlcSuccessResolver: htlcSuccessResolver{},
|
||||
}
|
||||
if err := inContestRes.Decode(resReader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res = inContestRes
|
||||
res, err = newIncomingContestResolverFromReader(
|
||||
resReader, resolverCfg,
|
||||
)
|
||||
|
||||
case resolverUnilateralSweep:
|
||||
sweepRes := &commitSweepResolver{}
|
||||
if err := sweepRes.Decode(resReader); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res = sweepRes
|
||||
res, err = newCommitSweepResolverFromReader(
|
||||
resReader, resolverCfg,
|
||||
)
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unknown resolver type: %v", resType)
|
||||
}
|
||||
|
||||
res.AttachConfig(resolverCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
contracts = append(contracts, res)
|
||||
return nil
|
||||
})
|
||||
|
@ -262,35 +262,40 @@ func (c *commitSweepResolver) Encode(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode attempts to decode an encoded ContractResolver from the passed Reader
|
||||
// instance, returning an active ContractResolver instance.
|
||||
//
|
||||
// NOTE: Part of the ContractResolver interface.
|
||||
func (c *commitSweepResolver) Decode(r io.Reader) error {
|
||||
// newCommitSweepResolverFromReader attempts to decode an encoded
|
||||
// ContractResolver from the passed Reader instance, returning an active
|
||||
// ContractResolver instance.
|
||||
func newCommitSweepResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
||||
*commitSweepResolver, error) {
|
||||
|
||||
c := &commitSweepResolver{
|
||||
contractResolverKit: *newContractResolverKit(resCfg),
|
||||
}
|
||||
|
||||
if err := decodeCommitResolution(r, &c.commitResolution); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := binary.Read(r, endian, &c.resolved); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Read(r, endian, &c.broadcastHeight); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
_, err := io.ReadFull(r, c.chanPoint.Hash[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
err = binary.Read(r, endian, &c.chanPoint.Index)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Previously a sweep tx was deserialized at this point. Refactoring
|
||||
// removed this, but keep in mind that this data may still be present in
|
||||
// the database.
|
||||
|
||||
return nil
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// AttachConfig should be called once a resolved is successfully decoded from
|
||||
|
@ -296,18 +296,27 @@ func (h *htlcIncomingContestResolver) Encode(w io.Writer) error {
|
||||
return h.htlcSuccessResolver.Encode(w)
|
||||
}
|
||||
|
||||
// Decode attempts to decode an encoded ContractResolver from the passed Reader
|
||||
// instance, returning an active ContractResolver instance.
|
||||
//
|
||||
// NOTE: Part of the ContractResolver interface.
|
||||
func (h *htlcIncomingContestResolver) Decode(r io.Reader) error {
|
||||
// newIncomingContestResolverFromReader attempts to decode an encoded ContractResolver
|
||||
// from the passed Reader instance, returning an active ContractResolver
|
||||
// instance.
|
||||
func newIncomingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
||||
*htlcIncomingContestResolver, error) {
|
||||
|
||||
h := &htlcIncomingContestResolver{}
|
||||
|
||||
// We'll first read the one field unique to this resolver.
|
||||
if err := binary.Read(r, endian, &h.htlcExpiry); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Then we'll decode our internal resolver.
|
||||
return h.htlcSuccessResolver.Decode(r)
|
||||
successResolver, err := newSuccessResolverFromReader(r, resCfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.htlcSuccessResolver = *successResolver
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// AttachConfig should be called once a resolved is successfully decoded from
|
||||
|
@ -181,12 +181,19 @@ func (h *htlcOutgoingContestResolver) Encode(w io.Writer) error {
|
||||
return h.htlcTimeoutResolver.Encode(w)
|
||||
}
|
||||
|
||||
// Decode attempts to decode an encoded ContractResolver from the passed Reader
|
||||
// instance, returning an active ContractResolver instance.
|
||||
//
|
||||
// NOTE: Part of the ContractResolver interface.
|
||||
func (h *htlcOutgoingContestResolver) Decode(r io.Reader) error {
|
||||
return h.htlcTimeoutResolver.Decode(r)
|
||||
// newOutgoingContestResolverFromReader attempts to decode an encoded ContractResolver
|
||||
// from the passed Reader instance, returning an active ContractResolver
|
||||
// instance.
|
||||
func newOutgoingContestResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
||||
*htlcOutgoingContestResolver, error) {
|
||||
|
||||
h := &htlcOutgoingContestResolver{}
|
||||
timeoutResolver, err := newTimeoutResolverFromReader(r, resCfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.htlcTimeoutResolver = *timeoutResolver
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// AttachConfig should be called once a resolved is successfully decoded from
|
||||
|
@ -290,32 +290,37 @@ func (h *htlcSuccessResolver) Encode(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode attempts to decode an encoded ContractResolver from the passed Reader
|
||||
// instance, returning an active ContractResolver instance.
|
||||
//
|
||||
// NOTE: Part of the ContractResolver interface.
|
||||
func (h *htlcSuccessResolver) Decode(r io.Reader) error {
|
||||
// newSuccessResolverFromReader attempts to decode an encoded ContractResolver
|
||||
// from the passed Reader instance, returning an active ContractResolver
|
||||
// instance.
|
||||
func newSuccessResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
||||
*htlcSuccessResolver, error) {
|
||||
|
||||
h := &htlcSuccessResolver{
|
||||
contractResolverKit: *newContractResolverKit(resCfg),
|
||||
}
|
||||
|
||||
// First we'll decode our inner HTLC resolution.
|
||||
if err := decodeIncomingResolution(r, &h.htlcResolution); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Next, we'll read all the fields that are specified to the contract
|
||||
// resolver.
|
||||
if err := binary.Read(r, endian, &h.outputIncubating); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Read(r, endian, &h.resolved); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Read(r, endian, &h.broadcastHeight); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if _, err := io.ReadFull(r, h.payHash[:]); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// AttachConfig should be called once a resolved is successfully decoded from
|
||||
|
@ -406,34 +406,39 @@ func (h *htlcTimeoutResolver) Encode(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode attempts to decode an encoded ContractResolver from the passed Reader
|
||||
// instance, returning an active ContractResolver instance.
|
||||
//
|
||||
// NOTE: Part of the ContractResolver interface.
|
||||
func (h *htlcTimeoutResolver) Decode(r io.Reader) error {
|
||||
// newTimeoutResolverFromReader attempts to decode an encoded ContractResolver
|
||||
// from the passed Reader instance, returning an active ContractResolver
|
||||
// instance.
|
||||
func newTimeoutResolverFromReader(r io.Reader, resCfg ResolverConfig) (
|
||||
*htlcTimeoutResolver, error) {
|
||||
|
||||
h := &htlcTimeoutResolver{
|
||||
contractResolverKit: *newContractResolverKit(resCfg),
|
||||
}
|
||||
|
||||
// First, we'll read out all the mandatory fields of the
|
||||
// OutgoingHtlcResolution that we store.
|
||||
if err := decodeOutgoingResolution(r, &h.htlcResolution); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// With those fields read, we can now read back the fields that are
|
||||
// specific to the resolver itself.
|
||||
if err := binary.Read(r, endian, &h.outputIncubating); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Read(r, endian, &h.resolved); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if err := binary.Read(r, endian, &h.broadcastHeight); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := binary.Read(r, endian, &h.htlcIndex); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// AttachConfig should be called once a resolved is successfully decoded from
|
||||
|
Loading…
Reference in New Issue
Block a user