-
Notifications
You must be signed in to change notification settings - Fork 20
feat(ledger): Added ToPlutusData() function to gov proposal related types #1118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -16,6 +16,8 @@ package common | |
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"reflect" | ||
|
||
"github.com/blinklabs-io/gouroboros/cbor" | ||
"github.com/blinklabs-io/plutigo/pkg/data" | ||
|
@@ -108,12 +110,26 @@ type GovAnchor struct { | |
DataHash [32]byte | ||
} | ||
|
||
func (a *GovAnchor) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(0, | ||
data.NewByteString([]byte(a.Url)), | ||
data.NewByteString(a.DataHash[:]), | ||
) | ||
} | ||
|
||
type GovActionId struct { | ||
cbor.StructAsArray | ||
TransactionId [32]byte | ||
GovActionIdx uint32 | ||
} | ||
|
||
func (id *GovActionId) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(0, | ||
data.NewByteString(id.TransactionId[:]), | ||
data.NewInteger(big.NewInt(int64(id.GovActionIdx))), | ||
) | ||
} | ||
|
||
type ProposalProcedure struct { | ||
cbor.StructAsArray | ||
Deposit uint64 | ||
|
@@ -122,6 +138,14 @@ type ProposalProcedure struct { | |
Anchor GovAnchor | ||
} | ||
|
||
func (p *ProposalProcedure) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(0, | ||
data.NewInteger(new(big.Int).SetUint64(p.Deposit)), | ||
p.RewardAccount.ToPlutusData(), | ||
p.GovAction.ToPlutusData(), | ||
) | ||
} | ||
|
||
const ( | ||
GovActionTypeParameterChange = 0 | ||
GovActionTypeHardForkInitiation = 1 | ||
|
@@ -137,6 +161,10 @@ type GovActionWrapper struct { | |
Action GovAction | ||
} | ||
|
||
func (g *GovActionWrapper) ToPlutusData() data.PlutusData { | ||
return g.Action.ToPlutusData() | ||
} | ||
|
||
func (g *GovActionWrapper) UnmarshalCBOR(data []byte) error { | ||
// Determine action type | ||
actionType, err := cbor.DecodeIdFromList(data) | ||
|
@@ -178,6 +206,7 @@ func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) { | |
|
||
type GovAction interface { | ||
isGovAction() | ||
ToPlutusData() data.PlutusData | ||
} | ||
|
||
type ParameterChangeGovAction struct { | ||
|
@@ -188,6 +217,14 @@ type ParameterChangeGovAction struct { | |
PolicyHash []byte | ||
} | ||
|
||
func (a *ParameterChangeGovAction) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(0, | ||
a.ActionId.ToPlutusData(), | ||
data.NewByteString(a.ParamUpdate), | ||
data.NewByteString(a.PolicyHash), | ||
) | ||
} | ||
|
||
func (a ParameterChangeGovAction) isGovAction() {} | ||
|
||
type HardForkInitiationGovAction struct { | ||
|
@@ -201,6 +238,16 @@ type HardForkInitiationGovAction struct { | |
} | ||
} | ||
|
||
func (a *HardForkInitiationGovAction) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(1, | ||
a.ActionId.ToPlutusData(), | ||
data.NewConstr(0, | ||
data.NewInteger(new(big.Int).SetUint64(uint64(a.ProtocolVersion.Major))), | ||
data.NewInteger(new(big.Int).SetUint64(uint64(a.ProtocolVersion.Minor))), | ||
), | ||
) | ||
} | ||
|
||
func (a HardForkInitiationGovAction) isGovAction() {} | ||
|
||
type TreasuryWithdrawalGovAction struct { | ||
|
@@ -210,6 +257,20 @@ type TreasuryWithdrawalGovAction struct { | |
PolicyHash []byte | ||
} | ||
|
||
func (a *TreasuryWithdrawalGovAction) ToPlutusData() data.PlutusData { | ||
pairs := make([][2]data.PlutusData, 0, len(a.Withdrawals)) | ||
for addr, amount := range a.Withdrawals { | ||
pairs = append(pairs, [2]data.PlutusData{ | ||
data.NewConstr(0, addr.ToPlutusData()), | ||
data.NewInteger(new(big.Int).SetUint64(amount)), | ||
}) | ||
} | ||
return data.NewConstr(2, | ||
data.NewMap(pairs), | ||
data.NewByteString(a.PolicyHash), | ||
) | ||
} | ||
|
||
func (a TreasuryWithdrawalGovAction) isGovAction() {} | ||
|
||
type NoConfidenceGovAction struct { | ||
|
@@ -218,6 +279,12 @@ type NoConfidenceGovAction struct { | |
ActionId *GovActionId | ||
} | ||
|
||
func (a *NoConfidenceGovAction) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(3, | ||
a.ActionId.ToPlutusData(), | ||
) | ||
} | ||
|
||
func (a NoConfidenceGovAction) isGovAction() {} | ||
|
||
type UpdateCommitteeGovAction struct { | ||
|
@@ -229,6 +296,52 @@ type UpdateCommitteeGovAction struct { | |
Unknown cbor.Rat | ||
} | ||
|
||
func (a *UpdateCommitteeGovAction) ToPlutusData() data.PlutusData { | ||
removedItems := make([]data.PlutusData, 0, len(a.Credentials)) | ||
for _, cred := range a.Credentials { | ||
removedItems = append(removedItems, cred.ToPlutusData()) | ||
} | ||
|
||
addedPairs := make([][2]data.PlutusData, 0, len(a.CredEpochs)) | ||
for cred, epoch := range a.CredEpochs { | ||
addedPairs = append(addedPairs, [2]data.PlutusData{ | ||
cred.ToPlutusData(), | ||
data.NewInteger(new(big.Int).SetUint64(uint64(epoch))), | ||
}) | ||
} | ||
|
||
// Safe handling of Unknown Rat | ||
var num, den *big.Int | ||
if rat := a.Unknown; rat != (cbor.Rat{}) { | ||
val := reflect.ValueOf(rat) | ||
numField := val.FieldByName("num") | ||
denField := val.FieldByName("den") | ||
|
||
|
||
if numField.IsValid() && !numField.IsNil() { | ||
num = numField.Interface().(*big.Int) | ||
} | ||
if denField.IsValid() && !denField.IsNil() { | ||
den = denField.Interface().(*big.Int) | ||
} | ||
} | ||
|
||
// Default values if still nil | ||
if num == nil { | ||
num = big.NewInt(0) | ||
} | ||
if den == nil { | ||
den = big.NewInt(1) | ||
} | ||
|
||
return data.NewConstr(4, | ||
a.ActionId.ToPlutusData(), | ||
data.NewList(removedItems...), | ||
data.NewMap(addedPairs), | ||
data.NewInteger(num), | ||
data.NewInteger(den), | ||
) | ||
} | ||
|
||
func (a UpdateCommitteeGovAction) isGovAction() {} | ||
|
||
type NewConstitutionGovAction struct { | ||
|
@@ -242,11 +355,25 @@ type NewConstitutionGovAction struct { | |
} | ||
} | ||
|
||
func (a *NewConstitutionGovAction) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(5, | ||
a.ActionId.ToPlutusData(), | ||
data.NewConstr(0, | ||
a.Constitution.Anchor.ToPlutusData(), | ||
data.NewByteString(a.Constitution.ScriptHash), | ||
), | ||
) | ||
} | ||
|
||
func (a NewConstitutionGovAction) isGovAction() {} | ||
|
||
type InfoGovAction struct { | ||
cbor.StructAsArray | ||
Type uint | ||
} | ||
|
||
func (a *InfoGovAction) ToPlutusData() data.PlutusData { | ||
return data.NewConstr(6) | ||
} | ||
|
||
func (a InfoGovAction) isGovAction() {} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're here, rename this field to
Quorum
, since we can now see what it's supposed to be from looking at the Aiken code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed the comments, could you please review