Skip to content

Commit 20051ef

Browse files
committed
feat: WIP
1 parent 67cf221 commit 20051ef

File tree

24 files changed

+403
-117
lines changed

24 files changed

+403
-117
lines changed

internal/claimer/blockchain.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,18 @@ func (self *claimerBlockchain) submitClaimToBlockchain(
8585
txHash := common.Hash{}
8686
lastBlockNumber := new(big.Int).SetUint64(epoch.LastBlock)
8787
tx, err := ic.SubmitClaim(self.txOpts, application.IApplicationAddress,
88-
lastBlockNumber, *epoch.ClaimHash)
88+
lastBlockNumber, *epoch.OutputsMerkleRoot)
8989
if err != nil {
9090
self.logger.Error("submitClaimToBlockchain:failed",
9191
"appContractAddress", application.IApplicationAddress,
92-
"claimHash", *epoch.ClaimHash,
92+
"claimHash", *epoch.OutputsMerkleRoot,
9393
"last_block", epoch.LastBlock,
9494
"error", err)
9595
} else {
9696
txHash = tx.Hash()
9797
self.logger.Debug("submitClaimToBlockchain:success",
9898
"appContractAddress", application.IApplicationAddress,
99-
"claimHash", *epoch.ClaimHash,
99+
"claimHash", *epoch.OutputsMerkleRoot,
100100
"last_block", epoch.LastBlock,
101101
"TxHash", txHash)
102102
}

internal/claimer/claimer.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (s *Service) submitClaimsAndUpdateDatabase(
135135
s.Logger.Info("Claim submitted",
136136
"app", apps[key].IApplicationAddress,
137137
"receipt_block_number", receipt.BlockNumber,
138-
"claim_hash", fmt.Sprintf("%x", computedEpoch.ClaimHash),
138+
"claim_hash", fmt.Sprintf("%x", computedEpoch.OutputsMerkleRoot),
139139
"last_block", computedEpoch.LastBlock,
140140
"tx", txHash)
141141
delete(computedEpochs, key)
@@ -258,7 +258,7 @@ func (s *Service) submitClaimsAndUpdateDatabase(
258258
}
259259
s.Logger.Debug("Updating claim status to submitted",
260260
"app", app.IApplicationAddress,
261-
"claim_hash", fmt.Sprintf("%x", currEpoch.ClaimHash),
261+
"claim_hash", fmt.Sprintf("%x", currEpoch.OutputsMerkleRoot),
262262
"last_block", currEpoch.LastBlock,
263263
)
264264
txHash := currClaimSubmissionEvent.Raw.TxHash
@@ -277,21 +277,21 @@ func (s *Service) submitClaimsAndUpdateDatabase(
277277
s.Logger.Info("Claim previously submitted",
278278
"app", app.IApplicationAddress,
279279
"event_block_number", currClaimSubmissionEvent.Raw.BlockNumber,
280-
"claim_hash", fmt.Sprintf("%x", currEpoch.ClaimHash),
280+
"claim_hash", fmt.Sprintf("%x", currEpoch.OutputsMerkleRoot),
281281
"last_block", currEpoch.LastBlock,
282282
)
283283
} else if s.submissionEnabled {
284284
if prevEpoch != nil && prevEpoch.Status != model.EpochStatus_ClaimAccepted {
285285
s.Logger.Debug("Waiting previous claim to be accepted before submitting new one. Previous:",
286286
"app", app.IApplicationAddress,
287-
"claim_hash", fmt.Sprintf("%x", prevEpoch.ClaimHash),
287+
"claim_hash", fmt.Sprintf("%x", prevEpoch.OutputsMerkleRoot),
288288
"last_block", prevEpoch.LastBlock,
289289
)
290290
goto nextApp
291291
}
292292
s.Logger.Debug("Submitting claim to blockchain",
293293
"app", app.IApplicationAddress,
294-
"claim_hash", fmt.Sprintf("%x", currEpoch.ClaimHash),
294+
"claim_hash", fmt.Sprintf("%x", currEpoch.OutputsMerkleRoot),
295295
"last_block", currEpoch.LastBlock,
296296
)
297297
txHash, err := s.blockchain.submitClaimToBlockchain(ic, app, currEpoch)
@@ -304,7 +304,7 @@ func (s *Service) submitClaimsAndUpdateDatabase(
304304
} else {
305305
s.Logger.Debug("Claim submission disabled. Doing nothing",
306306
"app", app.IApplicationAddress,
307-
"claim_hash", fmt.Sprintf("%x", currEpoch.ClaimHash),
307+
"claim_hash", fmt.Sprintf("%x", currEpoch.OutputsMerkleRoot),
308308
"last_block", currEpoch.LastBlock,
309309
)
310310

@@ -409,7 +409,7 @@ func (s *Service) acceptClaimsAndUpdateDatabase(
409409
}
410410
s.Logger.Debug("Updating claim status to accepted",
411411
"app", app.IApplicationAddress,
412-
"claim_hash", fmt.Sprintf("%x", submittedEpoch.ClaimHash),
412+
"claim_hash", fmt.Sprintf("%x", submittedEpoch.OutputsMerkleRoot),
413413
"last_block", submittedEpoch.LastBlock,
414414
)
415415
txHash := currEvent.Raw.TxHash
@@ -482,7 +482,7 @@ func checkEpochConstraint(c *model.Epoch) error {
482482
return fmt.Errorf("unexpected epoch state. first_block: %v > last_block: %v", c.FirstBlock, c.LastBlock)
483483
}
484484
if c.Status == model.EpochStatus_ClaimSubmitted {
485-
if c.ClaimHash == nil {
485+
if c.OutputsMerkleRoot == nil {
486486
return fmt.Errorf("unexpected epoch state. missing claim_hash.")
487487
}
488488
}
@@ -520,13 +520,13 @@ func checkEpochSequenceConstraint(prevEpoch *model.Epoch, currEpoch *model.Epoch
520520

521521
func claimSubmittedEventMatches(application *model.Application, epoch *model.Epoch, event *iconsensus.IConsensusClaimSubmitted) bool {
522522
return application.IApplicationAddress == event.AppContract &&
523-
*epoch.ClaimHash == event.OutputsMerkleRoot &&
523+
*epoch.OutputsMerkleRoot == event.OutputsMerkleRoot &&
524524
epoch.LastBlock == event.LastProcessedBlockNumber.Uint64()
525525
}
526526

527527
func claimAcceptedEventMatches(application *model.Application, epoch *model.Epoch, event *iconsensus.IConsensusClaimAccepted) bool {
528528
return application.IApplicationAddress == event.AppContract &&
529-
*epoch.ClaimHash == event.OutputsMerkleRoot &&
529+
*epoch.OutputsMerkleRoot == event.OutputsMerkleRoot &&
530530
epoch.LastBlock == event.LastProcessedBlockNumber.Uint64()
531531
}
532532

internal/claimer/claimer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func makeEpoch(id int64, status model.EpochStatus, i uint64) *model.Epoch {
215215
LastBlock: i*10 + 9,
216216
Status: status,
217217
ClaimTransactionHash: &tx,
218-
ClaimHash: &hash,
218+
OutputsMerkleRoot: &hash,
219219
}
220220
return epoch
221221
}
@@ -250,7 +250,7 @@ func makeSubmittedEvent(app *model.Application, epoch *model.Epoch) *iconsensus.
250250
return &iconsensus.IConsensusClaimSubmitted{
251251
LastProcessedBlockNumber: new(big.Int).SetUint64(epoch.LastBlock),
252252
AppContract: app.IApplicationAddress,
253-
OutputsMerkleRoot: *epoch.ClaimHash,
253+
OutputsMerkleRoot: *epoch.OutputsMerkleRoot,
254254
Raw: types.Log{
255255
TxHash: common.HexToHash(epoch.ClaimTransactionHash.Hex()),
256256
},
@@ -261,7 +261,7 @@ func makeAcceptedEvent(app *model.Application, epoch *model.Epoch) *iconsensus.I
261261
return &iconsensus.IConsensusClaimAccepted{
262262
LastProcessedBlockNumber: new(big.Int).SetUint64(epoch.LastBlock),
263263
AppContract: app.IApplicationAddress,
264-
OutputsMerkleRoot: *epoch.ClaimHash,
264+
OutputsMerkleRoot: *epoch.OutputsMerkleRoot,
265265
Raw: types.Log{
266266
TxHash: common.HexToHash(epoch.ClaimTransactionHash.Hex()),
267267
},
@@ -560,7 +560,7 @@ func TestSubmitClaimWithAntecessorMismatch(t *testing.T) {
560560
prevEvent := &iconsensus.IConsensusClaimSubmitted{
561561
LastProcessedBlockNumber: new(big.Int).SetUint64(prevEpoch.LastBlock + 1),
562562
AppContract: app.IApplicationAddress,
563-
OutputsMerkleRoot: *prevEpoch.ClaimHash,
563+
OutputsMerkleRoot: *prevEpoch.OutputsMerkleRoot,
564564
}
565565
var currEvent *iconsensus.IConsensusClaimSubmitted = nil
566566

@@ -727,7 +727,7 @@ func TestAcceptClaimWithAntecessorMismatch(t *testing.T) {
727727
prevEvent := &iconsensus.IConsensusClaimAccepted{
728728
LastProcessedBlockNumber: new(big.Int).SetUint64(prevEpoch.LastBlock + 1),
729729
AppContract: app.IApplicationAddress,
730-
OutputsMerkleRoot: *prevEpoch.ClaimHash,
730+
OutputsMerkleRoot: *prevEpoch.OutputsMerkleRoot,
731731
}
732732
var currEvent *iconsensus.IConsensusClaimAccepted = nil
733733

internal/evmreader/evmreader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type EvmReaderRepository interface {
3737
) error
3838
GetEpoch(ctx context.Context, nameOrAddress string, index uint64) (*Epoch, error)
3939
ListEpochs(ctx context.Context, nameOrAddress string, f repository.EpochFilter, p repository.Pagination, descending bool) ([]*Epoch, uint64, error)
40-
UpdateEpoch(ctx context.Context, nameOrAddress string, e *Epoch) error
40+
UpdateEpochOutputsMerkleProof(ctx context.Context, nameOrAddress string, e *Epoch) error
4141
GetLastNonOpenEpoch(ctx context.Context, nameOrAddress string) (*Epoch, error)
4242

4343
GetNumberOfInputs(ctx context.Context, nameOrAddress string) (uint64, error)
@@ -85,7 +85,7 @@ func (r *Service) Run(ctx context.Context, ready chan struct{}) error {
8585

8686
r.Logger.Info("Restarting subscription",
8787
"attempt", attempt,
88-
"remaining", r.blockchainMaxRetries - attempt,
88+
"remaining", r.blockchainMaxRetries-attempt,
8989
"time_between_attempts", r.blockchainSubscriptionRetryInterval,
9090
)
9191

internal/evmreader/evmreader_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ func (m *MockRepository) SetupDefaultBehavior() *MockRepository {
576576
FirstBlock: 11,
577577
LastBlock: 20,
578578
Status: EpochStatus_Open,
579-
ClaimHash: nil,
579+
OutputsMerkleRoot: nil,
580580
ClaimTransactionHash: nil,
581581
}, nil).Twice()
582582
return m
@@ -642,7 +642,7 @@ func (m *MockRepository) GetOutput(ctx context.Context, nameOrAddress string, in
642642
return obj.(*Output), args.Error(1)
643643
}
644644

645-
func (m *MockRepository) UpdateEpoch(ctx context.Context, nameOrAddress string, e *Epoch) error {
645+
func (m *MockRepository) UpdateEpochOutputsMerkleProof(ctx context.Context, nameOrAddress string, e *Epoch) error {
646646
args := m.Called(ctx, nameOrAddress, e)
647647
return args.Error(0)
648648
}

internal/evmreader/sealedepochs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (r *Service) processSealedEpochEvent(
293293
}
294294

295295
prevEpoch.ClaimTransactionHash = &event.Raw.TxHash
296-
err = r.repository.UpdateEpoch(ctx, app.application.IApplicationAddress.Hex(), prevEpoch)
296+
err = r.repository.UpdateEpochOutputsMerkleProof(ctx, app.application.IApplicationAddress.Hex(), prevEpoch)
297297
if err != nil {
298298
return fmt.Errorf("failed to update previous epoch %d: %w", prevEpochNumber, err)
299299
}

internal/jsonrpc/jsonrpc-discover.json

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
],
123123
"result": {
124124
"name": "result",
125+
"description": "A paginated list of epochs, with the proofs omitted",
125126
"schema": {
126127
"$ref": "#/components/schemas/EpochListResult"
127128
}
@@ -1118,22 +1119,36 @@
11181119
"$ref": "#/components/schemas/Hash",
11191120
"nullable": true
11201121
},
1121-
"claim_hash": {
1122+
"outputs_merkle_root": {
11221123
"$ref": "#/components/schemas/Hash",
11231124
"nullable": true
11241125
},
1125-
"claim_transaction_hash": {
1126+
"outputs_merkle_proof": {
1127+
"type": "array",
1128+
"items": {
1129+
"$ref": "#/components/schemas/Hash"
1130+
},
1131+
"nullable": true
1132+
},
1133+
"commitment": {
11261134
"$ref": "#/components/schemas/Hash",
11271135
"nullable": true
11281136
},
1129-
"tournament_address": {
1130-
"$ref": "#/components/schemas/EthereumAddress",
1137+
"commitment_proof": {
1138+
"type": "array",
1139+
"items": {
1140+
"$ref": "#/components/schemas/Hash"
1141+
},
11311142
"nullable": true
11321143
},
1133-
"commitment": {
1144+
"claim_transaction_hash": {
11341145
"$ref": "#/components/schemas/Hash",
11351146
"nullable": true
11361147
},
1148+
"tournament_address": {
1149+
"$ref": "#/components/schemas/EthereumAddress",
1150+
"nullable": true
1151+
},
11371152
"status": {
11381153
"$ref": "#/components/schemas/EpochStatus"
11391154
},

internal/jsonrpc/jsonrpc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func TestMethod(t *testing.T) {
250250
err := s.repository.CreateEpoch(ctx, &model.Epoch{
251251
ApplicationID: appID,
252252
Index: nr,
253-
ClaimHash: &common.Hash{},
253+
OutputsMerkleRoot: &common.Hash{},
254254
ClaimTransactionHash: &common.Hash{},
255255
Status: model.EpochStatus_ClaimAccepted,
256256
})
@@ -285,7 +285,7 @@ func TestMethod(t *testing.T) {
285285
err := s.repository.CreateEpoch(ctx, &model.Epoch{
286286
ApplicationID: appID,
287287
Index: nr,
288-
ClaimHash: &common.Hash{},
288+
OutputsMerkleRoot: &common.Hash{},
289289
ClaimTransactionHash: &common.Hash{},
290290
Status: model.EpochStatus_ClaimAccepted,
291291
})
@@ -455,7 +455,7 @@ func TestMethod(t *testing.T) {
455455
err := s.repository.CreateEpoch(ctx, &model.Epoch{
456456
ApplicationID: appID,
457457
Index: epochIndex,
458-
ClaimHash: &common.Hash{},
458+
OutputsMerkleRoot: &common.Hash{},
459459
ClaimTransactionHash: &common.Hash{},
460460
Status: model.EpochStatus_ClaimAccepted,
461461
})

internal/merkle/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,12 @@ func (tree *Tree) ProveLast() *Proof {
208208
),
209209
one,
210210
)
211+
println("tree height:", tree.Height)
211212
return tree.ProveLeaf(index)
212213
}
213214

214215
func (tree *Tree) ProveLeafRec(index *big.Int) *Proof {
216+
println("ProveLeafRec index:", index.String())
215217
numLeafs := new(big.Int).Lsh(one, uint(tree.Height))
216218
if numLeafs.Cmp(index) <= 0 {
217219
panic(fmt.Sprintf("index out of bounds: %v, %v", numLeafs, index))

internal/model/models.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,11 @@ type Epoch struct {
574574
InputIndexLowerBound uint64 `json:"input_index_lower_bound"`
575575
InputIndexUpperBound uint64 `json:"input_index_upper_bound"`
576576
MachineHash *common.Hash `json:"machine_hash"`
577-
ClaimHash *common.Hash `json:"claim_hash"`
577+
OutputsMerkleRoot *common.Hash `json:"claim_hash"`
578+
OutputsMerkleProof []common.Hash `json:"outputs_merkle_proof,omitempty"`
578579
ClaimTransactionHash *common.Hash `json:"claim_transaction_hash"`
579580
Commitment *common.Hash `json:"commitment"`
581+
CommitmentProof []common.Hash `json:"commitment_proof,omitempty"`
580582
TournamentAddress *common.Address `json:"tournament_address"`
581583
Status EpochStatus `json:"status"`
582584
VirtualIndex uint64 `json:"virtual_index"`

0 commit comments

Comments
 (0)