Skip to content
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

feat(pkg/scale): VaryingDataTypeValue String method #2970

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dot/types/babe_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (d *BabePrimaryPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, error) {
// Index returns VDT index
func (BabePrimaryPreDigest) Index() uint { return 1 }

func (d BabePrimaryPreDigest) String() string {
return fmt.Sprintf("BabePrimaryPreDigest{AuthorityIndex=%d, SlotNumber=%d, "+
"VRFOutput=0x%x, VRFProof=0x%x}",
d.AuthorityIndex, d.SlotNumber, d.VRFOutput, d.VRFProof)
}

// BabeSecondaryPlainPreDigest is included in a block built by a secondary slot authorized producer
type BabeSecondaryPlainPreDigest struct {
AuthorityIndex uint32
Expand All @@ -86,6 +92,11 @@ func (d *BabeSecondaryPlainPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, e
// Index returns VDT index
func (BabeSecondaryPlainPreDigest) Index() uint { return 2 }

func (d BabeSecondaryPlainPreDigest) String() string {
return fmt.Sprintf("BabeSecondaryPlainPreDigest{AuthorityIndex=%d, SlotNumber: %d}",
d.AuthorityIndex, d.SlotNumber)
}

// BabeSecondaryVRFPreDigest is included in a block built by a secondary slot authorized producer
type BabeSecondaryVRFPreDigest struct {
AuthorityIndex uint32
Expand Down Expand Up @@ -114,6 +125,12 @@ func (d *BabeSecondaryVRFPreDigest) ToPreRuntimeDigest() (*PreRuntimeDigest, err
// Index returns VDT index
func (BabeSecondaryVRFPreDigest) Index() uint { return 3 }

func (d BabeSecondaryVRFPreDigest) String() string {
return fmt.Sprintf("BabeSecondaryVRFPreDigest{AuthorityIndex=%d, SlotNumber=%d, "+
"VrfOutput=0x%x, VrfProof=0x%x",
d.AuthorityIndex, d.SlotNumber, d.VrfOutput, d.VrfProof)
}

// toPreRuntimeDigest returns the VaryingDataTypeValue as a PreRuntimeDigest
func toPreRuntimeDigest(value scale.VaryingDataTypeValue) (*PreRuntimeDigest, error) {
digest := NewBabeDigest()
Expand Down
30 changes: 30 additions & 0 deletions dot/types/consensus_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type GrandpaScheduledChange struct {
// Index returns VDT index
func (GrandpaScheduledChange) Index() uint { return 1 }

func (g GrandpaScheduledChange) String() string {
return fmt.Sprintf("GrandpaScheduledChange{Auths=%v, Delay=%d", g.Auths, g.Delay)
}

// GrandpaForcedChange represents a GRANDPA forced authority change
type GrandpaForcedChange struct {
// BestFinalizedBlock is specified by the governance mechanism, defines
Expand All @@ -42,6 +46,11 @@ type GrandpaForcedChange struct {
// Index returns VDT index
func (GrandpaForcedChange) Index() uint { return 2 }

func (g GrandpaForcedChange) String() string {
return fmt.Sprintf("GrandpaForcedChange{BestFinalizedBlock=%d, Auths=%v, Delay=%d",
g.BestFinalizedBlock, g.Auths, g.Delay)
}

// GrandpaOnDisabled represents a GRANDPA authority being disabled
type GrandpaOnDisabled struct {
ID uint64
Expand All @@ -50,6 +59,10 @@ type GrandpaOnDisabled struct {
// Index returns VDT index
func (GrandpaOnDisabled) Index() uint { return 3 }

func (g GrandpaOnDisabled) String() string {
return fmt.Sprintf("GrandpaOnDisabled{ID=%d}", g.ID)
}

// GrandpaPause represents an authority set pause
type GrandpaPause struct {
Delay uint32
Expand All @@ -58,6 +71,10 @@ type GrandpaPause struct {
// Index returns VDT index
func (GrandpaPause) Index() uint { return 4 }

func (g GrandpaPause) String() string {
return fmt.Sprintf("GrandpaPause{Delay=%d}", g.Delay)
}

// GrandpaResume represents an authority set resume
type GrandpaResume struct {
Delay uint32
Expand All @@ -66,6 +83,10 @@ type GrandpaResume struct {
// Index returns VDT index
func (GrandpaResume) Index() uint { return 5 }

func (g GrandpaResume) String() string {
return fmt.Sprintf("GrandpaResume{Delay=%d}", g.Delay)
}

// NextEpochData is the digest that contains the data for the upcoming BABE epoch.
// It is included in the first block of every epoch to describe the next epoch.
type NextEpochData struct {
Expand Down Expand Up @@ -101,6 +122,10 @@ type BABEOnDisabled struct {
// Index returns VDT index
func (BABEOnDisabled) Index() uint { return 2 }

func (b BABEOnDisabled) String() string {
return fmt.Sprintf("BABEOnDisabled{ID=%d}", b.ID)
}

// NextConfigData is the digest that contains changes to the BABE configuration.
// It is potentially included in the first block of an epoch to describe the next epoch.
type NextConfigData struct {
Expand All @@ -112,6 +137,11 @@ type NextConfigData struct {
// Index returns VDT index
func (NextConfigData) Index() uint { return 3 }

func (d NextConfigData) String() string {
return fmt.Sprintf("NextConfigData{C1=%d, C2=%d, SecondarySlots=%d}",
d.C1, d.C2, d.SecondarySlots)
}

// ToConfigData returns the NextConfigData as ConfigData
func (d *NextConfigData) ToConfigData() *ConfigData {
return &ConfigData{
Expand Down
6 changes: 3 additions & 3 deletions dot/types/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type ChangesTrieRootDigest struct {
func (ChangesTrieRootDigest) Index() uint { return 2 }

// String returns the digest as a string
func (d *ChangesTrieRootDigest) String() string {
func (d ChangesTrieRootDigest) String() string {
return fmt.Sprintf("ChangesTrieRootDigest Hash=%s", d.Hash)
}

Expand All @@ -70,7 +70,7 @@ func NewBABEPreRuntimeDigest(data []byte) *PreRuntimeDigest {
}

// String returns the digest as a string
func (d *PreRuntimeDigest) String() string {
func (d PreRuntimeDigest) String() string {
return fmt.Sprintf("PreRuntimeDigest ConsensusEngineID=%s Data=0x%x", d.ConsensusEngineID.ToBytes(), d.Data)
}

Expand Down Expand Up @@ -98,6 +98,6 @@ type SealDigest struct {
func (SealDigest) Index() uint { return 5 }

// String returns the digest as a string
func (d *SealDigest) String() string {
func (d SealDigest) String() string {
return fmt.Sprintf("SealDigest ConsensusEngineID=%s Data=0x%x", d.ConsensusEngineID.ToBytes(), d.Data)
}
7 changes: 5 additions & 2 deletions dot/types/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type GrandpaAuthoritiesRaw struct {
ID uint64
}

func (g GrandpaAuthoritiesRaw) String() string {
return fmt.Sprintf("GrandpaAuthoritiesRaw{Key=0x%x, ID=%d}", g.Key, g.ID)
}

// FromRawEd25519 sets the Authority given GrandpaAuthoritiesRaw. It converts the byte representations of
// the authority public keys into a ed25519.PublicKey.
func (a *Authority) FromRawEd25519(raw GrandpaAuthoritiesRaw) error {
Expand Down Expand Up @@ -189,7 +193,6 @@ type GrandpaVote struct {
Number uint32
}

// String returns the Vote as a string
func (v *GrandpaVote) String() string {
func (v GrandpaVote) String() string {
return fmt.Sprintf("hash=%s number=%d", v.Hash, v.Number)
}
42 changes: 39 additions & 3 deletions lib/babe/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,24 @@ type Other string
// Index returns VDT index
func (Other) Index() uint { return 0 }

func (o Other) String() string { return string(o) }

// CannotLookup Failed to lookup some data
type CannotLookup struct{}

// Index returns VDT index
func (CannotLookup) Index() uint { return 1 }

func (CannotLookup) String() string { return "cannot lookup" }

// BadOrigin A bad origin
type BadOrigin struct{}

// Index returns VDT index
func (BadOrigin) Index() uint { return 2 }

func (BadOrigin) String() string { return "bad origin" }

// Module A custom error in a module
type Module struct {
Idx uint8
Expand All @@ -152,8 +158,12 @@ type Module struct {
// Index returns VDT index
func (Module) Index() uint { return 3 }

func (err Module) string() string {
return fmt.Sprintf("index: %d code: %d message: %x", err.Idx, err.Err, *err.Message)
func (err Module) String() string {
message := "nil"
if err.Message != nil {
message = *err.Message
}
return fmt.Sprintf("Module{Idx=%d, Err=%d Message=%s", err.Idx, err.Err, message)
}

// ValidityCannotLookup Could not lookup some information that is required to validate the transaction
Expand All @@ -162,78 +172,104 @@ type ValidityCannotLookup struct{}
// Index returns VDT index
func (ValidityCannotLookup) Index() uint { return 0 }

func (ValidityCannotLookup) String() string { return "validity cannot lookup" }

// NoUnsignedValidator No validator found for the given unsigned transaction
type NoUnsignedValidator struct{}

// Index returns VDT index
func (NoUnsignedValidator) Index() uint { return 1 }

func (NoUnsignedValidator) String() string { return "no unsigned validator" }

// UnknownCustom Any other custom unknown validity that is not covered
type UnknownCustom uint8

// Index returns VDT index
func (UnknownCustom) Index() uint { return 2 }

func (uc UnknownCustom) String() string { return fmt.Sprintf("UnknownCustom(%d)", uc) }

// Call The call of the transaction is not expected
type Call struct{}

// Index returns VDT index
func (Call) Index() uint { return 0 }

func (Call) String() string { return "call" }

// Payment General error to do with the inability to pay some fees (e.g. account balance too low)
type Payment struct{}

// Index returns VDT index
func (Payment) Index() uint { return 1 }

func (Payment) String() string { return "payment" }

// Future General error to do with the transaction not yet being valid (e.g. nonce too high)
type Future struct{}

// Index returns VDT index
func (Future) Index() uint { return 2 }

func (Future) String() string { return "future" }

// Stale General error to do with the transaction being outdated (e.g. nonce too low)
type Stale struct{}

// Index returns VDT index
func (Stale) Index() uint { return 3 }

func (Stale) String() string { return "stale" }

// BadProof General error to do with the transaction’s proofs (e.g. signature)
type BadProof struct{}

// Index returns VDT index
func (BadProof) Index() uint { return 4 }

func (BadProof) String() string { return "bad proof" }

// AncientBirthBlock The transaction birth block is ancient
type AncientBirthBlock struct{}

// Index returns VDT index
func (AncientBirthBlock) Index() uint { return 5 }

func (AncientBirthBlock) String() string { return "ancient birth block" }

// ExhaustsResources The transaction would exhaust the resources of current block
type ExhaustsResources struct{}

// Index returns VDT index
func (ExhaustsResources) Index() uint { return 6 }

func (ExhaustsResources) String() string { return "exhausts resources" }

// InvalidCustom Any other custom invalid validity that is not covered
type InvalidCustom uint8

// Index returns VDT index
func (InvalidCustom) Index() uint { return 7 }

func (ic InvalidCustom) String() string { return fmt.Sprintf("InvalidCustom(%d)", ic) }

// BadMandatory An extrinsic with a Mandatory dispatch resulted in Error
type BadMandatory struct{}

// Index returns VDT index
func (BadMandatory) Index() uint { return 8 }

func (BadMandatory) String() string { return "bad mandatory" }

// MandatoryDispatch A transaction with a mandatory dispatch
type MandatoryDispatch struct{}

// Index returns VDT index
func (MandatoryDispatch) Index() uint { return 9 }

func (MandatoryDispatch) String() string { return "mandatory dispatch" }

func determineErrType(vdt scale.VaryingDataType) error {
vdtVal, err := vdt.Value()
if err != nil {
Expand All @@ -247,7 +283,7 @@ func determineErrType(vdt scale.VaryingDataType) error {
case BadOrigin:
return &DispatchOutcomeError{"bad origin"}
case Module:
return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val.string())}
return &DispatchOutcomeError{fmt.Sprintf("custom module error: %s", val)}
case Call:
return &TransactionValidityError{errUnexpectedTxCall}
case Payment:
Expand Down
4 changes: 2 additions & 2 deletions lib/babe/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func TestApplyExtrinsicErrors(t *testing.T) {
{
name: "Dispatch custom module error empty",
test: []byte{0, 1, 3, 4, 5, 1, 0},
expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: ",
expected: "dispatch outcome error: custom module error: Module{Idx=4, Err=5 Message=",
},
{
name: "Dispatch custom module error",
test: []byte{0, 1, 3, 4, 5, 1, 0x04, 0x65},
expected: "dispatch outcome error: custom module error: index: 4 code: 5 message: 65",
expected: "dispatch outcome error: custom module error: Module{Idx=4, Err=5 Message=e",
},
{
name: "Dispatch unknown error",
Expand Down
Loading