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

Initial implementation of app-specific actions #580

Merged
merged 40 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fd7f82d
Add actions to the config file
lukasz-zimnoch Oct 14, 2020
624400b
Add TBTC actions and chain extensions stub
lukasz-zimnoch Oct 14, 2020
376dc70
Return struct from the connect function
lukasz-zimnoch Oct 14, 2020
52b3e48
Enable app-specific actions in the start command
lukasz-zimnoch Oct 14, 2020
3f318a3
Use TBTC contract bindings
lukasz-zimnoch Oct 14, 2020
ce789c2
Bump up tbtc go bindings
lukasz-zimnoch Oct 14, 2020
a48c965
Align with tbtc bindings changes
lukasz-zimnoch Oct 15, 2020
3890ebf
Actions structure change
lukasz-zimnoch Oct 15, 2020
c378e7e
Merge branch 'master' into app-specific-actions
lukasz-zimnoch Oct 15, 2020
40f2f9c
Use TBTCSystem contract binding and config rework
lukasz-zimnoch Oct 15, 2020
a438ec0
Rework the ethereum extensions structure
lukasz-zimnoch Oct 15, 2020
31e98b6
Merge branch 'master' into app-specific-actions
lukasz-zimnoch Oct 15, 2020
2d3019a
Implementation of the `retrievePubkey` action
lukasz-zimnoch Oct 16, 2020
e97914a
Add GoSec exception for random jitter generator
lukasz-zimnoch Oct 16, 2020
d6d2a26
Simplify extension section in the config sample
lukasz-zimnoch Oct 19, 2020
a64dbc2
Use extension-centric naming
lukasz-zimnoch Oct 19, 2020
f6dd208
Move `TBTCEthereumChain` to the `ethereum` package
lukasz-zimnoch Oct 19, 2020
6025d32
Merge branch 'master' into app-specific-actions
lukasz-zimnoch Oct 19, 2020
81118a8
Update tbtc bindings dependency
lukasz-zimnoch Oct 19, 2020
4a88afb
Use extension-centric naming - next part
lukasz-zimnoch Oct 19, 2020
e475ff7
Extract TBTC chain interfaces to separate file
lukasz-zimnoch Oct 19, 2020
0834f76
Extract `maxTransactionAttempts` constant
lukasz-zimnoch Oct 19, 2020
3523e60
Align some naming in the tbtc extensions code
lukasz-zimnoch Oct 19, 2020
a7c3cd0
Basic unit tests for app-specific extensions
lukasz-zimnoch Oct 19, 2020
302323f
Extended unit tests for app-specific extensions
lukasz-zimnoch Oct 20, 2020
caeb475
Add new GoSec exceptions
lukasz-zimnoch Oct 20, 2020
71851ad
Change the comment describing `Extensions.TBTC`
lukasz-zimnoch Oct 20, 2020
2f046a2
Change `pkg/chain` alias in tbtc extensions code
lukasz-zimnoch Oct 20, 2020
7899037
Improve general naming and logs
lukasz-zimnoch Oct 20, 2020
6c88853
Satisfy the GoSec checker
lukasz-zimnoch Oct 20, 2020
4a8b996
Update tbtc bindings
lukasz-zimnoch Oct 20, 2020
57fa352
Merge branch 'master' into app-specific-actions
lukasz-zimnoch Oct 23, 2020
8cac02d
Add additional explanation to `[Extensions.TBTC]`
lukasz-zimnoch Oct 23, 2020
bb6f6f0
Tidy the go.sum
lukasz-zimnoch Oct 23, 2020
b51ce89
Improve docs for `TBTC` config struct
lukasz-zimnoch Oct 23, 2020
89f436b
Minor naming improvements of tbtc extensions
lukasz-zimnoch Oct 23, 2020
71efdea
Minor naming improvements of local chain and tests
lukasz-zimnoch Oct 23, 2020
3b6082d
Add a panic in case of `createKeepWithMembers` err
lukasz-zimnoch Oct 23, 2020
0be18c6
Remove the `nosec` from local `OpenKeep`
lukasz-zimnoch Oct 23, 2020
becf9e3
Change timeout message for tbtc extension
lukasz-zimnoch Oct 23, 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
37 changes: 35 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"context"
"fmt"
"github.com/keep-network/keep-core/pkg/diagnostics"
"time"

"github.com/keep-network/keep-core/pkg/diagnostics"

"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/metrics"
"github.com/keep-network/keep-core/pkg/net"
Expand All @@ -19,11 +20,12 @@ import (
"github.com/keep-network/keep-core/pkg/net/libp2p"
"github.com/keep-network/keep-core/pkg/net/retransmission"
"github.com/keep-network/keep-core/pkg/operator"
"github.com/keep-network/keep-ecdsa/pkg/firewall"

"github.com/keep-network/keep-ecdsa/internal/config"
"github.com/keep-network/keep-ecdsa/pkg/chain/ethereum"
"github.com/keep-network/keep-ecdsa/pkg/client"
"github.com/keep-network/keep-ecdsa/pkg/extensions/tbtc"
"github.com/keep-network/keep-ecdsa/pkg/firewall"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -168,6 +170,8 @@ func Start(c *cli.Context) error {
)
logger.Debugf("initialized operator with address: [%s]", ethereumKey.Address.String())

initializeExtensions(ctx, config.Extensions, ethereumChain)

initializeMetrics(ctx, config, networkProvider, stakeMonitor, ethereumKey.Address.Hex())
initializeDiagnostics(config, networkProvider)

Expand All @@ -183,6 +187,35 @@ func Start(c *cli.Context) error {
}
}

func initializeExtensions(
ctx context.Context,
config config.Extensions,
ethereumChain *ethereum.EthereumChain,
) {
if len(config.TBTC.TBTCSystem) > 0 {
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
tbtcEthereumChain, err := ethereum.WithTBTCExtension(
ethereumChain,
config.TBTC.TBTCSystem,
)
if err != nil {
logger.Errorf(
"could not initialize tbtc chain extension: [%v]",
err,
)
return
}

err = tbtc.Initialize(ctx, tbtcEthereumChain)
if err != nil {
logger.Errorf(
"could not initialize tbtc extension: [%v]",
err,
)
return
}
}
}

func initializeMetrics(
ctx context.Context,
config *config.Config,
Expand Down
10 changes: 9 additions & 1 deletion configs/config.toml.SAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,12 @@
# The port on which the `/diagnostics` endpoint will be available can be
# customized below.
# [Diagnostics]
# Port = 8081
# Port = 8081

# Uncomment to enable tBTC-specific extension. This extension takes care of
# executing actions that are assumed by tBTC to be the signer's responsibility,
# for example, retrieve public key from keep to tBTC deposit or
# increase redemption fee on tBTC deposit.
# [Extensions.TBTC]
# TBTCSystem = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ require (
github.com/gogo/protobuf v1.3.1
github.com/google/gofuzz v1.1.0
github.com/ipfs/go-log v1.0.4
github.com/keep-network/keep-common v1.2.1-0.20201002105641-e04cc579ff66
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/pkg/errors v0.9.1
github.com/urfave/cli v1.22.1
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,12 @@ github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec h
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec/go.mod h1:xR8jf3/VJAjh3nWu5tFe8Yxnt2HvWsqZHfGef1P5oDk=
github.com/keep-network/keep-common v1.2.0 h1:hVd2tTd7vL+9CQP5Ntk5kjs+GYvkgrRNBcNvTuhHhVk=
github.com/keep-network/keep-common v1.2.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
github.com/keep-network/keep-common v1.2.1-0.20201002105641-e04cc579ff66 h1:x/9fra2wLrBDhS8LOQztFajoM2q13FgBN5jFiqBGBog=
github.com/keep-network/keep-common v1.2.1-0.20201002105641-e04cc579ff66/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
github.com/keep-network/keep-common v1.2.1-0.20201020114759-19c123cbd4f4 h1:CivupPSFswHACua5xZGKdeYxsCQ2cmRomTIBh8kfk70=
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=
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 internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
TSS tss.Config
Metrics Metrics
Diagnostics Diagnostics
Extensions Extensions
}

// SanctionedApplications contains addresses of applications approved by the
Expand Down Expand Up @@ -70,6 +71,17 @@ type Diagnostics struct {
Port int
}

// Extensions stores app-specific extensions configuration.
type Extensions struct {
TBTC TBTC
}

// TBTC stores configuration of application extension responsible for
// executing signer actions specific for TBTC application.
type TBTC struct {
TBTCSystem string
}

// ReadConfig reads in the configuration file in .toml format. Ethereum key file
// password is expected to be provided as environment variable.
func ReadConfig(filePath string) (*Config, error) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/chain/ethereum/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/keep-network/keep-common/pkg/chain/ethereum"
"github.com/keep-network/keep-common/pkg/chain/ethereum/blockcounter"
"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil"
eth "github.com/keep-network/keep-ecdsa/pkg/chain"
"github.com/keep-network/keep-ecdsa/pkg/chain/gen/contract"
)

Expand Down Expand Up @@ -60,7 +59,7 @@ type EthereumChain struct {

// Connect performs initialization for communication with Ethereum blockchain
// based on provided config.
func Connect(accountKey *keystore.Key, config *ethereum.Config) (eth.Handle, error) {
func Connect(accountKey *keystore.Key, config *ethereum.Config) (*EthereumChain, error) {
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
client, err := ethclient.Dial(config.URL)
if err != nil {
return nil, err
Expand Down
153 changes: 153 additions & 0 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package ethereum

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/keep-network/keep-common/pkg/subscription"
"github.com/keep-network/tbtc/pkg/chain/ethereum/gen/contract"
)

// TBTCEthereumChain represents an Ethereum chain handle with
// TBTC-specific capabilities.
type TBTCEthereumChain struct {
*EthereumChain

tbtcSystemContract *contract.TBTCSystem
}

// WithTBTCExtension extends the Ethereum chain handle with
// TBTC-specific capabilities.
func WithTBTCExtension(
ethereumChain *EthereumChain,
tbtcSystemContractAddress string,
) (*TBTCEthereumChain, error) {
if !common.IsHexAddress(tbtcSystemContractAddress) {
return nil, fmt.Errorf("incorrect TBTCSystem contract address")
}

tbtcSystemContract, err := contract.NewTBTCSystem(
common.HexToAddress(tbtcSystemContractAddress),
ethereumChain.accountKey,
ethereumChain.client,
ethereumChain.nonceManager,
ethereumChain.miningWaiter,
ethereumChain.transactionMutex,
)
if err != nil {
return nil, err
}

return &TBTCEthereumChain{
EthereumChain: ethereumChain,
tbtcSystemContract: tbtcSystemContract,
}, nil
}

// OnDepositCreated installs a callback that is invoked when an
// on-chain notification of a new deposit creation is seen.
func (tec *TBTCEthereumChain) OnDepositCreated(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
return tec.tbtcSystemContract.WatchCreated(
func(
DepositContractAddress common.Address,
KeepAddress common.Address,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
},
func(err error) error {
return fmt.Errorf("watch deposit created failed: [%v]", err)
},
nil,
nil,
)
}

// OnDepositRegisteredPubkey installs a callback that is invoked when an
// on-chain notification of a deposit's pubkey registration is seen.
func (tec *TBTCEthereumChain) OnDepositRegisteredPubkey(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
return tec.tbtcSystemContract.WatchRegisteredPubkey(
func(
DepositContractAddress common.Address,
SigningGroupPubkeyX [32]uint8,
SigningGroupPubkeyY [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
},
func(err error) error {
return fmt.Errorf("watch deposit created failed: [%v]", err)
},
nil,
)
}

// KeepAddress returns the underlying keep address for the
// provided deposit.
func (tec *TBTCEthereumChain) KeepAddress(
depositAddress string,
) (string, error) {
deposit, err := tec.getDepositContract(depositAddress)
if err != nil {
return "", err
}

keepAddress, err := deposit.KeepAddress()
if err != nil {
return "", err
}

return keepAddress.Hex(), nil
}

// RetrieveSignerPubkey retrieves the signer public key for the
// provided deposit.
func (tec *TBTCEthereumChain) RetrieveSignerPubkey(
depositAddress string,
) error {
deposit, err := tec.getDepositContract(depositAddress)
if err != nil {
return err
}

transaction, err := deposit.RetrieveSignerPubkey()
if err != nil {
return err
}

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

return nil
}

func (tec *TBTCEthereumChain) getDepositContract(
address string,
) (*contract.Deposit, error) {
if !common.IsHexAddress(address) {
return nil, fmt.Errorf("incorrect deposit contract address")
}

depositContract, err := contract.NewDeposit(
common.HexToAddress(address),
tec.accountKey,
tec.client,
tec.nonceManager,
tec.miningWaiter,
tec.transactionMutex,
)
if err != nil {
return nil, err
}

return depositContract, nil
}
67 changes: 67 additions & 0 deletions pkg/chain/local/bonded_ecdsa_keep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type localKeep struct {
status keepStatus

signatureRequestedHandlers map[int]func(event *eth.SignatureRequestedEvent)

keepClosedHandlers map[int]func(event *eth.KeepClosedEvent)
keepTerminatedHandlers map[int]func(event *eth.KeepTerminatedEvent)
}

func (c *localChain) requestSignature(keepAddress common.Address, digest [32]byte) error {
Expand All @@ -47,3 +50,67 @@ func (c *localChain) requestSignature(keepAddress common.Address, digest [32]byt

return nil
}

func (c *localChain) closeKeep(keepAddress common.Address) error {
c.handlerMutex.Lock()
defer c.handlerMutex.Unlock()

keep, ok := c.keeps[keepAddress]
if !ok {
return fmt.Errorf(
"failed to find keep with address: [%s]",
keepAddress.String(),
)
}

if keep.status != active {
return fmt.Errorf("only active keeps can be closed")
}

keep.status = closed

keepClosedEvent := &eth.KeepClosedEvent{}

for _, handler := range keep.keepClosedHandlers {
go func(
handler func(event *eth.KeepClosedEvent),
keepClosedEvent *eth.KeepClosedEvent,
) {
handler(keepClosedEvent)
}(handler, keepClosedEvent)
}

return nil
}

func (c *localChain) terminateKeep(keepAddress common.Address) error {
c.handlerMutex.Lock()
defer c.handlerMutex.Unlock()

keep, ok := c.keeps[keepAddress]
if !ok {
return fmt.Errorf(
"failed to find keep with address: [%s]",
keepAddress.String(),
)
}

if keep.status != active {
return fmt.Errorf("only active keeps can be terminated")
}

keep.status = terminated

keepTerminatedEvent := &eth.KeepTerminatedEvent{}

for _, handler := range keep.keepTerminatedHandlers {
go func(
handler func(event *eth.KeepTerminatedEvent),
keepTerminatedEvent *eth.KeepTerminatedEvent,
) {
handler(keepTerminatedEvent)
}(handler, keepTerminatedEvent)
}

return nil
}
Loading