lazily fetch block status

This commit is contained in:
Dan Laine 2020-06-16 18:14:16 -04:00
parent 4223e1f9d5
commit ddcc2d73a2
2 changed files with 49 additions and 3 deletions

View File

@ -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

View File

@ -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)
}
}