Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4465 from oasisprotocol/ptrus/feature/typed-attri…
…butes go/consensus: use typed attributes in all services
- Loading branch information
Showing
36 changed files
with
425 additions
and
379 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,4 @@ | ||
Use typed attributes in all consensus services | ||
|
||
Beacon, keymanager, registry, roothash and scheduler events are updated to use | ||
the typed event attribute API. |
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,7 @@ | ||
consensus: base64 encode all tendermint event values | ||
|
||
Tendermint 0.35 requires all events to be actual strings and doesn't support | ||
binary data anymore. It also requires no dashes in attribute keys. | ||
|
||
Although oasis-core still uses Tendermint 0.34, events are updated now to | ||
avoid breaking changes later. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package events | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common/cbor" | ||
) | ||
|
||
// TypedAttribute is an interface implemented by types which can be transparently used as event | ||
// attributes with CBOR-marshalled value. | ||
type TypedAttribute interface { | ||
// EventKind returns a string representation of this event's kind. | ||
EventKind() string | ||
} | ||
|
||
// CustomTypedAttribute is an interface implemented by types which can be transparently used as event | ||
// attributes with custom value encoding. | ||
type CustomTypedAttribute interface { | ||
TypedAttribute | ||
|
||
// EventValue returns a string representation of this events value. | ||
EventValue() string | ||
|
||
// DecodeValue decodes the value encoded vy the EventValue. | ||
DecodeValue(value string) error | ||
} | ||
|
||
// IsAttributeKind checks whether the given attribute key corresponds to the passed typed attribute. | ||
func IsAttributeKind(key []byte, kind TypedAttribute) bool { | ||
return bytes.Equal(key, []byte(kind.EventKind())) | ||
} | ||
|
||
// DecodeValue decodes the attribute event value. | ||
func DecodeValue(value string, ev TypedAttribute) error { | ||
// Use custom decode if this is a custom typed attribute. | ||
if cta, ok := ev.(CustomTypedAttribute); ok { | ||
return cta.DecodeValue(value) | ||
} | ||
// Otherwise assume default base64 encoded CBOR marshalled value. | ||
decoded, err := base64.StdEncoding.DecodeString(value) | ||
if err != nil { | ||
return fmt.Errorf("invalid value: %w", err) | ||
} | ||
return cbor.Unmarshal(decoded, ev) | ||
} | ||
|
||
// EncodeValue encodes the attribute event value. | ||
func EncodeValue(ev TypedAttribute) []byte { | ||
// Use custom decode if this is a custom typed attribute. | ||
if cta, ok := ev.(CustomTypedAttribute); ok { | ||
return []byte(cta.EventValue()) | ||
} | ||
// Otherwise default to base64 encoded CBOR marshalled value. | ||
return []byte(base64.StdEncoding.EncodeToString(cbor.Marshal(ev))) | ||
} |
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
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
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
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
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.