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

Generalize expiry based de-duplication, dsmr #1810

Merged
merged 80 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
e5ecf69
eliminate emapChunk
tsachiherman Nov 22, 2024
ba644c3
update
tsachiherman Nov 22, 2024
2939543
update
tsachiherman Nov 25, 2024
08f0d59
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Nov 25, 2024
659f1fb
update per CR
tsachiherman Nov 25, 2024
921b908
lint
tsachiherman Nov 25, 2024
508cc92
apply requested changes.
tsachiherman Nov 25, 2024
7a3fed6
fix code + test.
tsachiherman Nov 25, 2024
b312a3c
rollback unneeded changes.
tsachiherman Nov 25, 2024
1238460
rename testing chain indexer.
tsachiherman Nov 25, 2024
8c1ab98
update
tsachiherman Nov 25, 2024
f8e42a8
add unit test.
tsachiherman Nov 26, 2024
b000edf
update unit test.
tsachiherman Nov 26, 2024
1d2536e
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Nov 27, 2024
6911f26
lint
tsachiherman Nov 27, 2024
f2816e7
update
tsachiherman Nov 28, 2024
e78f847
lint
tsachiherman Nov 28, 2024
270245a
update
tsachiherman Nov 28, 2024
144ddd4
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Dec 5, 2024
2fa701d
fix few CR feedback.
tsachiherman Dec 5, 2024
2220b5e
add certSet
tsachiherman Dec 6, 2024
39905b9
remove type declaration block.
tsachiherman Dec 6, 2024
d54e1a0
fix tests
tsachiherman Dec 6, 2024
8e83b6d
update
tsachiherman Dec 6, 2024
fdd4639
update
tsachiherman Dec 6, 2024
9e127f1
update
tsachiherman Dec 9, 2024
0f3fe73
create a wrapper for the block in dsmr (#1829)
tsachiherman Dec 9, 2024
fc87f31
update
tsachiherman Dec 10, 2024
cf82891
update
tsachiherman Dec 10, 2024
864acc1
updatge
tsachiherman Dec 10, 2024
d96160e
fix typo
tsachiherman Dec 10, 2024
9ea09b4
undo unwanted changes
tsachiherman Dec 10, 2024
30bcdce
update
tsachiherman Dec 10, 2024
8d52c05
update
tsachiherman Dec 10, 2024
32207b9
update unit test
tsachiherman Dec 12, 2024
5d62af7
update test
tsachiherman Dec 12, 2024
7ca779b
update
tsachiherman Dec 12, 2024
383a7ef
update
tsachiherman Dec 12, 2024
7902a6a
update per review feedback,.
tsachiherman Dec 13, 2024
1f143ea
address feedback.
tsachiherman Dec 19, 2024
6d4f94c
update per cr
tsachiherman Dec 20, 2024
dd419bb
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Dec 20, 2024
c87b033
update
tsachiherman Dec 23, 2024
293c6b9
Merge branch 'tsachi/refactor_validity_window3' of github.com:ava-lab…
tsachiherman Dec 23, 2024
b501cec
update per CR feedback.
tsachiherman Dec 23, 2024
68603db
update per CR
tsachiherman Dec 23, 2024
7e72626
update per feedback.
tsachiherman Dec 23, 2024
7edef6e
update
tsachiherman Dec 23, 2024
185fbf0
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Dec 23, 2024
9759244
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 7, 2025
44a0e8a
initialize chunkCerts during NewValidityWindowBlock
tsachiherman Jan 7, 2025
6b8692f
update
tsachiherman Jan 9, 2025
a43f23a
update
tsachiherman Jan 9, 2025
b4b9267
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 9, 2025
4fe3602
pin rust toolset to use 1.83.0 for now.
tsachiherman Jan 9, 2025
b900fd1
update
tsachiherman Jan 9, 2025
962cb57
update
tsachiherman Jan 9, 2025
745fea9
update
tsachiherman Jan 9, 2025
aad23ac
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 9, 2025
3540422
undo unneeded changes.
tsachiherman Jan 9, 2025
84157c2
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 10, 2025
58ed219
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 13, 2025
8f243b2
update testing
tsachiherman Jan 13, 2025
69c6996
Merge branch 'tsachi/refactor_validity_window3' of github.com:ava-lab…
tsachiherman Jan 13, 2025
2e879bc
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 13, 2025
43d5d4c
update
tsachiherman Jan 13, 2025
c088d7e
update
tsachiherman Jan 13, 2025
fd641ae
lint
tsachiherman Jan 13, 2025
2db87c4
update
tsachiherman Jan 13, 2025
fa3fc67
lint
tsachiherman Jan 13, 2025
c513464
rollback few unneeded empty lines
tsachiherman Jan 14, 2025
4dfe80b
address CR feedback.
tsachiherman Jan 14, 2025
f56f017
lint
tsachiherman Jan 14, 2025
919e852
Update internal/validitywindow/validitywindow_test.go
tsachiherman Jan 15, 2025
e0a910c
Update internal/validitywindow/validitywindow_test.go
tsachiherman Jan 15, 2025
b0f1782
remove unit test
tsachiherman Jan 15, 2025
7f21c2b
Merge branch 'main' into tsachi/refactor_validity_window3
tsachiherman Jan 15, 2025
23219b0
remove the testing for duplicate containerid from chain.ExecutionBlock
tsachiherman Jan 15, 2025
081381b
Merge branch 'tsachi/refactor_validity_window3' of github.com:ava-lab…
tsachiherman Jan 15, 2025
3f81853
update
tsachiherman Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chain/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewExecutionBlock(block *StatelessBlock) (*ExecutionBlock, error) {
}, nil
}

func (b *ExecutionBlock) ContainsTx(id ids.ID) bool {
func (b *ExecutionBlock) Contains(id ids.ID) bool {
return b.txsSet.Contains(id)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/validitywindow/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type ExecutionBlock[Container emap.Item] interface {
Timestamp() int64
Height() uint64
Txs() []Container
ContainsTx(ids.ID) bool
Contains(ids.ID) bool
}

type ChainIndex[Container emap.Item] interface {
GetExecutionBlock(ctx context.Context, blkID ids.ID) (ExecutionBlock[Container], error)
GetExecutionBlock(ctx context.Context, blkID ids.ID) (ExecutionBlock[Container], bool, error)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding the boolean here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that this is to return true/false, and use the error if something goes wrong instead of just returning the block type + an error, and you have to introspect the error using a check against database.ErrNotFound.

Copy link
Contributor Author

@tsachiherman tsachiherman Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaronbuchwald see @joshua-kim's request above to change the function signature to GetExecutionBlock() (ExecutionBlock[T], bool, error)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is it valid to not have an execution block that the validity window requests?

We should always have a window of blocks that goes past the time validity window. I am not sure of any case where not having a block ie. returning false should not also be treated as an error. Unless there is such a case, I think we're better off just returning an error and not adding to the return type here.

}
5 changes: 3 additions & 2 deletions internal/validitywindow/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ func (s *Syncer[Container]) start(ctx context.Context, lastAcceptedBlock Executi
seenValidityWindow = false
validityWindow = s.getValidityWindow(lastAcceptedBlock.Timestamp())
err error
hasBlock bool
)
for {
parent, err = s.chainIndex.GetExecutionBlock(ctx, parent.Parent())
if err != nil {
parent, hasBlock, err = s.chainIndex.GetExecutionBlock(ctx, parent.Parent())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ok is usually idiomatic in go for existence checks

if err != nil || !hasBlock {
break // If we can't fetch far enough back or we've gone past genesis, execute what we can
}
parents = append(parents, parent)
Expand Down
22 changes: 17 additions & 5 deletions internal/validitywindow/validitywindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"sync"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
Expand Down Expand Up @@ -57,8 +58,8 @@ func (v *TimeValidityWindow[Container]) VerifyExpiryReplayProtection(
if blk.Height() <= v.lastAcceptedBlockHeight {
return nil
}
parent, err := v.chainIndex.GetExecutionBlock(ctx, blk.Parent())
if err != nil {
parent, hasBlock, err := v.chainIndex.GetExecutionBlock(ctx, blk.Parent())
if err != nil || !hasBlock {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should return a boolean for hasBlock here, but if we did not have the block, we would not be able correctly verify the block. It's not safe to mark a block as passing verification if we are missing the information required to verify it, we must guarantee we don't reach a point like that in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're correct. I'll be adding an error case here which would be returned in case the hasBlock is false ( since it shouldn't happen ).

return err
}

Expand All @@ -69,6 +70,16 @@ func (v *TimeValidityWindow[Container]) VerifyExpiryReplayProtection(
if dup.Len() > 0 {
return fmt.Errorf("%w: duplicate in ancestry", ErrDuplicateContainer)
}
// make sure we have no repeats within the block itself.
// set.Set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo? Looks like we meant to remove this line

blkTxsIDs := set.NewSet[ids.ID](len(blk.Txs()))
for _, tx := range blk.Txs() {
id := tx.GetID()
if blkTxsIDs.Contains(id) {
return fmt.Errorf("%w: duplicate in block", ErrDuplicateContainer)
}
blkTxsIDs.Add(id)
}
return nil
}

Expand Down Expand Up @@ -97,6 +108,7 @@ func (v *TimeValidityWindow[Container]) isRepeat(
defer v.lock.Unlock()

var err error
var hasBlock bool
for {
if ancestorBlk.Timestamp() < oldestAllowed {
return marker, nil
Expand All @@ -110,16 +122,16 @@ func (v *TimeValidityWindow[Container]) isRepeat(
if marker.Contains(i) {
continue
}
if ancestorBlk.ContainsTx(tx.GetID()) {
if ancestorBlk.Contains(tx.GetID()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment on tx.GetID() here

marker.Add(i)
if stop {
return marker, nil
}
}
}

ancestorBlk, err = v.chainIndex.GetExecutionBlock(ctx, ancestorBlk.Parent())
if err != nil {
ancestorBlk, hasBlock, err = v.chainIndex.GetExecutionBlock(ctx, ancestorBlk.Parent())
if err != nil || !hasBlock {
return marker, err
}
}
Expand Down
6 changes: 3 additions & 3 deletions vm/resolutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func (vm *VM) LastAcceptedStatefulBlock() *StatefulBlock {
return vm.lastAccepted
}

func (vm *VM) GetExecutionBlock(ctx context.Context, blkID ids.ID) (validitywindow.ExecutionBlock[*chain.Transaction], error) {
func (vm *VM) GetExecutionBlock(ctx context.Context, blkID ids.ID) (validitywindow.ExecutionBlock[*chain.Transaction], bool, error) {
_, span := vm.tracer.Start(ctx, "VM.GetExecutionBlock")
defer span.End()

blk, err := vm.GetStatefulBlock(ctx, blkID)
if err != nil {
return nil, err
return nil, true, err
}
return blk.ExecutionBlock, nil
return blk.ExecutionBlock, true, nil
}

func (vm *VM) LastAcceptedBlockResult() *chain.ExecutedBlock {
Expand Down
2 changes: 1 addition & 1 deletion x/dsmr/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (b *BlockHandler[T, S, B, R]) Accept(ctx context.Context, block *Block) err
}

// Assemble and execute the block
innerBlock, result, state, err := b.Assembler.AssembleBlock(ctx, b.lastAcceptedState, b.lastAcceptedBlock, block.Timestamp, block.Height+1, txs)
innerBlock, result, state, err := b.Assembler.AssembleBlock(ctx, b.lastAcceptedState, b.lastAcceptedBlock, block.Tmstmp, block.Hght+1, txs)
if err != nil {
return err
}
Expand Down
57 changes: 52 additions & 5 deletions x/dsmr/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ package dsmr
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"

"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/internal/emap"
"github.com/ava-labs/hypersdk/utils"
)

const InitialChunkSize = 250 * 1024

type Tx interface {
GetID() ids.ID
GetExpiry() int64
emap.Item
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exposes the internal/emap package into the caller. I think the previous wrapping pattern where we wrapped this interface w/ a type that implemented the emap interface actually looked cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once merged, we won't need wrapping interface anymore. . unless I'm missing something ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @joshua-kim 's point is to have the internal type implemented as makes sense in this package and then wrap it with another type that converts between that structure and the required interface when we need to use it in the validity window or expiry map where we need a specific interface. Correct me if I'm wrong @joshua-kim

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

GetSponsor() codec.Address
}

Expand Down Expand Up @@ -51,6 +52,14 @@ func (c *Chunk[T]) init() error {
return nil
}

func (c Chunk[T]) GetID() ids.ID {
return c.id
}

func (c Chunk[T]) GetExpiry() int64 {
return c.Expiry
}

func signChunk[T Tx](
chunk UnsignedChunk[T],
networkID uint32,
Expand Down Expand Up @@ -106,16 +115,54 @@ func ParseChunk[T Tx](chunkBytes []byte) (Chunk[T], error) {
}

type Block struct {
ParentID ids.ID `serialize:"true"`
Height uint64 `serialize:"true"`
Timestamp int64 `serialize:"true"`
ParentID ids.ID `serialize:"true"`
Hght uint64 `serialize:"true"`
Tmstmp int64 `serialize:"true"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike us naming fields like this because of the interface signature we're trying to implement... is there a way we can get around this?


ChunkCerts []*ChunkCertificate `serialize:"true"`

blkID ids.ID
blkBytes []byte
certSet set.Set[ids.ID]
}

func NewBlock(parentID ids.ID, height uint64, timestamp int64, chunkCerts []*ChunkCertificate) Block {
blk := Block{
ParentID: parentID,
Hght: height,
Tmstmp: timestamp,
ChunkCerts: chunkCerts,
}
blk.init()
return blk
}

func (b Block) GetID() ids.ID {
return b.blkID
}

func (b Block) Parent() ids.ID {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care which we pick, but we should be consistent on the naming of either Foo() or GetFoo().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to dodge this one by saying that this method is no longer needed.

return b.ParentID
}

func (b Block) Timestamp() int64 {
return b.Tmstmp
}

func (b Block) Height() uint64 {
return b.Hght
}

func (b Block) Txs() []*ChunkCertificate {
return b.ChunkCerts
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems weird because these are not returning txs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I agree. let's discuss this as a group ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the term containers?


func (b Block) Contains(id ids.ID) bool {
return b.certSet.Contains(id)
}

func (b Block) init() {
for _, c := range b.ChunkCerts {
b.certSet.Add(c.ChunkID)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the cert set to the execution block type?

5 changes: 2 additions & 3 deletions x/dsmr/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ type ChunkCertificate struct {
Signature *warp.BitSetSignature `serialize:"true"`
}

func (c *ChunkCertificate) GetChunkID() ids.ID { return c.ChunkID }

func (c *ChunkCertificate) GetSlot() int64 { return c.Expiry }
func (c ChunkCertificate) GetID() ids.ID { return c.ChunkID }
func (c ChunkCertificate) GetExpiry() int64 { return c.Expiry }

func (c *ChunkCertificate) Bytes() []byte {
bytes, err := Codec.Marshal(CodecVersion, c)
Expand Down
Loading
Loading