mirror of https://github.com/poanetwork/gecko.git
Merge pull request #78 from ava-labs/optimize-state
Optimize core block / state
This commit is contained in:
commit
e5a6c00fce
|
@ -34,8 +34,7 @@ type Block struct {
|
||||||
func (b *Block) Initialize(bytes []byte, vm *SnowmanVM) {
|
func (b *Block) Initialize(bytes []byte, vm *SnowmanVM) {
|
||||||
b.VM = vm
|
b.VM = vm
|
||||||
b.Metadata.Initialize(bytes)
|
b.Metadata.Initialize(bytes)
|
||||||
status := b.VM.State.GetStatus(vm.DB, b.ID())
|
b.SetStatus(choices.Unknown) // don't set status until it is queried
|
||||||
b.SetStatus(status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParentID returns [b]'s parent's ID
|
// 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
|
// Recall that b.vm.DB.Commit() must be called to persist to the DB
|
||||||
func (b *Block) Accept() error {
|
func (b *Block) Accept() error {
|
||||||
b.SetStatus(choices.Accepted) // Change state of this block
|
b.SetStatus(choices.Accepted) // Change state of this block
|
||||||
|
|
||||||
blkID := b.ID()
|
blkID := b.ID()
|
||||||
|
|
||||||
// Persist data
|
// Persist data
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
// The unique ID of this key/typeID pair
|
||||||
uID := s.uniqueID(key, typeID)
|
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
|
// Get the value from the database
|
||||||
valueBytes, err := db.Get(uID.Bytes())
|
valueBytes, err := db.Get(uID.Bytes())
|
||||||
if err != nil {
|
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
|
// Unmarshal the value from bytes and return it
|
||||||
|
|
Loading…
Reference in New Issue