Skip to content
This repository has been archived by the owner on Nov 12, 2021. It is now read-only.

Recover deposit #62

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 35 additions & 34 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ const (
BENCH
)

const (
// How long do we wait for the deposit proof after the deposit phase is over.
depositProofGrace = time.Second * 30
)

// Trust describes how we perceive the operator.
type Trust = string

Expand Down Expand Up @@ -305,11 +300,11 @@ func (c *Client) CmdDeposit(status chan *CmdStatus, args ...string) {
}
}()
go func() {
depEndBlock := c.params.DepositDoneBlock(epoch)
depEndBlock := c.params.TxDoneBlock(epoch)
c.logOffChain("Waiting for deposit proof until block #%d", depEndBlock)
err := c.ethClient.WaitForBlock(ctx, depEndBlock) // Add PowDepth here if needed
if err == nil {
c.logOffChain("Deposit Phase for Epoch %d ended in block %d", epoch, depEndBlock)
time.Sleep(depositProofGrace)
}
waitErr <- err
}()
Expand Down Expand Up @@ -350,8 +345,8 @@ func (c *Client) CmdDeposit(status chan *CmdStatus, args ...string) {
status <- &CmdStatus{War: "Deposit proof: Operator timed out - resuming protocol"}
c.setOpTrust(UNKNOWN)
}
// TODO challenge
status <- &CmdStatus{Err: errors.New("TODO challenge")}

c.challenge(status)
}

// BalanceProofWatcher waits for the balance proof of an epoch and disputes
Expand Down Expand Up @@ -410,15 +405,8 @@ func (c *Client) FrozenWatcher() {
func (c *Client) handleFrozen(event *bindings.ErdstallFrozen) {
epoch := event.Epoch
c.setOpTrust(UNTRUSTED)
c.log("❄️ Contract Frozen in epoch #%d", epoch)
c.log("❄️Contract Frozen in epoch #%d", epoch)

c.balMtx.Lock()
defer c.balMtx.Unlock()
bal, ok := c.balances[epoch]
if !ok {
c.logOffChain("No balance-proof available for freeze")
return
}
status := make(chan *CmdStatus)
defer close(status)
go func() {
Expand All @@ -430,14 +418,30 @@ func (c *Client) handleFrozen(event *bindings.ErdstallFrozen) {
}
}
}()
_, err := c.sendTx("WithdrawFrozen", func(auth *bind.TransactOpts) (*types.Transaction, error) {
return c.contract.WithdrawFrozen(auth, bindings.ErdstallBalance{Epoch: epoch, Account: bal.Account, Value: bal.Value}, bal.Bal.Sig)
}, status)
if err != nil {
status <- &CmdStatus{Err: fmt.Errorf("WithdrawFrozen TX: %w", err)}
return

c.balMtx.Lock()
bal, ok := c.balances[epoch]
c.balMtx.Unlock()
// Use WithdrawFrozen if we have a balance-proof and RecoverDeposit otherwise.
if ok {
_, err := c.sendTx("WithdrawFrozen", func(auth *bind.TransactOpts) (*types.Transaction, error) {
return c.contract.WithdrawFrozen(auth, bindings.ErdstallBalance{Epoch: epoch, Account: bal.Account, Value: bal.Value}, bal.Bal.Sig)
}, status)
if err != nil {
status <- &CmdStatus{Err: fmt.Errorf("WithdrawFrozen TX: %w", err)}
return
}
c.logOnChain("❄️WithdrawFrozen: Complete")
} else {
_, err := c.sendTx("RecoverDeposit", func(auth *bind.TransactOpts) (*types.Transaction, error) {
return c.contract.RecoverDeposit(auth)
}, status)
if err != nil {
status <- &CmdStatus{Err: fmt.Errorf("❄RecoverDeposit TX: %w", err)}
return
}
c.logOnChain("❄️RecoverDeposit: Complete")
}
c.log("❄️ WithdrawFrozen: Complete")
}

func (c *Client) lastBal() *EpochBalance {
Expand Down Expand Up @@ -485,13 +489,16 @@ func (c *Client) CmdChallenge(status chan *CmdStatus, args ...string) {
lastBlock := atomic.LoadUint64(&c.lastBlock)
if c.params.IsChallengeResponsePhase(lastBlock + 1) {
status <- &CmdStatus{Msg: "Waiting for next epoch"}
next := c.params.DepositDoneBlock(c.params.ExitEpoch(lastBlock + 1))
next := c.params.DepositDoneBlock(c.params.DepositEpoch(lastBlock + 1))
if err := c.ethClient.WaitForBlock(c.Ctx(), next); err != nil {
status <- &CmdStatus{Err: err}
return
}
}
c.challenge(status)
}

func (c *Client) challenge(status chan *CmdStatus) {
tx, err := c.sendTx("Challenge", func(auth *bind.TransactOpts) (*types.Transaction, error) {
return c.contract.Challenge(auth)
}, status)
Expand Down Expand Up @@ -521,7 +528,7 @@ func (c *Client) CmdChallenge(status chan *CmdStatus, args ...string) {

select {
case <-done: // ChallengeReponse phase is over, freeze.
c.log("❄️ Freezing Contract")
c.log("❄️Freezing Contract")
_, err := c.sendTx("Freeze", func(auth *bind.TransactOpts) (*types.Transaction, error) {
return c.contract.Freeze(auth)
}, status)
Expand Down Expand Up @@ -555,14 +562,8 @@ func (c *Client) sendTx(name string, f func(*bind.TransactOpts) (*types.Transact
return nil, err
}
status <- &CmdStatus{Msg: name + " TX: Mining"}
receipt, err := bind.WaitMined(txCtx(), c.ethClient, tx)
if err != nil {
return nil, err
}
if receipt.Status == types.ReceiptStatusFailed {
return nil, errors.New("Receipt failed")
}
return receipt, nil

return c.ethClient.ConfirmTransaction(txCtx(), tx, c.ethClient.Account())
}

func (c *Client) withdraw(exitEpoch uint64, status chan *CmdStatus) {
Expand Down
5 changes: 1 addition & 4 deletions client/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,10 @@ func (c *Client) call(ctx context.Context, call func(*bind.TransactOpts) (*types
return fmt.Errorf("calling contract: %w", err)
}

rec, err := bind.WaitMined(ctx, c.ethClient, tx)
rec, err := c.ethClient.ConfirmTransaction(ctx, tx, c.ethClient.Account())
if err != nil {
return fmt.Errorf("waiting for block containing TX: %w", err)
}
if rec.Status == types.ReceiptStatusFailed {
return fmt.Errorf("execution of contract call failed")
}
c.SetMinedBlockNum(uint64(rec.BlockNumber.Int64()))

return nil
Expand Down
4 changes: 3 additions & 1 deletion cmd/operator/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"ResponseDuration": 1,
"PowDepth": 0,
"Port": 8080,
"RespondChallenges": true
"RespondChallenges": true,
"SendDepositProofs": true,
"SendBalanceProofs": true
}
2 changes: 1 addition & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}
log.SetLevel(lvl)

_operator := operator.Setup(cfg)
_operator := operator.Setup(*cfg)
err = _operator.Serve(cfg.Port)
operator.AssertNoError(err)
}
1 change: 0 additions & 1 deletion eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func NewClientForWallet(
// NewTransactor creates a new transactor.
func (cl *Client) NewTransactor(ctx context.Context) (*bind.TransactOpts, error) {
tr, err := cl.ContractBackend.NewTransactor(ctx,
big.NewInt(0),
DefaultGasLimit,
cl.account)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
perun.network/go-perun v0.5.2
perun.network/go-perun v0.5.3-0.20201130161943-52ca47c0a417
)
Loading