Repository navigation
feat(cbor): a deterministic CBOR codec (#785) - #789
Merged
Merged
Conversation
Smithy RPC v2 CBOR is one of the three protocols CloudWatch's model declares,
and substrate had no CBOR encoder or decoder at all — only a hardcoded
[]byte{0xa0} in GetMetricData, which left the other nine operations answering
XML to a client that had asked for CBOR.
This adds the codec on its own, wired to nothing, so it can be reviewed against
RFC 8949 and Smithy's protocol tests rather than alongside a plugin rewrite.
The writer has exactly one output for any input, which is what a replayable
event log needs: definite length with minimal arguments, smallest-representation
integers, doubles always 0xfb, timestamps always tag 1 over a float64, and
member order from an explicit ordered type rather than Go's randomized map
iteration. The reader is liberal where the specification requires it —
indefinite length, non-minimal arguments, a double as float16/32/64 or an
integer, 0xf7 as null, an unrecognized tag decoding to its payload — and
refuses reserved additional information, a declared count larger than the
input, and nesting past 64 levels before either can size an allocation or
recurse.
Tests assert wire bytes rather than round trips, since a self-consistently
wrong codec passes every round trip. One table holds smithy-go v1.28.1's own
encoder output, which corrected a premise: it has no indefinite-length path, so
substrate's definite-length choice matches what aws-sdk-go-v2 puts on the wire
instead of diverging from it, and the two agree on timestamps byte for byte.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The first of three PRs teaching CloudWatch to speak the protocols its clients actually
speak. This one adds
emulator/cbor.goand its tests and wires them to nothing, sothe codec can be reviewed against RFC 8949 and Smithy's protocol tests on its own.
Why substrate has its own codec
Substrate had no CBOR at all — the only CBOR in the repo was a hardcoded
[]byte{0xa0}in
getMetricData, so the other nine CloudWatch operations answered XML to a client thathad asked for CBOR.
aws-sdk-go-v2reportsdeserialization failed, expected map for struct, got major type 1: the<of<GetMetricDataResponseread as a CBOR head.Rather than depend on
github.com/aws/smithy-go: the root module has no AWS SDKdependency, and adding one to emit a response format is a large surface for a small need.
More to the point, substrate's premise is that the same inputs produce the same bytes. A
library free to choose length form, member order or float width per call makes a recorded
response non-reproducible, and none offers a contract that says otherwise.
Writer: one output per input
Definite length with minimal arguments, smallest-representation integers, doubles always
0xfb, timestamps always tag 1 over a float64 at millisecond resolution, and memberorder from an explicit ordered type.
map[string]anyis refused — sorting it wouldgive stable bytes in an order no model declares, and accepting it at all makes the ordered
type optional.
Reader: liberal exactly where the spec requires
Indefinite-length maps, arrays and strings; non-minimal length arguments (the AWS
query-compatible vectors encode a two-entry map as
b9 00 02); a double arriving asfloat16, float32, float64 or an integer; a tag 1 payload in either numeric form;
0xf7as null; and an unrecognized tag decoding to its payload, so an unmodeled member isconsumed structurally rather than failing the request.
Refused: reserved additional information 28–30, a declared element count larger than the
input, and nesting past 64 levels — a body is attacker-controlled, and
9b ffffffffffffffffis nine bytes asking for 2⁶⁴ elements.Tests assert wire bytes, not round trips
A codec that is self-consistently wrong passes every round trip and fails against every
real client. 115 subtests over RFC 8949 Appendix A and Smithy's normative rpcv2Cbor
vectors.
One table holds the literal output of
smithy-gov1.28.1'sencoding/cbor.Encode— theencoder
aws-sdk-go-v2actually uses — so agreement with the peer is recorded rather thaninferred. Every one of substrate's own encodings was also fed back through
cbor.Decodeand accepted.
That cross-check corrected a premise:
smithy-go's encoder has no indefinite-lengthpath, so substrate's definite-length choice matches what the Go SDK puts on the wire
rather than diverging from it, and the two produce byte-identical timestamps
(
c1fb41cc37db380fbe77). The indefinite forms come from the test vectors and other SDKs'streaming writers — which is why the reader must take them and the writer need not produce
them. The doc comments and CHANGELOG say this rather than the reverse, which is what an
earlier draft claimed.
Verification
gofmt,go build ./...,go vet ./...,golangci-lint run ./...— all clean, 0 issuesmake teston go1.27.1 andGOTOOLCHAIN=go1.26.6— both exit 0, 0 failuresmake coverage— emulator 83.5% (was 83.4%), total 83.0% (was 82.9%)make docs-reference docs-reference-check docs-versions— clean, no driftgo vet -C test/e2e ./...andgo test -C test/e2e ./...— exit 0Part of #785; the plugin wiring follows in the next two PRs.