Ejemplo n.º 1
0
// NewUpdateTx is called on Channels which are in phase OPEN. It makes a new UpdateTx,
// signs it, saves it as MyProposedUpdateTx, and sends it to the Counterparty.
func (a *CallerAPI) NewUpdateTx(state []byte, channelID string, fast bool) error {
	var err error
	return a.DB.Update(func(tx *bolt.Tx) error {
		ch := &core.Channel{}
		ch, err = access.GetChannel(tx, channelID)
		if err != nil {
			return err
		}

		utx := ch.NewUpdateTx(state, fast)

		ev, err := core.SerializeUpdateTx(utx)
		if err != nil {
			return err
		}

		ch.SignProposedUpdateTx(ev, utx)

		err = a.CounterpartyClient.AddProposedUpdateTx(ev, ch.Counterparty.Address)
		if err != nil {
			return err
		}

		err = access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		return nil
	})
}
Ejemplo n.º 2
0
// This gets the channel from the judge and checks if it has changed, and does stuff if it has
func (a *CallerAPI) CheckChannel(chId string) error {
	return a.DB.Update(func(tx *bolt.Tx) error {
		ch, err := access.GetChannel(tx, chId)
		if err != nil {
			return err
		}

		b, err := a.JudgeClient.GetChannel(chId, ch.Judge.Address)
		if err != nil {
			return err
		}

		jch := &core.Channel{}
		json.Unmarshal(b, jch)

		// This means that the judge has signed the channel
		if ch.Phase == core.PENDING_OPEN && jch.Phase == core.OPEN {
			ch.Open(jch.OpeningTxEnvelope, jch.OpeningTx)
			if err != nil {
				return err
			}
		}

		err = access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		return nil
	})
}
func (a *CounterpartyAPI) AddFullUpdateTx(ev *wire.Envelope) error {
	return a.DB.Update(func(tx *bolt.Tx) error {
		utx := &wire.UpdateTx{}
		err := proto.Unmarshal(ev.Payload, utx)
		if err != nil {
			return err
		}
		ch, err := access.GetChannel(tx, utx.ChannelId)
		if err != nil {
			return err
		}

		err = ch.AddFullUpdateTx(ev, utx)
		if err != nil {
			return err
		}

		access.SetChannel(tx, ch)
		if err != nil {
			return errors.New("database error")
		}

		return nil
	})
}
Ejemplo n.º 4
0
// ProposeChannel is called to propose a new channel. It creates and signs an
// OpeningTx, sends it to the Counterparty and saves it in a new Channel.
func (a *CallerAPI) ProposeChannel(
	channelId string,
	state []byte,
	myPubkey []byte,
	theirPubkey []byte,
	holdPeriod uint64,
) (*core.Channel, error) {
	ch := &core.Channel{}
	err := a.DB.Update(func(tx *bolt.Tx) error {
		acct, err := access.GetAccount(tx, myPubkey)
		if err != nil {
			return err
		}

		cpt, err := access.GetCounterparty(tx, theirPubkey)
		if err != nil {
			return err
		}

		otx, err := acct.NewOpeningTx(channelId, cpt, state, holdPeriod)
		if err != nil {
			return err
		}

		ev, err := core.SerializeOpeningTx(otx)
		if err != nil {
			return err
		}

		acct.AppendSignature(ev)

		ch, err = core.NewChannel(ev, otx, acct, cpt)
		if err != nil {
			return err
		}

		err = a.CounterpartyClient.AddChannel(ev, cpt.Address)
		if err != nil {
			return err
		}

		err = access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return ch, nil
}
func (a *CounterpartyAPI) AddChannel(ev *wire.Envelope) error {
	var err error

	otx := &wire.OpeningTx{}
	err = proto.Unmarshal(ev.Payload, otx)
	if err != nil {
		return err
	}

	acct := &core.Account{}
	cpt := &core.Counterparty{}
	err = a.DB.Update(func(tx *bolt.Tx) error {
		_, nilErr := access.GetChannel(tx, otx.ChannelId)
		if nilErr == nil {
			return errors.New("channel already exists")
		}
		_, ok := nilErr.(*access.NilError)
		if !ok {
			return err
		}

		cpt, err = access.GetCounterparty(tx, otx.Pubkeys[0])
		if err != nil {
			return err
		}

		acct, err = access.GetAccount(tx, otx.Pubkeys[1])
		if err != nil {
			return err
		}

		err = acct.CheckOpeningTx(ev, cpt)
		if err != nil {
			return err
		}

		ch, err := core.NewChannel(ev, otx, acct, cpt)
		if err != nil {
			return err
		}

		access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 6
0
// CosignProposedUpdateTx cosigns the Channel's TheirProposedUpdateTx, saves it to
// LastFullUpdateTx, and sends it to the Counterparty.
func (a *CallerAPI) CosignProposedUpdateTx(channelID string) error {
	return a.DB.Update(func(tx *bolt.Tx) error {
		ch, err := access.GetChannel(tx, channelID)
		if err != nil {
			return err
		}

		ev := ch.CosignProposedUpdateTx()

		err = access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		err = a.CounterpartyClient.AddFullUpdateTx(ev, ch.Counterparty.Address)
		if err != nil {
			return err
		}

		return nil
	})
}
Ejemplo n.º 7
0
// AcceptChannel is called on Channels which are in phase PENDING_OPEN. It signs
// the Channel's OpeningTx and sends it to the Judge.
func (a *CallerAPI) AcceptChannel(channelID string) error {
	var err error
	return a.DB.Update(func(tx *bolt.Tx) error {
		var ch *core.Channel
		ch, err = access.GetChannel(tx, channelID)
		if err != nil {
			return err
		}

		ch.Account.AppendSignature(ch.OpeningTxEnvelope)

		err = access.SetChannel(tx, ch)
		if err != nil {
			return err
		}

		err = a.JudgeClient.AddChannel(ch.OpeningTxEnvelope, ch.Judge.Address)
		if err != nil {
			return err
		}

		return nil
	})
}