Skip to content

Commit

Permalink
Refactor BPT parameters [#3610]
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jun 18, 2024
1 parent efa1957 commit 8e970fb
Show file tree
Hide file tree
Showing 15 changed files with 630 additions and 501 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ require (
github.com/sergi/go-diff v1.2.0
github.com/ulikunitz/xz v0.5.11
github.com/vektra/mockery/v2 v2.42.3
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240613214617-0be27c3bb950
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240618183058-91669372cfa3
gitlab.com/firelizzard/go-script v0.0.0-20240404234115-d5f0a716003d
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.14.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,8 @@ github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240613214617-0be27c3bb950 h1:dlYE/sYVb60AYrO2Tk4oP3UfmHDn3UVzhZGVXj/HjQs=
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240613214617-0be27c3bb950/go.mod h1:FTl7W44SWhDenzAtvKkLu30Cin8DAr249mH4eg7BNLY=
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240618183058-91669372cfa3 h1:RyNDul6B2nebcjA8+KSBR79MRUftOWWO0NRqN3x0Vh4=
gitlab.com/accumulatenetwork/core/schema v0.1.1-0.20240618183058-91669372cfa3/go.mod h1:FTl7W44SWhDenzAtvKkLu30Cin8DAr249mH4eg7BNLY=
gitlab.com/bosi/decorder v0.4.1 h1:VdsdfxhstabyhZovHafFw+9eJ6eU0d2CkFNJcZz/NU4=
gitlab.com/bosi/decorder v0.4.1/go.mod h1:jecSqWUew6Yle1pCr2eLWTensJMmsxHsBwt+PVbkAqA=
gitlab.com/ethan.reesor/vscode-notebooks/go-playbooks v0.0.0-20220417214602-1121b9fae118 h1:UnyYFTz6dWVMBzLUyqHPIQwMrdpiuE+CE7p/5kUfvmk=
Expand Down
82 changes: 0 additions & 82 deletions pkg/database/bpt/bpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cometbft/cometbft/libs/log"
"gitlab.com/accumulatenetwork/accumulate/internal/database/record"
"gitlab.com/accumulatenetwork/accumulate/pkg/database"
"gitlab.com/accumulatenetwork/accumulate/pkg/database/values"
"gitlab.com/accumulatenetwork/accumulate/pkg/errors"
)

Expand Down Expand Up @@ -49,40 +48,6 @@ func (b *BPT) GetRootHash() ([32]byte, error) {
return h, nil
}

func (b *BPT) newState() values.Value[*stateData] {
v := values.NewValue(b.logger.L, b.store, b.key.Append("Root"), false, values.Struct[stateData]())
return paramsRecord{v}
}

// paramsRecord is a wrapper around Value that sets the power to 8 if the
// parameters have not been configured.
type paramsRecord struct {
values.Value[*stateData]
}

// Get loads the parameters, initializing them to the default values if they
// have not been set.
func (p paramsRecord) Get() (*stateData, error) {
v, err := p.Value.Get()
switch {
case err == nil:
return v, nil
case !errors.Is(err, errors.NotFound):
return nil, errors.UnknownError.Wrap(err)
}

// TODO Allow power to be configurable?
v = new(stateData)
v.Power = 8
v.Mask = v.Power - 1
err = p.Value.Put(v)
if err != nil {
return nil, errors.UnknownError.Wrap(err)
}

return v, nil
}

// nodeKeyAt
// We need a key to address nodes in the protocol. These nodes need a unique key
// for debugging purposes.
Expand Down Expand Up @@ -128,19 +93,6 @@ func parseNodeKey(nodeKey [32]byte) (height uint64, key [32]byte, ok bool) { //n
return byteIdx*8 + 8 - bit, key, true
}

// getRoot returns the root branch node, creating it if necessary.
func (b *BPT) getRoot() *branch {
return values.GetOrCreate(b, &b.root, (*BPT).newRoot).branch
}

func (b *BPT) newRoot() *rootRecord {
e := new(branch)
e.bpt = b
e.Height = 0
e.Key, _ = nodeKeyAt(0, [32]byte{})
return &rootRecord{e}
}

// Get retrieves the latest hash associated with the given key.
func (b *BPT) Get(key *record.Key) ([]byte, error) {
if v, ok := b.pending[key.Hash()]; ok {
Expand Down Expand Up @@ -199,37 +151,3 @@ again:
e = g
goto again
}

// Resolve implements [database.Record].
func (b *BPT) Resolve(key *database.Key) (database.Record, *database.Key, error) {
if key.Len() == 0 {
return nil, nil, errors.InternalError.With("bad key for bpt")
}

// Execute any pending updates
err := b.executePending()
if err != nil {
return nil, nil, errors.UnknownError.Wrap(err)
}

if key.Get(0) == "Root" {
return b.getState(), key.SliceI(1), nil
}

nodeKey, ok := key.Get(0).([32]byte)
if !ok {
return nil, nil, errors.InternalError.With("bad key for bpt")
}
e, err := b.getRoot().getBranch(nodeKey)
if err != nil {
return nil, nil, errors.UnknownError.WithFormat("bad key for BPT: %x", err)
}

// Ensure the node is loaded
err = e.load()
if err != nil {
return nil, nil, errors.UnknownError.WithFormat("load node: %w", err)
}

return nodeRecord{e}, key.SliceI(1), nil
}
17 changes: 0 additions & 17 deletions pkg/database/bpt/enums.yml

This file was deleted.

101 changes: 0 additions & 101 deletions pkg/database/bpt/enums_gen.go

This file was deleted.

44 changes: 0 additions & 44 deletions pkg/database/bpt/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,15 @@ import (
"io"

"gitlab.com/accumulatenetwork/accumulate/pkg/errors"
"gitlab.com/accumulatenetwork/accumulate/pkg/types/encoding"
"gitlab.com/accumulatenetwork/accumulate/pkg/types/record"
)

// paramsStateSize is the marshaled size of [parameters].
const paramsStateSize = 1 + 2 + 2 + 32

// branchStateSize is the marshaled size of [branch].
const branchStateSize = 32 + 1 + 32

// leafStateSize is the marshaled size of [leaf].
const leafStateSize = 32 + 32

func (r *stateData) MarshalBinary() ([]byte, error) {
// Marshal the fields
var data []byte
data = append(data, byte(r.MaxHeight))
data = append(data, byte(r.Power>>8), byte(r.Power))
data = append(data, byte(r.Mask>>8), byte(r.Mask))
data = append(data, r.RootHash[:]...)
return data, nil
}

func (r *stateData) UnmarshalBinary(data []byte) error {
// Check the size
if len(data) != paramsStateSize {
return encoding.ErrNotEnoughData
}

// Unmarshal the fields
r.MaxHeight = uint64(data[0])
r.Power = uint64(data[1])<<8 + uint64(data[2])
r.Mask = uint64(data[3])<<8 + uint64(data[4])
r.RootHash = *(*[32]byte)(data[5:])
return nil
}

func (r *stateData) UnmarshalBinaryFrom(rd io.Reader) error {
// Read paramStateSize bytes
var buf [paramsStateSize]byte
_, err := io.ReadFull(rd, buf[:])
if err != nil {
return err
}

// Unmarshal
return r.UnmarshalBinary(buf[:])
}

// tryWrite writes the bytes to the writer if err is nil. If the write returns
// an error, tryWrite assigns it to err.
func tryWrite(err *error, wr io.Writer, b []byte) {
Expand Down Expand Up @@ -104,10 +64,6 @@ func (n *branch) readFrom(rd *bytes.Buffer, o marshalOpts) error {
return nil
}

func (*emptyNode) Type() nodeType { return nodeTypeEmpty }

func (*branch) Type() nodeType { return nodeTypeBranch }

func (v *leaf) Type() nodeType {
if isExpandedKey(v.Key) {
return nodeTypeLeafWithExpandedKey
Expand Down
67 changes: 67 additions & 0 deletions pkg/database/bpt/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package bpt

import (
"gitlab.com/accumulatenetwork/accumulate/pkg/database"
"gitlab.com/accumulatenetwork/accumulate/pkg/database/values"
"gitlab.com/accumulatenetwork/accumulate/pkg/errors"
)

func (v *stateData) CopyAsInterface() any { return v.Copy() }

func (b *BPT) newState() values.Value[*stateData] {
v := values.NewValue(b.logger.L, b.store, b.key.Append("Root"), false, values.Struct[stateData]())
return paramsRecord{v}
}

// getRoot returns the root branch node, creating it if necessary.
func (b *BPT) getRoot() *branch {
return values.GetOrCreate(b, &b.root, (*BPT).newRoot).branch
}

func (b *BPT) newRoot() *rootRecord {
e := new(branch)
e.bpt = b
e.Height = 0
e.Key, _ = nodeKeyAt(0, [32]byte{})
return &rootRecord{e}
}

// Resolve implements [database.Record].
func (b *BPT) Resolve(key *database.Key) (database.Record, *database.Key, error) {
if key.Len() == 0 {
return nil, nil, errors.InternalError.With("bad key for bpt")
}

// Execute any pending updates
err := b.executePending()
if err != nil {
return nil, nil, errors.UnknownError.Wrap(err)
}

if key.Get(0) == "Root" {
return b.getState(), key.SliceI(1), nil
}

nodeKey, ok := key.Get(0).([32]byte)
if !ok {
return nil, nil, errors.InternalError.With("bad key for bpt")
}
e, err := b.getRoot().getBranch(nodeKey)
if err != nil {
return nil, nil, errors.UnknownError.WithFormat("bad key for BPT: %x", err)
}

// Ensure the node is loaded
err = e.load()
if err != nil {
return nil, nil, errors.UnknownError.WithFormat("load node: %w", err)
}

return nodeRecord{e}, key.SliceI(1), nil
}
6 changes: 0 additions & 6 deletions pkg/database/bpt/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@ import (
"gitlab.com/accumulatenetwork/accumulate/pkg/errors"
)

// nodeType is the type of an [Node].
type nodeType uint64

// node is an node in a [BPT].
type node interface {
// Type is the type of the node.
Type() nodeType

// CopyAsInterface implements [encoding.BinaryValue].
CopyAsInterface() any

// IsDirty returns true if the node has been modified.
IsDirty() bool

Expand Down
Loading

0 comments on commit 8e970fb

Please sign in to comment.