github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/blockchain/plugin.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package blockchain
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kaleido-io/firefly/internal/config"
    23  	"github.com/kaleido-io/firefly/pkg/fftypes"
    24  )
    25  
    26  // Plugin is the interface implemented by each blockchain plugin
    27  type Plugin interface {
    28  	fftypes.Named
    29  
    30  	// InitPrefix initializes the set of configuration options that are valid, with defaults. Called on all plugins.
    31  	InitPrefix(prefix config.Prefix)
    32  
    33  	// Init initializes the plugin, with configuration
    34  	// Returns the supported featureset of the interface
    35  	Init(ctx context.Context, prefix config.Prefix, callbacks Callbacks) error
    36  
    37  	// Blockchain interface must not deliver any events until start is called
    38  	Start() error
    39  
    40  	// Capabilities returns capabilities - not called until after Init
    41  	Capabilities() *Capabilities
    42  
    43  	// VerifyIdentitySyntax verifies that the supplied identity string is valid syntax according to the protocol.
    44  	// Can apply transformations to the supplied signing identity (only), such as lower case
    45  	VerifyIdentitySyntax(ctx context.Context, identity *fftypes.Identity) error
    46  
    47  	// SubmitBatchPin sequences a batch of message globally to all viewers of a given ledger
    48  	// The returned tracking ID will be used to correlate with any subsequent transaction tracking updates
    49  	SubmitBatchPin(ctx context.Context, ledgerID *fftypes.UUID, identity *fftypes.Identity, batch *BatchPin) (txTrackingID string, err error)
    50  }
    51  
    52  // Callbacks is the interface provided to the blockchain plugin, to allow it to pass events back to firefly.
    53  //
    54  // Events must be delivered sequentially, such that event 2 is not delivered until the callback invoked for event 1
    55  // has completed. However, it does not matter if these events are workload balance between the firefly core
    56  // cluster instances of the node.
    57  type Callbacks interface {
    58  	// TxSubmissionUpdate notifies firefly of an update to a transaction. Only success/failure and errorMessage (for errors) are modeled.
    59  	// additionalInfo can be used to add opaque protocol specific JSON from the plugin (protocol transaction ID etc.)
    60  	// Note this is an optional hook information, and stored separately to the confirmation of the actual event that was being submitted/sequenced.
    61  	// Only the party submitting the transaction will see this data.
    62  	//
    63  	// Error should will only be returned in shutdown scenarios
    64  	TxSubmissionUpdate(txTrackingID string, txState TransactionStatus, protocolTxID, errorMessage string, additionalInfo fftypes.JSONObject) error
    65  
    66  	// BatchPinComplete notifies on the arrival of a sequenced batch of messages, which might have been
    67  	// submitted by us, or by any other authorized party in the network.
    68  	// Will be combined with he index within the batch, to allocate a sequence to each message in the batch.
    69  	// For example a padded block number, followed by a padded transaction index within that block.
    70  	// additionalInfo can be used to add opaque protocol specific JSON from the plugin (block numbers etc.)
    71  	//
    72  	// Error should will only be returned in shutdown scenarios
    73  	BatchPinComplete(batch *BatchPin, signingIdentity string, protocolTxID string, additionalInfo fftypes.JSONObject) error
    74  }
    75  
    76  // Capabilities the supported featureset of the blockchain
    77  // interface implemented by the plugin, with the specified config
    78  type Capabilities struct {
    79  	// GlobalSequencer means submitting an ordered piece of data visible to all
    80  	// participants of the network (requires an all-participant chain)
    81  	GlobalSequencer bool
    82  }
    83  
    84  // TransactionStatus is the only architecturally significant thing that Firefly tracks on blockchain transactions.
    85  // All other data is consider protocol specific, and hence stored as opaque data.
    86  type TransactionStatus = fftypes.OpStatus
    87  
    88  // BatchPin is the set of data pinned to the blockchain for a batch - whether it's private or broadcast.
    89  type BatchPin struct {
    90  
    91  	// Namespace goes in the clear on the chain
    92  	Namespace string
    93  
    94  	// TransactionID is the firefly transaction ID allocated before transaction submission for correlation with events (it's a UUID so no leakage)
    95  	TransactionID *fftypes.UUID
    96  
    97  	// BatchID is the id of the batch - not strictly required, but writing this in plain text to the blockchain makes for easy human correlation on-chain/off-chain (it's a UUID so no leakage)
    98  	BatchID *fftypes.UUID
    99  
   100  	// BatchHash is the SHA256 hash of the batch
   101  	BatchHash *fftypes.Bytes32
   102  
   103  	// BatchPaylodRef is a 32 byte fixed length binary value that can be passed to the storage interface to retrieve the payload. Nil for private messages
   104  	BatchPaylodRef *fftypes.Bytes32
   105  
   106  	// Contexts is an array of hashes that allow the FireFly runtimes to identify whether one of the messgages in
   107  	// that batch is the next message for a sequence that involves that node. If so that means the FireFly runtime must
   108  	//
   109  	// - The primary subject of each hash is a "context"
   110  	// - The context is a function of:
   111  	//   - A single topic declared in a message - topics are just a string representing a sequence of events that must be processed in order
   112  	//   - A ledger - everone with access to this ledger will see these hashes (Fabric channel, Ethereum chain, EEA privacy group, Corda linear ID)
   113  	//   - A restricted group - if the mesage is private, these are the nodes that are elible to receive a copy of the private message+data
   114  	// - Each message might choose to include multiple topics (and hence attach to multiple contexts)
   115  	//   - This allows multiple contexts to merge - very important in multi-party data matching scenarios
   116  	// - A batch contains many messages, each with one or more topics
   117  	//   - The array of sequence hashes will cover every unique context within the batch
   118  	// - For private group communications, the hash is augmented as follow:
   119  	//   - The hashes are salted with a UUID that is only passed off chain (the UUID of the Group).
   120  	//   - The hashes are made unique to the sender
   121  	//   - The hashes contain a sender specific nonce that is a monotomically increasing number
   122  	//     for batches sent by that sender, within the context (maintined by the sender FireFly node)
   123  	Contexts []*fftypes.Bytes32
   124  }