Skip to content

Commit f1b9a8f

Browse files
authored
Refactor epochid package (#419)
* feat: rename epochid to identityPreimage and make length arbitrary
1 parent 67a0637 commit f1b9a8f

38 files changed

+437
-480
lines changed

rolling-shutter/cmd/cryptocmd/cryptocmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"github.com/shutter-network/shutter/shlib/shcrypto"
1313

14-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
14+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
1515
)
1616

1717
var (
@@ -223,12 +223,12 @@ func parseEonKey(f string) (*shcrypto.EonPublicKey, error) {
223223
return eonKey, err
224224
}
225225

226-
func parseEpochID(f string) (epochid.EpochID, error) {
226+
func parseEpochID(f string) (identitypreimage.IdentityPreimage, error) {
227227
epochIDBytes, err := parseHex(f)
228228
if err != nil {
229-
return epochid.EpochID{}, err
229+
return identitypreimage.IdentityPreimage{}, err
230230
}
231-
return epochid.BytesToEpochID(epochIDBytes)
231+
return identitypreimage.IdentityPreimage(epochIDBytes), nil
232232
}
233233

234234
func parseSigma(f string) (shcrypto.Block, error) {

rolling-shutter/collator/batcher/batcher.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/shutter-network/rolling-shutter/rolling-shutter/collator/batchhandler"
1818
"github.com/shutter-network/rolling-shutter/rolling-shutter/collator/config"
1919
"github.com/shutter-network/rolling-shutter/rolling-shutter/db/cltrdb"
20-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
20+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
2121
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
2222
)
2323

@@ -90,30 +90,30 @@ func NewBatcher(ctx context.Context, cfg *config.Config, dbpool *pgxpool.Pool) (
9090
func (btchr *Batcher) initializeNextBatch(
9191
ctx context.Context,
9292
db *cltrdb.Queries,
93-
) (epochid.EpochID, uint64, error) {
93+
) (identitypreimage.IdentityPreimage, uint64, error) {
9494
var (
95-
nextEpochID epochid.EpochID
96-
l1BlockNumber uint64
97-
err error
95+
nextIdentityPreimage identitypreimage.IdentityPreimage
96+
l1BlockNumber uint64
97+
err error
9898
)
9999
l1BlockNumber, err = btchr.l1EthClient.BlockNumber(ctx)
100100
if err != nil {
101-
return nextEpochID, l1BlockNumber, err
101+
return nextIdentityPreimage, l1BlockNumber, err
102102
}
103103
if l1BlockNumber > math.MaxInt64 {
104-
return nextEpochID, l1BlockNumber, errors.Errorf("block number too big: %d", l1BlockNumber)
104+
return nextIdentityPreimage, l1BlockNumber, errors.Errorf("block number too big: %d", l1BlockNumber)
105105
}
106106

107107
l2batchIndex, err := btchr.l2Client.GetBatchIndex(ctx)
108108
if err != nil {
109-
return nextEpochID, l1BlockNumber, err
109+
return nextIdentityPreimage, l1BlockNumber, err
110110
}
111-
nextEpochID = epochid.Uint64ToEpochID(l2batchIndex + 1)
111+
nextIdentityPreimage = identitypreimage.Uint64ToIdentityPreimage(l2batchIndex + 1)
112112
err = db.SetNextBatch(ctx, cltrdb.SetNextBatchParams{
113-
EpochID: nextEpochID.Bytes(),
113+
EpochID: nextIdentityPreimage.Bytes(),
114114
L1BlockNumber: int64(l1BlockNumber),
115115
})
116-
return nextEpochID, l1BlockNumber, err
116+
return nextIdentityPreimage, l1BlockNumber, err
117117
}
118118

119119
// earlyValidateTx validates a transaction for some basic properties.
@@ -186,14 +186,14 @@ func (btchr *Batcher) initChainState(ctx context.Context) error {
186186
btchr.nextBatchChainState = nil
187187
return err
188188
}
189-
log.Info().Uint64("batch-index", btchr.nextBatchChainState.epochID.Uint64()).
189+
log.Info().Uint64("batch-index", btchr.nextBatchChainState.identityPreimage.Uint64()).
190190
Msg("loaded chain state")
191191
return nil
192192
}
193193

194194
// loadAndApplyTransactions loads transactions from the database for the current batch.
195195
func (btchr *Batcher) loadAndApplyTransactions(ctx context.Context, db *cltrdb.Queries) error {
196-
txs, err := db.GetNonRejectedTransactionsByEpoch(ctx, btchr.nextBatchChainState.epochID.Bytes())
196+
txs, err := db.GetNonRejectedTransactionsByEpoch(ctx, btchr.nextBatchChainState.identityPreimage.Bytes())
197197
if err != nil {
198198
return err
199199
}
@@ -292,10 +292,7 @@ func (btchr *Batcher) closeBatchImpl(
292292
return err
293293
}
294294

295-
newEpoch, err := batchhandler.ComputeNextEpochID(nextBatchEpochID)
296-
if err != nil {
297-
return err
298-
}
295+
newEpoch := batchhandler.ComputeNextEpochID(nextBatchEpochID)
299296

300297
return db.SetNextBatch(ctx, cltrdb.SetNextBatchParams{
301298
EpochID: newEpoch.Bytes(),
@@ -406,10 +403,10 @@ func (btchr *Batcher) EnqueueTx(ctx context.Context, txBytes []byte) error {
406403
}
407404

408405
err = btchr.dbpool.BeginFunc(ctx, func(dbtx pgx.Tx) error {
409-
epochID := epochid.Uint64ToEpochID(tx.BatchIndex()).Bytes()
406+
identityPreimage := identitypreimage.Uint64ToIdentityPreimage(tx.BatchIndex()).Bytes()
410407
return cltrdb.New(dbtx).InsertTx(ctx, cltrdb.InsertTxParams{
411408
TxHash: tx.Hash().Bytes(),
412-
EpochID: epochID,
409+
EpochID: identityPreimage,
413410
TxBytes: txBytes,
414411
Status: txstatus,
415412
})

rolling-shutter/collator/batcher/batcher_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010

1111
"github.com/shutter-network/rolling-shutter/rolling-shutter/collator/batchhandler"
1212
"github.com/shutter-network/rolling-shutter/rolling-shutter/db/cltrdb"
13-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
13+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
1414
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
1515
)
1616

1717
func DefaultTestParams() TestParams {
1818
return TestParams{
19-
GasLimit: uint64(210000),
20-
BaseFee: big.NewInt(1),
21-
InitialBalance: big.NewInt(1000000),
22-
TxGasTipCap: big.NewInt(1),
23-
TxGasFeeCap: big.NewInt(2),
24-
InitialEpochID: epochid.Uint64ToEpochID(2000),
19+
GasLimit: uint64(210000),
20+
BaseFee: big.NewInt(1),
21+
InitialBalance: big.NewInt(1000000),
22+
TxGasTipCap: big.NewInt(1),
23+
TxGasFeeCap: big.NewInt(2),
24+
InitialIdentityPreimage: identitypreimage.Uint64ToIdentityPreimage(2000),
2525
}
2626
}
2727

@@ -32,7 +32,7 @@ func TestRejectBadTransactionsIntegration(t *testing.T) {
3232

3333
ctx := context.Background()
3434
fixtures := Setup(ctx, t, DefaultTestParams())
35-
nextBatchIndex := int(fixtures.Params.InitialEpochID.Uint64())
35+
nextBatchIndex := int(fixtures.Params.InitialIdentityPreimage.Uint64())
3636
batchIndexAcceptenceInterval := int(fixtures.Config.BatchIndexAcceptenceInterval)
3737
t.Run("Future", func(t *testing.T) {
3838
tx, _ := fixtures.MakeTx(t, 0, nextBatchIndex+batchIndexAcceptenceInterval, 0, 22000)
@@ -72,7 +72,7 @@ func TestRejectTxNotEnoughFundsIntegration(t *testing.T) {
7272

7373
ctx := context.Background()
7474
fixtures := Setup(ctx, t, DefaultTestParams())
75-
nextBatchIndex := int(fixtures.Params.InitialEpochID.Uint64())
75+
nextBatchIndex := int(fixtures.Params.InitialIdentityPreimage.Uint64())
7676
tx, _ := fixtures.MakeTx(t, 1, nextBatchIndex, 0, 22000)
7777
err := fixtures.Batcher.EnqueueTx(ctx, tx)
7878
assert.Error(t, err, ErrCannotPayGasFee.Error())
@@ -85,15 +85,15 @@ func TestConfirmTransactionsIntegration(t *testing.T) {
8585

8686
ctx := context.Background()
8787
fixtures := Setup(ctx, t, DefaultTestParams())
88-
nextBatchIndex := int(fixtures.Params.InitialEpochID.Uint64())
88+
nextBatchIndex := int(fixtures.Params.InitialIdentityPreimage.Uint64())
8989

9090
for nonce := 0; nonce < 5; nonce++ {
9191
tx, _ := fixtures.MakeTx(t, 0, nextBatchIndex, nonce, 22000)
9292
err := fixtures.Batcher.EnqueueTx(ctx, tx)
9393
assert.NilError(t, err)
9494
}
9595

96-
txs, err := fixtures.DB.GetTransactionsByEpoch(ctx, fixtures.Params.InitialEpochID.Bytes())
96+
txs, err := fixtures.DB.GetTransactionsByEpoch(ctx, fixtures.Params.InitialIdentityPreimage.Bytes())
9797
assert.NilError(t, err)
9898
assert.Equal(t, 5, len(txs), "should have exactly one tx: %+v", txs)
9999
for nonce := 0; nonce < 5; nonce++ {
@@ -120,7 +120,7 @@ func TestCloseBatchIntegration(t *testing.T) {
120120
assert.Check(t, fixtures.Batcher.nextBatchChainState == nil, "nextBatchChainState field initialized")
121121
nextBatchEpoch, _, err := batchhandler.GetNextBatch(ctx, fixtures.DB)
122122
assert.NilError(t, err)
123-
assert.Equal(t, nextBatchEpoch.Uint64(), fixtures.Params.InitialEpochID.Uint64()+1)
123+
assert.Equal(t, nextBatchEpoch.Uint64(), fixtures.Params.InitialIdentityPreimage.Uint64()+1)
124124
})
125125

126126
t.Run("initChainStateWaitForSequencer", func(t *testing.T) {
@@ -129,13 +129,13 @@ func TestCloseBatchIntegration(t *testing.T) {
129129
})
130130

131131
t.Run("batchAlreadyExists", func(t *testing.T) {
132-
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialEpochID.Uint64() + 1)
132+
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialIdentityPreimage.Uint64() + 1)
133133
err = fixtures.Batcher.initChainState(ctx)
134134
assert.Error(t, err, ErrBatchAlreadyExists.Error())
135135
})
136136

137137
t.Run("initChainStateSetsNextBatchChainState", func(t *testing.T) {
138-
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialEpochID.Uint64())
138+
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialIdentityPreimage.Uint64())
139139
err = fixtures.Batcher.initChainState(ctx)
140140
assert.NilError(t, err)
141141
assert.Check(t, fixtures.Batcher.nextBatchChainState != nil, "nextBatchChainState field not initialized")
@@ -160,7 +160,7 @@ func TestOpenNextBatch(t *testing.T) {
160160
assert.NilError(t, err)
161161
nextBatchIndex := nextBatchEpoch.Uint64()
162162

163-
assert.Equal(t, nextBatchIndex, fixtures.Params.InitialEpochID.Uint64()+1)
163+
assert.Equal(t, nextBatchIndex, fixtures.Params.InitialIdentityPreimage.Uint64()+1)
164164
// we should now be able to enqueue transactions. The batcher however doesn't have
165165
// information about the current nonce and the balances, so we're able to enqueue
166166
// transactions that later will be rejected when the l2 chain builds a new block.
@@ -174,20 +174,20 @@ func TestOpenNextBatch(t *testing.T) {
174174
err = fixtures.Batcher.EnqueueTx(ctx, tx2)
175175
assert.NilError(t, err)
176176

177-
txs, err := fixtures.DB.GetNonRejectedTransactionsByEpoch(ctx, epochid.Uint64ToEpochID(nextBatchIndex).Bytes())
177+
txs, err := fixtures.DB.GetNonRejectedTransactionsByEpoch(ctx, identitypreimage.Uint64ToIdentityPreimage(nextBatchIndex).Bytes())
178178
assert.NilError(t, err)
179179
assert.Equal(t, len(txs), 2)
180180

181181
// so, now let's let the l2 chain build a new block
182-
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialEpochID.Uint64())
182+
fixtures.EthL2Server.SetBatchIndex(fixtures.Params.InitialIdentityPreimage.Uint64())
183183

184184
tx3, _ := fixtures.MakeTx(t, 2, int(nextBatchIndex), 0, 22000)
185185
err = fixtures.Batcher.EnqueueTx(ctx, tx3)
186186
assert.Error(t, err, ErrCannotPayGasFee.Error())
187187

188188
assert.Check(t, fixtures.Batcher.nextBatchChainState != nil, "nextBatchChainState field not initialized")
189189

190-
txs, err = fixtures.DB.GetTransactionsByEpoch(ctx, epochid.Uint64ToEpochID(nextBatchIndex).Bytes())
190+
txs, err = fixtures.DB.GetTransactionsByEpoch(ctx, identitypreimage.Uint64ToIdentityPreimage(nextBatchIndex).Bytes())
191191
assert.NilError(t, err)
192192
assert.Equal(t, len(txs), 2)
193193

@@ -210,7 +210,7 @@ func TestDecryptionTriggerGeneratedIntegration(t *testing.T) {
210210
assert.NilError(t, err)
211211
nextBatchIndex := nextBatchEpoch.Uint64()
212212

213-
assert.Equal(t, nextBatchIndex, fixtures.Params.InitialEpochID.Uint64())
213+
assert.Equal(t, nextBatchIndex, fixtures.Params.InitialIdentityPreimage.Uint64())
214214

215215
tx, txHash := fixtures.MakeTx(t, 0, int(nextBatchIndex), 0, 22000)
216216
err = fixtures.Batcher.EnqueueTx(ctx, tx)
@@ -247,13 +247,13 @@ func TestDecryptionTriggerInsertOrderingIntegration(t *testing.T) {
247247
fixtures := Setup(ctx, t, DefaultTestParams())
248248

249249
trigger1 := cltrdb.InsertTriggerParams{
250-
EpochID: epochid.Uint64ToEpochID(2).Bytes(),
250+
EpochID: identitypreimage.Uint64ToIdentityPreimage(2).Bytes(),
251251
BatchHash: common.BytesToHash([]byte{1, 0}).Bytes(),
252252
L1BlockNumber: 666,
253253
}
254254

255255
trigger2 := cltrdb.InsertTriggerParams{
256-
EpochID: epochid.Uint64ToEpochID(1).Bytes(),
256+
EpochID: identitypreimage.Uint64ToIdentityPreimage(1).Bytes(),
257257
BatchHash: common.BytesToHash([]byte{0, 1}).Bytes(),
258258
L1BlockNumber: 42,
259259
}

rolling-shutter/collator/batcher/chainstate.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
txtypes "github.com/shutter-network/txtypes/types"
99

1010
"github.com/shutter-network/rolling-shutter/rolling-shutter/collator/batchhandler/batch"
11-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
1212
)
1313

1414
var (
@@ -23,26 +23,28 @@ var (
2323
// decide if transactions will be included into the next block. The collator creates a new
2424
// ChainState object for each block and calls ApplyTx for each transaction that it wants to apply.
2525
type ChainState struct {
26-
balances map[common.Address]*big.Int
27-
nonces map[common.Address]uint64
28-
gasUsed uint64
29-
sizeInBytes uint64
30-
numTransactions uint64
31-
signer txtypes.Signer
32-
baseFee *big.Int
33-
blockGasLimit uint64
34-
epochID epochid.EpochID
26+
balances map[common.Address]*big.Int
27+
nonces map[common.Address]uint64
28+
gasUsed uint64
29+
sizeInBytes uint64
30+
numTransactions uint64
31+
signer txtypes.Signer
32+
baseFee *big.Int
33+
blockGasLimit uint64
34+
identityPreimage identitypreimage.IdentityPreimage
3535
}
3636

37-
func NewChainState(signer txtypes.Signer, baseFee *big.Int, blockGasLimit uint64, epochID epochid.EpochID) *ChainState {
37+
func NewChainState(signer txtypes.Signer, baseFee *big.Int, blockGasLimit uint64,
38+
identityPreimage identitypreimage.IdentityPreimage,
39+
) *ChainState {
3840
return &ChainState{
39-
balances: make(map[common.Address]*big.Int),
40-
nonces: make(map[common.Address]uint64),
41-
gasUsed: 0,
42-
signer: signer,
43-
baseFee: baseFee,
44-
blockGasLimit: blockGasLimit,
45-
epochID: epochID,
41+
balances: make(map[common.Address]*big.Int),
42+
nonces: make(map[common.Address]uint64),
43+
gasUsed: 0,
44+
signer: signer,
45+
baseFee: baseFee,
46+
blockGasLimit: blockGasLimit,
47+
identityPreimage: identityPreimage,
4648
}
4749
}
4850

rolling-shutter/collator/batcher/setup_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/shutter-network/rolling-shutter/rolling-shutter/db/cltrdb"
2323
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/configuration"
2424
enctime "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/time"
25-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
25+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
2626
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testdb"
2727
)
2828

@@ -95,12 +95,12 @@ func newTestConfig(t *testing.T) *config.Config {
9595
}
9696

9797
type TestParams struct {
98-
GasLimit uint64
99-
InitialBalance *big.Int
100-
BaseFee *big.Int
101-
TxGasTipCap *big.Int
102-
TxGasFeeCap *big.Int
103-
InitialEpochID epochid.EpochID
98+
GasLimit uint64
99+
InitialBalance *big.Int
100+
BaseFee *big.Int
101+
TxGasTipCap *big.Int
102+
TxGasFeeCap *big.Int
103+
InitialIdentityPreimage identitypreimage.IdentityPreimage
104104
}
105105

106106
type Fixture struct {
@@ -135,7 +135,7 @@ func Setup(ctx context.Context, t *testing.T, params TestParams) *Fixture {
135135
ethL2 := sequencer.RunMockEthServer(t)
136136
t.Cleanup(ethL2.Teardown)
137137
cfg.SequencerURL = ethL2.URL
138-
ethL2.SetBatchIndex(params.InitialEpochID.Uint64() - 1)
138+
ethL2.SetBatchIndex(params.InitialIdentityPreimage.Uint64() - 1)
139139

140140
db, dbpool, dbteardown := testdb.NewCollatorTestDB(ctx, t)
141141
t.Cleanup(dbteardown)

rolling-shutter/collator/batchhandler/batchhandler.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,26 @@ import (
77
"github.com/pkg/errors"
88

99
"github.com/shutter-network/rolling-shutter/rolling-shutter/db/cltrdb"
10-
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid"
10+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
1111
)
1212

1313
// ComputeNextEpochID takes an epoch id as parameter and returns the id of the epoch following it.
14-
func ComputeNextEpochID(epochID epochid.EpochID) (epochid.EpochID, error) {
15-
n := epochID.Big()
16-
return epochid.BigToEpochID(n.Add(n, common.Big1))
14+
func ComputeNextEpochID(identityPreimage identitypreimage.IdentityPreimage) identitypreimage.IdentityPreimage {
15+
n := identityPreimage.Big()
16+
return identitypreimage.BigToIdentityPreimage(n.Add(n, common.Big1))
1717
}
1818

1919
// GetNextBatch gets the epochID and block number that will be used in the next batch.
20-
func GetNextBatch(ctx context.Context, db *cltrdb.Queries) (epochid.EpochID, uint64, error) {
20+
func GetNextBatch(ctx context.Context, db *cltrdb.Queries) (identitypreimage.IdentityPreimage, uint64, error) {
2121
b, err := db.GetNextBatch(ctx)
2222
if err != nil {
2323
// There should already be an epochID in the database so not finding a row is an error
24-
return epochid.EpochID{}, 0, err
25-
}
26-
epochID, err := epochid.BytesToEpochID(b.EpochID)
27-
if err != nil {
28-
return epochid.EpochID{}, 0, err
24+
return identitypreimage.IdentityPreimage{}, 0, err
2925
}
26+
identityPreimage := identitypreimage.IdentityPreimage(b.EpochID)
3027
if b.L1BlockNumber < 0 {
31-
return epochid.EpochID{}, 0, errors.Errorf("negative l1 block number in db")
28+
return identitypreimage.IdentityPreimage{}, 0, errors.Errorf("negative l1 block number in db")
3229
}
3330
l1BlockNumber := uint64(b.L1BlockNumber)
34-
return epochID, l1BlockNumber, nil
31+
return identityPreimage, l1BlockNumber, nil
3532
}

0 commit comments

Comments
 (0)