Skip to content

Commit

Permalink
all: update state element
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 30, 2024
1 parent b338b73 commit 07634d5
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 124 deletions.
4 changes: 2 additions & 2 deletions chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type memState struct {
index types.ChainIndex
utxos map[types.Hash256]types.SiacoinElement
utxos map[types.SiacoinOutputID]types.SiacoinElement
chainIndexElements []types.ChainIndexElement
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func (ms *memState) SpendableElement(t *testing.T) (se types.SiacoinElement) {

func newMemState() *memState {
return &memState{
utxos: make(map[types.Hash256]types.SiacoinElement),
utxos: make(map[types.SiacoinOutputID]types.SiacoinElement),
}
}

Expand Down
12 changes: 6 additions & 6 deletions chain/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ func (db *DBStore) getElementProof(leafIndex, numLeaves uint64) (proof []types.H
func (db *DBStore) getSiacoinElement(id types.SiacoinOutputID, numLeaves uint64) (sce types.SiacoinElement, ok bool) {
ok = db.bucket(bSiacoinElements).get(id[:], &sce)
if ok {
sce.MerkleProof = db.getElementProof(sce.LeafIndex, numLeaves)
sce.StateElement.MerkleProof = db.getElementProof(sce.StateElement.LeafIndex, numLeaves)
}
return
}

func (db *DBStore) putSiacoinElement(sce types.SiacoinElement) {
sce.MerkleProof = nil
sce.StateElement.MerkleProof = nil
db.bucket(bSiacoinElements).put(sce.ID[:], sce)
}

Expand All @@ -369,13 +369,13 @@ func (db *DBStore) deleteSiacoinElement(id types.SiacoinOutputID) {
func (db *DBStore) getSiafundElement(id types.SiafundOutputID, numLeaves uint64) (sfe types.SiafundElement, ok bool) {
ok = db.bucket(bSiafundElements).get(id[:], &sfe)
if ok {
sfe.MerkleProof = db.getElementProof(sfe.LeafIndex, numLeaves)
sfe.StateElement.MerkleProof = db.getElementProof(sfe.StateElement.LeafIndex, numLeaves)
}
return
}

func (db *DBStore) putSiafundElement(sfe types.SiafundElement) {
sfe.MerkleProof = nil
sfe.StateElement.MerkleProof = nil
db.bucket(bSiafundElements).put(sfe.ID[:], sfe)
}

Expand All @@ -386,13 +386,13 @@ func (db *DBStore) deleteSiafundElement(id types.SiafundOutputID) {
func (db *DBStore) getFileContractElement(id types.FileContractID, numLeaves uint64) (fce types.FileContractElement, ok bool) {
ok = db.bucket(bFileContractElements).get(id[:], &fce)
if ok {
fce.MerkleProof = db.getElementProof(fce.LeafIndex, numLeaves)
fce.StateElement.MerkleProof = db.getElementProof(fce.StateElement.LeafIndex, numLeaves)
}
return
}

func (db *DBStore) putFileContractElement(fce types.FileContractElement) {
fce.MerkleProof = nil
fce.StateElement.MerkleProof = nil
db.bucket(bFileContractElements).put(fce.ID[:], fce)
}

Expand Down
66 changes: 33 additions & 33 deletions chain/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,48 +667,48 @@ func (m *Manager) revertPoolUpdate(cru consensus.RevertUpdate, cs consensus.Stat
// applying a block can make ephemeral elements in the txpool non-ephemeral;
// here, we undo that
var uncreated map[types.Hash256]bool
replaceEphemeral := func(e *types.StateElement) {
replaceEphemeral := func(id types.Hash256, e *types.StateElement) {
if e.LeafIndex == types.UnassignedLeafIndex {
return
} else if uncreated == nil {
uncreated = make(map[types.Hash256]bool)
cru.ForEachSiacoinElement(func(sce types.SiacoinElement, created, spent bool) {
if created {
uncreated[sce.ID] = true
uncreated[types.Hash256(sce.ID)] = true
}
})
cru.ForEachSiafundElement(func(sfe types.SiafundElement, created, spent bool) {
if created {
uncreated[sfe.ID] = true
uncreated[types.Hash256(sfe.ID)] = true
}
})
cru.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool) {
if created {
uncreated[fce.ID] = true
uncreated[types.Hash256(fce.ID)] = true
}
})
cru.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) {
if created {
uncreated[fce.ID] = true
uncreated[types.Hash256(fce.ID)] = true
}
})
}
if uncreated[e.ID] {
*e = types.StateElement{ID: e.ID, LeafIndex: types.UnassignedLeafIndex}
if uncreated[id] {
*e = types.StateElement{LeafIndex: types.UnassignedLeafIndex}
}
}
for _, txn := range m.txpool.v2txns {
for i := range txn.SiacoinInputs {
replaceEphemeral(&txn.SiacoinInputs[i].Parent.StateElement)
for i, si := range txn.SiacoinInputs {
replaceEphemeral(types.Hash256(si.Parent.ID), &txn.SiacoinInputs[i].Parent.StateElement)
}
for i := range txn.SiafundInputs {
replaceEphemeral(&txn.SiafundInputs[i].Parent.StateElement)
for i, si := range txn.SiafundInputs {
replaceEphemeral(types.Hash256(si.Parent.ID), &txn.SiafundInputs[i].Parent.StateElement)
}
for i := range txn.FileContractRevisions {
replaceEphemeral(&txn.FileContractRevisions[i].Parent.StateElement)
for i, fcr := range txn.FileContractRevisions {
replaceEphemeral(types.Hash256(fcr.Parent.ID), &txn.FileContractRevisions[i].Parent.StateElement)
}
for i := range txn.FileContractResolutions {
replaceEphemeral(&txn.FileContractResolutions[i].Parent.StateElement)
for i, fcr := range txn.FileContractResolutions {
replaceEphemeral(types.Hash256(fcr.Parent.ID), &txn.FileContractResolutions[i].Parent.StateElement)
}
}

Expand All @@ -724,48 +724,48 @@ func (m *Manager) revertPoolUpdate(cru consensus.RevertUpdate, cs consensus.Stat
func (m *Manager) applyPoolUpdate(cau consensus.ApplyUpdate, cs consensus.State) {
// applying a block can make ephemeral elements in the txpool non-ephemeral
var newElements map[types.Hash256]types.StateElement
replaceEphemeral := func(e *types.StateElement) {
replaceEphemeral := func(id types.Hash256, e *types.StateElement) {
if e.LeafIndex != types.UnassignedLeafIndex {
return
} else if newElements == nil {
newElements = make(map[types.Hash256]types.StateElement)
cau.ForEachSiacoinElement(func(sce types.SiacoinElement, created, spent bool) {
if created {
newElements[sce.ID] = sce.StateElement
newElements[types.Hash256(sce.ID)] = sce.StateElement
}
})
cau.ForEachSiafundElement(func(sfe types.SiafundElement, created, spent bool) {
if created {
newElements[sfe.ID] = sfe.StateElement
newElements[types.Hash256(sfe.ID)] = sfe.StateElement
}
})
cau.ForEachFileContractElement(func(fce types.FileContractElement, created bool, rev *types.FileContractElement, resolved, valid bool) {
if created {
newElements[fce.ID] = fce.StateElement
newElements[types.Hash256(fce.ID)] = fce.StateElement
}
})
cau.ForEachV2FileContractElement(func(fce types.V2FileContractElement, created bool, rev *types.V2FileContractElement, res types.V2FileContractResolutionType) {
if created {
newElements[fce.ID] = fce.StateElement
newElements[types.Hash256(fce.ID)] = fce.StateElement
}
})
}
if se, ok := newElements[e.ID]; ok {
if se, ok := newElements[id]; ok {
*e = se
}
}
for _, txn := range m.txpool.v2txns {
for i := range txn.SiacoinInputs {
replaceEphemeral(&txn.SiacoinInputs[i].Parent.StateElement)
for i, si := range txn.SiacoinInputs {
replaceEphemeral(types.Hash256(si.Parent.ID), &txn.SiacoinInputs[i].Parent.StateElement)
}
for i := range txn.SiafundInputs {
replaceEphemeral(&txn.SiafundInputs[i].Parent.StateElement)
for i, si := range txn.SiafundInputs {
replaceEphemeral(types.Hash256(si.Parent.ID), &txn.SiafundInputs[i].Parent.StateElement)
}
for i := range txn.FileContractRevisions {
replaceEphemeral(&txn.FileContractRevisions[i].Parent.StateElement)
for i, fcr := range txn.FileContractRevisions {
replaceEphemeral(types.Hash256(fcr.Parent.ID), &txn.FileContractRevisions[i].Parent.StateElement)
}
for i := range txn.FileContractResolutions {
replaceEphemeral(&txn.FileContractResolutions[i].Parent.StateElement)
for i, fcr := range txn.FileContractResolutions {
replaceEphemeral(types.Hash256(fcr.Parent.ID), &txn.FileContractResolutions[i].Parent.StateElement)
}
}

Expand Down Expand Up @@ -1082,12 +1082,12 @@ func (m *Manager) updateV2TransactionProofs(txns []types.V2Transaction, from, to
confirmedStateElements := make(map[types.Hash256]types.StateElement)
cau.ForEachSiacoinElement(func(sce types.SiacoinElement, created, spent bool) {
if created {
confirmedStateElements[sce.ID] = sce.StateElement
confirmedStateElements[types.Hash256(sce.ID)] = sce.StateElement
}
})
cau.ForEachSiafundElement(func(sfe types.SiafundElement, created, spent bool) {
if created {
confirmedStateElements[sfe.ID] = sfe.StateElement
confirmedStateElements[types.Hash256(sfe.ID)] = sfe.StateElement
}
})

Expand All @@ -1100,7 +1100,7 @@ func (m *Manager) updateV2TransactionProofs(txns []types.V2Transaction, from, to

// update the state elements for any confirmed ephemeral elements
for j := range txns[i].SiacoinInputs {
if txns[i].SiacoinInputs[j].Parent.LeafIndex != types.UnassignedLeafIndex {
if txns[i].SiacoinInputs[j].Parent.StateElement.LeafIndex != types.UnassignedLeafIndex {
continue
}
se, ok := confirmedStateElements[types.Hash256(txns[i].SiacoinInputs[j].Parent.ID)]
Expand All @@ -1112,7 +1112,7 @@ func (m *Manager) updateV2TransactionProofs(txns []types.V2Transaction, from, to

// update the state elements for any confirmed ephemeral elements
for j := range txns[i].SiafundInputs {
if txns[i].SiafundInputs[j].Parent.LeafIndex != types.UnassignedLeafIndex {
if txns[i].SiafundInputs[j].Parent.StateElement.LeafIndex != types.UnassignedLeafIndex {
continue
}
se, ok := confirmedStateElements[types.Hash256(txns[i].SiafundInputs[j].Parent.ID)]
Expand Down
2 changes: 1 addition & 1 deletion miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestV2MineBlocks(t *testing.T) {
// mine until just before the allow height
mineBlocks(t, 4)

elements := make(map[types.Hash256]types.SiacoinElement)
elements := make(map[types.SiacoinOutputID]types.SiacoinElement)
_, applied, err := cm.UpdatesSince(types.ChainIndex{}, 500)
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 6 additions & 12 deletions rhp/v4/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,9 @@ func RPCRenewContract(ctx context.Context, t TransportClient, tp TxPool, signer
FileContractResolutions: []types.V2FileContractResolution{
{
Parent: types.V2FileContractElement{
StateElement: types.StateElement{
// the other parts of the state element are not required
// for signing the transaction. Let the host fill them
// in.
ID: types.Hash256(params.ContractID),
},
ID: params.ContractID,
// the state element field is not required for signing the
// transaction. The host will fill it in.
},
Resolution: &renewal,
},
Expand Down Expand Up @@ -787,12 +784,9 @@ func RPCRefreshContract(ctx context.Context, t TransportClient, tp TxPool, signe
FileContractResolutions: []types.V2FileContractResolution{
{
Parent: types.V2FileContractElement{
StateElement: types.StateElement{
// the other parts of the state element are not required
// for signing the transaction. Let the host fill them
// in.
ID: types.Hash256(params.ContractID),
},
ID: params.ContractID,
// the state element field is not required for signing the
// transaction. The host will fill it in.
},
Resolution: &renewal,
},
Expand Down
17 changes: 8 additions & 9 deletions testutil/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ func (et *ephemeralWalletUpdateTxn) WalletStateElements() (elements []types.Stat
return
}

func (et *ephemeralWalletUpdateTxn) UpdateWalletStateElements(elements []types.StateElement) error {
for _, se := range elements {
utxo, ok := et.store.utxos[types.SiacoinOutputID(se.ID)]
if !ok {
panic(fmt.Sprintf("siacoin element %q does not exist", se.ID))
}
utxo.StateElement = se
et.store.utxos[types.SiacoinOutputID(se.ID)] = utxo
// UpdateWalletSiacoinElementProofs updates the proofs of all state elements
// affected by the update. ProofUpdater.UpdateElementProof must be called
// for each state element in the database.
func (et *ephemeralWalletUpdateTxn) UpdateWalletSiacoinElementProofs(pu wallet.ProofUpdater) error {
for _, se := range et.store.utxos {
pu.UpdateElementProof(&se.StateElement)
et.store.utxos[se.ID] = se
}
return nil
}
Expand Down Expand Up @@ -131,7 +130,7 @@ func (es *EphemeralWalletStore) UnspentSiacoinElements() (utxos []types.SiacoinE
defer es.mu.Unlock()

for _, se := range es.utxos {
se.MerkleProof = append([]types.Hash256(nil), se.MerkleProof...)
se.StateElement.MerkleProof = append([]types.Hash256(nil), se.StateElement.MerkleProof...)
utxos = append(utxos, se)
}
return utxos, nil
Expand Down
Loading

0 comments on commit 07634d5

Please sign in to comment.