Skip to content

Commit

Permalink
Add CheckExpirationCondition to UnlockConditionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Oct 8, 2023
1 parent 4e43c6d commit 6a8dbb3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
1 change: 0 additions & 1 deletion nodeclient/blockissuer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var (
)

type (

// BlockIssuerClient is a client which queries the optional blockissuer functionality of a node.
BlockIssuerClient interface {
// Info returns the info of the block issuer.
Expand Down
21 changes: 21 additions & 0 deletions unlock_cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,27 @@ func (f UnlockConditionSet) HasExpirationCondition() bool {
return f.Expiration() != nil
}

// CheckExpirationCondition returns the expiration return ident in case an expiration condition was set and
// the future bound slot is greater than the expiration slot.
// In case the past bound slot is smaller than the expiration slot, "nil" is returned to indicate that the original owner can unlock the output.
// The range in between is not unlockable by anyone and an "ErrExpirationConditionUnlockFailed" error will be returned.
func (f UnlockConditionSet) CheckExpirationCondition(commitmentInputSlot SlotIndex, protocolParameters ProtocolParameters) (Address, error) {
if f.HasExpirationCondition() {
futureBoundedSlotIndex := commitmentInputSlot + protocolParameters.MinCommittableAge()
if ok, returnIdent := f.ReturnIdentCanUnlock(futureBoundedSlotIndex); ok {
return returnIdent, nil
}

pastBoundedSlotIndex := commitmentInputSlot + protocolParameters.MaxCommittableAge()
if !f.OwnerIdentCanUnlock(pastBoundedSlotIndex) {
return nil, ErrExpirationConditionUnlockFailed
}
}

//nolint:nilnil // nil, nil is ok in this context, even if it is not go idiomatic
return nil, nil
}

// HasTimelockCondition tells whether this set has a TimelockUnlockCondition.
func (f UnlockConditionSet) HasTimelockCondition() bool {
return f.Timelock() != nil
Expand Down
24 changes: 1 addition & 23 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,28 +443,6 @@ func identToUnlock(transaction *iotago.Transaction, input iotago.Output, inputIn
}
}

func checkExpiration(output iotago.Output, commitmentInput VMCommitmentInput, protocolParameters iotago.ProtocolParameters) (iotago.Address, error) {
if output.UnlockConditionSet().HasExpirationCondition() {
if commitmentInput == nil {
return nil, iotago.ErrExpirationConditionCommitmentInputRequired
}

futureBoundedSlotIndex := commitmentInput.Slot + protocolParameters.MinCommittableAge()
if ok, returnIdent := output.UnlockConditionSet().ReturnIdentCanUnlock(futureBoundedSlotIndex); ok {
return returnIdent, nil
}

pastBoundedSlotIndex := commitmentInput.Slot + protocolParameters.MaxCommittableAge()
if output.UnlockConditionSet().OwnerIdentCanUnlock(pastBoundedSlotIndex) {
return nil, nil
}

return nil, iotago.ErrExpirationConditionUnlockFailed
}

return nil, nil
}

func unlockIdent(ownerIdent iotago.Address, unlock iotago.Unlock, inputIndex uint16, unlockedIdentities UnlockedIdentities, essenceMsgToSign []byte, checkUnlockOnly bool) error {
switch owner := ownerIdent.(type) {
case iotago.ChainAddress:
Expand Down Expand Up @@ -552,7 +530,7 @@ func unlockOutput(transaction *iotago.Transaction, commitmentInput VMCommitmentI
return ierrors.Errorf("unable to retrieve ident to unlock of input %d: %w", inputIndex, err)
}

if actualIdentToUnlock, err := checkExpiration(input, commitmentInput, transaction.API.ProtocolParameters()); err != nil {
if actualIdentToUnlock, err := input.UnlockConditionSet().CheckExpirationCondition(commitmentInput.Slot, transaction.API.ProtocolParameters()); err != nil {
return err
} else if actualIdentToUnlock != nil {
ownerIdent = actualIdentToUnlock
Expand Down

0 comments on commit 6a8dbb3

Please sign in to comment.