Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Redemption flow actions #585

Merged
merged 34 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6d3e2da
Stub of the provide redemption signature action
lukasz-zimnoch Oct 22, 2020
2fa23da
Provide redemption signature action
lukasz-zimnoch Oct 22, 2020
f84d2b0
Merge branch 'master' into redemption-actions
lukasz-zimnoch Oct 23, 2020
43292b4
Simplify the iterator of past signature events
lukasz-zimnoch Oct 23, 2020
b13b76f
Remove the direct dependency on `gen/abi`
lukasz-zimnoch Oct 23, 2020
c120196
Add line break on start event casting
lukasz-zimnoch Oct 23, 2020
b5621ba
Implementation of the redemption proof action
lukasz-zimnoch Oct 23, 2020
230da52
Merge branch 'master' into redemption-actions
lukasz-zimnoch Oct 23, 2020
8bf4841
Set the right timeout for redemption proof action
lukasz-zimnoch Oct 23, 2020
db1536f
Move auxiliary types above `monitorAndAct`
lukasz-zimnoch Oct 26, 2020
3396b77
Change alias of the `pkg/chain` package
lukasz-zimnoch Oct 26, 2020
2307ccd
Update tbtc and use the `TBTCSystemEventLog`
lukasz-zimnoch Oct 26, 2020
4482cbd
Rename `BondedECDSAKeepFilterer`
lukasz-zimnoch Oct 26, 2020
513cc8f
Add TODO about chain reorgs
lukasz-zimnoch Oct 26, 2020
01696f3
Change signature lookup during action execution
lukasz-zimnoch Oct 26, 2020
6c03e6b
Optimize the start block when fetching past events
lukasz-zimnoch Oct 26, 2020
1c9aa09
Happy path test for provide redemption signature
lukasz-zimnoch Oct 27, 2020
1d4bcc1
Add GoSec exception for local chain randomness
lukasz-zimnoch Oct 27, 2020
073fcf0
Add remaining tests for provide signature action
lukasz-zimnoch Oct 27, 2020
5c55f3c
Happy path test for provide redemption proof
lukasz-zimnoch Oct 27, 2020
46e1780
Fix failing unit tests
lukasz-zimnoch Oct 27, 2020
fa9e034
Add remaining tests for provide proof action
lukasz-zimnoch Oct 27, 2020
a9a5a8d
Tidy the go.sum file
lukasz-zimnoch Oct 29, 2020
909ea90
Add comment to `BlockTimestamp` method
lukasz-zimnoch Oct 29, 2020
d507bf5
Add TODO about rate limiter
lukasz-zimnoch Oct 29, 2020
10f673d
Move TBTC chain interface to `pkg/chain`
lukasz-zimnoch Oct 29, 2020
2f77416
Fix import alias
lukasz-zimnoch Oct 29, 2020
2f38d37
Add discussion link to the TODOs section
lukasz-zimnoch Oct 29, 2020
c3df0ec
Remove signature submitted event lookup loop
lukasz-zimnoch Nov 9, 2020
5d17df5
Align mutex names in local chain implementations
lukasz-zimnoch Nov 9, 2020
d869e2d
Add ctx param to local chain constructor
lukasz-zimnoch Nov 9, 2020
2dca954
Add test for local `SubmitSignature` method
lukasz-zimnoch Nov 9, 2020
8cb25fd
Merge branch 'master' into redemption-actions
lukasz-zimnoch Nov 9, 2020
9f67bfa
Add comment about `IncreaseRedemptionFee` call
lukasz-zimnoch Nov 9, 2020
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
7 changes: 7 additions & 0 deletions pkg/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ type BondedECDSAKeep interface {

// GetOpenedTimestamp returns timestamp when the keep was created.
GetOpenedTimestamp(keepAddress common.Address) (time.Time, error)

// PastSignatureSubmittedEvents returns all signature submitted events
// for the given keep which occurred after the provided start block.
PastSignatureSubmittedEvents(
keepAddress string,
startBlock uint64,
) ([]*SignatureSubmittedEvent, error)
}
48 changes: 48 additions & 0 deletions pkg/chain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/keep-network/keep-ecdsa/pkg/chain/gen/abi"

"github.com/ethereum/go-ethereum/common"

"github.com/ipfs/go-log"
Expand Down Expand Up @@ -507,3 +510,48 @@ func (ec *EthereumChain) GetOpenedTimestamp(keepAddress common.Address) (time.Ti

return keepOpenTime, nil
}

// PastSignatureSubmittedEvents returns all signature submitted events
// for the given keep which occurred after the provided start block.
func (ec *EthereumChain) PastSignatureSubmittedEvents(
keepAddress string,
startBlock uint64,
) ([]*eth.SignatureSubmittedEvent, error) {
if !common.IsHexAddress(keepAddress) {
return nil, fmt.Errorf("invalid keep address: [%v]", keepAddress)
}

keepContractAbi, err := abi.NewBondedECDSAKeep(
common.HexToAddress(keepAddress),
ec.client,
)
if err != nil {
return nil, err
}

iterator, err := keepContractAbi.FilterSignatureSubmitted(
&bind.FilterOpts{Start: startBlock},
nil,
)
if err != nil {
return nil, err
}

result := make([]*eth.SignatureSubmittedEvent, 0)

hasNext := true
for hasNext {
hasNext = iterator.Next()
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
if hasNext {
event := iterator.Event
result = append(result, &eth.SignatureSubmittedEvent{
Digest: event.Digest,
R: event.R,
S: event.S,
RecoveryID: event.RecoveryID,
})
}
}

return result, nil
}
158 changes: 157 additions & 1 deletion pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,111 @@ func (tec *TBTCEthereumChain) OnDepositRegisteredPubkey(
handler(DepositContractAddress.Hex())
},
func(err error) error {
return fmt.Errorf("watch deposit created failed: [%v]", err)
return fmt.Errorf(
"watch deposit registered pubkey failed: [%v]",
err,
)
},
nil,
)
}

// OnDepositRedemptionRequested installs a callback that is invoked when an
// on-chain notification of a deposit redemption request is seen.
func (tec *TBTCEthereumChain) OnDepositRedemptionRequested(
handler func(
depositAddress string,
requesterAddress string,
digest [32]uint8,
utxoValue *big.Int,
redeemerOutputScript []uint8,
requestedFee *big.Int,
outpoint []uint8,
blockNumber uint64,
),
) (subscription.EventSubscription, error) {
return tec.tbtcSystemContract.WatchRedemptionRequested(
func(
DepositContractAddress common.Address,
Requester common.Address,
Digest [32]uint8,
UtxoValue *big.Int,
RedeemerOutputScript []uint8,
RequestedFee *big.Int,
Outpoint []uint8,
blockNumber uint64,
) {
handler(
DepositContractAddress.Hex(),
Requester.Hex(),
Digest,
UtxoValue,
RedeemerOutputScript,
RequestedFee,
Outpoint,
blockNumber,
)
},
func(err error) error {
return fmt.Errorf(
"watch deposit redemption requested failed: [%v]",
err,
)
},
nil,
nil,
nil,
)
}

// OnDepositGotRedemptionSignature installs a callback that is invoked when an
// on-chain notification of a deposit receiving a redemption signature is seen.
func (tec *TBTCEthereumChain) OnDepositGotRedemptionSignature(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
return tec.tbtcSystemContract.WatchGotRedemptionSignature(
func(
DepositContractAddress common.Address,
Digest [32]uint8,
R [32]uint8,
S [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
},
func(err error) error {
return fmt.Errorf(
"watch deposit got redemption signature failed: [%v]",
err,
)
},
nil,
nil,
)
}

// OnDepositRedeemed installs a callback that is invoked when an
// on-chain notification of a deposit redemption is seen.
func (tec *TBTCEthereumChain) OnDepositRedeemed(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
return tec.tbtcSystemContract.WatchRedeemed(
func(
DepositContractAddress common.Address,
Txid [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
},
func(err error) error {
return fmt.Errorf(
"watch deposit redeemed failed: [%v]",
err,
)
},
nil,
nil,
)
}
Expand Down Expand Up @@ -130,6 +233,59 @@ func (tec *TBTCEthereumChain) RetrieveSignerPubkey(
return nil
}

// ProvideRedemptionSignature provides the redemption signature for the
// provided deposit.
func (tec *TBTCEthereumChain) ProvideRedemptionSignature(
depositAddress string,
v uint8,
r [32]uint8,
s [32]uint8,
) error {
deposit, err := tec.getDepositContract(depositAddress)
if err != nil {
return err
}

transaction, err := deposit.ProvideRedemptionSignature(v, r, s)
if err != nil {
return err
}

logger.Debugf(
"submitted ProvideRedemptionSignature transaction with hash: [%x]",
transaction.Hash(),
)

return nil
}

// IncreaseRedemptionFee increases the redemption fee for the provided deposit.
func (tec *TBTCEthereumChain) IncreaseRedemptionFee(
depositAddress string,
previousOutputValueBytes [8]uint8,
newOutputValueBytes [8]uint8,
) error {
deposit, err := tec.getDepositContract(depositAddress)
if err != nil {
return err
}

transaction, err := deposit.IncreaseRedemptionFee(
previousOutputValueBytes,
newOutputValueBytes,
)
if err != nil {
return err
}

logger.Debugf(
"submitted IncreaseRedemptionFee transaction with hash: [%x]",
transaction.Hash(),
)

return nil
}

func (tec *TBTCEthereumChain) getDepositContract(
address string,
) (*contract.Deposit, error) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/chain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ type KeepTerminatedEvent struct {
BlockNumber uint64
}

// SignatureSubmittedEvent is an event emitted when a keep submits a signature.
type SignatureSubmittedEvent struct {
Digest [32]byte
R [32]byte
S [32]byte
RecoveryID uint8
}

// IsMember checks if list of members contains the given address.
func (e *BondedECDSAKeepCreatedEvent) IsMember(address common.Address) bool {
for _, member := range e.Members {
Expand Down
7 changes: 7 additions & 0 deletions pkg/chain/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ func (lc *localChain) GetOpenedTimestamp(keepAddress common.Address) (time.Time,
panic("implement")
}

func (lc *localChain) PastSignatureSubmittedEvents(
keepAddress string,
startBlock uint64,
) ([]*eth.SignatureSubmittedEvent, error) {
panic("implement")
}

func generateHandlerID() int {
// #nosec G404 (insecure random number source (rand))
// Local chain implementation doesn't require secure randomness.
Expand Down
45 changes: 45 additions & 0 deletions pkg/chain/local/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package local
import (
"bytes"
"fmt"
"math/big"
"sync"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -106,6 +107,33 @@ func (tlc *TBTCLocalChain) OnDepositRegisteredPubkey(
}), nil
}

func (tlc *TBTCLocalChain) OnDepositRedemptionRequested(
handler func(
depositAddress string,
requesterAddress string,
digest [32]uint8,
utxoValue *big.Int,
redeemerOutputScript []uint8,
requestedFee *big.Int,
outpoint []uint8,
blockNumber uint64,
),
) (subscription.EventSubscription, error) {
panic("not implemented") // TODO: Implementation for unit testing purposes.
}

func (tlc *TBTCLocalChain) OnDepositGotRedemptionSignature(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
panic("not implemented") // TODO: Implementation for unit testing purposes.
}

func (tlc *TBTCLocalChain) OnDepositRedeemed(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
panic("not implemented") // TODO: Implementation for unit testing purposes.
}

func (tlc *TBTCLocalChain) KeepAddress(depositAddress string) (string, error) {
tlc.mutex.Lock()
defer tlc.mutex.Unlock()
Expand Down Expand Up @@ -167,6 +195,23 @@ func (tlc *TBTCLocalChain) RetrieveSignerPubkey(depositAddress string) error {
return nil
}

func (tlc *TBTCLocalChain) ProvideRedemptionSignature(
depositAddress string,
v uint8,
r [32]uint8,
s [32]uint8,
) error {
panic("not implemented") // TODO: Implementation for unit testing purposes.
}

func (tlc *TBTCLocalChain) IncreaseRedemptionFee(
depositAddress string,
previousOutputValueBytes [8]uint8,
newOutputValueBytes [8]uint8,
) error {
panic("not implemented") // TODO: Implementation for unit testing purposes.
}

func (tlc *TBTCLocalChain) DepositPubkey(
depositAddress string,
) ([]byte, error) {
Expand Down
Loading