payload

package
v0.2.4-0...-2c2ceb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2020 License: Apache-2.0, BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const IDLength = 32

IDLength defined the amount of bytes in a payload id (32 bytes hash value).

View Source
const (
	// ObjectName defines the name of the value object.
	ObjectName = "value"
)

Variables

View Source
var Type = payload.NewType(1, ObjectName, func(data []byte) (payload payload.Payload, err error) {
	payload, _, err = FromBytes(data)

	return
})

Type represents the identifier which addresses the value Payload type.

Functions

func FromObjectStorage

func FromObjectStorage(key []byte, data []byte) (result objectstorage.StorableObject, err error)

FromObjectStorage is a factory method that creates a new Payload from the ObjectStorage.

Types

type CachedPayload

type CachedPayload struct {
	objectstorage.CachedObject
}

CachedPayload is a wrapper for the object storage, that takes care of type casting the managed objects. Since go does not have generics (yet), the object storage works based on the generic "interface{}" type, which means that we have to regularly type cast the returned objects, to match the expected type. To reduce the burden of manually managing these type, we create a wrapper that does this for us. This way, we can consistently handle the specialized types of CachedObjects, without having to manually type cast over and over again.

func (*CachedPayload) Consume

func (c *CachedPayload) Consume(consumer func(payload *Payload)) bool

Consume wraps the underlying method to return the correctly typed objects in the callback.

func (*CachedPayload) Retain

func (c *CachedPayload) Retain() *CachedPayload

Retain wraps the underlying method to return a new "wrapped object".

func (*CachedPayload) Unwrap

func (c *CachedPayload) Unwrap() *Payload

Unwrap provides a way to "Get" a type casted version of the underlying object.

type ID

type ID [IDLength]byte

ID represents the hash of a payload that is used to identify the given payload.

var GenesisID ID

GenesisID contains the zero value of this ID which represents the genesis.

func IDFromBytes

func IDFromBytes(bytes []byte, optionalTargetObject ...*ID) (result ID, consumedBytes int, err error)

IDFromBytes unmarshals a payload id from a sequence of bytes. It either creates a new payload id or fills the optionally provided object with the parsed information.

func NewID

func NewID(base58EncodedString string) (result ID, err error)

NewID creates a payload id from a base58 encoded string.

func ParseID

func ParseID(marshalUtil *marshalutil.MarshalUtil) (ID, error)

ParseID is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.

func RandomID

func RandomID() (id ID)

RandomID creates a random payload id which can for example be used in unit tests.

func (ID) Bytes

func (id ID) Bytes() []byte

Bytes returns a marshaled version of this ID.

func (ID) String

func (id ID) String() string

String returns a base58 encoded version of the payload id.

type Payload

type Payload struct {
	objectstorage.StorableObjectFlags
	// contains filtered or unexported fields
}

Payload represents the entity that forms the Tangle by referencing other Payloads using their parent1 and parent2. A Payload contains a transaction and defines, where in the Tangle a transaction is attached.

Example
// 1. create value transfer (user provides this)
valueTransfer := transaction.New(
	// inputs
	transaction.NewInputs(
		transaction.NewOutputID(address.Random(), transaction.RandomID()),
		transaction.NewOutputID(address.Random(), transaction.RandomID()),
	),

	// outputs
	transaction.NewOutputs(map[address.Address][]*balance.Balance{
		address.Random(): {
			balance.New(balance.ColorIOTA, 1337),
		},
	}),
)

// 2. create value payload (the ontology creates this and wraps the user provided transfer accordingly)
valuePayload := New(
	// parent1 in "value transfer ontology" (filled by ontology tipSelector)
	GenesisID,

	// parent2 in "value transfer ontology"  (filled by ontology tipSelector)
	GenesisID,

	// value transfer
	valueTransfer,
)

// 3. build actual transaction (the base layer creates this and wraps the ontology provided payload)
tx := tangle.NewMessage(
	[]tangle.MessageID{tangle.EmptyMessageID},
	[]tangle.MessageID{},

	// the time when the transaction was created
	time.Now(),

	// public key of the issuer
	ed25519.PublicKey{},

	// the ever increasing sequence number of this transaction
	0,

	// payload
	valuePayload,

	// nonce to check PoW
	0,

	// signature
	ed25519.Signature{},
)

fmt.Println(tx)
Output:

func FromBytes

func FromBytes(bytes []byte) (result *Payload, consumedBytes int, err error)

FromBytes parses the marshaled version of a Payload into an object. It either returns a new Payload or fills an optionally provided Payload with the parsed information.

func New

func New(parent1PayloadID, parent2PayloadID ID, valueTransfer *transaction.Transaction) *Payload

New is the constructor of a Payload and creates a new Payload object from the given details.

func Parse

func Parse(marshalUtil *marshalutil.MarshalUtil) (result *Payload, err error)

Parse unmarshals a Payload using the given marshalUtil (for easier marshaling/unmarshaling).

func (*Payload) Bytes

func (p *Payload) Bytes() []byte

Bytes returns a marshaled version of this Payload.

func (*Payload) ID

func (p *Payload) ID() ID

ID returns the identifier if the Payload.

func (*Payload) ObjectStorageKey

func (p *Payload) ObjectStorageKey() []byte

ObjectStorageKey returns the bytes that are used a key when storing the Payload in an objectstorage.

func (*Payload) ObjectStorageValue

func (p *Payload) ObjectStorageValue() (bytes []byte)

ObjectStorageValue returns the bytes that represent all remaining information (not stored in the key) of a marshaled Parent2.

func (*Payload) Parent1ID

func (p *Payload) Parent1ID() ID

Parent1ID returns the first Payload that is referenced by this Payload.

func (*Payload) Parent2ID

func (p *Payload) Parent2ID() ID

Parent2ID returns the second Payload that is referenced by this Payload.

func (*Payload) String

func (p *Payload) String() string

func (*Payload) Transaction

func (p *Payload) Transaction() *transaction.Transaction

Transaction returns the Transaction that is being attached in this Payload.

func (*Payload) Type

func (p *Payload) Type() payload.Type

Type returns the type of the Payload.

func (*Payload) Update

func (p *Payload) Update(other objectstorage.StorableObject)

Update is disabled but needs to be implemented to be compatible with the objectstorage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL