Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
chore: add Nonce UnmarshalText custom method (#1862)
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct authored Aug 30, 2023
1 parent db156bf commit bbfbad0
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion types/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func (h *Header) IsGenesis() bool {
return h.Hash != ZeroHash && h.Number == 0
}

type Nonce [8]byte
const NonceLength = 8

type Nonce [NonceLength]byte

func (n Nonce) String() string {
return hex.EncodeToHex(n[:])
Expand All @@ -62,6 +64,28 @@ func (n Nonce) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}

func (n *Nonce) UnmarshalText(input []byte) error {
buf := StringToBytes(string(input))
if len(buf) != NonceLength {
return fmt.Errorf("incorrect length")
}

*n = BytesToNonce(buf)

return nil
}

func BytesToNonce(b []byte) Nonce {
var n Nonce

size := len(b)
min := min(size, NonceLength)

copy(n[NonceLength-min:], b[len(b)-min:])

return n
}

func (h *Header) Copy() *Header {
newHeader := &Header{
ParentHash: h.ParentHash,
Expand Down

0 comments on commit bbfbad0

Please sign in to comment.