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 12 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
218 changes: 218 additions & 0 deletions internal/validitywindow/validitywindow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package validitywindow

import (
"context"
"testing"

"github.com/ava-labs/avalanchego/database"
"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"
"github.com/stretchr/testify/require"

"github.com/ava-labs/hypersdk/internal/validitywindow/validitywindowtest"
)

func TestValidityWindowVerifyExpiryReplayProtection(t *testing.T) {
tests := []struct {
Name string
Accepted []validitywindowtest.ExecutionBlock
VerifyBlock validitywindowtest.ExecutionBlock
OldestAllowed int64
ExpectedError error
}{
{
Name: "no duplicate",
Accepted: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
VerifyBlock: validitywindowtest.NewExecutionBlock(2, 3, 3, 3),
OldestAllowed: 1,
},
{
Name: "expected duplicate",
Accepted: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
VerifyBlock: validitywindowtest.NewExecutionBlock(2, 3, 3, 2),
OldestAllowed: 1,
ExpectedError: ErrDuplicateContainer,
},
{
Name: "duplicate outside window",
Accepted: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
VerifyBlock: validitywindowtest.NewExecutionBlock(2, 3, 3, 2),
OldestAllowed: 2,
ExpectedError: nil,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
r := require.New(t)
r.NoError(nil)
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved

chainIndex := &testChainIndex{}
tvw := NewTimeValidityWindow(&logging.NoLog{}, trace.Noop, chainIndex)
r.NotNil(tvw)
for _, acceptedBlk := range test.Accepted {
tvw.Accept(acceptedBlk)
chainIndex.set(acceptedBlk.GetID(), acceptedBlk)
}
r.ErrorIs(tvw.VerifyExpiryReplayProtection(context.Background(), test.VerifyBlock, test.OldestAllowed), test.ExpectedError)
})
}
}

func TestValidityWindowIsRepeat(t *testing.T) {
tests := []struct {
Name string
Blocks []validitywindowtest.ExecutionBlock
Accepted uint64 // index into Blocks of the last accepted block.
ParentBlock validitywindowtest.ExecutionBlock
Containers []validitywindowtest.Container
OldestAllowewd int64
ExpectedError error
ExpectedBits set.Bits
}{
{
Name: "no containers",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
Containers: []validitywindowtest.Container{},
OldestAllowewd: 1,
ExpectedError: nil,
ExpectedBits: set.NewBits(),
},
{
Name: "no repeats",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(2, 2, 2, 3),
Containers: []validitywindowtest.Container{
validitywindowtest.NewContainer(5),
},
OldestAllowewd: 0,
ExpectedError: nil,
ExpectedBits: set.NewBits(),
},
{
Name: "repeats outside validity window",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(2, 2, 2, 3),
Containers: []validitywindowtest.Container{
validitywindowtest.NewContainer(1),
},
OldestAllowewd: 2,
ExpectedError: nil,
ExpectedBits: set.NewBits(),
},
{
Name: "repeats within latest accepted validity window block",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1, 2),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(2, 2, 2, 3),
Containers: []validitywindowtest.Container{
validitywindowtest.NewContainer(1),
},
OldestAllowewd: 1,
ExpectedError: nil,
ExpectedBits: set.NewBits(0),
},
{
Name: "repeats after latest accepted block",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1),
validitywindowtest.NewExecutionBlock(2, 2, 2, 2),
validitywindowtest.NewExecutionBlock(3, 3, 3, 3),
validitywindowtest.NewExecutionBlock(4, 4, 4, 4),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(5, 5, 5),
Containers: []validitywindowtest.Container{
validitywindowtest.NewContainer(3),
},
OldestAllowewd: 1,
ExpectedError: nil,
ExpectedBits: set.NewBits(0),
},
{
Name: "missing block in ancestery",
Blocks: []validitywindowtest.ExecutionBlock{
validitywindowtest.NewExecutionBlock(0, 0, 0),
validitywindowtest.NewExecutionBlock(1, 1, 1, 1),
validitywindowtest.NewExecutionBlock(2, 2, 2, 2),
},
Accepted: 1,
ParentBlock: validitywindowtest.NewExecutionBlock(5, 5, 5),
Containers: []validitywindowtest.Container{
validitywindowtest.NewContainer(3),
},
OldestAllowewd: 1,
ExpectedError: database.ErrNotFound,
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
r := require.New(t)
r.NoError(nil)
tsachiherman marked this conversation as resolved.
Show resolved Hide resolved

chainIndex := &testChainIndex{}
tvw := NewTimeValidityWindow(&logging.NoLog{}, trace.Noop, chainIndex)
r.NotNil(tvw)
for i, blk := range test.Blocks {
if i <= int(test.Accepted) {
tvw.Accept(blk)
}
chainIndex.set(blk.GetID(), blk)
}
bits, err := tvw.IsRepeat(context.Background(), test.ParentBlock, test.Containers, test.OldestAllowewd)
r.ErrorIs(err, test.ExpectedError)
if err == nil {
r.Equal(test.ExpectedBits.Bytes(), bits.Bytes())
}
})
}
}

// testing structures.
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 remove comments that do not add any new information?

type testChainIndex struct {
blocks map[ids.ID]ExecutionBlock[validitywindowtest.Container]
}

func (t testChainIndex) GetExecutionBlock(_ context.Context, blkID ids.ID) (ExecutionBlock[validitywindowtest.Container], error) {
if blk, ok := t.blocks[blkID]; ok {
return blk, nil
}
return nil, database.ErrNotFound
}

func (t *testChainIndex) set(blkID ids.ID, blk ExecutionBlock[validitywindowtest.Container]) {
if t.blocks == nil {
t.blocks = make(map[ids.ID]ExecutionBlock[validitywindowtest.Container])
}
t.blocks[blkID] = blk
}
34 changes: 34 additions & 0 deletions internal/validitywindow/validitywindowtest/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package validitywindowtest

import (
"github.com/ava-labs/avalanchego/ids"

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

var _ emap.Item = (*Container)(nil)

type Container struct {
ID ids.ID
Expiry int64
}

// method for returning an id of the item
func (c Container) GetID() ids.ID {
return c.ID
}

// method for returning this items timestamp
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 remove comments that do not add any new information?

func (c Container) GetExpiry() int64 {
return c.Expiry
}

func NewContainer(expiry int64) Container {
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 this package to the validity window test file if this test package is not required externally?

return Container{
Expiry: expiry,
ID: int64ToID(expiry),
}
}
66 changes: 66 additions & 0 deletions internal/validitywindow/validitywindowtest/executionblock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package validitywindowtest

import (
"encoding/binary"

"github.com/ava-labs/avalanchego/ids"
)

type ExecutionBlock struct {
Prnt ids.ID
Tmstmp int64
Hght uint64
Ctrs []Container
ID ids.ID
}

func (e ExecutionBlock) GetID() ids.ID {
return e.ID
}

func (e ExecutionBlock) Parent() ids.ID {
return e.Prnt
}

func (e ExecutionBlock) Timestamp() int64 {
return e.Tmstmp
}

func (e ExecutionBlock) Height() uint64 {
return e.Hght
}

func (e ExecutionBlock) Containers() []Container {
return e.Ctrs
}

func (e ExecutionBlock) Contains(id ids.ID) bool {
for _, c := range e.Ctrs {
if c.GetID() == id {
return true
}
}
return false
}

func NewExecutionBlock(parent int64, timestamp int64, height uint64, contrainers ...int64) ExecutionBlock {
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 fix the typo contrainers ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This constructor is fairly difficult to read in the tests as it is a sequence of similar numbers including a variadic final parameter. Could we switch the last argument to being a slice, so that it's clearer to read which numbers are the containers?

e := ExecutionBlock{
Prnt: int64ToID(parent),
Tmstmp: timestamp,
Hght: height,
ID: int64ToID(parent + 1),
}
for _, c := range contrainers {
e.Ctrs = append(e.Ctrs, NewContainer(c))
}
return e
}

func int64ToID(n int64) ids.ID {
var id ids.ID
binary.LittleEndian.PutUint64(id[0:8], uint64(n))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use BigEndian as we use it throughout the rest of the repo?

return id
}
20 changes: 10 additions & 10 deletions x/dsmr/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func ParseChunk[T Tx](chunkBytes []byte) (Chunk[T], error) {
// validityWindowBlock bridge the gap between the dsmr's block implementation and the validity window's execution block interface.
aaronbuchwald marked this conversation as resolved.
Show resolved Hide resolved
type validityWindowBlock struct {
Block
certs set.Set[ids.ID]
certs set.Set[ids.ID]
chunkCerts []*emapChunkCertificate
}

func (e validityWindowBlock) Timestamp() int64 {
Expand All @@ -129,25 +130,24 @@ func (e validityWindowBlock) Parent() ids.ID {
}

func (e validityWindowBlock) Containers() []*emapChunkCertificate {
chunkCerts := make([]*emapChunkCertificate, len(e.Block.ChunkCerts))
for i := range chunkCerts {
chunkCerts[i] = &emapChunkCertificate{*e.Block.ChunkCerts[i]}
}
return chunkCerts
return e.chunkCerts
}

func NewValidityWindowBlock(innerBlock Block) validityWindowBlock {
certSet := set.Set[ids.ID]{}
for _, c := range innerBlock.ChunkCerts {
chunkCerts := make([]*emapChunkCertificate, len(innerBlock.ChunkCerts))
for i, c := range innerBlock.ChunkCerts {
certSet.Add(c.ChunkID)
chunkCerts[i] = &emapChunkCertificate{*c}
}
return validityWindowBlock{
Block: innerBlock,
certs: certSet,
Block: innerBlock,
certs: certSet,
chunkCerts: chunkCerts,
}
}

type Block struct {
type BlockHeader struct {
ParentID ids.ID `serialize:"true"`
Height uint64 `serialize:"true"`
Timestamp int64 `serialize:"true"`
Expand Down
Loading
Loading