Ejemplo n.º 1
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
}
func Test(t *testing.T) {
	otx, err := c1_Account.NewOpeningTx("shibby", c1_Counterparty, []byte{166, 179}, 86400)
	if err != nil {
		t.Fatal(err)
	}
	ev, err := c.SerializeOpeningTx(otx)
	if err != nil {
		t.Fatal(err)
	}

	c1_Account.AppendSignature(ev)

	ch1, err := c.NewChannel(ev, otx, c1_Account, c1_Counterparty)
	if err != nil {
		t.Fatal(err)
	}

	// --- Send to second party ---

	err = c2_Account.CheckOpeningTx(ev, c2_Counterparty)
	if err != nil {
		t.Fatal(err)
	}

	ch2, err := c.NewChannel(ev, otx, c2_Account, c2_Counterparty)
	if err != nil {
		t.Fatal(err)
	}
	c2_Account.AppendSignature(ev)

	// --- Send to judge ---

	jch, err := j_judge.AddChannel(ev, otx, j_c1, j_c2)
	if err != nil {
		t.Fatal(err)
	}

	jch.Confirm()

	// --- Send back to accounts ---

	ch1.Open(jch.OpeningTxEnvelope, jch.OpeningTx)
	if err != nil {
		t.Fatal(err)
	}

	ch2.Open(jch.OpeningTxEnvelope, jch.OpeningTx)
	if err != nil {
		t.Fatal(err)
	}

	// --- Make update tx

	utx := ch1.NewUpdateTx([]byte{164, 179}, false)

	utxEv, err := c.SerializeUpdateTx(utx)
	if err != nil {
		t.Fatal(err)
	}

	ch1.SignProposedUpdateTx(utxEv, utx)

	// --- Send to second party ---
	fmt.Println(utxEv.Signatures[0])
	err = ch2.AddProposedUpdateTx(utxEv, utx)
	if err != nil {
		t.Fatal(err)
	}

	ch2.CosignProposedUpdateTx()
	fmt.Println(utxEv.Signatures[1])

	//, --- Make follow on tx

	ftx := ch2.NewFollowOnTx([]byte{0, 4})

	ftxEv, err := c.SerializeFollowOnTx(ftx)
	if err != nil {
		t.Fatal(err)
	}

	ch2.AddFollowOnTx(ftxEv)

	// // --- Send to judge ---

	// err = jch.AddProposedUpdateTx(utxEv, utx)
	// if err != nil {
	// 	t.Fatal(err)
	// }

	// jch.Judge.AppendSignature(utxEv)

	// // --- Back to participants ---

	// _, err = ch1.AddFullUpdateTx(utxEv, utx)
	// if err != nil {
	// 	t.Fatal(err)
	// }

	// _, err = ch2.AddFullUpdateTx(utxEv, utx)
	// if err != nil {
	// 	t.Fatal(err)
	// }
}