From ddcc2d73a2022c260f0d0b24906435e46ae29765 Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Tue, 16 Jun 2020 18:14:16 -0400 Subject: [PATCH] lazily fetch block status --- vms/components/core/block.go | 4 +-- vms/components/core/block_test.go | 48 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 vms/components/core/block_test.go diff --git a/vms/components/core/block.go b/vms/components/core/block.go index 3609477..6d1d37b 100644 --- a/vms/components/core/block.go +++ b/vms/components/core/block.go @@ -34,8 +34,7 @@ type Block struct { func (b *Block) Initialize(bytes []byte, vm *SnowmanVM) { b.VM = vm b.Metadata.Initialize(bytes) - status := b.VM.State.GetStatus(vm.DB, b.ID()) - b.SetStatus(status) + b.SetStatus(choices.Unknown) // don't set status until it is queried } // ParentID returns [b]'s parent's ID @@ -55,7 +54,6 @@ func (b *Block) Parent() snowman.Block { // Recall that b.vm.DB.Commit() must be called to persist to the DB func (b *Block) Accept() error { b.SetStatus(choices.Accepted) // Change state of this block - blkID := b.ID() // Persist data diff --git a/vms/components/core/block_test.go b/vms/components/core/block_test.go new file mode 100644 index 0000000..d9d30bc --- /dev/null +++ b/vms/components/core/block_test.go @@ -0,0 +1,48 @@ +package core + +import ( + "testing" + + "github.com/ava-labs/gecko/snow/choices" + "github.com/ava-labs/gecko/snow/consensus/snowman" + + "github.com/ava-labs/gecko/ids" + + "github.com/ava-labs/gecko/database/memdb" + "github.com/ava-labs/gecko/database/versiondb" +) + +func TestBlock(t *testing.T) { + parentID := ids.NewID([32]byte{1, 2, 3, 4, 5}) + db := versiondb.New(memdb.New()) + state, err := NewSnowmanState(func([]byte) (snowman.Block, error) { return nil, nil }) + if err != nil { + t.Fatal(err) + } + b := NewBlock(parentID) + + b.Initialize([]byte{1, 2, 3}, &SnowmanVM{ + DB: db, + State: state, + }) + + // should be unknown until someone queries for it + if status := b.Metadata.status; status != choices.Unknown { + t.Fatalf("status should be unknown but is %s", status) + } + + // querying should change status to processing + if status := b.Status(); status != choices.Processing { + t.Fatalf("status should be processing but is %s", status) + } + + b.Accept() + if status := b.Status(); status != choices.Accepted { + t.Fatalf("status should be accepted but is %s", status) + } + + b.Reject() + if status := b.Status(); status != choices.Rejected { + t.Fatalf("status should be rejected but is %s", status) + } +}