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

Commit

Permalink
Merge pull request #585 from keep-network/redemption-actions
Browse files Browse the repository at this point in the history
Redemption flow actions

Expands the TBTC extension by adding two new actions specific to the deposit redemption flow.

ProvideRedemptionSignature action
- Starts the monitoring on new RedemptionRequested event. That event is emitted 
either by requestRedemption or increaseRedemptionFee contract methods.
- Stops the monitoring if GotRedemptionSignature or Redeemed event occurs for the 
observed deposit. This means either provideRedemptionSignature or provideRedemptionProof 
has been called by someone else.
- Performs the provideRedemptionSignature action once the deposit is close to exceeding the 
redemption signature timeout.

ProvideRedemptionProof action
- Starts the monitoring on new GotRedemptionSignature event. This event occurs once 
provideRedemptionSignature contract method is called.
- Stops the monitoring if RedemptionRequested or Redeemed event occurs for the observed 
deposit. These events can be emitted by increaseRedemptionFee or provideRedemptionProof 
respectively.
- Performs the increaseRedemptionFee action once the deposit is close to exceeding the
redemption proof timeout. This part will be extended soon by adding the ability of submitting
the redemption proof directy if the BTC transaction exists.
  • Loading branch information
pdyraga authored Nov 9, 2020
2 parents 24d3618 + 9f67bfa commit 5498656
Show file tree
Hide file tree
Showing 20 changed files with 3,119 additions and 242 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/ipfs/go-log v1.0.4
github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4
github.com/keep-network/keep-core v1.3.0
github.com/keep-network/tbtc v1.1.1-0.20201020115551-5f9077c74826
github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718
github.com/pkg/errors v0.9.1
github.com/urfave/cli v1.22.1
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:Civu
github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
github.com/keep-network/keep-core v1.3.0 h1:7Tb33EmO/ntHOEbOiYciRlBhqu5Ln6KemWCaYK0Z6LA=
github.com/keep-network/keep-core v1.3.0/go.mod h1:1KsSSTQoN754TrFLW7kLy50pOG2CQ4BOfnJqdvEG7FA=
github.com/keep-network/tbtc v1.1.1-0.20201020115551-5f9077c74826 h1:ijlpSs+mEtur4F1DQA8450Ubuhdk4lGjIoPZr3yf7vc=
github.com/keep-network/tbtc v1.1.1-0.20201020115551-5f9077c74826/go.mod h1:igBF2MPTFkzOdZ3gcwt8h0Zb5pZaHnij/iPZoMB9IKM=
github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718 h1:/ZNMBY7y6hfzCYA8mgtHnspGO26OmWV3sDehyGnqRyY=
github.com/keep-network/tbtc v1.1.1-0.20201026093513-cb9246987718/go.mod h1:igBF2MPTFkzOdZ3gcwt8h0Zb5pZaHnij/iPZoMB9IKM=
github.com/keep-network/toml v0.3.0 h1:G+NJwWR/ZiORqeLBsDXDchYoL29PXHdxOPcCueA7ctE=
github.com/keep-network/toml v0.3.0/go.mod h1:Zeyd3lxbIlMYLREho3UK1dMP2xjqt2gLkQ5E5vM6K38=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand Down
12 changes: 12 additions & 0 deletions pkg/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Handle interface {
StakeMonitor() (chain.StakeMonitor, error)
// BlockCounter returns a block counter.
BlockCounter() chain.BlockCounter
// BlockTimestamp returns given block's timestamp.
// In case the block is not yet mined, an error should be returned.
BlockTimestamp(blockNumber *big.Int) (uint64, error)

BondedECDSAKeepFactory
BondedECDSAKeep
Expand Down Expand Up @@ -143,4 +146,13 @@ 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.
// All implementations should returns those events sorted by the
// block number in the ascending order.
PastSignatureSubmittedEvents(
keepAddress string,
startBlock uint64,
) ([]*SignatureSubmittedEvent, error)
}
23 changes: 23 additions & 0 deletions pkg/chain/ethereum/connect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ethereum

import (
"context"
"fmt"
"math/big"
"sync"
Expand Down Expand Up @@ -41,6 +42,7 @@ type EthereumChain struct {
blockCounter *blockcounter.EthereumBlockCounter
miningWaiter *ethutil.MiningWaiter
nonceManager *ethutil.NonceManager
blockTimestampFn blockTimestampFn

// transactionMutex allows interested parties to forcibly serialize
// transaction submission.
Expand All @@ -65,6 +67,10 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain,
return nil, err
}

// TODO: Add rate limiting to (keep-ecdsa/pull/585#discussion_r513351032):
// - `createBlockTimestampFn`
// - `blockCounter`
// - `miningWaiter`
wrappedClient := addClientWrappers(config, client)

transactionMutex := &sync.Mutex{}
Expand Down Expand Up @@ -120,6 +126,7 @@ func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain,
nonceManager: nonceManager,
miningWaiter: miningWaiter,
transactionMutex: transactionMutex,
blockTimestampFn: createBlockTimestampFn(client),
}, nil
}

Expand Down Expand Up @@ -149,3 +156,19 @@ func addClientWrappers(

return loggingBackend
}

type blockTimestampFn func(blockNumber *big.Int) (uint64, error)

func createBlockTimestampFn(client *ethclient.Client) blockTimestampFn {
return func(blockNumber *big.Int) (uint64, error) {
ctx, cancelCtx := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancelCtx()

header, err := client.HeaderByNumber(ctx, blockNumber)
if err != nil {
return 0, err
}

return header.Time, nil
}
}
55 changes: 55 additions & 0 deletions pkg/chain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package ethereum
import (
"fmt"
"math/big"
"sort"
"time"

"github.com/keep-network/keep-ecdsa/pkg/chain/gen/eventlog"

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

"github.com/ipfs/go-log"
Expand Down Expand Up @@ -507,3 +510,55 @@ 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.
// Returned events are sorted by the block number in the ascending order.
func (ec *EthereumChain) PastSignatureSubmittedEvents(
keepAddress string,
startBlock uint64,
) ([]*eth.SignatureSubmittedEvent, error) {
if !common.IsHexAddress(keepAddress) {
return nil, fmt.Errorf("invalid keep address: [%v]", keepAddress)
}

keepContractEventLog, err := eventlog.NewBondedECDSAKeepEventLog(
common.HexToAddress(keepAddress),
ec.client,
)
if err != nil {
return nil, err
}

events, err := keepContractEventLog.PastSignatureSubmittedEvents(
startBlock,
nil,
)
if err != nil {
return nil, err
}

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

for _, event := range events {
result = append(result, &eth.SignatureSubmittedEvent{
Digest: event.Digest,
R: event.R,
S: event.S,
RecoveryID: event.RecoveryID,
BlockNumber: event.BlockNumber,
})
}

// Make sure events are sorted by block number in ascending order.
sort.SliceStable(result, func(i, j int) bool {
return result[i].BlockNumber < result[j].BlockNumber
})

return result, nil
}

// BlockTimestamp returns given block's timestamp.
func (ec *EthereumChain) BlockTimestamp(blockNumber *big.Int) (uint64, error) {
return ec.blockTimestampFn(blockNumber)
}
Loading

0 comments on commit 5498656

Please sign in to comment.