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

Commit

Permalink
updated event subscription API in tBTC ethereum extension
Browse files Browse the repository at this point in the history
Adjusted the code to the regenerated Go contract bindings in tbtc
repository. The regenerated bindings contains keep-common improvements
around event resubscription mechanism and background event monitoring
loop fetching events from the chain periodically in case some event was
dropped by websocket subscription.
  • Loading branch information
pdyraga committed Jan 25, 2021
1 parent b684710 commit a5153fa
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 329 deletions.
9 changes: 1 addition & 8 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,7 @@ func initializeExtensions(
return
}

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

Expand Down
162 changes: 72 additions & 90 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func WithTBTCExtension(
ethereumChain.client,
ethereumChain.nonceManager,
ethereumChain.miningWaiter,
ethereumChain.blockCounter,
ethereumChain.transactionMutex,
)
if err != nil {
Expand All @@ -52,129 +53,109 @@ func WithTBTCExtension(
// 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)
},
) subscription.EventSubscription {
onEvent := func(
DepositContractAddress common.Address,
KeepAddress common.Address,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
}

return tec.tbtcSystemContract.Created(
nil,
nil,
)
nil,
).OnEvent(onEvent)
}

// 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 registered pubkey failed: [%v]",
err,
)
},
nil,
)
) subscription.EventSubscription {
onEvent := func(
DepositContractAddress common.Address,
SigningGroupPubkeyX [32]uint8,
SigningGroupPubkeyY [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
}

return tec.tbtcSystemContract.RegisteredPubkey(nil, nil).OnEvent(onEvent)
}

// 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),
) (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())
},
func(err error) error {
return fmt.Errorf(
"watch deposit redemption requested failed: [%v]",
err,
)
},
) subscription.EventSubscription {
onEvent := 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())
}

return tec.tbtcSystemContract.RedemptionRequested(
nil,
nil,
nil,
)
nil,
).OnEvent(onEvent)
}

// 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,
)
},
) subscription.EventSubscription {
onEvent := func(
DepositContractAddress common.Address,
Digest [32]uint8,
R [32]uint8,
S [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
}

return tec.tbtcSystemContract.GotRedemptionSignature(
nil,
nil,
)
nil,
).OnEvent(onEvent)
}

// 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,
)
},
) subscription.EventSubscription {
onEvent := func(
DepositContractAddress common.Address,
Txid [32]uint8,
Timestamp *big.Int,
blockNumber uint64,
) {
handler(DepositContractAddress.Hex())
}

return tec.tbtcSystemContract.Redeemed(
nil,
nil,
)
nil,
).OnEvent(onEvent)
}

// PastDepositRedemptionRequestedEvents returns all redemption requested
Expand Down Expand Up @@ -384,6 +365,7 @@ func (tec *TBTCEthereumChain) getDepositContract(
tec.client,
tec.nonceManager,
tec.miningWaiter,
tec.blockCounter,
tec.transactionMutex,
)
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions pkg/chain/local/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (tlc *TBTCLocalChain) CreateDeposit(

func (tlc *TBTCLocalChain) OnDepositCreated(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
) subscription.EventSubscription {
tlc.tbtcLocalChainMutex.Lock()
defer tlc.tbtcLocalChainMutex.Unlock()

Expand All @@ -151,12 +151,12 @@ func (tlc *TBTCLocalChain) OnDepositCreated(
defer tlc.tbtcLocalChainMutex.Unlock()

delete(tlc.depositCreatedHandlers, handlerID)
}), nil
})
}

func (tlc *TBTCLocalChain) OnDepositRegisteredPubkey(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
) subscription.EventSubscription {
tlc.tbtcLocalChainMutex.Lock()
defer tlc.tbtcLocalChainMutex.Unlock()

Expand All @@ -169,7 +169,7 @@ func (tlc *TBTCLocalChain) OnDepositRegisteredPubkey(
defer tlc.tbtcLocalChainMutex.Unlock()

delete(tlc.depositRegisteredPubkeyHandlers, handlerID)
}), nil
})
}

func (tlc *TBTCLocalChain) RedeemDeposit(depositAddress string) error {
Expand Down Expand Up @@ -240,7 +240,7 @@ func (tlc *TBTCLocalChain) RedeemDeposit(depositAddress string) error {

func (tlc *TBTCLocalChain) OnDepositRedemptionRequested(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
) subscription.EventSubscription {
tlc.tbtcLocalChainMutex.Lock()
defer tlc.tbtcLocalChainMutex.Unlock()

Expand All @@ -253,12 +253,12 @@ func (tlc *TBTCLocalChain) OnDepositRedemptionRequested(
defer tlc.tbtcLocalChainMutex.Unlock()

delete(tlc.depositRedemptionRequestedHandlers, handlerID)
}), nil
})
}

func (tlc *TBTCLocalChain) OnDepositGotRedemptionSignature(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
) subscription.EventSubscription {
tlc.tbtcLocalChainMutex.Lock()
defer tlc.tbtcLocalChainMutex.Unlock()

Expand All @@ -271,12 +271,12 @@ func (tlc *TBTCLocalChain) OnDepositGotRedemptionSignature(
defer tlc.tbtcLocalChainMutex.Unlock()

delete(tlc.depositGotRedemptionSignatureHandlers, handlerID)
}), nil
})
}

func (tlc *TBTCLocalChain) OnDepositRedeemed(
handler func(depositAddress string),
) (subscription.EventSubscription, error) {
) subscription.EventSubscription {
tlc.tbtcLocalChainMutex.Lock()
defer tlc.tbtcLocalChainMutex.Unlock()

Expand All @@ -289,7 +289,7 @@ func (tlc *TBTCLocalChain) OnDepositRedeemed(
defer tlc.tbtcLocalChainMutex.Unlock()

delete(tlc.depositRedeemedHandlers, handlerID)
}), nil
})
}

func (tlc *TBTCLocalChain) PastDepositRedemptionRequestedEvents(
Expand Down
10 changes: 5 additions & 5 deletions pkg/chain/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,32 @@ type TBTCSystem interface {
// on-chain notification of a new deposit creation is seen.
OnDepositCreated(
handler func(depositAddress string),
) (subscription.EventSubscription, error)
) subscription.EventSubscription

// OnDepositRegisteredPubkey installs a callback that is invoked when an
// on-chain notification of a deposit's pubkey registration is seen.
OnDepositRegisteredPubkey(
handler func(depositAddress string),
) (subscription.EventSubscription, error)
) subscription.EventSubscription

// OnDepositRedemptionRequested installs a callback that is invoked when an
// on-chain notification of a deposit redemption request is seen.
OnDepositRedemptionRequested(
handler func(depositAddress string),
) (subscription.EventSubscription, error)
) subscription.EventSubscription

// OnDepositGotRedemptionSignature installs a callback that is invoked
// when an on-chain notification of a deposit receiving a redemption
// signature is seen.
OnDepositGotRedemptionSignature(
handler func(depositAddress string),
) (subscription.EventSubscription, error)
) subscription.EventSubscription

// OnDepositRedeemed installs a callback that is invoked when an
// on-chain notification of a deposit redemption is seen.
OnDepositRedeemed(
handler func(depositAddress string),
) (subscription.EventSubscription, error)
) subscription.EventSubscription

// PastDepositRedemptionRequestedEvents returns all redemption requested
// events for the given deposit which occurred after the provided start block.
Expand Down
Loading

0 comments on commit a5153fa

Please sign in to comment.