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

Add tests for v2 changes #124

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 37 additions & 7 deletions consensus/update.go
Expand Up @@ -349,6 +349,24 @@ func (ms *MidState) reviseFileContractElement(fce types.FileContractElement, rev
}
}

func (ms *MidState) reviseV2FileContractElement(fce types.V2FileContractElement, rev types.V2FileContract) {
if i, ok := ms.ephemeral[fce.ID]; ok {
ms.v2fces[i].V2FileContract = rev
} else {
if r, ok := ms.v2revs[fce.ID]; ok {
r.V2FileContract = rev
} else {
// store the original
fce.MerkleProof = append([]types.Hash256(nil), fce.MerkleProof...)
ms.v2fces = append(ms.v2fces, fce)
// store the revision
fce.MerkleProof = append([]types.Hash256(nil), fce.MerkleProof...)
fce.V2FileContract = rev
ms.v2revs[fce.ID] = &fce
}
}
}

func (ms *MidState) resolveFileContractElement(fce types.FileContractElement, txid types.TransactionID) {
ms.spends[fce.ID] = txid
fce.MerkleProof = append([]types.Hash256(nil), fce.MerkleProof...)
Expand All @@ -361,10 +379,10 @@ func (ms *MidState) addV2FileContractElement(fce types.V2FileContractElement) {
ms.siafundPool = ms.siafundPool.Add(ms.base.V2FileContractTax(fce.V2FileContract))
}

func (ms *MidState) reviseV2FileContractElement(fce types.V2FileContractElement, rev types.V2FileContract) {
fce.MerkleProof = append([]types.Hash256(nil), fce.MerkleProof...)
ms.v2fces = append(ms.v2fces, fce)
}
// func (ms *MidState) reviseV2FileContractElement(fce types.V2FileContractElement, rev types.V2FileContract) {
// fce.MerkleProof = append([]types.Hash256(nil), fce.MerkleProof...)
// ms.v2fces = append(ms.v2fces, fce)
// }

func (ms *MidState) resolveV2FileContractElement(fce types.V2FileContractElement, txid types.TransactionID) {
ms.spends[fce.ID] = txid
Expand Down Expand Up @@ -590,7 +608,11 @@ func (ms *MidState) forEachElementLeaf(fn func(elementLeaf)) {
}
}
for i := range ms.v2fces {
fn(v2FileContractLeaf(&ms.v2fces[i], ms.isSpent(ms.v2fces[i].ID)))
if r, ok := ms.v2revs[ms.v2fces[i].ID]; ok {
fn(v2FileContractLeaf(r, ms.isSpent(ms.v2fces[i].ID)))
} else {
fn(v2FileContractLeaf(&ms.v2fces[i], ms.isSpent(ms.v2fces[i].ID)))
}
}
for i := range ms.aes {
fn(attestationLeaf(&ms.aes[i]))
Expand Down Expand Up @@ -622,7 +644,15 @@ func (au ApplyUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, sp
// au. If the contract was revised, rev is non-nil.
func (au ApplyUpdate) ForEachFileContractElement(fn func(fce types.FileContractElement, rev *types.FileContractElement, resolved bool)) {
for _, fce := range au.ms.fces {
fn(fce, au.ms.revision(fce.ID), au.ms.isSpent(fce.ID))
fn(fce, au.ms.revs[fce.ID], au.ms.isSpent(fce.ID))
}
}

// ForEachV2FileContractElement calls fn on each V2 file contract element
// related to au. If the contract was revised, rev is non-nil.
func (au ApplyUpdate) ForEachV2FileContractElement(fn func(fce types.V2FileContractElement, rev *types.V2FileContractElement, resolved bool)) {
for _, fce := range au.ms.v2fces {
fn(fce, au.ms.v2revs[fce.ID], au.ms.isSpent(fce.ID))
}
}

Expand Down Expand Up @@ -687,7 +717,7 @@ func (ru RevertUpdate) ForEachSiafundElement(fn func(sfe types.SiafundElement, s
func (ru RevertUpdate) ForEachFileContractElement(fn func(fce types.FileContractElement, rev *types.FileContractElement, resolved bool)) {
for i := range ru.ms.fces {
fce := ru.ms.fces[len(ru.ms.fces)-i-1]
fn(fce, ru.ms.revision(fce.ID), ru.ms.isSpent(fce.ID))
fn(fce, ru.ms.revs[fce.ID], ru.ms.isSpent(fce.ID))
}
}

Expand Down
10 changes: 4 additions & 6 deletions consensus/validation.go
Expand Up @@ -46,7 +46,7 @@ func validateMinerPayouts(s State, b types.Block) error {
}
}
if len(b.MinerPayouts) != 1 {
return errors.New("block has multiple miner payouts")
return errors.New("block must have exactly one miner payout")
}
}

Expand Down Expand Up @@ -101,6 +101,7 @@ type MidState struct {
ephemeral map[types.Hash256]int // indices into element slices
spends map[types.Hash256]types.TransactionID
revs map[types.Hash256]*types.FileContractElement
v2revs map[types.Hash256]*types.V2FileContractElement
siafundPool types.Currency
foundationPrimary types.Address
foundationFailsafe types.Address
Expand Down Expand Up @@ -164,10 +165,6 @@ func (ms *MidState) mustFileContractElement(ts V1TransactionSupplement, id types
return fce
}

func (ms *MidState) revision(id types.Hash256) *types.FileContractElement {
return ms.revs[id]
}

func (ms *MidState) spent(id types.Hash256) (types.TransactionID, bool) {
txid, ok := ms.spends[id]
return txid, ok
Expand All @@ -185,6 +182,7 @@ func NewMidState(s State) *MidState {
ephemeral: make(map[types.Hash256]int),
spends: make(map[types.Hash256]types.TransactionID),
revs: make(map[types.Hash256]*types.FileContractElement),
v2revs: make(map[types.Hash256]*types.V2FileContractElement),
siafundPool: s.SiafundPool,
foundationPrimary: s.FoundationPrimaryAddress,
foundationFailsafe: s.FoundationFailsafeAddress,
Expand Down Expand Up @@ -657,7 +655,7 @@ func validateSpendPolicy(s State, p types.SpendPolicy, sigHash types.Hash256, si
verify = func(p types.SpendPolicy) error {
switch p := p.Type.(type) {
case types.PolicyTypeAbove:
if s.Index.Height > uint64(p) {
if s.Index.Height >= uint64(p) {
return nil
}
return fmt.Errorf("height not above %v", uint64(p))
Expand Down