-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpc/server: add notification subscription
Note that the protocol differs a bit from #895 in its notifications format, to avoid additional server-side processing we're omitting some metadata like: * block size and confirmations * transaction fees, confirmations, block hash and timestamp * application execution doesn't have ScriptHash populated Some block fields may also differ in encoding compared to `getblock` results (like nonce field). I think these differences are unnoticieable for most use cases, so we can leave them as is, but it can be changed in the future.
- Loading branch information
1 parent
bbeaf53
commit 81e3e3b
Showing
7 changed files
with
688 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package response | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ( | ||
// EventID represents an event type happening on the chain. | ||
EventID byte | ||
) | ||
|
||
const ( | ||
// InvalidEventID is an invalid event id that is the default value of | ||
// EventID. It's only used as an initial value similar to nil. | ||
InvalidEventID EventID = iota | ||
// BlockEventID is a `block_added` event. | ||
BlockEventID | ||
// TransactionEventID corresponds to `transaction_added` event. | ||
TransactionEventID | ||
// NotificationEventID represents `notification_from_execution` events. | ||
NotificationEventID | ||
// ExecutionEventID is used for `transaction_executed` events. | ||
ExecutionEventID | ||
) | ||
|
||
// String is a good old Stringer implementation. | ||
func (e EventID) String() string { | ||
switch e { | ||
case BlockEventID: | ||
return "block_added" | ||
case TransactionEventID: | ||
return "transaction_added" | ||
case NotificationEventID: | ||
return "notification_from_execution" | ||
case ExecutionEventID: | ||
return "transaction_executed" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
|
||
// GetEventIDFromString converts input string into an EventID if it's possible. | ||
func GetEventIDFromString(s string) (EventID, error) { | ||
switch s { | ||
case "block_added": | ||
return BlockEventID, nil | ||
case "transaction_added": | ||
return TransactionEventID, nil | ||
case "notification_from_execution": | ||
return NotificationEventID, nil | ||
case "transaction_executed": | ||
return ExecutionEventID, nil | ||
default: | ||
return 255, errors.New("invalid stream name") | ||
} | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler interface. | ||
func (e EventID) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(e.String()) | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler interface. | ||
func (e *EventID) UnmarshalJSON(b []byte) error { | ||
var s string | ||
|
||
err := json.Unmarshal(b, &s) | ||
if err != nil { | ||
return err | ||
} | ||
id, err := GetEventIDFromString(s) | ||
if err != nil { | ||
return err | ||
} | ||
*e = id | ||
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
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
Oops, something went wrong.