Skip to content

Commit

Permalink
Merge pull request #19 from saharshagrawal/endorsements
Browse files Browse the repository at this point in the history
Added support for endorsement operation.
  • Loading branch information
gavi-anchorlabs authored Mar 16, 2020
2 parents 45093c6 + 08a2be6 commit 498e670
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contents_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const (
ContentsTagOrigination ContentsTag = 109
// ContentsTagDelegation is the tag for delegations
ContentsTagDelegation ContentsTag = 110
// ContentsTagEndorsement is the tag for endorsements
ContentsTagEndorsement ContentsTag = 0
)
80 changes: 80 additions & 0 deletions endorsement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package tezosprotocol

import (
"bytes"
"encoding/binary"
"fmt"

"golang.org/x/xerrors"
)

// Endorsement models the tezos endorsement operation type
type Endorsement struct {
Level int32
}

func (e *Endorsement) String() string {
return fmt.Sprintf("%#v", e)
}

// GetTag implements OperationContents
func (e *Endorsement) GetTag() ContentsTag {
return ContentsTagEndorsement
}

// MarshalBinary implements encoding.BinaryMarshaler
func (e *Endorsement) MarshalBinary() ([]byte, error) {
buf := bytes.Buffer{}

// tag
buf.WriteByte(byte(e.GetTag()))

// Level
levelBytesBuf := new(bytes.Buffer)
err := binary.Write(levelBytesBuf, binary.BigEndian, e.Level)
if err != nil {
return []byte(""), xerrors.Errorf("%w", err)
}
_, err = buf.Write(levelBytesBuf.Bytes())
if err != nil {
return []byte(""), xerrors.Errorf("%w", err)
}

return buf.Bytes(), nil
}

func readInt32(data []byte) (ret int32, err error) {
buf := bytes.NewBuffer(data)
err = binary.Read(buf, binary.BigEndian, &ret)
return ret, err
}

// UnmarshalBinary implements encoding.BinaryUnmarshaler
func (e *Endorsement) UnmarshalBinary(data []byte) (err error) {
// cleanly recover from out of bounds exceptions
defer func() {
if err == nil {
if r := recover(); r != nil {
err = catchOutOfRangeExceptions(r)
}
}
}()

dataPtr := data

// tag
tag := ContentsTag(dataPtr[0])
if tag != ContentsTagEndorsement {
return xerrors.Errorf("invalid tag for endorsement. Expected %d, saw %d", ContentsTagEndorsement, tag)
}
dataPtr = dataPtr[1:]

// Level
level, err := readInt32(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal level: %w", err)
}
e.Level = level

return nil
}
30 changes: 30 additions & 0 deletions endorsement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tezosprotocol_test

import (
"encoding/hex"
"testing"

"github.com/anchorageoss/tezosprotocol/v2"
"github.com/stretchr/testify/require"
)

func TestEncodeEndorsement(t *testing.T) {
require := require.New(t)
origination := &tezosprotocol.Endorsement{
Level: 999,
}
encodedBytes, err := origination.MarshalBinary()
require.NoError(err)
encoded := hex.EncodeToString(encodedBytes)
expected := "00000003e7"
require.Equal(expected, encoded)
}

func TestDecodeEndorsement(t *testing.T) {
require := require.New(t)
encoded, err := hex.DecodeString("00000003e7")
require.NoError(err)
endorsement := tezosprotocol.Endorsement{}
require.NoError(endorsement.UnmarshalBinary(encoded))
require.Equal(int32(999), endorsement.Level)
}

0 comments on commit 498e670

Please sign in to comment.