Skip to content

Commit 45e63a1

Browse files
committed
feat(op): added AtBlockNumber to events
1 parent 75f97d7 commit 45e63a1

File tree

8 files changed

+127
-43
lines changed

8 files changed

+127
-43
lines changed

rolling-shutter/keyperimpl/optimism/keyper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (kpr *Keyper) newBlock(_ context.Context, ev *shopevent.LatestBlock) error
9999

100100
// TODO: sanity checks
101101

102-
idPreimage := identitypreimage.BigToIdentityPreimage(ev.Number)
102+
idPreimage := identitypreimage.BigToIdentityPreimage(ev.Number.Int)
103103
trig := &epochkghandler.DecryptionTrigger{
104104
BlockNumber: ev.Number.Uint64(),
105105
IdentityPreimages: []identitypreimage.IdentityPreimage{idPreimage},
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package event
22

33
import (
4-
"math/big"
5-
64
"github.com/ethereum/go-ethereum/common"
5+
6+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/number"
77
)
88

99
type (
@@ -12,16 +12,22 @@ type (
1212
Members []common.Address
1313
Threshold uint64
1414
Eon uint64
15+
16+
AtBlockNumber *number.BlockNumber
1517
}
1618
EonPublicKey struct {
1719
Eon uint64
1820
Key []byte
19-
}
20-
LatestBlock struct {
21-
Number *big.Int
22-
BlockHash common.Hash
21+
22+
AtBlockNumber *number.BlockNumber
2323
}
2424
ShutterState struct {
2525
Active bool
26+
27+
AtBlockNumber *number.BlockNumber
28+
}
29+
LatestBlock struct {
30+
Number *number.BlockNumber
31+
BlockHash common.Hash
2632
}
2733
)

rolling-shutter/keyperimpl/optimism/sync/syncer/eonpubkey.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func (s *EonPubKeySyncer) Start(ctx context.Context, runner service.Runner) erro
7474
func (s *EonPubKeySyncer) getInitialPubKeys(ctx context.Context) ([]*event.EonPublicKey, error) {
7575
// This blocknumber specifies AT what state
7676
// the contract is called
77-
// XXX: does the call-opts blocknumber -1 also means latest?
7877
opts := &bind.CallOpts{
7978
Context: ctx,
8079
BlockNumber: s.StartBlock.Int,
@@ -113,9 +112,14 @@ func (s *EonPubKeySyncer) logCallError(attrName string, err error) {
113112
}
114113

115114
func (s *EonPubKeySyncer) GetEonPubKeyForEon(ctx context.Context, opts *bind.CallOpts, eon uint64) (*event.EonPublicKey, error) {
116-
if opts == nil {
117-
opts = &bind.CallOpts{
118-
Context: ctx,
115+
var err error
116+
if err := guardCallOpts(opts, true); err != nil {
117+
return nil, err
118+
}
119+
if number.BigToBlockNumber(opts.BlockNumber).IsLatest() {
120+
opts, err = fixCallOpts(ctx, s.Client, opts)
121+
if err != nil {
122+
return nil, err
119123
}
120124
}
121125
key, err := s.KeyBroadcast.GetEonKey(opts, eon)
@@ -125,8 +129,9 @@ func (s *EonPubKeySyncer) GetEonPubKeyForEon(ctx context.Context, opts *bind.Cal
125129
return nil, err
126130
}
127131
return &event.EonPublicKey{
128-
Eon: eon,
129-
Key: key,
132+
Eon: eon,
133+
Key: key,
134+
AtBlockNumber: number.BigToBlockNumber(opts.BlockNumber),
130135
}, nil
131136
}
132137

@@ -137,9 +142,11 @@ func (s *EonPubKeySyncer) watchNewEonPubkey(ctx context.Context) error {
137142
if !ok {
138143
return nil
139144
}
145+
bn := newEonKey.Raw.BlockNumber
140146
ev := &event.EonPublicKey{
141-
Eon: newEonKey.Eon,
142-
Key: newEonKey.Key,
147+
Eon: newEonKey.Eon,
148+
Key: newEonKey.Key,
149+
AtBlockNumber: number.NewBlockNumber(&bn),
143150
}
144151
err := s.Handler(ctx, ev)
145152
if err != nil {

rolling-shutter/keyperimpl/optimism/sync/syncer/keyperset.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,33 @@ func (s *KeyperSetSyncer) Start(ctx context.Context, runner service.Runner) erro
8282
}
8383

8484
func (s *KeyperSetSyncer) getInitialKeyperSets(ctx context.Context) ([]*event.KeyperSet, error) {
85-
// This blocknumber specifies AT what state
86-
// the contract is called
87-
// XXX: does the call-opts blocknumber -1 also means latest?
8885
opts := &bind.CallOpts{
8986
Context: ctx,
9087
BlockNumber: s.StartBlock.Int,
9188
}
92-
numKS, err := s.Contract.GetNumKeyperSets(opts)
93-
if err != nil {
89+
if err := guardCallOpts(opts, false); err != nil {
9490
return nil, err
9591
}
92+
bn := s.StartBlock.ToUInt64Ptr()
93+
if bn == nil {
94+
// this should not be the case
95+
return nil, errors.New("start block is 'latest'")
96+
}
9697

9798
initialKeyperSets := []*event.KeyperSet{}
9899
// this blocknumber specifies the argument to the contract
99100
// getter
100-
ks, err := s.GetKeyperSetForBlock(ctx, nil, s.StartBlock)
101+
ks, err := s.GetKeyperSetForBlock(ctx, opts, s.StartBlock)
101102
if err != nil {
102103
return nil, err
103104
}
104105
initialKeyperSets = append(initialKeyperSets, ks)
105106

107+
numKS, err := s.Contract.GetNumKeyperSets(opts)
108+
if err != nil {
109+
return nil, err
110+
}
111+
106112
for i := ks.Eon + 1; i < numKS; i++ {
107113
ks, err = s.GetKeyperSetByIndex(ctx, opts, i)
108114
if err != nil {
@@ -115,10 +121,8 @@ func (s *KeyperSetSyncer) getInitialKeyperSets(ctx context.Context) ([]*event.Ke
115121
}
116122

117123
func (s *KeyperSetSyncer) GetKeyperSetByIndex(ctx context.Context, opts *bind.CallOpts, index uint64) (*event.KeyperSet, error) {
118-
if opts == nil {
119-
opts = &bind.CallOpts{
120-
Context: ctx,
121-
}
124+
if err := guardCallOpts(opts, true); err != nil {
125+
return nil, err
122126
}
123127
actBl, err := s.Contract.GetKeyperSetActivationBlock(opts, index)
124128
if err != nil {
@@ -134,18 +138,15 @@ func (s *KeyperSetSyncer) GetKeyperSetByIndex(ctx context.Context, opts *bind.Ca
134138
func (s *KeyperSetSyncer) GetKeyperSetForBlock(ctx context.Context, opts *bind.CallOpts, b *number.BlockNumber) (*event.KeyperSet, error) {
135139
var latestBlock uint64
136140
var err error
141+
if err := guardCallOpts(opts, true); err != nil {
142+
return nil, err
143+
}
137144

138145
if b.Equal(number.LatestBlock) {
139146
latestBlock, err = s.Client.BlockNumber(ctx)
140147
} else {
141148
latestBlock = b.Uint64()
142149
}
143-
if opts == nil {
144-
// call at "latest block" state
145-
opts = &bind.CallOpts{
146-
Context: ctx,
147-
}
148-
}
149150
idx, err := s.Contract.GetKeyperSetIndexByBlock(opts, latestBlock)
150151
if err != nil {
151152
return nil, errors.Wrap(err, "could not retrieve keyper set index")
@@ -159,34 +160,31 @@ func (s *KeyperSetSyncer) newEvent(
159160
keyperSetContract common.Address,
160161
activationBlock uint64,
161162
) (*event.KeyperSet, error) {
162-
callOpts := opts
163-
if callOpts == nil {
164-
callOpts = &bind.CallOpts{
165-
Context: ctx,
166-
}
163+
if err := guardCallOpts(opts, false); err != nil {
164+
return nil, err
167165
}
168166
ks, err := bindings.NewKeyperSet(keyperSetContract, s.Client)
169167
if err != nil {
170168
return nil, errors.Wrap(err, "could not bind to KeyperSet contract")
171169
}
172170
// the manager only accepts final keyper sets,
173171
// so we expect this to be final now.
174-
final, err := ks.IsFinalized(callOpts)
172+
final, err := ks.IsFinalized(opts)
175173
if err != nil {
176174
return nil, makeCallError("IsFinalized", err)
177175
}
178176
if !final {
179177
return nil, errors.New("contract did accept unfinalized keyper-sets")
180178
}
181-
members, err := ks.GetMembers(callOpts)
179+
members, err := ks.GetMembers(opts)
182180
if err != nil {
183181
return nil, makeCallError("Members", err)
184182
}
185-
threshold, err := ks.GetThreshold(callOpts)
183+
threshold, err := ks.GetThreshold(opts)
186184
if err != nil {
187185
return nil, makeCallError("Threshold", err)
188186
}
189-
eon, err := s.Contract.GetKeyperSetIndexByBlock(callOpts, activationBlock)
187+
eon, err := s.Contract.GetKeyperSetIndexByBlock(opts, activationBlock)
190188
if err != nil {
191189
return nil, makeCallError("KeyperSetIndexByBlock", err)
192190
}
@@ -195,6 +193,7 @@ func (s *KeyperSetSyncer) newEvent(
195193
Members: members,
196194
Threshold: threshold,
197195
Eon: eon,
196+
AtBlockNumber: number.BigToBlockNumber(opts.BlockNumber),
198197
}, nil
199198
}
200199

@@ -205,9 +204,10 @@ func (s *KeyperSetSyncer) watchNewKeypersService(ctx context.Context) error {
205204
if !ok {
206205
return nil
207206
}
207+
opts := logToCallOpts(ctx, &newKeypers.Raw)
208208
newKeyperSet, err := s.newEvent(
209209
ctx,
210-
nil,
210+
opts,
211211
newKeypers.KeyperSetContract,
212212
newKeypers.ActivationBlock,
213213
)

rolling-shutter/keyperimpl/optimism/sync/syncer/unsafehead.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/optimism/sync/client"
1111
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/optimism/sync/event"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/number"
1213
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
1314
)
1415

@@ -49,7 +50,7 @@ func (s *UnsafeHeadSyncer) watchLatestUnsafeHead(ctx context.Context) error {
4950
return nil
5051
}
5152
ev := &event.LatestBlock{
52-
Number: newHeader.Number,
53+
Number: number.BigToBlockNumber(newHeader.Number),
5354
BlockHash: newHeader.Hash(),
5455
}
5556
err := s.Handler(ctx, ev)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package syncer
2+
3+
import (
4+
"context"
5+
"errors"
6+
"math/big"
7+
8+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
9+
"github.com/ethereum/go-ethereum/core/types"
10+
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/optimism/sync/client"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/number"
13+
)
14+
15+
var (
16+
errNilCallOpts = errors.New("nil call-opts")
17+
errLatestBlock = errors.New("'nil' latest block")
18+
)
19+
20+
func logToCallOpts(ctx context.Context, log *types.Log) *bind.CallOpts {
21+
block := new(big.Int)
22+
block.SetUint64(log.BlockNumber)
23+
return &bind.CallOpts{
24+
BlockNumber: block,
25+
Context: ctx,
26+
}
27+
}
28+
29+
func guardCallOpts(opts *bind.CallOpts, allowLatest bool) error {
30+
if opts == nil {
31+
return errNilCallOpts
32+
}
33+
if !allowLatest {
34+
n := number.BlockNumber{Int: opts.BlockNumber}
35+
if n.IsLatest() {
36+
return errLatestBlock
37+
}
38+
}
39+
return nil
40+
}
41+
42+
func fixCallOpts(ctx context.Context, c client.Client, opts *bind.CallOpts) (*bind.CallOpts, error) {
43+
err := guardCallOpts(opts, false)
44+
if err != nil {
45+
return opts, nil
46+
}
47+
// query the current latest block and fix it
48+
latest, err := c.BlockNumber(ctx)
49+
if err != nil {
50+
return nil, err
51+
}
52+
blockNumber := number.NewBlockNumber(&latest)
53+
if errors.Is(err, errNilCallOpts) {
54+
opts = &bind.CallOpts{
55+
Context: ctx,
56+
BlockNumber: blockNumber.Int,
57+
}
58+
return opts, nil
59+
}
60+
if errors.Is(err, errLatestBlock) {
61+
opts.BlockNumber = blockNumber.Int
62+
return opts, nil
63+
}
64+
return nil, err
65+
}

rolling-shutter/medley/encodeable/number/block.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ var (
1111
LatestStr = []byte("latest")
1212
)
1313

14+
func BigToBlockNumber(i *big.Int) *BlockNumber {
15+
return &BlockNumber{
16+
Int: i,
17+
}
18+
}
19+
1420
func NewBlockNumber(u *uint64) *BlockNumber {
1521
b := &BlockNumber{
1622
Int: &big.Int{},

rolling-shutter/medley/identitypreimage/identitypreimage.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
type IdentityPreimage []byte
1111

12-
// BigToIdentityPreimage converts n to an epoch id. It fails if n is too big.
1312
func BigToIdentityPreimage(n *big.Int) IdentityPreimage {
1413
return IdentityPreimage(n.Bytes())
1514
}

0 commit comments

Comments
 (0)