Skip to content

Commit f61111e

Browse files
gtklockerkaleofdutystchrysa
committed
Improvements:
- OCR3.1 polishing Based on e4315a2e7237ce8906c8922179d805a579f880b1. Co-authored-by: kaleofduty <[email protected]> Co-authored-by: stchrysa <[email protected]>
1 parent 7ab8191 commit f61111e

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

offchainreporting2plus/internal/ocr3_1/protocol/outcome_generation.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,35 +553,62 @@ func (outgen *outcomeGenerationState[RI]) tryToMoveCertAndKVStateToCommitQC(comm
553553
return
554554
}
555555

556-
stb, err := tx.ReadUnattestedStateTransitionBlock(commitQC.CommitSeqNr, commitQC.StateTransitionInputsDigest)
556+
ustb, err := tx.ReadUnattestedStateTransitionBlock(commitQC.CommitSeqNr, commitQC.StateTransitionInputsDigest)
557557
if err != nil {
558558
outgen.logger.Error("error during ReadUnattestedStateTransitionBlock", commontypes.LogFields{
559559
"commitQCSeqNr": commitQC.CommitSeqNr,
560560
"error": err,
561561
})
562562
return
563563
}
564-
if stb == nil {
564+
if ustb == nil {
565565
outgen.logger.Debug("unattested state transition block not found, can't move kv state", commontypes.LogFields{
566566
"commitQCSeqNr": commitQC.CommitSeqNr,
567567
})
568568
return
569569
}
570-
if err := outgen.isCompatibleUnattestedStateTransitionBlockSanityCheck(commitQC, *stb); err != nil {
570+
if err := outgen.isCompatibleUnattestedStateTransitionBlockSanityCheck(commitQC, ustb); err != nil {
571571
outgen.logger.Critical("sanity check of unattested state transition block failed, very surprising!", commontypes.LogFields{
572-
"commitQCSeqNr": commitQC.CommitSeqNr,
573-
"error": err,
572+
"commitQCSeqNr": commitQC.CommitSeqNr,
573+
"commitQC": commitQC,
574+
"unattestedStateTransitionBlock": ustb,
575+
"error": err,
574576
})
575577
return
576578
}
577579

578-
astb := AttestedStateTransitionBlock{
579-
*stb,
580+
commitQCStateTransitionBlock := StateTransitionBlock{
581+
commitQC.PrevHistoryDigest,
582+
commitQC.CommitEpoch,
583+
commitQC.CommitSeqNr,
584+
commitQC.StateTransitionInputsDigest,
585+
ustb.StateWriteSet,
586+
commitQC.StateRootDigest,
587+
commitQC.ReportsPlusPrecursorDigest,
588+
}
589+
590+
commitQCAttestedStateTransitionBlock := AttestedStateTransitionBlock{
591+
commitQCStateTransitionBlock,
580592
commitQC.CommitQuorumCertificate,
581593
}
582594

583-
// write astb
584-
err = tx.WriteAttestedStateTransitionBlock(commitQC.CommitSeqNr, astb)
595+
if err := commitQCAttestedStateTransitionBlock.Verify(
596+
outgen.config.ConfigDigest,
597+
outgen.config.OracleIdentities,
598+
outgen.config.ByzQuorumSize(),
599+
); err != nil {
600+
outgen.logger.Critical("commitQCAttestedStateTransitionBlock is invalid, very surprising!", commontypes.LogFields{
601+
"commitQCSeqNr": commitQC.CommitSeqNr,
602+
"commitQC": commitQC,
603+
"commitQCAttestedStateTransitionBlock": commitQCAttestedStateTransitionBlock,
604+
"unattestedStateTransitionBlock": ustb,
605+
"error": err,
606+
})
607+
return
608+
}
609+
610+
// write commitQCAttestedStateTransitionBlock
611+
err = tx.WriteAttestedStateTransitionBlock(commitQC.CommitSeqNr, commitQCAttestedStateTransitionBlock)
585612
if err != nil {
586613
outgen.logger.Error("error writing attested state transition block", commontypes.LogFields{
587614
"commitQCSeqNr": commitQC.CommitSeqNr,
@@ -591,7 +618,7 @@ func (outgen *outcomeGenerationState[RI]) tryToMoveCertAndKVStateToCommitQC(comm
591618
}
592619

593620
// apply write set
594-
stateRootDigest, err := tx.ApplyWriteSet(stb.StateWriteSet.Entries)
621+
stateRootDigest, err := tx.ApplyWriteSet(commitQCStateTransitionBlock.StateWriteSet.Entries)
595622
if err != nil {
596623
outgen.logger.Error("error applying write set", commontypes.LogFields{
597624
"commitQCSeqNr": commitQC.CommitSeqNr,
@@ -600,10 +627,10 @@ func (outgen *outcomeGenerationState[RI]) tryToMoveCertAndKVStateToCommitQC(comm
600627
return
601628
}
602629

603-
if stateRootDigest != stb.StateRootDigest {
630+
if stateRootDigest != commitQCStateTransitionBlock.StateRootDigest {
604631
outgen.logger.Error("state root digest mismatch from write set application", commontypes.LogFields{
605632
"commitQCSeqNr": commitQC.CommitSeqNr,
606-
"expected": stb.StateRootDigest,
633+
"expected": commitQCStateTransitionBlock.StateRootDigest,
607634
"actual": stateRootDigest,
608635
})
609636
return
@@ -646,13 +673,16 @@ func (outgen *outcomeGenerationState[RI]) persistUnattestedStateTransitionBlockA
646673
return nil
647674
}
648675

649-
func (outgen *outcomeGenerationState[RI]) isCompatibleUnattestedStateTransitionBlockSanityCheck(commitQC *CertifiedCommit, stb StateTransitionBlock) error {
676+
func (outgen *outcomeGenerationState[RI]) isCompatibleUnattestedStateTransitionBlockSanityCheck(commitQC *CertifiedCommit, stb *StateTransitionBlock) error {
650677
stbStateWriteSetDigest := MakeStateWriteSetDigest(
651678
outgen.config.ConfigDigest,
652679
stb.BlockSeqNr,
653680
stb.StateWriteSet.Entries,
654681
)
655682

683+
if stb.PrevHistoryDigest != commitQC.PrevHistoryDigest {
684+
return fmt.Errorf("local state transition block prev history digest does not match commitQC: expected %s but got %s", commitQC.PrevHistoryDigest, stb.PrevHistoryDigest)
685+
}
656686
if stbStateWriteSetDigest != commitQC.StateWriteSetDigest {
657687
return fmt.Errorf("local state transition block write set digest does not match commitQC: expected %s but got %s", commitQC.StateWriteSetDigest, stbStateWriteSetDigest)
658688
}

offchainreporting2plus/internal/ocr3_1/protocol/state_sync_block.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ func (stasy *stateSyncState[RI]) getPendingBlocksToRequest() []seqNrRange {
8282
// [lastSeqNr+1..seqNr) (exclusive) is a gap to fill
8383

8484
for rangeStartSeqNr := lastSeqNr + 1; rangeStartSeqNr < seqNr; rangeStartSeqNr += cfgMaxBlocksPerBlockSyncResponse {
85-
rangeEndExclSeqNr := rangeStartSeqNr + cfgMaxBlocksPerBlockSyncResponse
86-
if rangeEndExclSeqNr > seqNr {
87-
rangeEndExclSeqNr = seqNr
88-
}
85+
rangeEndExclSeqNr := min(rangeStartSeqNr+cfgMaxBlocksPerBlockSyncResponse, seqNr)
8986
pending = append(pending, seqNrRange{rangeStartSeqNr, rangeEndExclSeqNr})
9087
}
9188
}
@@ -94,11 +91,11 @@ func (stasy *stateSyncState[RI]) getPendingBlocksToRequest() []seqNrRange {
9491
})
9592

9693
for rangeStartSeqNr := lastSeqNr + 1; rangeStartSeqNr <= stasy.highestPersistedStateTransitionBlockSeqNr+cfgBlockSyncLookahead && rangeStartSeqNr <= stasy.highestHeardSeqNr; rangeStartSeqNr += cfgMaxBlocksPerBlockSyncResponse {
97-
rangeEndExclSeqNr := rangeStartSeqNr + cfgMaxBlocksPerBlockSyncResponse
98-
if rangeEndExclSeqNr > stasy.highestPersistedStateTransitionBlockSeqNr+cfgBlockSyncLookahead {
99-
rangeEndExclSeqNr = stasy.highestPersistedStateTransitionBlockSeqNr + cfgBlockSyncLookahead
100-
}
101-
// no check for rangeEndExclSeqNr > stasy.highestHeardSeqNr, because there is no harm in asking for more than exists
94+
maxRangeEndExclSeqNr := stasy.highestPersistedStateTransitionBlockSeqNr + cfgBlockSyncLookahead + 1
95+
rangeEndExclSeqNr := min(rangeStartSeqNr+cfgMaxBlocksPerBlockSyncResponse, maxRangeEndExclSeqNr)
96+
// If this is the last range that is pending, we're fine with asking for
97+
// blocks beyond highestHeardSeqNr, as long as we're not asking for more
98+
// than cfgMaxBlocksPerBlockSyncResponse blocks.
10299
pending = append(pending, seqNrRange{rangeStartSeqNr, rangeEndExclSeqNr})
103100
}
104101

offchainreporting2plus/internal/shim/ocr3_1_key_value_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ func initializeSchema(keyValueDatabase ocr3_1types.KeyValueDatabase) error {
11471147
it := rawTx.Range(nil, nil)
11481148
defer it.Close()
11491149
for it.Next() {
1150-
return fmt.Errorf("database is not empty")
1150+
return fmt.Errorf("database is not empty, this database was likely created with an unsupported alpha release of libocr")
11511151
}
11521152
if err := it.Err(); err != nil {
11531153
return fmt.Errorf("failed to ensure database is not empty, iteration error: %w", err)

0 commit comments

Comments
 (0)