Merge pull request #78 from ava-labs/optimize-state

Optimize core block / state
This commit is contained in:
Stephen Buttolph 2020-06-17 16:00:27 -04:00 committed by GitHub
commit e5a6c00fce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 13 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)
}
}

View File

@ -128,19 +128,10 @@ func (s *state) Get(db database.Database, typeID uint64, key ids.ID) (interface{
// The unique ID of this key/typeID pair
uID := s.uniqueID(key, typeID)
// See if exists in database
exists, err := db.Has(uID.Bytes())
if err != nil {
return nil, err
}
if !exists {
return nil, database.ErrNotFound
}
// Get the value from the database
valueBytes, err := db.Get(uID.Bytes())
if err != nil {
return nil, fmt.Errorf("problem getting value from database: %w", err)
return nil, err
}
// Unmarshal the value from bytes and return it