Skip to content

Commit

Permalink
chain: Add fallback supplementBlock decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Dec 2, 2024
1 parent 6172c9c commit 321c298
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions chain/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ func (sb *supplementedBlock) DecodeFrom(d *types.Decoder) {
}
}

func (sb *supplementedBlock) decodeOldSupplement(d *types.Decoder) {
if v := d.ReadUint8(); v != 2 {
d.SetErr(fmt.Errorf("incompatible version (%d)", v))
}
(*types.V2Block)(&sb.Block).DecodeFrom(d)
if d.ReadBool() {
sb.Supplement = new(consensus.V1BlockSupplement)
types.DecodeSliceFn(d, &sb.Supplement.Transactions, func(d *types.Decoder) (ts consensus.V1TransactionSupplement) {
types.DecodeSlice(d, &ts.SiacoinInputs)
types.DecodeSlice(d, &ts.SiafundInputs)
types.DecodeSlice(d, &ts.RevisedFileContracts)
var fces []types.FileContractElement
types.DecodeSlice(d, &fces)
ts.StorageProofs = make([]consensus.V1StorageProofSupplement, len(fces))
for i, fce := range fces {
ts.StorageProofs[i] = consensus.V1StorageProofSupplement{FileContract: fce}
}
return
})
types.DecodeSlice(d, &sb.Supplement.ExpiringFileContracts)
}
}

// helper type for decoding just the header information from a block
type supplementedHeader struct {
ParentID types.BlockID
Expand Down Expand Up @@ -306,8 +329,22 @@ func (db *DBStore) putState(cs consensus.State) {

func (db *DBStore) getBlock(id types.BlockID) (b types.Block, bs *consensus.V1BlockSupplement, _ bool) {
var sb supplementedBlock
ok := db.bucket(bBlocks).get(id[:], &sb)
return sb.Block, sb.Supplement, ok

val := db.bucket(bBlocks).getRaw(id[:])
if val == nil {
return b, bs, false
}
// fallback to old encoding before panicking
d := types.NewBufDecoder(val)
sb.DecodeFrom(d)
if d.Err() != nil {
d = types.NewBufDecoder(val)
sb.decodeOldSupplement(d)
}
if d.Err() != nil {
check(fmt.Errorf("error decoding %T: %w", &sb, d.Err()))
}
return sb.Block, sb.Supplement, true
}

func (db *DBStore) putBlock(b types.Block, bs *consensus.V1BlockSupplement) {
Expand Down

0 comments on commit 321c298

Please sign in to comment.