Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus: Expose ephemerality of elements in Apply/RevertUpdate #174

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions consensus/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ type elementApplyUpdate struct {
}

func (eau *elementApplyUpdate) updateElementProof(e *types.StateElement) {
if e.LeafIndex == types.EphemeralLeafIndex {
if e.LeafIndex == types.UnassignedLeafIndex {
panic("cannot update an ephemeral element")
} else if e.LeafIndex >= eau.oldNumLeaves {
return // newly-added element
Expand All @@ -417,7 +417,7 @@ type elementRevertUpdate struct {
}

func (eru *elementRevertUpdate) updateElementProof(e *types.StateElement) {
if e.LeafIndex == types.EphemeralLeafIndex {
if e.LeafIndex == types.UnassignedLeafIndex {
panic("cannot update an ephemeral element")
} else if e.LeafIndex >= eru.numLeaves {
panic("cannot update an element that is not present in the accumulator")
Expand Down
4 changes: 2 additions & 2 deletions consensus/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func TestUpdateElementProof(t *testing.T) {
expectProofLen int
}{
{
name: "EphemeralLeafIndexPanic",
leafIndex: types.EphemeralLeafIndex,
name: "UnassignedLeafIndexPanic",
leafIndex: types.UnassignedLeafIndex,
numLeaves: 5,
expectPanic: true,
},
Expand Down
17 changes: 11 additions & 6 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (s State) AttestationSigHash(a types.Attestation) types.Hash256 {
// A MidState represents the state of the chain within a block.
type MidState struct {
base State
ephemeral map[types.Hash256]int // indices into element slices
created map[types.Hash256]int // indices into element slices
spends map[types.Hash256]types.TransactionID
revs map[types.Hash256]*types.FileContractElement
res map[types.Hash256]bool
Expand All @@ -596,7 +596,7 @@ type MidState struct {
foundationPrimary types.Address
foundationFailsafe types.Address

// elements updated/added by block
// elements created/updated by block
sces []types.SiacoinElement
sfes []types.SiafundElement
fces []types.FileContractElement
Expand All @@ -606,21 +606,21 @@ type MidState struct {
}

func (ms *MidState) siacoinElement(ts V1TransactionSupplement, id types.SiacoinOutputID) (types.SiacoinElement, bool) {
if i, ok := ms.ephemeral[types.Hash256(id)]; ok {
if i, ok := ms.created[types.Hash256(id)]; ok {
return ms.sces[i], true
}
return ts.siacoinElement(id)
}

func (ms *MidState) siafundElement(ts V1TransactionSupplement, id types.SiafundOutputID) (types.SiafundElement, bool) {
if i, ok := ms.ephemeral[types.Hash256(id)]; ok {
if i, ok := ms.created[types.Hash256(id)]; ok {
return ms.sfes[i], true
}
return ts.siafundElement(id)
}

func (ms *MidState) fileContractElement(ts V1TransactionSupplement, id types.FileContractID) (types.FileContractElement, bool) {
if i, ok := ms.ephemeral[types.Hash256(id)]; ok {
if i, ok := ms.created[types.Hash256(id)]; ok {
return ms.fces[i], true
}
return ts.fileContractElement(id)
Expand Down Expand Up @@ -660,11 +660,16 @@ func (ms *MidState) isSpent(id types.Hash256) bool {
return ok
}

func (ms *MidState) isCreated(id types.Hash256) bool {
_, ok := ms.created[id]
return ok || id == ms.cie.ID
}

// NewMidState constructs a MidState initialized to the provided base state.
func NewMidState(s State) *MidState {
return &MidState{
base: s,
ephemeral: make(map[types.Hash256]int),
created: make(map[types.Hash256]int),
spends: make(map[types.Hash256]types.TransactionID),
revs: make(map[types.Hash256]*types.FileContractElement),
res: make(map[types.Hash256]bool),
Expand Down
Loading