Skip to content

Commit

Permalink
Implement read only flag
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrm50 committed Oct 3, 2023
1 parent e77d5da commit b191e42
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 53 deletions.
9 changes: 7 additions & 2 deletions pkg/protocol/engine/ledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,14 @@ func (l *Ledger) resolveState(stateRef iotago.Input) *promise.Promise[mempool.St
}

return p.Resolve(loadedCommitment)

Check failure on line 726 in pkg/protocol/engine/ledger/ledger/ledger.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/protocol/engine/ledger/ledger/ledger.go#L726

cannot use loadedCommitment (variable of type *iotago.Commitment) as mempool.State value in argument to p.Resolve: *iotago.Commitment does not implement mempool.State (missing method ReadOnly) (typecheck)
Raw output
pkg/protocol/engine/ledger/ledger/ledger.go:726:20: cannot use loadedCommitment (variable of type *iotago.Commitment) as mempool.State value in argument to p.Resolve: *iotago.Commitment does not implement mempool.State (missing method ReadOnly) (typecheck)
		return p.Resolve(loadedCommitment)
		                 ^
case iotago.InputBlockIssuanceCredit, iotago.InputReward:
case iotago.InputBlockIssuanceCredit:
// these are always resolved as they depend on the commitment or UTXO inputs
return p.Resolve(stateRef)
//nolint:forcetypeassert // we can safely assume that this is a BlockIssuanceCreditInput
return p.Resolve(stateRef.(*iotago.BlockIssuanceCreditInput))

Check failure on line 730 in pkg/protocol/engine/ledger/ledger/ledger.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/protocol/engine/ledger/ledger/ledger.go#L730

cannot use stateRef.(*iotago.BlockIssuanceCreditInput) (comma, ok expression of type *iotago.BlockIssuanceCreditInput) as mempool.State value in argument to p.Resolve: *iotago.BlockIssuanceCreditInput does not implement mempool.State (missing method ReadOnly) (typecheck)
Raw output
pkg/protocol/engine/ledger/ledger/ledger.go:730:20: cannot use stateRef.(*iotago.BlockIssuanceCreditInput) (comma, ok expression of type *iotago.BlockIssuanceCreditInput) as mempool.State value in argument to p.Resolve: *iotago.BlockIssuanceCreditInput does not implement mempool.State (missing method ReadOnly) (typecheck)
		return p.Resolve(stateRef.(*iotago.BlockIssuanceCreditInput))
		                 ^
case iotago.InputReward:
// these are always resolved as they depend on the commitment or UTXO inputs
//nolint:forcetypeassert // we can safely assume that this is a RewardInput
return p.Resolve(stateRef.(*iotago.RewardInput))

Check failure on line 734 in pkg/protocol/engine/ledger/ledger/ledger.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/protocol/engine/ledger/ledger/ledger.go#L734

cannot use stateRef.(*iotago.RewardInput) (comma, ok expression of type *iotago.RewardInput) as mempool.State value in argument to p.Resolve: *iotago.RewardInput does not implement mempool.State (missing method ReadOnly) (typecheck)
Raw output
pkg/protocol/engine/ledger/ledger/ledger.go:734:20: cannot use stateRef.(*iotago.RewardInput) (comma, ok expression of type *iotago.RewardInput) as mempool.State value in argument to p.Resolve: *iotago.RewardInput does not implement mempool.State (missing method ReadOnly) (typecheck)
		return p.Resolve(stateRef.(*iotago.RewardInput))
		                 ^
default:
return p.Reject(ierrors.Errorf("unsupported input type %s", stateRef.Type()))
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/protocol/engine/ledger/tests/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (m *MockedState) Type() iotago.StateType {
return iotago.InputUTXO
}

func (m *MockedState) ReadOnly() bool {
return false
}

func (m *MockedState) OutputID() iotago.OutputID {
return m.id
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/ledger/tests/state_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (s *MockStateResolver) DestroyOutputState(stateID mempool.StateID) {

func (s *MockStateResolver) ResolveOutputState(reference iotago.Input) *promise.Promise[mempool.State] {
if reference.Type() == iotago.InputUTXO {
output, exists := s.statesByID.Get(reference.StateID())
output, exists := s.statesByID.Get(reference.ReferencedStateID())

Check failure on line 36 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / Unit tests -race

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)

Check failure on line 36 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / Unit tests

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)

Check failure on line 36 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/protocol/engine/ledger/tests/state_resolver.go#L36

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)
Raw output
pkg/protocol/engine/ledger/tests/state_resolver.go:36:48: reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)
if !exists {
return promise.New[mempool.State]().Reject(ierrors.Errorf("output %s not found: %w", reference.StateID().ToHex(), mempool.ErrStateNotFound))
return promise.New[mempool.State]().Reject(ierrors.Errorf("output %s not found: %w", reference.ReferencedStateID().ToHex(), mempool.ErrStateNotFound))

Check failure on line 38 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / Unit tests -race

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)

Check failure on line 38 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / Unit tests

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)

Check failure on line 38 in pkg/protocol/engine/ledger/tests/state_resolver.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/protocol/engine/ledger/tests/state_resolver.go#L38

reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)
Raw output
pkg/protocol/engine/ledger/tests/state_resolver.go:38:99: reference.ReferencedStateID undefined (type iotago.Input has no field or method ReferencedStateID)
}

return promise.New[mempool.State]().Resolve(output)
Expand Down
38 changes: 0 additions & 38 deletions pkg/protocol/engine/ledger/tests/stored_state_reference.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/protocol/engine/mempool/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ type State interface {
StateID() StateID

Type() iotago.StateType

ReadOnly() bool
}
6 changes: 3 additions & 3 deletions pkg/protocol/engine/mempool/tests/testframework.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (t *TestFramework) CreateTransaction(alias string, referencedContextStates,
TransactionOutputIndex: i,
}

t.stateIDByAlias[alias+":"+strconv.Itoa(int(i))] = t.referencesByAlias[alias+":"+strconv.Itoa(int(i))].StateID()
t.stateIDByAlias[alias+":"+strconv.Itoa(int(i))] = t.referencesByAlias[alias+":"+strconv.Itoa(int(i))].ReferencedStateID()
}
}

Expand Down Expand Up @@ -170,11 +170,11 @@ func (t *TestFramework) OutputStateMetadata(alias string) (mempool.StateMetadata

func (t *TestFramework) StateID(alias string) mempool.StateID {
if alias == "genesis" {
return (&iotago.UTXOInput{}).StateID()
return (&iotago.UTXOInput{}).ReferencedStateID()
}

stateID, exists := t.stateIDByAlias[alias]
require.True(t.test, exists, "StateID with alias '%s' does not exist", alias)
require.True(t.test, exists, "ReferencedStateID with alias '%s' does not exist", alias)

return stateID
}
Expand Down
28 changes: 20 additions & 8 deletions pkg/protocol/engine/mempool/v1/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (m *MemPool[VoteRank]) TransactionMetadata(id iotago.TransactionID) (transa

// StateMetadata returns the metadata of the output state with the given ID.
func (m *MemPool[VoteRank]) StateMetadata(stateReference mempool.StateReference) (state mempool.StateMetadata, err error) {
stateRequest, exists := m.cachedStateRequests.Get(stateReference.StateID())
stateRequest, exists := m.cachedStateRequests.Get(stateReference.ReferencedStateID())

// create a new request that does not wait for missing states
if !exists || !stateRequest.WasCompleted() {
Expand Down Expand Up @@ -225,7 +225,7 @@ func (m *MemPool[VoteRank]) solidifyInputs(transaction *TransactionMetadata) {
for i, inputReference := range transaction.inputReferences {
stateReference, index := inputReference, i

request, created := m.cachedStateRequests.GetOrCreate(stateReference.StateID(), func() *promise.Promise[*StateMetadata] {
request, created := m.cachedStateRequests.GetOrCreate(stateReference.ReferencedStateID(), func() *promise.Promise[*StateMetadata] {
return m.requestState(stateReference, true)
})

Expand Down Expand Up @@ -261,14 +261,26 @@ func (m *MemPool[VoteRank]) executeTransaction(executionContext context.Context,

func (m *MemPool[VoteRank]) bookTransaction(transaction *TransactionMetadata) {
if m.optForkAllTransactions {
m.forkTransaction(transaction, ds.NewSet(lo.Map(transaction.inputs, func(stateMetadata *StateMetadata) mempool.StateID {
return stateMetadata.state.StateID()
})...))
m.forkTransaction(
transaction,
ds.NewSet(
lo.Map(
lo.Filter(transaction.inputs, func(metadata *StateMetadata) bool {
return !metadata.state.ReadOnly()
}),
func(stateMetadata *StateMetadata) mempool.StateID {
return stateMetadata.state.StateID()
},
)...,
),
)
} else {
lo.ForEach(transaction.inputs, func(input *StateMetadata) {
input.OnDoubleSpent(func() {
m.forkTransaction(transaction, ds.NewSet(input.state.StateID()))
})
if !input.state.ReadOnly() {
input.OnDoubleSpent(func() {
m.forkTransaction(transaction, ds.NewSet(input.state.StateID()))
})
}
})
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/protocol/engine/utxoledger/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (o *Output) Type() iotago.StateType {
return iotago.InputUTXO
}

func (o *Output) ReadOnly() bool {
return false
}

func (o *Output) OutputID() iotago.OutputID {
return o.outputID
}
Expand Down

0 comments on commit b191e42

Please sign in to comment.