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

TBTC extension optimizations #600

Merged
merged 8 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
104 changes: 84 additions & 20 deletions pkg/extensions/tbtc/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

"github.com/keep-network/keep-common/pkg/cache"

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

"github.com/ipfs/go-log"
Expand All @@ -24,9 +26,11 @@ import (
var logger = log.Logger("tbtc-extension")

const (
maxActAttempts = 3
pastEventsLookbackBlocks = 10000
defaultBlockConfirmations = 12
maxActAttempts = 3
pastEventsLookbackBlocks = 10000
defaultBlockConfirmations = 12
monitoringCachePeriod = 24 * time.Hour
defaultSignerActionDelayStep = 5 * time.Minute
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
)

// TODO: Resume monitoring after client restart
Expand Down Expand Up @@ -80,15 +84,21 @@ func Initialize(ctx context.Context, chain chain.TBTCHandle) error {
}

type tbtc struct {
chain chain.TBTCHandle
monitoringLocks sync.Map
blockConfirmations uint64
chain chain.TBTCHandle
monitoringLocks sync.Map
blockConfirmations uint64
monitoredDepositsCache *cache.TimeCache
notMonitoredDepositsCache *cache.TimeCache
signerActionDelayStep time.Duration
}

func newTBTC(chain chain.TBTCHandle) *tbtc {
return &tbtc{
chain: chain,
blockConfirmations: defaultBlockConfirmations,
chain: chain,
blockConfirmations: defaultBlockConfirmations,
monitoredDepositsCache: cache.NewTimeCache(monitoringCachePeriod),
notMonitoredDepositsCache: cache.NewTimeCache(monitoringCachePeriod),
signerActionDelayStep: defaultSignerActionDelayStep,
}
}

Expand Down Expand Up @@ -141,6 +151,15 @@ func (t *tbtc) monitorRetrievePubKey(
return nil
}

timeoutFn := func(depositAddress string) (time.Duration, error) {
actionDelay, err := t.getSignerActionDelay(depositAddress)
if err != nil {
return 0, err
}

return timeout + actionDelay, nil
}

monitoringSubscription, err := t.monitorAndAct(
ctx,
"retrieve pubkey",
Expand All @@ -150,9 +169,7 @@ func (t *tbtc) monitorRetrievePubKey(
t.watchKeepClosed,
actFn,
actBackoffFn,
func(_ string) (time.Duration, error) {
return timeout, nil
},
timeoutFn,
)
if err != nil {
return err
Expand Down Expand Up @@ -313,6 +330,15 @@ func (t *tbtc) monitorProvideRedemptionSignature(
return nil
}

timeoutFn := func(depositAddress string) (time.Duration, error) {
actionDelay, err := t.getSignerActionDelay(depositAddress)
if err != nil {
return 0, err
}

return timeout + actionDelay, nil
}

monitoringSubscription, err := t.monitorAndAct(
ctx,
"provide redemption signature",
Expand All @@ -322,9 +348,7 @@ func (t *tbtc) monitorProvideRedemptionSignature(
t.watchKeepClosed,
actFn,
actBackoffFn,
func(_ string) (time.Duration, error) {
return timeout, nil
},
timeoutFn,
)
if err != nil {
return err
Expand Down Expand Up @@ -502,7 +526,12 @@ func (t *tbtc) monitorProvideRedemptionProof(
gotRedemptionSignatureTimestamp-redemptionRequestedTimestamp,
) * time.Second

return timeout - timeoutShift, nil
actionDelay, err := t.getSignerActionDelay(depositAddress)
if err != nil {
return 0, err
}

return (timeout - timeoutShift) + actionDelay, nil
}

monitoringSubscription, err := t.monitorAndAct(
Expand Down Expand Up @@ -761,23 +790,58 @@ func (t *tbtc) watchKeepClosed(
}

func (t *tbtc) shouldMonitorDeposit(depositAddress string) bool {
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
t.monitoredDepositsCache.Sweep()
t.notMonitoredDepositsCache.Sweep()

if t.monitoredDepositsCache.Has(depositAddress) {
return true
}

if t.notMonitoredDepositsCache.Has(depositAddress) {
return false
}

if _, err := t.getSignerIndex(depositAddress); err != nil {
t.notMonitoredDepositsCache.Add(depositAddress)
return false
}
pdyraga marked this conversation as resolved.
Show resolved Hide resolved

t.monitoredDepositsCache.Add(depositAddress)
return true
}

func (t *tbtc) getSignerIndex(depositAddress string) (int, error) {
keepAddress, err := t.chain.KeepAddress(depositAddress)
if err != nil {
return false
return 0, err
}

members, err := t.chain.GetMembers(common.HexToAddress(keepAddress))
if err != nil {
return false
return 0, err
}

for _, member := range members {
for index, member := range members {
if member == t.chain.Address() {
return true
return index, nil
}
}

return false
return 0, fmt.Errorf(
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
"operator is not a signer of deposit [%v]",
depositAddress,
)
}

func (t *tbtc) getSignerActionDelay(
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
depositAddress string,
) (time.Duration, error) {
signerIndex, err := t.getSignerIndex(depositAddress)
if err != nil {
return 0, err
}

return time.Duration(signerIndex) * t.signerActionDelayStep, nil
}

func (t *tbtc) waitDepositStateChangeConfirmation(
Expand Down
Loading