Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman committed Nov 22, 2024
1 parent e5ecf69 commit ba644c3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 75 deletions.
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
31 changes: 28 additions & 3 deletions x/dsmr/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ 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"`

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

Expand All @@ -127,3 +127,28 @@ type Block struct {
func (b Block) GetID() ids.ID {
return b.blkID
}

func (b Block) Parent() ids.ID {
return b.ParentID
}

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

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

Check failure on line 140 in x/dsmr/block.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

unnecessary conversion (unconvert)
}

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

func (b Block) ContainsTx(id ids.ID) bool {
for _, c := range b.ChunkCerts {
if c.ChunkID == id {
return true
}
}
return false
}
62 changes: 40 additions & 22 deletions x/dsmr/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/network/p2p/acp118"
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/logging"
"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/validitywindow"
"github.com/ava-labs/hypersdk/proto/pb/dsmr"
"github.com/ava-labs/hypersdk/utils"
)
Expand Down Expand Up @@ -52,7 +55,7 @@ func New[T Tx](
return nil, err
}

return &Node[T]{
node := &Node[T]{
nodeID: nodeID,
networkID: networkID,
chainID: chainID,
Expand Down Expand Up @@ -80,25 +83,35 @@ func New[T Tx](
storage: storage,
},
storage: storage,
}, nil
log: logging.NewLogger("dsmr"),
}
node.validityWindow = validitywindow.NewTimeValidityWindow(node.log, node.tracer, node)
return node, nil
}

type Node[T Tx] struct {
nodeID ids.NodeID
networkID uint32
chainID ids.ID
pk *bls.PublicKey
signer warp.Signer
getChunkClient *TypedClient[*dsmr.GetChunkRequest, Chunk[T], []byte]
getChunkSignatureClient *TypedClient[*dsmr.GetChunkSignatureRequest, *dsmr.GetChunkSignatureResponse, []byte]
chunkCertificateGossipClient *TypedClient[[]byte, []byte, *dsmr.ChunkCertificateGossip]
validators []Validator

GetChunkHandler *GetChunkHandler[T]
GetChunkSignatureHandler *acp118.Handler
ChunkCertificateGossipHandler *ChunkCertificateGossipHandler[T]
storage *chunkStorage[T]
}
type (
validityWindow[T Tx] *validitywindow.TimeValidityWindow[Chunk[T]]

Node[T Tx] struct {
nodeID ids.NodeID
networkID uint32
chainID ids.ID
pk *bls.PublicKey
signer warp.Signer
getChunkClient *TypedClient[*dsmr.GetChunkRequest, Chunk[T], []byte]
getChunkSignatureClient *TypedClient[*dsmr.GetChunkSignatureRequest, *dsmr.GetChunkSignatureResponse, []byte]
chunkCertificateGossipClient *TypedClient[[]byte, []byte, *dsmr.ChunkCertificateGossip]
validators []Validator
validityWindow validityWindow[T]

GetChunkHandler *GetChunkHandler[T]
GetChunkSignatureHandler *acp118.Handler
ChunkCertificateGossipHandler *ChunkCertificateGossipHandler[T]
storage *chunkStorage[T]
log logging.Logger
tracer trace.Tracer
}
)

// BuildChunk builds transactions into a Chunk
// TODO handle frozen sponsor + validator assignments
Expand Down Expand Up @@ -172,8 +185,9 @@ func (n *Node[T]) BuildChunk(
return chunk, n.storage.AddLocalChunkWithCert(chunk, &chunkCert)
}

// BuildBlock(ctx context.Context, parentView state.View, parent *ExecutionBlock) (*ExecutionBlock, *ExecutedBlock, merkledb.View, error)
func (n *Node[T]) BuildBlock(parent Block, timestamp int64) (Block, error) {
if timestamp <= parent.Timestamp {
if timestamp <= parent.Tmstmp {
return Block{}, ErrTimestampNotMonotonicallyIncreasing
}

Expand All @@ -193,8 +207,8 @@ func (n *Node[T]) BuildBlock(parent Block, timestamp int64) (Block, error) {

blk := Block{
ParentID: parent.GetID(),
Height: parent.Height + 1,
Timestamp: timestamp,
Hght: parent.Hght + 1,
Tmstmp: timestamp,
ChunkCerts: availableChunkCerts,
}

Expand Down Expand Up @@ -264,5 +278,9 @@ func (n *Node[T]) Accept(ctx context.Context, block Block) error {
}
}

return n.storage.SetMin(block.Timestamp, chunkIDs)
return n.storage.SetMin(block.Tmstmp, chunkIDs)
}

func (n *Node[T]) GetExecutionBlock(ctx context.Context, blkID ids.ID) (validitywindow.ExecutionBlock[Chunk[T]], error) {

Check failure on line 284 in x/dsmr/node.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 284 in x/dsmr/node.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

unused-parameter: parameter 'blkID' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 284 in x/dsmr/node.go

View workflow job for this annotation

GitHub Actions / hypersdk-lint

unused-receiver: method receiver 'n' is not referenced in method's body, consider removing or renaming it as _ (revive)
return nil, nil
}
98 changes: 49 additions & 49 deletions x/dsmr/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ func TestNode_GetChunk_AvailableChunk(t *testing.T) {

blk, err := node.BuildBlock(
Block{
ParentID: ids.GenerateTestID(),
Height: 0,
Timestamp: 1,
ParentID: ids.GenerateTestID(),
Hght: 0,
Tmstmp: 1,
},
2,
)
Expand Down Expand Up @@ -579,9 +579,9 @@ func TestNode_BuiltChunksAvailableOverGetChunk(t *testing.T) {

block, err := node.BuildBlock(
Block{
ParentID: ids.GenerateTestID(),
Height: 0,
Timestamp: 1,
ParentID: ids.GenerateTestID(),
Hght: 0,
Tmstmp: 1,
},
2,
)
Expand Down Expand Up @@ -883,9 +883,9 @@ func TestNode_GetChunkSignature_DuplicateChunk(t *testing.T) {
r.NoError(err)
blk, err := node.BuildBlock(
Block{
ParentID: ids.GenerateTestID(),
Height: 0,
Timestamp: 1,
ParentID: ids.GenerateTestID(),
Hght: 0,
Tmstmp: 1,
},
2,
)
Expand Down Expand Up @@ -1004,9 +1004,9 @@ func TestGetChunkSignature_PersistAttestedBlocks(t *testing.T) {
for {
blk, err = node1.BuildBlock(
Block{
ParentID: ids.Empty,
Height: 0,
Timestamp: 0,
ParentID: ids.Empty,
Hght: 0,
Tmstmp: 0,
},
1,
)
Expand Down Expand Up @@ -1062,10 +1062,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
{
name: "no chunk certs",
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 2,
// TODO should we be able to build empty blocks?
Expand All @@ -1074,21 +1074,21 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
{
name: "timestamp equal to parent",
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 1,
wantErr: ErrTimestampNotMonotonicallyIncreasing,
},
{
name: "timestamp older than parent",
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 0,
wantErr: ErrTimestampNotMonotonicallyIncreasing,
Expand All @@ -1107,10 +1107,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
},
},
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 2,
wantErr: ErrNoAvailableChunkCerts,
Expand Down Expand Up @@ -1147,10 +1147,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
},
},
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 5,
wantErr: ErrNoAvailableChunkCerts,
Expand All @@ -1169,10 +1169,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
},
},
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 2,
},
Expand Down Expand Up @@ -1208,10 +1208,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
},
},
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 2,
},
Expand All @@ -1238,10 +1238,10 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
},
},
parent: Block{
ParentID: ids.GenerateTestID(),
Height: 1,
Timestamp: 1,
blkID: ids.GenerateTestID(),
ParentID: ids.GenerateTestID(),
Hght: 1,
Tmstmp: 1,
blkID: ids.GenerateTestID(),
},
timestamp: 2,
},
Expand Down Expand Up @@ -1315,8 +1315,8 @@ func TestNode_NewBlock_IncludesChunkCerts(t *testing.T) {
}

r.Equal(tt.parent.GetID(), blk.ParentID)
r.Equal(tt.parent.Height+1, blk.Height)
r.Greater(blk.Timestamp, tt.parent.Timestamp)
r.Equal(tt.parent.Hght+1, blk.Hght)
r.Greater(blk.Tmstmp, tt.parent.Tmstmp)
r.NotEmpty(blk.GetID())
r.Len(blk.ChunkCerts, len(wantChunks))

Expand Down Expand Up @@ -1384,9 +1384,9 @@ func TestAccept_RequestReferencedChunks(t *testing.T) {
)
r.NoError(err)
blk, err := node1.BuildBlock(Block{
ParentID: ids.GenerateTestID(),
Height: 0,
Timestamp: 0,
ParentID: ids.GenerateTestID(),
Hght: 0,
Tmstmp: 0,
}, 1)
r.NoError(err)
r.NoError(node1.Accept(context.Background(), blk))
Expand Down

0 comments on commit ba644c3

Please sign in to comment.