From b8978dc03b7d44b243918b55a1198015769fd851 Mon Sep 17 00:00:00 2001 From: Paladz Date: Wed, 16 Jun 2021 18:25:52 +0800 Subject: [PATCH] edit the code little bit (#1967) Co-authored-by: paladz --- protocol/bc/hash.go | 8 ++++---- protocol/bc/{output.go => original_output.go} | 0 protocol/bc/tx.go | 17 +++++++++++------ protocol/bc/vote_output.go | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) rename protocol/bc/{output.go => original_output.go} (100%) diff --git a/protocol/bc/hash.go b/protocol/bc/hash.go index a1420303..ba2988de 100644 --- a/protocol/bc/hash.go +++ b/protocol/bc/hash.go @@ -50,6 +50,7 @@ func (h *Hash) UnmarshalText(v []byte) error { if len(v) != 64 { return fmt.Errorf("bad length hash string %d", len(v)) } + _, err := hex.Decode(b[:], v) *h = NewHash(b) return err @@ -63,6 +64,7 @@ func (h *Hash) UnmarshalJSON(b []byte) error { *h = Hash{} return nil } + var s string if err := json.Unmarshal(b, &s); err != nil { return err @@ -89,6 +91,7 @@ func (h *Hash) ReadFrom(r io.Reader) (int64, error) { if err != nil { return int64(n), err } + *h = NewHash(b32) return int64(n), nil } @@ -96,8 +99,5 @@ func (h *Hash) ReadFrom(r io.Reader) (int64, error) { // IsZero tells whether a Hash pointer is nil or points to an all-zero // hash. func (h *Hash) IsZero() bool { - if h == nil { - return true - } - return *h == Hash{} + return h == nil || *h == Hash{} } diff --git a/protocol/bc/output.go b/protocol/bc/original_output.go similarity index 100% rename from protocol/bc/output.go rename to protocol/bc/original_output.go diff --git a/protocol/bc/tx.go b/protocol/bc/tx.go index dfcd14b8..e8cd0e3d 100644 --- a/protocol/bc/tx.go +++ b/protocol/bc/tx.go @@ -5,6 +5,12 @@ import ( "github.com/bytom/bytom/errors" ) +// Convenience routines for accessing entries of specific types by ID. +var ( + ErrEntryType = errors.New("invalid entry type") + ErrMissingEntry = errors.New("missing entry") +) + // Tx is a wrapper for the entries-based representation of a transaction. type Tx struct { *TxHeader @@ -26,18 +32,13 @@ func (tx *Tx) SigHash(n uint32) (hash Hash) { return hash } -// Convenience routines for accessing entries of specific types by ID. -var ( - ErrEntryType = errors.New("invalid entry type") - ErrMissingEntry = errors.New("missing entry") -) - // OriginalOutput try to get the output entry by given hash func (tx *Tx) OriginalOutput(id Hash) (*OriginalOutput, error) { e, ok := tx.Entries[id] if !ok || e == nil { return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) } + o, ok := e.(*OriginalOutput) if !ok { return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) @@ -51,6 +52,7 @@ func (tx *Tx) Spend(id Hash) (*Spend, error) { if !ok || e == nil { return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) } + sp, ok := e.(*Spend) if !ok { return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) @@ -64,6 +66,7 @@ func (tx *Tx) Issuance(id Hash) (*Issuance, error) { if !ok || e == nil { return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) } + iss, ok := e.(*Issuance) if !ok { return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) @@ -77,6 +80,7 @@ func (tx *Tx) VetoInput(id Hash) (*VetoInput, error) { if !ok || e == nil { return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) } + sp, ok := e.(*VetoInput) if !ok { return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) @@ -90,6 +94,7 @@ func (tx *Tx) VoteOutput(id Hash) (*VoteOutput, error) { if !ok || e == nil { return nil, errors.Wrapf(ErrMissingEntry, "id %x", id.Bytes()) } + o, ok := e.(*VoteOutput) if !ok { return nil, errors.Wrapf(ErrEntryType, "entry %x has unexpected type %T", id.Bytes(), e) diff --git a/protocol/bc/vote_output.go b/protocol/bc/vote_output.go index f8fb7e23..a616979d 100644 --- a/protocol/bc/vote_output.go +++ b/protocol/bc/vote_output.go @@ -2,7 +2,7 @@ package bc import "io" -func (VoteOutput) typ() string { return "voteoutput1" } +func (VoteOutput) typ() string { return "voteOutput1" } func (o *VoteOutput) writeForHash(w io.Writer) { mustWriteForHash(w, o.Source) mustWriteForHash(w, o.ControlProgram) -- 2.11.0