Skip to content

Commit

Permalink
Merge pull request #14 from perun-network:refactor-transactor-client
Browse files Browse the repository at this point in the history
Separate signing and broadcasting of contract calls
  • Loading branch information
iljabvh committed Apr 30, 2024
2 parents fea237f + aeb5c7c commit 704f389
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 175 deletions.
19 changes: 9 additions & 10 deletions channel/adjudicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var DefaultChallengeDuration = time.Duration(20) * time.Second
type Adjudicator struct {
challengeDuration *time.Duration
log log.Embedding
StellarClient *client.Client
CB *client.ContractBackend
acc *wallet.Account
assetAddr xdr.ScAddress
perunAddr xdr.ScAddress
Expand All @@ -43,10 +43,10 @@ type Adjudicator struct {
}

// NewAdjudicator returns a new Adjudicator.
func NewAdjudicator(acc *wallet.Account, stellarClient *client.Client, perunID xdr.ScAddress, assetID xdr.ScAddress) *Adjudicator {
func NewAdjudicator(acc *wallet.Account, cb *client.ContractBackend, perunID xdr.ScAddress, assetID xdr.ScAddress) *Adjudicator {
return &Adjudicator{
challengeDuration: &DefaultChallengeDuration,
StellarClient: stellarClient,
CB: cb,
acc: acc,
perunAddr: perunID,
assetAddr: assetID,
Expand All @@ -65,10 +65,9 @@ func (a *Adjudicator) GetAssetAddr() xdr.ScAddress {
}

func (a *Adjudicator) Subscribe(ctx context.Context, cid pchannel.ID) (pchannel.AdjudicatorSubscription, error) {
c := a.StellarClient
perunAddr := a.GetPerunAddr()
assetAddr := a.GetAssetAddr()
return NewAdjudicatorSub(ctx, cid, c, perunAddr, assetAddr, a.challengeDuration)
return NewAdjudicatorSub(ctx, cid, a.CB, perunAddr, assetAddr, a.challengeDuration)
}

func (a *Adjudicator) Withdraw(ctx context.Context, req pchannel.AdjudicatorReq, smap pchannel.StateMap) error {
Expand All @@ -77,7 +76,7 @@ func (a *Adjudicator) Withdraw(ctx context.Context, req pchannel.AdjudicatorReq,
log.Println("Withdraw called")

if err := a.Close(ctx, req.Tx.State, req.Tx.Sigs); err != nil {
chanControl, errChanState := a.StellarClient.GetChannelInfo(ctx, a.perunAddr, req.Tx.State.ID)
chanControl, errChanState := a.CB.GetChannelInfo(ctx, a.perunAddr, req.Tx.State.ID)
if errChanState != nil {
return errChanState
}
Expand Down Expand Up @@ -110,15 +109,15 @@ func (a *Adjudicator) Withdraw(ctx context.Context, req pchannel.AdjudicatorReq,

func (a *Adjudicator) withdraw(ctx context.Context, req pchannel.AdjudicatorReq) error {
perunAddress := a.GetPerunAddr()
return a.StellarClient.Withdraw(ctx, perunAddress, req)
return a.CB.Withdraw(ctx, perunAddress, req)
}

func (a *Adjudicator) Close(ctx context.Context, state *pchannel.State, sigs []pwallet.Sig) error {

log.Println("Close called")
perunAddr := a.GetPerunAddr()

return a.StellarClient.Close(ctx, perunAddr, state, sigs)
return a.CB.Close(ctx, perunAddr, state, sigs)
}

// Register registers and disputes a channel.
Expand All @@ -133,11 +132,11 @@ func (a *Adjudicator) Register(ctx context.Context, req pchannel.AdjudicatorReq,

func (a *Adjudicator) Dispute(ctx context.Context, state *pchannel.State, sigs []pwallet.Sig) error {
contractAddress := a.GetPerunAddr()
return a.StellarClient.Dispute(ctx, contractAddress, state, sigs)
return a.CB.Dispute(ctx, contractAddress, state, sigs)
}

func (a *Adjudicator) ForceClose(ctx context.Context, state *pchannel.State, sigs []pwallet.Sig) error {
return a.StellarClient.ForceClose(ctx, a.perunAddr, state.ID)
return a.CB.ForceClose(ctx, a.perunAddr, state.ID)
}

func (a Adjudicator) Progress(ctx context.Context, req pchannel.ProgressReq) error {
Expand Down
10 changes: 5 additions & 5 deletions channel/adjudicator_sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (

type AdjEventSub struct {
challengeDuration *time.Duration
stellarClient *client.Client
cb *client.ContractBackend
chanControl wire.Control
cid pchannel.ID
perunAddr xdr.ScAddress
Expand All @@ -48,11 +48,11 @@ type AdjEventSub struct {
log log.Embedding
}

func NewAdjudicatorSub(ctx context.Context, cid pchannel.ID, stellarClient *client.Client, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress, challengeDuration *time.Duration) (pchannel.AdjudicatorSubscription, error) {
func NewAdjudicatorSub(ctx context.Context, cid pchannel.ID, cb *client.ContractBackend, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress, challengeDuration *time.Duration) (pchannel.AdjudicatorSubscription, error) {

sub := &AdjEventSub{
challengeDuration: challengeDuration,
stellarClient: stellarClient,
cb: cb,
chanControl: wire.Control{},
cid: cid,
perunAddr: perunAddr,
Expand All @@ -72,7 +72,7 @@ func NewAdjudicatorSub(ctx context.Context, cid pchannel.ID, stellarClient *clie

func (s *AdjEventSub) run(ctx context.Context) {
s.log.Log().Info("Listening for channel state changes")
chanControl, err := s.stellarClient.GetChannelInfo(ctx, s.perunAddr, s.cid)
chanControl, err := s.cb.GetChannelInfo(ctx, s.perunAddr, s.cid)
if err != nil {
s.subErrors <- err
}
Expand All @@ -95,7 +95,7 @@ polling:
finish(nil)
return
case <-time.After(s.pollInterval):
newChanInfo, err := s.stellarClient.GetChannelInfo(ctx, s.perunAddr, s.cid)
newChanInfo, err := s.cb.GetChannelInfo(ctx, s.perunAddr, s.cid)
newChanControl = newChanInfo.Control

if err != nil {
Expand Down
10 changes: 3 additions & 7 deletions channel/adjudicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,15 @@ func TestHappyChannel(t *testing.T) {
_, err = adjAlice.Subscribe(ctx, next.ID)
require.NoError(t, err)
require.NoError(t, adjAlice.Withdraw(ctxAliceWithdraw, reqAlice, nil))
// ev := subAlice.Next()
// if _, ok := ev.(pchannel.ConcludedEvent); ok {
// fmt.Println("event concluded is here")
// }

perunAddrAlice := adjAlice.GetPerunAddr()
stellarChanAlice, err := adjAlice.StellarClient.GetChannelInfo(ctx, perunAddrAlice, next.ID)
stellarChanAlice, err := adjAlice.CB.GetChannelInfo(ctx, perunAddrAlice, next.ID)
require.True(t, stellarChanAlice.Control.WithdrawnA)

require.NoError(t, err)
require.NoError(t, adjBob.Withdraw(ctx, reqBob, nil))
perunAddrBob := adjBob.GetPerunAddr()
stellarChanBob, err := adjBob.StellarClient.GetChannelInfo(ctxBobWithdraw, perunAddrBob, next.ID)
stellarChanBob, err := adjBob.CB.GetChannelInfo(ctxBobWithdraw, perunAddrBob, next.ID)

require.NoError(t, err)

Expand Down Expand Up @@ -156,7 +152,7 @@ func TestChannel_RegisterFinal(t *testing.T) {
require.NoError(t, adjAlice.Register(ctxAliceRegister, reqAlice, nil))

perunAddrAlice := adjAlice.GetPerunAddr()
stellarChanAlice, err := adjAlice.StellarClient.GetChannelInfo(ctx, perunAddrAlice, next.ID)
stellarChanAlice, err := adjAlice.CB.GetChannelInfo(ctx, perunAddrAlice, next.ID)

require.True(t, stellarChanAlice.Control.Disputed)

Expand Down
14 changes: 7 additions & 7 deletions channel/funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ const MaxIterationsUntilAbort = 20
const DefaultPollingInterval = time.Duration(6) * time.Second

type Funder struct {
stellarClient *client.Client
cb *client.ContractBackend
perunAddr xdr.ScAddress
assetAddr xdr.ScAddress
maxIters int
pollingInterval time.Duration
}

func NewFunder(acc *wallet.Account, stellarClient *client.Client, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress) *Funder {
func NewFunder(acc *wallet.Account, contractBackend *client.ContractBackend, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress) *Funder {
return &Funder{
stellarClient: stellarClient,
cb: contractBackend,
perunAddr: perunAddr,
assetAddr: assetAddr,
maxIters: MaxIterationsUntilAbort,
Expand Down Expand Up @@ -91,7 +91,7 @@ func (f *Funder) fundParty(ctx context.Context, req pchannel.FundingReq) error {
case <-time.After(f.pollingInterval):

log.Printf("%s: Polling for opened channel...\n", party)
chanState, err := f.stellarClient.GetChannelInfo(ctx, f.perunAddr, req.State.ID)
chanState, err := f.cb.GetChannelInfo(ctx, f.perunAddr, req.State.ID)
if err != nil {
log.Printf("%s: Error while polling for opened channel: %v\n", party, err)
continue
Expand Down Expand Up @@ -122,7 +122,7 @@ func (f *Funder) fundParty(ctx context.Context, req pchannel.FundingReq) error {
}

func (f *Funder) openChannel(ctx context.Context, req pchannel.FundingReq) error {
err := f.stellarClient.Open(ctx, f.perunAddr, req.Params, req.State)
err := f.cb.Open(ctx, f.perunAddr, req.Params, req.State)
if err != nil {
return errors.New("error while opening channel in party A")
}
Expand All @@ -140,11 +140,11 @@ func (f *Funder) FundChannel(ctx context.Context, state *pchannel.State, funderI
return errors.New("asset address is not equal to the address stored in the state")
}

return f.stellarClient.Fund(ctx, f.perunAddr, f.assetAddr, state.ID, funderIdx)
return f.cb.Fund(ctx, f.perunAddr, f.assetAddr, state.ID, funderIdx)
}

func (f *Funder) AbortChannel(ctx context.Context, state *pchannel.State) error {
return f.stellarClient.Abort(ctx, f.perunAddr, state)
return f.cb.Abort(ctx, f.perunAddr, state)
}

func getPartyByIndex(funderIdx pchannel.Index) string {
Expand Down
69 changes: 38 additions & 31 deletions channel/test/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ const (
)

type Setup struct {
t *testing.T
accs []*wallet.Account
ws []*wallet.EphemeralWallet
stellarClients []*client.Client
Rng *mathrand.Rand
funders []*channel.Funder
adjs []*channel.Adjudicator
assetID pchannel.Asset
t *testing.T
accs []*wallet.Account
ws []*wallet.EphemeralWallet
cbs []*client.ContractBackend
Rng *mathrand.Rand
funders []*channel.Funder
adjs []*channel.Adjudicator
assetID pchannel.Asset
}

func (s *Setup) GetStellarClients() []*client.Client {
return s.stellarClients
func (s *Setup) GetStellarClients() []*client.ContractBackend {
return s.cbs
}

func (s *Setup) GetFunders() []*channel.Funder {
Expand Down Expand Up @@ -97,7 +97,7 @@ func getDataFilePath(filename string) (string, error) {
return "", err
}

fp := filepath.Join(root, "", filename) //filepath.Join(root, "testdata", filename)
fp := filepath.Join(root, "", filename)
return fp, nil
}

Expand All @@ -124,27 +124,28 @@ func NewTestSetup(t *testing.T) *Setup {
assetContractID, err := types.NewStellarAssetFromScAddress(tokenAddress)
require.NoError(t, err)

stellarClients := NewStellarClients(kpsToFund)
cbs := NewContractBackendsFromKeys(kpsToFund[:2])

aliceClient := stellarClients[0]
aliceCB := cbs[0]
aliceWallet := ws[0]
bobClient := stellarClients[1]

bobCB := cbs[1]
bobWallet := ws[1]

channelAccs := []*wallet.Account{accs[0], accs[1]}
channelClients := []*client.Client{aliceClient, bobClient}
channelCBs := []*client.ContractBackend{aliceCB, bobCB}
channelWallets := []*wallet.EphemeralWallet{aliceWallet, bobWallet}

funders, adjs := CreateFundersAndAdjudicators(channelAccs, stellarClients, perunAddress, tokenAddress)
funders, adjs := CreateFundersAndAdjudicators(channelAccs, cbs, perunAddress, tokenAddress)

setup := Setup{
t: t,
accs: channelAccs,
ws: channelWallets,
stellarClients: channelClients,
funders: funders,
adjs: adjs,
assetID: assetContractID,
t: t,
accs: channelAccs,
ws: channelWallets,
cbs: channelCBs,
funders: funders,
adjs: adjs,
assetID: assetContractID,
}

return &setup
Expand All @@ -157,23 +158,29 @@ func SetupAccountsAndContracts(t *testing.T, deployerKp *keypair.Full, kps []*ke
require.NoError(t, MintToken(deployerKp, tokenAddress, tokenBalance, addr))
}
}

func CreateFundersAndAdjudicators(accs []*wallet.Account, clients []*client.Client, perunAddress, tokenAddress xdr.ScAddress) ([]*channel.Funder, []*channel.Adjudicator) {
func CreateFundersAndAdjudicators(accs []*wallet.Account, cbs []*client.ContractBackend, perunAddress, tokenAddress xdr.ScAddress) ([]*channel.Funder, []*channel.Adjudicator) {
funders := make([]*channel.Funder, len(accs))
adjs := make([]*channel.Adjudicator, len(accs))
for i, acc := range accs {
funders[i] = channel.NewFunder(acc, clients[i], perunAddress, tokenAddress)
adjs[i] = channel.NewAdjudicator(acc, clients[i], perunAddress, tokenAddress)
funders[i] = channel.NewFunder(acc, cbs[i], perunAddress, tokenAddress)
adjs[i] = channel.NewAdjudicator(acc, cbs[i], perunAddress, tokenAddress)
}
return funders, adjs
}

func NewStellarClients(kps []*keypair.Full) []*client.Client {
clients := make([]*client.Client, len(kps))
func NewContractBackendsFromKeys(kps []*keypair.Full) []*client.ContractBackend {
cbs := make([]*client.ContractBackend, len(kps))
// generate Configs
for i, kp := range kps {
clients[i] = client.New(kp)
cbs[i] = NewContractBackendFromKey(kp)
}
return clients
return cbs
}

func NewContractBackendFromKey(kp *keypair.Full) *client.ContractBackend {
trConfig := client.TransactorConfig{}
trConfig.SetKeyPair(kp)
return client.NewContractBackend(&trConfig)
}

func MakeRandPerunAccsWallets(count int) ([]*wallet.Account, []*keypair.Full, []*wallet.EphemeralWallet) {
Expand Down
Loading

0 comments on commit 704f389

Please sign in to comment.