From 32ebf8b507af13f68a0b802a0048bb5522349643 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Fri, 1 Dec 2023 13:22:15 +0100 Subject: [PATCH] Fix: fixed tests --- pkg/core/buffer/unsolid_commitment_buffer.go | 2 +- pkg/protocol/chain.go | 6 +- pkg/protocol/commitment.go | 1 + .../blockdag/inmemoryblockdag/blockdag.go | 14 +---- pkg/protocol/protocol_warp_sync.go | 2 +- pkg/testsuite/mock/node.go | 60 +++++++++++++++++-- 6 files changed, 62 insertions(+), 23 deletions(-) diff --git a/pkg/core/buffer/unsolid_commitment_buffer.go b/pkg/core/buffer/unsolid_commitment_buffer.go index 27df0dd51..a6417a4d7 100644 --- a/pkg/core/buffer/unsolid_commitment_buffer.go +++ b/pkg/core/buffer/unsolid_commitment_buffer.go @@ -51,7 +51,7 @@ func (u *UnsolidCommitmentBuffer[V]) Add(commitmentID iotago.CommitmentID, value u.mutex.RLock() defer u.mutex.RUnlock() - if commitmentID.Slot() <= u.lastEvictedSlot { + if u.lastEvictedSlot != 0 && commitmentID.Slot() <= u.lastEvictedSlot { return false } diff --git a/pkg/protocol/chain.go b/pkg/protocol/chain.go index 8c26912dd..aeaa16c02 100644 --- a/pkg/protocol/chain.go +++ b/pkg/protocol/chain.go @@ -92,7 +92,7 @@ func newChain(chains *Chains) *Chain { ClaimedWeight: reactive.NewVariable[uint64](), AttestedWeight: reactive.NewVariable[uint64](), VerifiedWeight: reactive.NewVariable[uint64](), - WarpSyncMode: reactive.NewVariable[bool](), + WarpSyncMode: reactive.NewVariable[bool]().Init(true), WarpSyncThreshold: reactive.NewVariable[iotago.SlotIndex](), OutOfSyncThreshold: reactive.NewVariable[iotago.SlotIndex](), RequestAttestations: reactive.NewVariable[bool](), @@ -326,9 +326,9 @@ func (c *Chain) deriveOutOfSyncThreshold(latestSeenSlot reactive.ReadableVariabl // committable age or 0 if this would cause an overflow to the negative numbers). func (c *Chain) deriveWarpSyncThreshold(latestSeenSlot reactive.ReadableVariable[iotago.SlotIndex], engineInstance *engine.Engine) func() { return c.WarpSyncThreshold.DeriveValueFrom(reactive.NewDerivedVariable(func(_ iotago.SlotIndex, latestSeenSlot iotago.SlotIndex) iotago.SlotIndex { - warpSyncOffset := engineInstance.LatestAPI().ProtocolParameters().MaxCommittableAge() + warpSyncOffset := engineInstance.LatestAPI().ProtocolParameters().MinCommittableAge() if warpSyncOffset < latestSeenSlot { - return latestSeenSlot - warpSyncOffset + return latestSeenSlot - warpSyncOffset + 1 } return 0 diff --git a/pkg/protocol/commitment.go b/pkg/protocol/commitment.go index ca06a32ab..d739bc43f 100644 --- a/pkg/protocol/commitment.go +++ b/pkg/protocol/commitment.go @@ -158,6 +158,7 @@ func (c *Commitment) initDerivedProperties() (shutdown func()) { return lo.Batch( // mark commitments that are marked as root as verified c.IsCommitted.InheritFrom(c.IsRoot), + c.IsAboveLatestVerifiedCommitment.InheritFrom(c.IsRoot), // mark commitments that are marked as verified as attested, fully booked and committable c.IsAttested.InheritFrom(c.IsCommitted), diff --git a/pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go b/pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go index 7a2c1828f..6a9c3cdf4 100644 --- a/pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go +++ b/pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go @@ -1,10 +1,8 @@ package inmemoryblockdag import ( - "fmt" "sync/atomic" - "github.com/iotaledger/hive.go/ds" "github.com/iotaledger/hive.go/ierrors" "github.com/iotaledger/hive.go/runtime/event" "github.com/iotaledger/hive.go/runtime/module" @@ -76,8 +74,6 @@ func (b *BlockDAG) setupBlock(block *blocks.Block) { var unsolidParentsCount atomic.Int32 unsolidParentsCount.Store(int32(len(block.Parents()))) - unsolidParents := ds.NewSet[iotago.BlockID]() - block.ForEachParent(func(parent iotago.Parent) { parentBlock, exists := b.blockCache.Block(parent.ID) if !exists { @@ -86,17 +82,9 @@ func (b *BlockDAG) setupBlock(block *blocks.Block) { return } - unsolidParents.Add(parent.ID) - fmt.Println("unsolid Parents of ", block.ID(), unsolidParents) - parentBlock.Solid().OnUpdateOnce(func(_ bool, _ bool) { - unsolidParents.Delete(parent.ID) - fmt.Println("unsolid Parents of ", block.ID(), unsolidParents) - - if counter := unsolidParentsCount.Add(-1); counter == 0 { - fmt.Println("unsolid counter", counter) + if unsolidParentsCount.Add(-1) == 0 { if block.SetSolid() { - fmt.Println("unsolid Trigger", counter) b.events.BlockSolid.Trigger(block) } } diff --git a/pkg/protocol/protocol_warp_sync.go b/pkg/protocol/protocol_warp_sync.go index 844881d89..fb6a3c111 100644 --- a/pkg/protocol/protocol_warp_sync.go +++ b/pkg/protocol/protocol_warp_sync.go @@ -251,7 +251,7 @@ func (w *WarpSyncProtocol) ProcessResponse(commitmentID iotago.CommitmentID, blo // Let's assume that MCA is 5: when we want to book 15, we expect to have the commitment of 10 to load // accounts from it, hence why we make committable the slot at - MCA + 1 with respect of the current slot. minimumCommittableAge := w.protocol.APIForSlot(commitmentID.Slot()).ProtocolParameters().MinCommittableAge() - if committableCommitment, exists := chain.Commitment(commitmentID.Slot() - minimumCommittableAge); exists { + if committableCommitment, exists := chain.Commitment(commitmentID.Slot() - minimumCommittableAge + 1); exists { committableCommitment.IsCommittable.Set(true) } }) diff --git a/pkg/testsuite/mock/node.go b/pkg/testsuite/mock/node.go index 2d1ca30fc..b9031ee2e 100644 --- a/pkg/testsuite/mock/node.go +++ b/pkg/testsuite/mock/node.go @@ -45,6 +45,11 @@ func UnregisterIDAliases() { idAliases = make(map[peer.ID]string) } +type InvalidSignedTransactionEvent struct { + Metadata mempool.SignedTransactionMetadata + Error error +} + type Node struct { Testing *testing.T @@ -72,10 +77,11 @@ type Node struct { logHandler slog.Handler enableEngineLogging bool - mutex syncutils.RWMutex - attachedBlocks []*blocks.Block - currentSlot iotago.SlotIndex - filteredBlockEvents []*postsolidfilter.BlockFilteredEvent + mutex syncutils.RWMutex + attachedBlocks []*blocks.Block + currentSlot iotago.SlotIndex + filteredBlockEvents []*postsolidfilter.BlockFilteredEvent + invalidTransactionEvents map[iotago.SignedTransactionID]InvalidSignedTransactionEvent } func NewNode(t *testing.T, net *Network, partition string, name string, validator bool, logHandler slog.Handler) *Node { @@ -113,7 +119,8 @@ func NewNode(t *testing.T, net *Network, partition string, name string, validato logHandler: logHandler, enableEngineLogging: true, - attachedBlocks: make([]*blocks.Block, 0), + attachedBlocks: make([]*blocks.Block, 0), + invalidTransactionEvents: make(map[iotago.SignedTransactionID]InvalidSignedTransactionEvent), } } @@ -179,6 +186,33 @@ func (n *Node) hookEvents() { n.filteredBlockEvents = append(n.filteredBlockEvents, event) }) + + n.Protocol.Engines.Main.Get().Ledger.MemPool().OnSignedTransactionAttached( + func(signedTransactionMetadata mempool.SignedTransactionMetadata) { + signedTxID := signedTransactionMetadata.ID() + + signedTransactionMetadata.OnSignaturesInvalid(func(err error) { + n.mutex.Lock() + defer n.mutex.Unlock() + + n.invalidTransactionEvents[signedTxID] = InvalidSignedTransactionEvent{ + Metadata: signedTransactionMetadata, + Error: err, + } + }) + + transactionMetadata := signedTransactionMetadata.TransactionMetadata() + + transactionMetadata.OnInvalid(func(err error) { + n.mutex.Lock() + defer n.mutex.Unlock() + + n.invalidTransactionEvents[signedTxID] = InvalidSignedTransactionEvent{ + Metadata: signedTransactionMetadata, + Error: err, + } + }) + }) } func (n *Node) hookLogging(failOnBlockFiltered bool) { @@ -373,6 +407,14 @@ func (n *Node) attachEngineLogsWithName(failOnBlockFiltered bool, instance *engi instance.LogTrace("ConflictDAG.SpendAccepted", "conflictID", conflictID) }) + instance.Ledger.MemPool().OnSignedTransactionAttached( + func(signedTransactionMetadata mempool.SignedTransactionMetadata) { + signedTransactionMetadata.OnSignaturesInvalid(func(err error) { + instance.LogTrace("MemPool.SignedTransactionSignaturesInvalid", "tx", signedTransactionMetadata.ID(), "err", err) + }) + }, + ) + instance.Ledger.OnTransactionAttached(func(transactionMetadata mempool.TransactionMetadata) { instance.LogTrace("Ledger.TransactionAttached", "tx", transactionMetadata.ID()) @@ -487,6 +529,14 @@ func (n *Node) MainEngineSwitchedCount() int { return int(n.mainEngineSwitchedCount.Load()) } +func (n *Node) TransactionFailure(txID iotago.SignedTransactionID) (InvalidSignedTransactionEvent, bool) { + n.mutex.RLock() + defer n.mutex.RUnlock() + event, exists := n.invalidTransactionEvents[txID] + + return event, exists +} + func (n *Node) AttachedBlocks() []*blocks.Block { n.mutex.RLock() defer n.mutex.RUnlock()