package simple
import "go.dedis.ch/dela/core/validation/simple"
Package simple implements a validation service that executes a batch of transactions sequentially.
Documentation Last Review: 08.10.2020
Index ¶
- func RegisterResultFormat(f serde.Format, e serde.FormatEngine)
- func RegisterTransactionResultFormat(f serde.Format, e serde.FormatEngine)
- type Result
- func NewResult(results []TransactionResult) Result
- func (d Result) Fingerprint(w io.Writer) error
- func (d Result) GetTransactionResults() []validation.TransactionResult
- func (d Result) Serialize(ctx serde.Context) ([]byte, error)
- type ResultFactory
- func NewResultFactory(f txn.Factory) ResultFactory
- func (f ResultFactory) Deserialize(ctx serde.Context, data []byte) (serde.Message, error)
- func (f ResultFactory) ResultOf(ctx serde.Context, data []byte) (validation.Result, error)
- type ResultKey
- type Service
- func NewService(exec execution.Service, f txn.Factory) Service
- func (s Service) Accept(store store.Readable, tx txn.Transaction, leeway validation.Leeway) error
- func (s Service) GetFactory() validation.ResultFactory
- func (s Service) GetNonce(store store.Readable, ident access.Identity) (uint64, error)
- func (s Service) Validate(store store.Snapshot, txs []txn.Transaction) (validation.Result, error)
- type TransactionKey
- type TransactionResult
- func NewTransactionResult(tx txn.Transaction, accepted bool, reason string) TransactionResult
- func (res TransactionResult) GetStatus() (bool, string)
- func (res TransactionResult) GetTransaction() txn.Transaction
- func (res TransactionResult) Serialize(ctx serde.Context) ([]byte, error)
- type TransactionResultFactory
Examples ¶
Functions ¶
func RegisterResultFormat ¶
func RegisterResultFormat(f serde.Format, e serde.FormatEngine)
RegisterResultFormat registers the engine for the provided format.
func RegisterTransactionResultFormat ¶
func RegisterTransactionResultFormat(f serde.Format, e serde.FormatEngine)
RegisterTransactionResultFormat registers the engine for the provided format.
Types ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is the result of a standard validation.
- implements validation.Result
func NewResult ¶
func NewResult(results []TransactionResult) Result
NewResult creates a new result from a list of transaction results.
func (Result) Fingerprint ¶
Fingerprint implements serde.Fingerprinter. It writes a deterministic binary representation of the result.
func (Result) GetTransactionResults ¶
func (d Result) GetTransactionResults() []validation.TransactionResult
GetTransactionResults implements validation.Result. It returns the transaction results.
func (Result) Serialize ¶
Serialize implements serde.Message. It returns the serialized data of the result.
type ResultFactory ¶
type ResultFactory struct {
// contains filtered or unexported fields
}
ResultFactory is the factory to deserialize results.
- implements validation.ResultFactory
func NewResultFactory ¶
func NewResultFactory(f txn.Factory) ResultFactory
NewResultFactory creates a new result factory.
func (ResultFactory) Deserialize ¶
Deserialize implements serde.Factory. It populates the result if appropriate, otherwise it returns an error.
func (ResultFactory) ResultOf ¶
func (f ResultFactory) ResultOf(ctx serde.Context, data []byte) (validation.Result, error)
ResultOf implements validation.ResultFactory. It returns the result from the serialized data if appropriate, otherwise it returns an error.
type ResultKey ¶
type ResultKey struct{}
ResultKey is the key of the transaction result factory.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is a standard validation service that will process the batch and update the snapshot accordingly.
- implements validation.Service
func NewService ¶
NewService creates a new validation service.
func (Service) Accept ¶
func (s Service) Accept(store store.Readable, tx txn.Transaction, leeway validation.Leeway) error
Accept implements validation.Service. It returns nil if the transaction would be accepted by the service given some leeway and a snapshot of the storage.
func (Service) GetFactory ¶
func (s Service) GetFactory() validation.ResultFactory
GetFactory implements validation.Service. It returns the result factory.
func (Service) GetNonce ¶
GetNonce implements validation.Service. It reads the latest nonce in the
storage for the given identity and returns the next valid nonce.
Code:
Output:Example¶
{
exec := native.NewExecution()
srvc := NewService(exec, signed.NewTransactionFactory())
signer := bls.NewSigner()
store := newStore()
nonce, err := srvc.GetNonce(store, signer.GetPublicKey())
if err != nil {
panic("cannot get nonce: " + err.Error())
}
fmt.Println(nonce)
// Output: 0
}
0
func (Service) Validate ¶
func (s Service) Validate(store store.Snapshot, txs []txn.Transaction) (validation.Result, error)
Validate implements validation.Service. It processes the list of transactions
while updating the snapshot then returns a bundle of the transaction results.
Code:
Output:Example¶
{
exec := native.NewExecution()
exec.Set("example", exampleContract{})
srvc := NewService(exec, signed.NewTransactionFactory())
signer := bls.NewSigner()
arg := signed.WithArg(native.ContractArg, []byte("example"))
txA, err := signed.NewTransaction(0, signer.GetPublicKey(), arg)
if err != nil {
panic("cannot create transaction A: " + err.Error())
}
txB, err := signed.NewTransaction(1, signer.GetPublicKey(), arg)
if err != nil {
panic("cannot create transaction B: " + err.Error())
}
txC, err := signed.NewTransaction(3, signer.GetPublicKey(), arg)
if err != nil {
panic("cannot create transaction C: " + err.Error())
}
store := newStore()
res, err := srvc.Validate(store, []txn.Transaction{txA, txB, txC})
if err != nil {
panic("validation failed: " + err.Error())
}
for _, txRes := range res.GetTransactionResults() {
accepted, reason := txRes.GetStatus()
if accepted {
fmt.Println("accepted")
} else {
fmt.Println("refused", reason)
}
}
// Output: accepted
// accepted
// refused nonce is invalid, expected 2, got 3
}
accepted
accepted
refused nonce is invalid, expected 2, got 3
type TransactionKey ¶
type TransactionKey struct{}
TransactionKey is the key of the transaction factory.
type TransactionResult ¶
type TransactionResult struct {
// contains filtered or unexported fields
}
TransactionResult is the result of a transaction processing. It contains the transaction and its state of success.
- implements validation.TransactionResult
func NewTransactionResult ¶
func NewTransactionResult(tx txn.Transaction, accepted bool, reason string) TransactionResult
NewTransactionResult creates a new transaction result for the provided transaction.
func (TransactionResult) GetStatus ¶
func (res TransactionResult) GetStatus() (bool, string)
GetStatus implements validation.TransactionResult. It returns true if the transaction has been accepted, otherwise false with the reason.
func (TransactionResult) GetTransaction ¶
func (res TransactionResult) GetTransaction() txn.Transaction
GetTransaction implements validation.TransactionResult. It returns the transaction associated to the result.
func (TransactionResult) Serialize ¶
func (res TransactionResult) Serialize(ctx serde.Context) ([]byte, error)
Serialize implements serde.Message. It returns the transaction result serialized.
type TransactionResultFactory ¶
type TransactionResultFactory struct {
// contains filtered or unexported fields
}
TransactionResultFactory is the factory to deserialize transaction results.
- implements serde.Factory
func NewTransactionResultFactory ¶
func NewTransactionResultFactory(f txn.Factory) TransactionResultFactory
NewTransactionResultFactory creates a new transaction result factory.
func (TransactionResultFactory) Deserialize ¶
func (f TransactionResultFactory) Deserialize(ctx serde.Context, data []byte) (serde.Message, error)
Deserialize implements serde.Factory. It populates the transaction result if appropriate, otherwise it returns an error.
Source Files ¶
result.go simple.go
Directories ¶
Path | Synopsis |
---|---|
core/validation/simple/json |
- Version
- v0.1.0 (latest)
- Published
- Apr 10, 2024
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 1 month ago –
Tools for package owners.