model

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

View Source
const (
	CborNull       = byte(0xf6)
	CborNullLength = 1

	// Signature initial 2 bytes:
	// 0x58: 0b010_11000: major type 2 (010) + additional information 24 means 1-byte length (11000)
	// 0x41: 1-byte length: 64 bytes
	SignatureCborInitialLength = 2
	SignatureCborDataLength    = 64

	// Transaction is serialized as a CBOR array
	// Transaction initial byte: major type 4 (100) + array length 7 (00111)
	TransactionCborInitial       = byte(0b100_00111)
	TransactionCborInitialLength = 1

	// BlockHeader is serialized as a CBOR array
	// BlockHeader initial byte: major type 4 (100) + array length 9 (01001)
	BlockHeaderCborInitial       = byte(0b100_01001)
	BlockHeaderCborInitialLength = 1

	// Block is serialized as a CBOR array
	// Block initial byte: major type 4 (100) + array length 2 (00010)
	BlockCborInitial       = byte(0b100_00010)
	BlockCborInitialLength = 1
)
View Source
const (
	HashSize      = 32
	AddressSize   = 32
	SignatureSize = 64
)

Variables

View Source
var BlockCborInitialBytes = []byte{BlockCborInitial}
View Source
var (
	ErrInvalidBytes = errors.New("failed to unmarshal because of invalid bytes")
)

Functions

This section is empty.

Types

type Address

type Address = crpt.Address

32-byte account address

type Block

type Block struct {

	// Block header
	Header *BlockHeader `json:"header"`

	// TransactionSlice contained in the block
	Txs TransactionSlice `json:"transactions,omitempty"`
}

Block defines a block that can be encoded into CBOR or JSON representation.

func BlocksFromExts

func BlocksFromExts(bxs []BlockExt) []Block

BlocksFromExts returns the Blocks wrapped in the BlockExts.

func (*Block) InitNilEmbeddedStruct

func (t *Block) InitNilEmbeddedStruct()

func (*Block) MarshalCBOR

func (t *Block) MarshalCBOR(w io.Writer) (n int, err error)

func (Block) Ptr

func (b Block) Ptr() marsha.StructPtr

Ptr implements marsha.Struct

func (*Block) Size added in v0.0.4

func (b *Block) Size() uint64

Size calculates the estimated occupied memory of Block in bytes.

func (*Block) UnmarshalCBOR

func (t *Block) UnmarshalCBOR(r io.Reader) (int, error)

func (*Block) Val

func (b *Block) Val() marsha.Struct

Val implements marsha.StructPtr

type BlockExt

type BlockExt struct {

	// Extended block header
	Header *BlockHeaderExt

	// Extended transactions
	Txs TransactionExtSlice
	// contains filtered or unexported fields
}

BlockExt extends Block to hold some useful data that can be computed once and used in multiple places.

func (*BlockExt) Raw added in v0.0.4

func (bx *BlockExt) Raw() *Block

func (*BlockExt) Size added in v0.0.4

func (bx *BlockExt) Size() uint64

Size calculates the estimated occupied memory of Block in bytes.

func (*BlockExt) WriteTo added in v0.0.8

func (bx *BlockExt) WriteTo(w io.Writer) (n int, err error)

WriteTo writes CBOR encoded block to the io.Writer.

type BlockHash

type BlockHash = Hash32

32-byte block hash

type BlockHeader

type BlockHeader struct {

	// Creator
	Creator Address `json:"creator"`

	// Timestamp in Unix time
	Time Timestamp `json:"timestamp"`

	// Previous block hashes
	PrevHashes []BlockHash `json:"prevHashes,omitempty"`

	// Block height
	Height BlockHeight `json:"height"`

	// Root hash of some kind of hash-based tree (e.g., Merkle tree) of transactions
	TxRoot TransactionsRootHash `json:"transactionsRoot"`

	// Number of transactions contained in the block
	TxCount uint64 `json:"transactionCount"`

	// Arbitrary byte array returned by the ACEI application after executing and commiting the previous block.
	// It serves as the basis for validating any merkle proofs that comes from the ACEI application
	// and represents the state of the actual application rather than the state of the ledger itself.
	// The first block's block.Header.AppHash is given by ResponseInitLedger.app_hash in ACEI.
	AppHash []byte `json:"apphash,omitempty"`

	// Extra metadata (optional)
	// This can be used for e.g., consensus-specific data
	Extra []byte `json:"extra,omitempty"`

	// Signature (omitted when calculating the signature)
	// Put it last in the CBOR array, so it can be efficiently appended.
	Sig Signature `json:"signature,omitempty"`
}

BlockHeader defines a block header that can be encoded into CBOR or JSON representation.

func BlockHeadersFromExts

func BlockHeadersFromExts(bhxs []BlockHeaderExt) []BlockHeader

BlockHeadersFromExts returns the BlockHeaders wrapped in the BlockHeaderExts.

func (*BlockHeader) InitNilEmbeddedStruct

func (t *BlockHeader) InitNilEmbeddedStruct()

func (*BlockHeader) MarshalCBOR

func (t *BlockHeader) MarshalCBOR(w io.Writer) (n int, err error)

func (BlockHeader) Ptr

func (bh BlockHeader) Ptr() marsha.StructPtr

Ptr implements marsha.Struct

func (*BlockHeader) Size added in v0.0.4

func (bh *BlockHeader) Size() uint64

Size calculates the estimated occupied memory of BlockHeader in bytes.

func (*BlockHeader) UnmarshalCBOR

func (t *BlockHeader) UnmarshalCBOR(r io.Reader) (int, error)

func (*BlockHeader) Val

func (bh *BlockHeader) Val() marsha.Struct

Val implements marsha.StructPtr

type BlockHeaderExt

type BlockHeaderExt struct {
	*BlockHeader

	// CBOR encoded BlockHeader
	Bytes []byte

	// Block hash (the same as BlockHeader hash)
	Hash BlockHash

	// Pointer to the unmarshaled Transaction.Extra field
	ExtraUnmarshaled ExtraPtr
}

BlockHeaderExt extends BlockHeader to hold some useful data that can be computed once and used in multiple places.

func (*BlockHeaderExt) Size added in v0.0.4

func (bhx *BlockHeaderExt) Size() uint64

Size calculates the estimated occupied memory of BlockHeaderExt in bytes.

type BlockHeight

type BlockHeight uint64

func DecodeBlockHeight

func DecodeBlockHeight(bin []byte) BlockHeight

DecodeBlockHeight decodes block number from big endian encoded bytes.

func (BlockHeight) Encode

func (n BlockHeight) Encode() []byte

Encodes a block height as big endian uint64, so that it can be used as storage key and retain the order. From: https://github.com/ethereum/go-ethereum/blob/72c2c0ae7e2332b08d3e1ebfe5f850a92e26e8a1/core/rawdb/schema.go#L142

type ExtraCtor added in v0.0.9

type ExtraCtor[T ExtraPtr] func() T

ExtraCtor is a function that creates a new instance of T, which is an ExtraPtr.

type ExtraPtr added in v0.0.4

type ExtraPtr interface {
	marsha.StructPtr

	// Size calculates the estimated occupied memory of the struct ExtraPtr points to in bytes.
	Size() uint64
}

Extra is the interface the struct pointers to be put in TransactionExt.UnmarshaledExtra and BlockHeaderExt.UnmarshaledExtra must implement.

type Hash32

type Hash32 = []byte

32-byte hash

type LedgerID added in v0.0.6

type LedgerID = TransactionHash

LedgerID is the hash of the transaction that created the ledger.

type Signature

type Signature = crpt.Signature

64-byte signature

type Timestamp

type Timestamp uint64

Unix time (the number of seconds that have elapsed since the Unix epoch, minus leap seconds)

type Transaction

type Transaction struct {

	// Transaction type
	Type TransactionType `json:"type"`

	// Sender address
	From Address `json:"from"`

	// Nonce
	Nonce uint64 `json:"nonce"`

	// Target address (optional)
	To Address `json:"to,omitempty"`

	// Payload data (optional)
	Data []byte `json:"data,omitempty"`

	// Extra metadata (optional)
	// This can be used for e.g., encryption with secret sharing scheme
	Extra []byte `json:"extra,omitempty"`

	// Signature (omitted when calculating the signature)
	// Put it last in the CBOR array, so it can be efficiently appended.
	Sig Signature `json:"signature,omitempty"`
}

Transaction defines a transaction that can be encoded into CBOR or JSON representation.

func (*Transaction) InitNilEmbeddedStruct

func (t *Transaction) InitNilEmbeddedStruct()

func (*Transaction) MarshalCBOR

func (t *Transaction) MarshalCBOR(w io.Writer) (n int, err error)

func (Transaction) Ptr

func (t Transaction) Ptr() marsha.StructPtr

Ptr implements marsha.Struct

func (*Transaction) Size

func (t *Transaction) Size() uint64

Size calculates the estimated occupied memory of Transaction in bytes.

func (*Transaction) UnmarshalCBOR

func (t *Transaction) UnmarshalCBOR(r io.Reader) (int, error)

func (*Transaction) Val

func (t *Transaction) Val() marsha.Struct

Val implements marsha.StructPtr

type TransactionExt

type TransactionExt struct {
	*Transaction

	// CBOR encoded Transaction
	Bytes []byte

	// Transaction hash
	Hash TransactionHash

	// Pointer to the unmarshaled Transaction.Extra field
	ExtraUnmarshaled ExtraPtr
}

TransactionExt extends Transaction to hold some useful data that can be computed once and used in multiple places.

func (*TransactionExt) Size

func (txx *TransactionExt) Size() uint64

Size calculates the estimated occupied memory of TransactionExt in bytes.

type TransactionExtSlice added in v0.0.4

type TransactionExtSlice []*TransactionExt

TransactionSlice is a slice of pointers to TransactionExtSlice

func (TransactionExtSlice) Raw added in v0.0.4

Raw returns the wrapped Transactions.

func (TransactionExtSlice) RawSize added in v0.0.4

func (txxs TransactionExtSlice) RawSize() uint64

RawSize calculates the estimated occupied memory of underlying Transactions in bytes.

func (TransactionExtSlice) Size added in v0.0.4

func (txxs TransactionExtSlice) Size() uint64

Size calculates the estimated occupied memory of TransactionExtSlice in bytes.

type TransactionHash

type TransactionHash = Hash32

32-byte transaction hash

type TransactionSlice added in v0.0.4

type TransactionSlice []Transaction

TransactionSlice is a slice of Transaction

func TransactionSliceFromExtSlice added in v0.0.4

func TransactionSliceFromExtSlice(txxs TransactionExtSlice) TransactionSlice

TransactionsFromExtPtrs returns the Transactions wrapped in the TransactionExtSlice.

func (*TransactionSlice) Append added in v0.0.4

func (txs *TransactionSlice) Append(m cborgen.StructPtr)

Append implements cborgen.StructSlicePtr

func (*TransactionSlice) NewStructPtr added in v0.0.4

func (*TransactionSlice) NewStructPtr() marsha.StructPtr

NewStructPtr implements cborgen.StructSlicePtr

func (TransactionSlice) Size added in v0.0.4

func (txs TransactionSlice) Size() uint64

Size calculates the estimated occupied memory of TransactionSlice in bytes.

func (*TransactionSlice) Val added in v0.0.4

func (txs *TransactionSlice) Val() []marsha.StructPtr

Val implements marsha.StructSlicePtr

type TransactionType

type TransactionType uint8

Transaction type (0~255)

type TransactionsRootHash

type TransactionsRootHash = Hash32

32-byte root hash of some kind of hash-based tree (e.g., Merkle tree) of transactions

type Util added in v0.0.4

type Util struct {
	Mrsh marsha.Marsha
	Crpt crpt.Crpt
	// contains filtered or unexported fields
}

Util provides utility methods to work with DOUBL models.

func New added in v0.0.4

func New(mrsh marsha.Marsha, crpt crpt.Crpt) *Util

New creates a new Util with the specified Marsha and Crpt instances.

func (*Util) BlockExtFromBytes deprecated added in v0.0.4

func (u *Util) BlockExtFromBytes(bin []byte) (bx *BlockExt, err error)

BlockExtFromBytes unmarshals Block and extends it into a BlockExt. BlockHeaderExt.Bytes and TransactionExt.Bytes point to the same underlying memory as `bins` for performance consideration, it's not safe to modify them anywhere.

NOTE: ExtraUnmarshaled is not set yet.

Deprecated: Use ReadBlockExtFrom instead.

func (*Util) BlockHeaderExtFromBytes deprecated added in v0.0.4

func (u *Util) BlockHeaderExtFromBytes(bin []byte) (*BlockHeaderExt, error)

BlockHeaderExtFromBytes unmarshals BlockHeader and extends it into a BlockHeaderExt. BlockHeaderExt.Bytes points to the same underlying memory as `bins` for performance consideration, it's not safe to modify it anywhere.

NOTE: ExtraUnmarshaled is not set yet.

Deprecated: Use ReadBlockHeaderExtFrom instead.

func (*Util) ExtendBlock added in v0.0.4

func (u *Util) ExtendBlock(b *Block) (*BlockExt, error)

ExtendBlock extends a Block into a BlockExt.

func (*Util) ExtendBlockHeader added in v0.0.4

func (u *Util) ExtendBlockHeader(bh *BlockHeader) (*BlockHeaderExt, error)

ExtendBlockHeader extends a BlockHeader into a BlockHeaderExt.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ExtendTransaction added in v0.0.4

func (u *Util) ExtendTransaction(tx *Transaction) (*TransactionExt, error)

ExtendTransaction extends a Transaction into a TransactionExt.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ExtendTransactionSlice added in v0.0.4

func (u *Util) ExtendTransactionSlice(txs TransactionSlice) (TransactionExtSlice, error)

ExtendTransaction extends TransactionSlice into TransactionExtSlice.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ExtractBlockHeaderExtFromBlockBytes deprecated added in v0.0.4

func (u *Util) ExtractBlockHeaderExtFromBlockBytes(bin []byte) (*BlockHeaderExt, error)

ExtractBlockHeaderExtFromBlockBytes extracts bytes corresponding to the BlockHeader from Block bytes and unmarshals it into a BlockHeaderExt.

NOTE: ExtraUnmarshaled is not set yet.

Deprecated: Use ReadBlockHeaderExtFromBlockStream instead.

func (*Util) GenRootHashFromTransactionExtSlice added in v0.0.5

func (u *Util) GenRootHashFromTransactionExtSlice(txxs TransactionExtSlice) TransactionsRootHash

GenRootHashFromTransactionExtSlice computes the merkle tree root hash from TransactionExtSlice.

func (*Util) GenRootHashFromTransactionHashes added in v0.0.4

func (u *Util) GenRootHashFromTransactionHashes(transactionHashes []TransactionHash) TransactionsRootHash

GenRootHashFromTransactionHashes computes the merkle tree root hash from TransactionHashes.

func (*Util) GenRootHashFromTransactionSlice added in v0.0.4

func (u *Util) GenRootHashFromTransactionSlice(txs TransactionSlice) (TransactionsRootHash, error)

GenRootHashFromTransactions computes the merkle tree root hash from TransactionSlice.

func (*Util) HashBlockHeader added in v0.0.4

func (u *Util) HashBlockHeader(bh *BlockHeader) (BlockHash, error)

HashBlockHeader computes the hash of the BlockHeader.

func (*Util) HashTransaction added in v0.0.4

func (u *Util) HashTransaction(tx *Transaction) (TransactionHash, error)

HashTransaction computes the hash of the transaction.

func (*Util) HashTransactionNoSig added in v0.0.4

func (u *Util) HashTransactionNoSig(tx *Transaction) (TransactionHash, error)

HashTransactionNoSig computes the hash of the transaction without signature.

func (*Util) HashTransactionSlice added in v0.0.4

func (u *Util) HashTransactionSlice(txs TransactionSlice) (hs []TransactionHash, err error)

HashTransactionSlice computes the hashes of the transactions.

func (*Util) ReadBlockExtFrom added in v0.0.8

func (u *Util) ReadBlockExtFrom(r io.Reader) (bx *BlockExt, n int64, err error)

ReadBlockExtFrom reads and unmarshals the encoded block from `r` and extends it into a BlockExt, it also returns the number of bytes read.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ReadBlockHeaderExtFrom added in v0.0.8

func (u *Util) ReadBlockHeaderExtFrom(r io.Reader) (bhx *BlockHeaderExt, n int64, err error)

ReadBlockHeaderExtFrom reads and unmarshals the encoded block header from `r` and extends it into a BlockHeaderExt, it also returns the number of bytes read.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ReadBlockHeaderExtFromBlockStream added in v0.0.8

func (u *Util) ReadBlockHeaderExtFromBlockStream(r io.Reader) (bhx *BlockHeaderExt, n int64, err error)

ReadBlockHeaderExtFromBlockStream reads and unmarshals the encoded block header from byte stream of a Block and unmarshals it into a BlockHeaderExt.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ReadTransactionExtFrom added in v0.0.8

func (u *Util) ReadTransactionExtFrom(r io.Reader) (txx *TransactionExt, n int64, err error)

ReadTransactionExtFrom reads and unmarshals the encoded transaction from `r` and extends it into a TransactionExt, it also returns the number of bytes read.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) ReadTransactionExtSliceFrom added in v0.0.8

func (u *Util) ReadTransactionExtSliceFrom(r io.Reader) (
	txxs TransactionExtSlice, n int64, err error,
)

ReadTransactionExtSliceFrom reads and unmarshals the encoded transactions from `r` and extends them into TransactionExtSlice, it also returns the number of bytes read.

func (*Util) TransactionExtFromBytes deprecated added in v0.0.4

func (u *Util) TransactionExtFromBytes(bin []byte) (*TransactionExt, error)

TransactionExtFromBytes unmarshals Transaction and extends it into a TransactionExt. TransactionExt.Bytes points to the same underlying memory as `bin` for performance consideration, it's not safe to modify it anywhere.

NOTE: ExtraUnmarshaled is not set yet.

Deprecated: Use ReadTransactionExtFrom instead.

func (*Util) TransactionExtSliceFromBytesSlice added in v0.0.4

func (u *Util) TransactionExtSliceFromBytesSlice(bins [][]byte) (TransactionExtSlice, error)

TransactionExtSliceFromBytesSlice unmarshal Transaction byte slices and extends them into TransactionExtSlice. TransactionExt.Bytes points to the same underlying memory as `bins` for performance consideration, it's not safe to modify it anywhere.

NOTE: ExtraUnmarshaled is not set yet.

func (*Util) TransactionExtSliceFromTransactionsBytes deprecated added in v0.0.4

func (u *Util) TransactionExtSliceFromTransactionsBytes(bin []byte) (TransactionExtSlice, error)

TransactionExtSliceFromTransactionsBytes unmarshals a single Transactions bytes into TransactionSlice and extends them into TransactionExtSlice.

TransactionExt.Bytes points to the same underlying memory as `bins` for performance consideration, it's not safe to modify it anywhere.

Deprecated: Use ReadTransactionExtSliceFrom instead.

func (*Util) VerifyTransactionExtSignature added in v0.0.4

func (u *Util) VerifyTransactionExtSignature(txx *TransactionExt) (bool, error)

VerifyTransactionExtSignature verifies the transaction signature from TransactionExt. TODO: Prepend genesis block hash to bin

func (*Util) VerifyTransactionSignature added in v0.0.4

func (u *Util) VerifyTransactionSignature(tx *Transaction) (bool, error)

VerifyTransactionSignature verifies the transaction signature. Should prefer using VerifyTransactionExtSignature instead for better performance. TODO: prepend genesis block hash to bin

func (*Util) WriteMarshalTransactionExtSliceTo added in v0.0.8

func (u *Util) WriteMarshalTransactionExtSliceTo(txxs TransactionExtSlice, w io.Writer,
) (n int, err error)

MarshalTransactionExtSlice encodes and writes the transactions in the TransactionExtSlice to io.Writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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