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 28 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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to implement a solution in keep-common for a workaround in keep-network/tbtc#740 before merging this PR, right? Otherwise, we would end up with a situation where keep-ecdsa master would not build against tbtc master.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we don't have to do anything in keep-common.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need generators for eventlog? From the description of the referenced PR:

Added a TBTCSystemEventLog allowing to access past events emitted by the TBTCSystem contract. This is a temporary workaround because contract bindings generated by the keep-common generator don't support this feature yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need the abi file which is already generated and tracked by the VCS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do need to implement a change to the generator in keep-common so that it generates TBTCSystemEventLog, correct?

Let me put it this way:

We build tBTC go package from scratch:

  • we remove all generated files from pkg/chain/ethereum/gen
  • we do go generate ./... there
  • we push a new version

Then we build keep-ecdsa:

  • we update a dependency to tbtc
  • we run go build

My understanding is that the compilation will fail because TBTCSystemEventLog will not be there.

In other words, we are depending on a file that should be generated but it's not generated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite. Eventually, we should extend the existing keep-common generator in that way it generates the Filter* methods straight into the TBTCSystem binding struct. TBTCSystemEventLog is just a temporary workaround created manually without using the generator. It is also tracked by VCS similarly as the regular bindings. So, when we build tBTC package from scratch we shouldn't delete anything. We should only run go generate ./... to capture possible contract changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. So just to be clear - I am absolutely fine with merging this PR having TBTCSystemEventLog created manually just like now but I do not want to release a new version before we have this file generated or having Filter* methods generated into TBTCSystem struct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this discussion open until the proper solution is implemented. I do not want to block this PR, though.

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=
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
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)
pdyraga marked this conversation as resolved.
Show resolved Hide resolved

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
}
}
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
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