feat(primitives): add name state get aliases
This commit is contained in:
parent
95d083bee0
commit
5f2b2832de
5 changed files with 218 additions and 2 deletions
|
|
@ -13,6 +13,11 @@ func (ns *NameState) Delta() *NameDelta {
|
|||
return ns.delta
|
||||
}
|
||||
|
||||
// GetDelta is an alias for Delta.
|
||||
func (ns *NameState) GetDelta() *NameDelta {
|
||||
return ns.Delta()
|
||||
}
|
||||
|
||||
// Inject copies another name state into this one.
|
||||
func (ns *NameState) Inject(other NameState) *NameState {
|
||||
ns.Name = other.Name
|
||||
|
|
@ -33,6 +38,11 @@ func (ns *NameState) Inject(other NameState) *NameState {
|
|||
return ns
|
||||
}
|
||||
|
||||
// GetInject is an alias for Inject.
|
||||
func (ns *NameState) GetInject(other NameState) *NameState {
|
||||
return ns.Inject(other)
|
||||
}
|
||||
|
||||
// Clone returns a shallow copy of the name state.
|
||||
func (ns NameState) Clone() NameState {
|
||||
return NameState{
|
||||
|
|
@ -54,17 +64,32 @@ func (ns NameState) Clone() NameState {
|
|||
}
|
||||
}
|
||||
|
||||
// GetClone is an alias for Clone.
|
||||
func (ns NameState) GetClone() NameState {
|
||||
return ns.Clone()
|
||||
}
|
||||
|
||||
// Clear discards any cached delta and returns the receiver.
|
||||
func (ns *NameState) Clear() *NameState {
|
||||
ns.delta = nil
|
||||
return ns
|
||||
}
|
||||
|
||||
// GetClear is an alias for Clear.
|
||||
func (ns *NameState) GetClear() *NameState {
|
||||
return ns.Clear()
|
||||
}
|
||||
|
||||
// HasDelta reports whether a cached delta exists and records changes.
|
||||
func (ns NameState) HasDelta() bool {
|
||||
return ns.delta != nil && !ns.delta.IsNull()
|
||||
}
|
||||
|
||||
// GetHasDelta is an alias for HasDelta.
|
||||
func (ns NameState) GetHasDelta() bool {
|
||||
return ns.HasDelta()
|
||||
}
|
||||
|
||||
func (ns *NameState) setHeight(height uint32) *NameState {
|
||||
if ns.Height == height {
|
||||
return ns
|
||||
|
|
@ -291,6 +316,11 @@ func (ns *NameState) Reset(height uint32) *NameState {
|
|||
return ns
|
||||
}
|
||||
|
||||
// GetReset is an alias for Reset.
|
||||
func (ns *NameState) GetReset(height uint32) *NameState {
|
||||
return ns.Reset(height)
|
||||
}
|
||||
|
||||
// Set assigns the raw name and resets the mutable chain state.
|
||||
func (ns *NameState) Set(name []byte, height uint32) *NameState {
|
||||
ns.Name = name
|
||||
|
|
@ -298,6 +328,11 @@ func (ns *NameState) Set(name []byte, height uint32) *NameState {
|
|||
return ns
|
||||
}
|
||||
|
||||
// GetSet is an alias for Set.
|
||||
func (ns *NameState) GetSet(name []byte, height uint32) *NameState {
|
||||
return ns.Set(name, height)
|
||||
}
|
||||
|
||||
// MaybeExpire resets the state if it has expired, preserving the current data.
|
||||
func (ns *NameState) MaybeExpire(height uint32, rules NameStateRules) bool {
|
||||
if !ns.IsExpired(height, rules) {
|
||||
|
|
@ -312,6 +347,11 @@ func (ns *NameState) MaybeExpire(height uint32, rules NameStateRules) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// GetMaybeExpire is an alias for MaybeExpire.
|
||||
func (ns *NameState) GetMaybeExpire(height uint32, rules NameStateRules) bool {
|
||||
return ns.MaybeExpire(height, rules)
|
||||
}
|
||||
|
||||
// ApplyState merges a sparse delta into the name state.
|
||||
func (ns *NameState) ApplyState(delta *NameDelta) *NameState {
|
||||
if delta == nil {
|
||||
|
|
@ -373,6 +413,11 @@ func (ns *NameState) ApplyState(delta *NameDelta) *NameState {
|
|||
return ns
|
||||
}
|
||||
|
||||
// GetApplyState is an alias for ApplyState.
|
||||
func (ns *NameState) GetApplyState(delta *NameDelta) *NameState {
|
||||
return ns.ApplyState(delta)
|
||||
}
|
||||
|
||||
// IsNull reports whether the name state carries any non-default data.
|
||||
func (ns NameState) IsNull() bool {
|
||||
return ns.Height == 0 &&
|
||||
|
|
@ -389,3 +434,8 @@ func (ns NameState) IsNull() bool {
|
|||
!ns.Expired &&
|
||||
!ns.Weak
|
||||
}
|
||||
|
||||
// GetIsNull is an alias for IsNull.
|
||||
func (ns NameState) GetIsNull() bool {
|
||||
return ns.IsNull()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,11 @@ func (ns NameState) Format(height uint32, rules NameStateRules) NameStateJSON {
|
|||
}
|
||||
}
|
||||
|
||||
// GetFormat is an alias for Format.
|
||||
func (ns NameState) GetFormat(height uint32, rules NameStateRules) NameStateJSON {
|
||||
return ns.Format(height, rules)
|
||||
}
|
||||
|
||||
// FromJSON populates the name state from its JSON representation.
|
||||
func (ns *NameState) FromJSON(json NameStateJSON) error {
|
||||
if len(json.Name) > nameStateJSONMaxSize {
|
||||
|
|
@ -103,3 +108,8 @@ func (ns *NameState) FromJSON(json NameStateJSON) error {
|
|||
ns.delta = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFromJSON is an alias for FromJSON.
|
||||
func (ns *NameState) GetFromJSON(json NameStateJSON) error {
|
||||
return ns.FromJSON(json)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,41 +91,81 @@ func (ns NameState) State(height uint32, rules NameStateRules) NameStateStatus {
|
|||
return NameStateClosed
|
||||
}
|
||||
|
||||
// GetState is an alias for State.
|
||||
func (ns NameState) GetState(height uint32, rules NameStateRules) NameStateStatus {
|
||||
return ns.State(height, rules)
|
||||
}
|
||||
|
||||
// IsOpening reports whether the name is currently in the opening phase.
|
||||
func (ns NameState) IsOpening(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateOpening
|
||||
}
|
||||
|
||||
// GetIsOpening is an alias for IsOpening.
|
||||
func (ns NameState) GetIsOpening(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsOpening(height, rules)
|
||||
}
|
||||
|
||||
// IsLocked reports whether the name is currently in the claim lockup phase.
|
||||
func (ns NameState) IsLocked(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateLocked
|
||||
}
|
||||
|
||||
// GetIsLocked is an alias for IsLocked.
|
||||
func (ns NameState) GetIsLocked(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsLocked(height, rules)
|
||||
}
|
||||
|
||||
// IsBidding reports whether the name is currently in the bidding phase.
|
||||
func (ns NameState) IsBidding(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateBidding
|
||||
}
|
||||
|
||||
// GetIsBidding is an alias for IsBidding.
|
||||
func (ns NameState) GetIsBidding(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsBidding(height, rules)
|
||||
}
|
||||
|
||||
// IsReveal reports whether the name is currently in the reveal phase.
|
||||
func (ns NameState) IsReveal(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateReveal
|
||||
}
|
||||
|
||||
// GetIsReveal is an alias for IsReveal.
|
||||
func (ns NameState) GetIsReveal(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsReveal(height, rules)
|
||||
}
|
||||
|
||||
// IsClosed reports whether the name is currently in the closed phase.
|
||||
func (ns NameState) IsClosed(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateClosed
|
||||
}
|
||||
|
||||
// GetIsClosed is an alias for IsClosed.
|
||||
func (ns NameState) GetIsClosed(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsClosed(height, rules)
|
||||
}
|
||||
|
||||
// IsRevoked reports whether the name has been revoked.
|
||||
func (ns NameState) IsRevoked(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) == NameStateRevoked
|
||||
}
|
||||
|
||||
// GetIsRevoked is an alias for IsRevoked.
|
||||
func (ns NameState) GetIsRevoked(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsRevoked(height, rules)
|
||||
}
|
||||
|
||||
// IsRedeemable reports whether the name has reached the closed state.
|
||||
func (ns NameState) IsRedeemable(height uint32, rules NameStateRules) bool {
|
||||
return ns.State(height, rules) >= NameStateClosed
|
||||
}
|
||||
|
||||
// GetIsRedeemable is an alias for IsRedeemable.
|
||||
func (ns NameState) GetIsRedeemable(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsRedeemable(height, rules)
|
||||
}
|
||||
|
||||
// IsClaimable reports whether the name is still within the claim window.
|
||||
func (ns NameState) IsClaimable(height uint32, rules NameStateRules) bool {
|
||||
if ns.Claimed == 0 || rules.NoReserved {
|
||||
|
|
@ -135,6 +175,11 @@ func (ns NameState) IsClaimable(height uint32, rules NameStateRules) bool {
|
|||
return height < rules.ClaimPeriod
|
||||
}
|
||||
|
||||
// GetIsClaimable is an alias for IsClaimable.
|
||||
func (ns NameState) GetIsClaimable(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsClaimable(height, rules)
|
||||
}
|
||||
|
||||
// IsExpired reports whether the name should be treated as expired.
|
||||
func (ns NameState) IsExpired(height uint32, rules NameStateRules) bool {
|
||||
if ns.Revoked != 0 {
|
||||
|
|
@ -159,3 +204,8 @@ func (ns NameState) IsExpired(height uint32, rules NameStateRules) bool {
|
|||
// If nobody revealed their bids, restart the auction.
|
||||
return ns.Owner.IsNull()
|
||||
}
|
||||
|
||||
// GetIsExpired is an alias for IsExpired.
|
||||
func (ns NameState) GetIsExpired(height uint32, rules NameStateRules) bool {
|
||||
return ns.IsExpired(height, rules)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,3 +163,8 @@ func (ns NameState) ToStats(height uint32, rules NameStateRules) *NameStateStats
|
|||
|
||||
return stats
|
||||
}
|
||||
|
||||
// GetToStats is an alias for ToStats.
|
||||
func (ns NameState) GetToStats(height uint32, rules NameStateRules) *NameStateStats {
|
||||
return ns.ToStats(height, rules)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -147,6 +148,102 @@ func TestNameStateDeltaHelpers(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNameStateGetAliases(t *testing.T) {
|
||||
rules := NameStateRules{
|
||||
TreeInterval: 10,
|
||||
LockupPeriod: 20,
|
||||
BiddingPeriod: 30,
|
||||
RevealPeriod: 40,
|
||||
ClaimPeriod: 140,
|
||||
RenewalWindow: 50,
|
||||
AuctionMaturity: 25,
|
||||
BlocksPerDay: 250,
|
||||
TransferLockup: 15,
|
||||
}
|
||||
|
||||
ns := NameState{
|
||||
Name: []byte("example"),
|
||||
NameHash: Hash{1, 2, 3, 4},
|
||||
Height: 100,
|
||||
Renewal: 150,
|
||||
Owner: Outpoint{TxHash: Hash{5, 6, 7, 8}, Index: 9},
|
||||
Value: 10,
|
||||
Highest: 11,
|
||||
Data: []byte{0xaa, 0xbb},
|
||||
Transfer: 180,
|
||||
Claimed: 1,
|
||||
Renewals: 12,
|
||||
Registered: true,
|
||||
Weak: true,
|
||||
}
|
||||
|
||||
if ns.GetDelta() == nil {
|
||||
t.Fatal("GetDelta should allocate a delta on demand")
|
||||
}
|
||||
|
||||
if ns.GetHasDelta() {
|
||||
t.Fatal("GetHasDelta should stay false until the delta carries changes")
|
||||
}
|
||||
|
||||
ns.GetDelta().Height = new(uint32)
|
||||
if !ns.GetHasDelta() {
|
||||
t.Fatal("GetHasDelta should report a populated delta")
|
||||
}
|
||||
|
||||
if got := ns.GetClone(); got.NameHash != ns.NameHash || got.Renewal != ns.Renewal {
|
||||
t.Fatalf("GetClone returned %+v, want %+v", got, ns)
|
||||
}
|
||||
|
||||
if got := new(NameState).GetInject(ns); got == nil || got.NameHash != ns.NameHash {
|
||||
t.Fatalf("GetInject returned %+v, want %+v", got, ns)
|
||||
}
|
||||
|
||||
if got := new(NameState).GetSet([]byte("alias"), 200); got == nil || got.Height != 200 || string(got.Name) != "alias" {
|
||||
t.Fatalf("GetSet returned %+v", got)
|
||||
}
|
||||
|
||||
if got := new(NameState).GetReset(200); got == nil || got.Height != 200 || got.Renewal != 200 {
|
||||
t.Fatalf("GetReset returned %+v", got)
|
||||
}
|
||||
|
||||
if got := new(NameState).GetClear(); got == nil {
|
||||
t.Fatal("GetClear should return the receiver")
|
||||
}
|
||||
|
||||
if got := (NameState{}).GetIsNull(); !got {
|
||||
t.Fatal("GetIsNull should report the zero-value state shape")
|
||||
}
|
||||
|
||||
if ns.GetIsNull() {
|
||||
t.Fatal("GetIsNull should reject populated states")
|
||||
}
|
||||
|
||||
if got := ns.GetState(100, rules); got != NameStateLocked {
|
||||
t.Fatalf("GetState returned %s, want LOCKED", got)
|
||||
}
|
||||
|
||||
if !ns.GetIsLocked(100, rules) || !ns.GetIsClaimable(120, rules) {
|
||||
t.Fatal("phase predicate aliases should match the underlying helpers")
|
||||
}
|
||||
|
||||
if ns.GetIsOpening(100, rules) || ns.GetIsBidding(140, rules) || ns.GetIsReveal(170, rules) {
|
||||
t.Fatal("phase predicate aliases should reflect the current state")
|
||||
}
|
||||
|
||||
if !ns.GetIsClosed(200, rules) || ns.GetIsRevoked(200, rules) || !ns.GetIsRedeemable(200, rules) {
|
||||
t.Fatal("state aliases should report closed names correctly")
|
||||
}
|
||||
|
||||
expired := ns.GetClone()
|
||||
if !expired.GetMaybeExpire(220, rules) {
|
||||
t.Fatal("GetMaybeExpire should expire the closed name after the renewal window")
|
||||
}
|
||||
|
||||
if stats := ns.GetClone().GetToStats(188, rules); stats == nil || stats.RenewalPeriodStart != 150 {
|
||||
t.Fatalf("GetToStats returned %+v", stats)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNameStateMaybeExpire(t *testing.T) {
|
||||
rules := NameStateRules{
|
||||
TreeInterval: 0,
|
||||
|
|
@ -326,6 +423,10 @@ func TestNameStateJSONRoundTrip(t *testing.T) {
|
|||
t.Fatalf("GetJSON should expose stats, got %+v", jsonView.Stats)
|
||||
}
|
||||
|
||||
if alias := original.GetFormat(188, rules); !reflect.DeepEqual(alias, jsonView) {
|
||||
t.Fatalf("GetFormat returned %+v, want %+v", alias, jsonView)
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(jsonView)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal returned error: %v", err)
|
||||
|
|
@ -337,8 +438,8 @@ func TestNameStateJSONRoundTrip(t *testing.T) {
|
|||
}
|
||||
|
||||
var roundTripped NameState
|
||||
if err := roundTripped.FromJSON(decoded); err != nil {
|
||||
t.Fatalf("FromJSON returned error: %v", err)
|
||||
if err := roundTripped.GetFromJSON(decoded); err != nil {
|
||||
t.Fatalf("GetFromJSON returned error: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(roundTripped.Name, original.Name) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue