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

Commit f240e81

Browse files
Merge pull request #679 from keep-network/pipe-it-all
tBTC chain extension: Incporporating the new Ethereum event subscription API and background event pull loop
2 parents 5b74d3b + 06beb89 commit f240e81

18 files changed

Lines changed: 2385 additions & 2266 deletions

cmd/start.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,7 @@ func initializeExtensions(
216216
return
217217
}
218218

219-
err = tbtc.Initialize(ctx, tbtcEthereumChain)
220-
if err != nil {
221-
logger.Errorf(
222-
"could not initialize tbtc extension: [%v]",
223-
err,
224-
)
225-
return
226-
}
219+
tbtc.Initialize(ctx, tbtcEthereumChain)
227220
}
228221
}
229222

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ require (
1818
github.com/gogo/protobuf v1.3.1
1919
github.com/google/gofuzz v1.1.0
2020
github.com/ipfs/go-log v1.0.4
21-
github.com/keep-network/keep-common v1.3.1-0.20210128105431-b3b56f6f88c8
21+
github.com/keep-network/keep-common v1.3.1-0.20210128152700-34905d2fe019
2222
github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb
23-
github.com/keep-network/tbtc v1.1.1-0.20201117095624-38508bdb562e
23+
github.com/keep-network/tbtc v1.1.1-0.20210128164215-c03b8cf351f0
2424
github.com/pkg/errors v0.9.1
2525
github.com/urfave/cli v1.22.1
2626
)

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,13 @@ github.com/keep-network/cli v1.20.0 h1:mEufpPsovOVdduTTkk+a2CxS3crIrGFqUsvs58gSi
331331
github.com/keep-network/cli v1.20.0/go.mod h1:nzsst4JjU+rGE8Q5J839fYxectxWHpLhxKNohQWtQhA=
332332
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec h1:2pXAsi4OUUjZKr5ds5UOF2IxXN+jVW0WetVO+czkf+A=
333333
github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec/go.mod h1:xR8jf3/VJAjh3nWu5tFe8Yxnt2HvWsqZHfGef1P5oDk=
334-
github.com/keep-network/keep-common v1.2.1-0.20201116151638-8af057907255/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
335334
github.com/keep-network/keep-common v1.3.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
336-
github.com/keep-network/keep-common v1.3.1-0.20210128105431-b3b56f6f88c8 h1:yo2Gj5Wwc+oU+IeGlF0shiVMth4C3gfQzQXUowLj5MQ=
337-
github.com/keep-network/keep-common v1.3.1-0.20210128105431-b3b56f6f88c8/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
335+
github.com/keep-network/keep-common v1.3.1-0.20210128152700-34905d2fe019 h1:WBq6cP/N3PFPo25k6AVuM1Hh46GL8DRPr2YzV97quuc=
336+
github.com/keep-network/keep-common v1.3.1-0.20210128152700-34905d2fe019/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo=
338337
github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb h1:pDhLagUiOYWOn72UiG3jSDVDXZ4dYlsfRa0ToF/ru18=
339338
github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb/go.mod h1:4KezOJWc//c5lbtAE1ob7qYE/Gs5+a1QTjGBzzv2tu8=
340-
github.com/keep-network/tbtc v1.1.1-0.20201117095624-38508bdb562e h1:go3irX4olJUZOi59gaBX7iYIFFbJEV5/zbfCG4tOPaQ=
341-
github.com/keep-network/tbtc v1.1.1-0.20201117095624-38508bdb562e/go.mod h1:LS1zQDXTJrP5f6AHFQKw6/iJPdfV+WqUCfh6GTfC6Rs=
339+
github.com/keep-network/tbtc v1.1.1-0.20210128164215-c03b8cf351f0 h1:zXW/085lHEs1NppQKZ+oOSnOWGxT4JlYelnChNML4WU=
340+
github.com/keep-network/tbtc v1.1.1-0.20210128164215-c03b8cf351f0/go.mod h1:lIFIL8HRHTibQVzE0GoWdGZVfoc19KuJUbhk4W4XNT0=
342341
github.com/keep-network/toml v0.3.0 h1:G+NJwWR/ZiORqeLBsDXDchYoL29PXHdxOPcCueA7ctE=
343342
github.com/keep-network/toml v0.3.0/go.mod h1:Zeyd3lxbIlMYLREho3UK1dMP2xjqt2gLkQ5E5vM6K38=
344343
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=

pkg/chain/ethereum/tbtc.go

Lines changed: 72 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func WithTBTCExtension(
3636
ethereumChain.client,
3737
ethereumChain.nonceManager,
3838
ethereumChain.miningWaiter,
39+
ethereumChain.blockCounter,
3940
ethereumChain.transactionMutex,
4041
)
4142
if err != nil {
@@ -52,129 +53,109 @@ func WithTBTCExtension(
5253
// on-chain notification of a new deposit creation is seen.
5354
func (tec *TBTCEthereumChain) OnDepositCreated(
5455
handler func(depositAddress string),
55-
) (subscription.EventSubscription, error) {
56-
return tec.tbtcSystemContract.WatchCreated(
57-
func(
58-
DepositContractAddress common.Address,
59-
KeepAddress common.Address,
60-
Timestamp *big.Int,
61-
blockNumber uint64,
62-
) {
63-
handler(DepositContractAddress.Hex())
64-
},
65-
func(err error) error {
66-
return fmt.Errorf("watch deposit created failed: [%v]", err)
67-
},
56+
) subscription.EventSubscription {
57+
onEvent := func(
58+
DepositContractAddress common.Address,
59+
KeepAddress common.Address,
60+
Timestamp *big.Int,
61+
blockNumber uint64,
62+
) {
63+
handler(DepositContractAddress.Hex())
64+
}
65+
66+
return tec.tbtcSystemContract.Created(
6867
nil,
6968
nil,
70-
)
69+
nil,
70+
).OnEvent(onEvent)
7171
}
7272

7373
// OnDepositRegisteredPubkey installs a callback that is invoked when an
7474
// on-chain notification of a deposit's pubkey registration is seen.
7575
func (tec *TBTCEthereumChain) OnDepositRegisteredPubkey(
7676
handler func(depositAddress string),
77-
) (subscription.EventSubscription, error) {
78-
return tec.tbtcSystemContract.WatchRegisteredPubkey(
79-
func(
80-
DepositContractAddress common.Address,
81-
SigningGroupPubkeyX [32]uint8,
82-
SigningGroupPubkeyY [32]uint8,
83-
Timestamp *big.Int,
84-
blockNumber uint64,
85-
) {
86-
handler(DepositContractAddress.Hex())
87-
},
88-
func(err error) error {
89-
return fmt.Errorf(
90-
"watch deposit registered pubkey failed: [%v]",
91-
err,
92-
)
93-
},
94-
nil,
95-
)
77+
) subscription.EventSubscription {
78+
onEvent := func(
79+
DepositContractAddress common.Address,
80+
SigningGroupPubkeyX [32]uint8,
81+
SigningGroupPubkeyY [32]uint8,
82+
Timestamp *big.Int,
83+
blockNumber uint64,
84+
) {
85+
handler(DepositContractAddress.Hex())
86+
}
87+
88+
return tec.tbtcSystemContract.RegisteredPubkey(nil, nil).OnEvent(onEvent)
9689
}
9790

9891
// OnDepositRedemptionRequested installs a callback that is invoked when an
9992
// on-chain notification of a deposit redemption request is seen.
10093
func (tec *TBTCEthereumChain) OnDepositRedemptionRequested(
10194
handler func(depositAddress string),
102-
) (subscription.EventSubscription, error) {
103-
return tec.tbtcSystemContract.WatchRedemptionRequested(
104-
func(
105-
DepositContractAddress common.Address,
106-
Requester common.Address,
107-
Digest [32]uint8,
108-
UtxoValue *big.Int,
109-
RedeemerOutputScript []uint8,
110-
RequestedFee *big.Int,
111-
Outpoint []uint8,
112-
blockNumber uint64,
113-
) {
114-
handler(DepositContractAddress.Hex())
115-
},
116-
func(err error) error {
117-
return fmt.Errorf(
118-
"watch deposit redemption requested failed: [%v]",
119-
err,
120-
)
121-
},
95+
) subscription.EventSubscription {
96+
onEvent := func(
97+
DepositContractAddress common.Address,
98+
Requester common.Address,
99+
Digest [32]uint8,
100+
UtxoValue *big.Int,
101+
RedeemerOutputScript []uint8,
102+
RequestedFee *big.Int,
103+
Outpoint []uint8,
104+
blockNumber uint64,
105+
) {
106+
handler(DepositContractAddress.Hex())
107+
}
108+
109+
return tec.tbtcSystemContract.RedemptionRequested(
122110
nil,
123111
nil,
124112
nil,
125-
)
113+
nil,
114+
).OnEvent(onEvent)
126115
}
127116

128117
// OnDepositGotRedemptionSignature installs a callback that is invoked when an
129118
// on-chain notification of a deposit receiving a redemption signature is seen.
130119
func (tec *TBTCEthereumChain) OnDepositGotRedemptionSignature(
131120
handler func(depositAddress string),
132-
) (subscription.EventSubscription, error) {
133-
return tec.tbtcSystemContract.WatchGotRedemptionSignature(
134-
func(
135-
DepositContractAddress common.Address,
136-
Digest [32]uint8,
137-
R [32]uint8,
138-
S [32]uint8,
139-
Timestamp *big.Int,
140-
blockNumber uint64,
141-
) {
142-
handler(DepositContractAddress.Hex())
143-
},
144-
func(err error) error {
145-
return fmt.Errorf(
146-
"watch deposit got redemption signature failed: [%v]",
147-
err,
148-
)
149-
},
121+
) subscription.EventSubscription {
122+
onEvent := func(
123+
DepositContractAddress common.Address,
124+
Digest [32]uint8,
125+
R [32]uint8,
126+
S [32]uint8,
127+
Timestamp *big.Int,
128+
blockNumber uint64,
129+
) {
130+
handler(DepositContractAddress.Hex())
131+
}
132+
133+
return tec.tbtcSystemContract.GotRedemptionSignature(
150134
nil,
151135
nil,
152-
)
136+
nil,
137+
).OnEvent(onEvent)
153138
}
154139

155140
// OnDepositRedeemed installs a callback that is invoked when an
156141
// on-chain notification of a deposit redemption is seen.
157142
func (tec *TBTCEthereumChain) OnDepositRedeemed(
158143
handler func(depositAddress string),
159-
) (subscription.EventSubscription, error) {
160-
return tec.tbtcSystemContract.WatchRedeemed(
161-
func(
162-
DepositContractAddress common.Address,
163-
Txid [32]uint8,
164-
Timestamp *big.Int,
165-
blockNumber uint64,
166-
) {
167-
handler(DepositContractAddress.Hex())
168-
},
169-
func(err error) error {
170-
return fmt.Errorf(
171-
"watch deposit redeemed failed: [%v]",
172-
err,
173-
)
174-
},
144+
) subscription.EventSubscription {
145+
onEvent := func(
146+
DepositContractAddress common.Address,
147+
Txid [32]uint8,
148+
Timestamp *big.Int,
149+
blockNumber uint64,
150+
) {
151+
handler(DepositContractAddress.Hex())
152+
}
153+
154+
return tec.tbtcSystemContract.Redeemed(
175155
nil,
176156
nil,
177-
)
157+
nil,
158+
).OnEvent(onEvent)
178159
}
179160

180161
// PastDepositRedemptionRequestedEvents returns all redemption requested
@@ -384,6 +365,7 @@ func (tec *TBTCEthereumChain) getDepositContract(
384365
tec.client,
385366
tec.nonceManager,
386367
tec.miningWaiter,
368+
tec.blockCounter,
387369
tec.transactionMutex,
388370
)
389371
if err != nil {

0 commit comments

Comments
 (0)