Skip to content

Commit

Permalink
Rename parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jun 18, 2024
1 parent a53916c commit efa1957
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions pkg/database/bpt/bpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,20 @@ func (b *BPT) GetRootHash() ([32]byte, error) {
return h, nil
}

func (b *BPT) newState() values.Value[*parameters] {
v := values.NewValue(b.logger.L, b.store, b.key.Append("Root"), false, values.Struct[parameters]())
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[*parameters]
values.Value[*stateData]
}

// Get loads the parameters, initializing them to the default values if they
// have not been set.
func (p paramsRecord) Get() (*parameters, error) {
func (p paramsRecord) Get() (*stateData, error) {
v, err := p.Value.Get()
switch {
case err == nil:
Expand All @@ -72,7 +72,7 @@ func (p paramsRecord) Get() (*parameters, error) {
}

// TODO Allow power to be configurable?
v = new(parameters)
v = new(stateData)
v.Power = 8
v.Mask = v.Power - 1
err = p.Value.Put(v)
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/bpt/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const branchStateSize = 32 + 1 + 32
// leafStateSize is the marshaled size of [leaf].
const leafStateSize = 32 + 32

func (r *parameters) MarshalBinary() ([]byte, error) {
func (r *stateData) MarshalBinary() ([]byte, error) {
// Marshal the fields
var data []byte
data = append(data, byte(r.MaxHeight))
Expand All @@ -36,7 +36,7 @@ func (r *parameters) MarshalBinary() ([]byte, error) {
return data, nil
}

func (r *parameters) UnmarshalBinary(data []byte) error {
func (r *stateData) UnmarshalBinary(data []byte) error {
// Check the size
if len(data) != paramsStateSize {
return encoding.ErrNotEnoughData
Expand All @@ -50,7 +50,7 @@ func (r *parameters) UnmarshalBinary(data []byte) error {
return nil
}

func (r *parameters) UnmarshalBinaryFrom(rd io.Reader) error {
func (r *stateData) UnmarshalBinaryFrom(rd io.Reader) error {
// Read paramStateSize bytes
var buf [paramsStateSize]byte
_, err := io.ReadFull(rd, buf[:])
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/bpt/model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
omitConstructor: true
type: state
key: Root
dataType: parameters
dataType: stateData
pointer: true
- name: Root
private: true
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/bpt/model_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type BPT struct {
key *record.Key
pending map[[32]byte]*mutation

state values.Value[*parameters]
state values.Value[*stateData]
root *rootRecord
}

func (c *BPT) Key() *record.Key { return c.key }

func (c *BPT) getState() values.Value[*parameters] {
func (c *BPT) getState() values.Value[*stateData] {
return values.GetOrCreate(c, &c.state, (*BPT).newState)
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/database/bpt/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type node interface {

// copyWith copies the receiver with the given branch as the parent of the
// new copy.
copyWith(_ *parameters, _ *branch, clean bool) node
copyWith(_ *stateData, _ *branch, clean bool) node

// writeTo marshals the node and writes it to the writer.
writeTo(io.Writer) error
Expand Down Expand Up @@ -190,13 +190,13 @@ func (e *branch) load() error {
}

// copyWith returns a new empty node with parent set to the given branch.
func (e *emptyNode) copyWith(s *parameters, p *branch, clean bool) node {
func (e *emptyNode) copyWith(s *stateData, p *branch, clean bool) node {
return &emptyNode{parent: p}
}

// copyWith returns a copy of the branch with parent set to the given branch.
// copyWith copies recursively if put is true.
func (e *branch) copyWith(s *parameters, p *branch, clean bool) node {
func (e *branch) copyWith(s *stateData, p *branch, clean bool) node {
// Ensure the hash is up to date
e.getHash()

Expand Down Expand Up @@ -231,7 +231,7 @@ func (e *branch) copyWith(s *parameters, p *branch, clean bool) node {
// copyWith returns a copy of the leaf node with parent set to the given branch.
// If the receiver's parent is nil, copyWith returns it instead after setting
// its parent.
func (e *leaf) copyWith(s *parameters, p *branch, clean bool) node {
func (e *leaf) copyWith(s *stateData, p *branch, clean bool) node {
f := *e
f.parent = p
return &f
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/bpt/types.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
stateData:
non-binary: true
non-json: true
fields:
Expand Down
10 changes: 5 additions & 5 deletions pkg/database/bpt/types_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type leaf struct {
parent *branch
}

type parameters struct {
type stateData struct {
RootHash [32]byte `json:"rootHash,omitempty" form:"rootHash" query:"rootHash" validate:"required"`
MaxHeight uint64 `json:"maxHeight,omitempty" form:"maxHeight" query:"maxHeight" validate:"required"`
Power uint64 `json:"power,omitempty" form:"power" query:"power" validate:"required"`
Expand Down Expand Up @@ -78,8 +78,8 @@ func (v *leaf) Copy() *leaf {

func (v *leaf) CopyAsInterface() interface{} { return v.Copy() }

func (v *parameters) Copy() *parameters {
u := new(parameters)
func (v *stateData) Copy() *stateData {
u := new(stateData)

u.RootHash = v.RootHash
u.MaxHeight = v.MaxHeight
Expand All @@ -89,7 +89,7 @@ func (v *parameters) Copy() *parameters {
return u
}

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

func (v *branch) Equal(u *branch) bool {
if !(v.Height == u.Height) {
Expand Down Expand Up @@ -126,7 +126,7 @@ func (v *leaf) Equal(u *leaf) bool {
return true
}

func (v *parameters) Equal(u *parameters) bool {
func (v *stateData) Equal(u *stateData) bool {
if !(v.RootHash == u.RootHash) {
return false
}
Expand Down

0 comments on commit efa1957

Please sign in to comment.