Skip to content

Commit

Permalink
types: Rename EphemeralLeafIndex -> UnassignedLeafIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Jun 26, 2024
1 parent ce12c88 commit d5a61b8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 31 deletions.
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
20 changes: 4 additions & 16 deletions consensus/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,7 @@ func ApplyOrphan(s State, b types.Block, targetTimestamp time.Time) State {

func (ms *MidState) addSiacoinElement(id types.SiacoinOutputID, sco types.SiacoinOutput) {
sce := types.SiacoinElement{
StateElement: types.StateElement{
ID: types.Hash256(id),
LeafIndex: types.EphemeralLeafIndex,
},
StateElement: types.StateElement{ID: types.Hash256(id)},
SiacoinOutput: sco,
}
ms.sces = append(ms.sces, sce)
Expand All @@ -361,10 +358,7 @@ func (ms *MidState) spendSiacoinElement(sce types.SiacoinElement, txid types.Tra

func (ms *MidState) addSiafundElement(id types.SiafundOutputID, sfo types.SiafundOutput) {
sfe := types.SiafundElement{
StateElement: types.StateElement{
ID: types.Hash256(id),
LeafIndex: types.EphemeralLeafIndex,
},
StateElement: types.StateElement{ID: types.Hash256(id)},
SiafundOutput: sfo,
ClaimStart: ms.siafundPool,
}
Expand All @@ -382,10 +376,7 @@ func (ms *MidState) spendSiafundElement(sfe types.SiafundElement, txid types.Tra

func (ms *MidState) addFileContractElement(id types.FileContractID, fc types.FileContract) {
fce := types.FileContractElement{
StateElement: types.StateElement{
ID: types.Hash256(id),
LeafIndex: types.EphemeralLeafIndex,
},
StateElement: types.StateElement{ID: types.Hash256(id)},
FileContract: fc,
}
ms.fces = append(ms.fces, fce)
Expand Down Expand Up @@ -421,10 +412,7 @@ func (ms *MidState) resolveFileContractElement(fce types.FileContractElement, va

func (ms *MidState) addV2FileContractElement(id types.FileContractID, fc types.V2FileContract) {
fce := types.V2FileContractElement{
StateElement: types.StateElement{
ID: types.Hash256(id),
LeafIndex: types.EphemeralLeafIndex,
},
StateElement: types.StateElement{ID: types.Hash256(id)},
V2FileContract: fc,
}
ms.v2fces = append(ms.v2fces, fce)
Expand Down
4 changes: 2 additions & 2 deletions consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ func validateV2Siacoins(ms *MidState, txn types.V2Transaction) error {
spent[sci.Parent.ID] = i

// check accumulator
if sci.Parent.LeafIndex == types.EphemeralLeafIndex {
if sci.Parent.LeafIndex == types.UnassignedLeafIndex {
if !ms.isCreated(sci.Parent.ID) {
return fmt.Errorf("siacoin input %v spends nonexistent ephemeral output %v", i, sci.Parent.ID)
}
Expand Down Expand Up @@ -645,7 +645,7 @@ func validateV2Siafunds(ms *MidState, txn types.V2Transaction) error {
spent[sfi.Parent.ID] = i

// check accumulator
if sfi.Parent.LeafIndex == types.EphemeralLeafIndex {
if sfi.Parent.LeafIndex == types.UnassignedLeafIndex {
if !ms.isCreated(sfi.Parent.ID) {
return fmt.Errorf("siafund input %v spends nonexistent ephemeral output %v", i, sfi.Parent.ID)
}
Expand Down
2 changes: 1 addition & 1 deletion types/multiproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func splitLeaves(ls []elementLeaf, mid uint64) (left, right []elementLeaf) {

func forEachElementLeaf(txns []V2Transaction, fn func(l elementLeaf)) {
visit := func(l elementLeaf) {
if l.LeafIndex != EphemeralLeafIndex {
if l.LeafIndex != UnassignedLeafIndex {
fn(l)
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/multiproof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func multiproofTxns(numTxns int, numElems int) []types.V2Transaction {
for i := range txns {
for j := range txns[i].SiacoinInputs {
if (n+1)%5 == 0 {
txns[i].SiacoinInputs[j].Parent.LeafIndex = types.EphemeralLeafIndex
txns[i].SiacoinInputs[j].Parent.LeafIndex = types.UnassignedLeafIndex
txns[i].SiacoinInputs[j].Parent.MerkleProof = nil
}
n++
Expand Down
18 changes: 11 additions & 7 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ const (
// a FileContract.
HostContractIndex = 1

// EphemeralLeafIndex is used as the LeafIndex of StateElements that are created
// and spent within the same block. Such elements do not require a proof of
// existence. They are, however, assigned a proper index and are incorporated
// into the state accumulator when the block is processed.
EphemeralLeafIndex = math.MaxUint64
// UnassignedLeafIndex is a sentinel value used as the LeafIndex of
// StateElements that have not been added to the accumulator yet. This is
// necessary for constructing blocks sets where later transactions reference
// elements created in earlier transactions.
//
// Most clients do not need to reference this value directly, and should
// instead use the EphemeralSiacoinElement and EphemeralSiafundElement
// functions to construct dependent transaction sets.
UnassignedLeafIndex = 10101010101010101010
)

// Various specifiers.
Expand Down Expand Up @@ -687,7 +691,7 @@ func (txn *V2Transaction) EphemeralSiacoinOutput(i int) SiacoinElement {
return SiacoinElement{
StateElement: StateElement{
ID: Hash256(txn.SiacoinOutputID(txn.ID(), i)),
LeafIndex: EphemeralLeafIndex,
LeafIndex: UnassignedLeafIndex,
},
SiacoinOutput: txn.SiacoinOutputs[i],
}
Expand All @@ -699,7 +703,7 @@ func (txn *V2Transaction) EphemeralSiafundOutput(i int) SiafundElement {
return SiafundElement{
StateElement: StateElement{
ID: Hash256(txn.SiafundOutputID(txn.ID(), i)),
LeafIndex: EphemeralLeafIndex,
LeafIndex: UnassignedLeafIndex,
},
SiafundOutput: txn.SiafundOutputs[i],
}
Expand Down

0 comments on commit d5a61b8

Please sign in to comment.