-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95947f5
commit e4e82d7
Showing
2 changed files
with
125 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
) | ||
|
||
// Active is the status when the dispute is active. | ||
type Active struct{} | ||
|
||
// Index returns the index of the type Active. | ||
func (Active) Index() uint { | ||
return 0 | ||
} | ||
|
||
// ConcludedFor is the status when the dispute is concluded for the candidate. | ||
type ConcludedFor struct { | ||
Since uint64 | ||
} | ||
|
||
// Index returns the index of the type ConcludedFor. | ||
func (ConcludedFor) Index() uint { | ||
return 1 | ||
} | ||
|
||
// ConcludedAgainst is the status when the dispute is concluded against the candidate. | ||
type ConcludedAgainst struct { | ||
Since uint64 | ||
} | ||
|
||
// Index returns the index of the type ConcludedAgainst. | ||
func (ConcludedAgainst) Index() uint { | ||
return 2 | ||
} | ||
|
||
// Confirmed is the status when the dispute is confirmed. | ||
type Confirmed struct{} | ||
|
||
// Index returns the index of the type Confirmed. | ||
func (Confirmed) Index() uint { | ||
return 3 | ||
} | ||
|
||
// Status is the status of a dispute. | ||
type Status scale.VaryingDataType | ||
|
||
// Set will set a VaryingDataTypeValue using the underlying VaryingDataType | ||
func (ds *Status) Set(val scale.VaryingDataTypeValue) (err error) { | ||
vdt := scale.VaryingDataType(*ds) | ||
err = vdt.Set(val) | ||
if err != nil { | ||
return fmt.Errorf("setting value to varying data type: %w", err) | ||
} | ||
*ds = Status(vdt) | ||
return nil | ||
} | ||
|
||
// Value returns the value from the underlying VaryingDataType | ||
func (ds *Status) Value() (scale.VaryingDataTypeValue, error) { | ||
vdt := scale.VaryingDataType(*ds) | ||
return vdt.Value() | ||
} | ||
|
||
// ConcludedAt returns the time the dispute was concluded, if it is concluded. | ||
func (ds *Status) ConcludedAt() (*uint64, error) { | ||
vdt := scale.VaryingDataType(*ds) | ||
val, err := vdt.Value() | ||
if err != nil { | ||
return nil, fmt.Errorf("getting value from DisputeStatus vdt: %w", err) | ||
} | ||
|
||
switch v := val.(type) { | ||
case Active, Confirmed: | ||
return nil, nil | ||
case ConcludedFor: | ||
return &v.Since, nil | ||
case ConcludedAgainst: | ||
return &v.Since, nil | ||
default: | ||
return nil, fmt.Errorf("invalid dispute status type") | ||
} | ||
} | ||
|
||
// NewDisputeStatus creates a new dispute status. | ||
func NewDisputeStatus() (Status, error) { | ||
vdt, err := scale.NewVaryingDataType(Active{}, ConcludedFor{}, ConcludedAgainst{}, Confirmed{}) | ||
if err != nil { | ||
return Status{}, fmt.Errorf("creating new dispute status vdt: %w", err) | ||
} | ||
|
||
return Status(vdt), nil | ||
} |