-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from saharshagrawal/endorsements
Added support for endorsement operation.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -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 | ||
} |
This file contains 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 |
---|---|---|
@@ -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) | ||
} |