Skip to content

Commit

Permalink
Merge pull request #6 from datatrails/dev/robin/8859-simplehash-schem…
Browse files Browse the repository at this point in the history
…a-v3

Dev/robin/8859 simplehash schema v3
  • Loading branch information
robinbryce authored Jan 24, 2024
2 parents 46e2be8 + bebf42d commit 4a38691
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 153 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: ci
on:
push:
release:
types: [published]

jobs:
test:
name: Code quality and unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup go-task
uses: arduino/setup-task@v1
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
token: ${{ secrets.GITHUB_TOKEN }}
cache-dependency-path: |
datatrails-common-api/go.sum
- name: Install Go quality tools
run: |
go install golang.org/x/tools/cmd/goimports@latest
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "${PWD}/bin" v1.55.2
echo "${PWD}/bin" >> $GITHUB_PATH
- name: Code Quality
run: |
# Note: it is by design that we don't use the builder
task codeqa
- name: Test
run: |
# Note: it is by design that we don't use the builder
task test
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ includes:

tasks:

qa-basic:
codeqa:
desc: |
format the source and run all the quality checks
(Does not run unit tests)
Expand All @@ -39,4 +39,4 @@ tasks:
test:
desc: run the tests
cmds:
- task: codeqa:unit-tests
- task: codeqa:test
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/datatrails/go-datatrails-simplehash
go 1.21

require (
github.com/datatrails/go-datatrails-common-api-gen v0.2.0
github.com/datatrails/go-datatrails-common-api-gen v0.3.7
github.com/golang/protobuf v1.5.3
github.com/zeebo/bencode v1.0.0
google.golang.org/protobuf v1.31.0
Expand All @@ -15,7 +15,7 @@ require (
github.com/cilium/ebpf v0.12.3 // indirect
github.com/containerd/cgroups/v3 v3.0.2 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/datatrails/go-datatrails-common v0.9.0 // indirect
github.com/datatrails/go-datatrails-common v0.10.2 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
Expand All @@ -30,10 +30,10 @@ require (
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ github.com/containerd/cgroups/v3 v3.0.2 h1:f5WFqIVSgo5IZmtTT3qVBo6TzI1ON6sycSBKk
github.com/containerd/cgroups/v3 v3.0.2/go.mod h1:JUgITrzdFqp42uI2ryGA+ge0ap/nxzYgkGmIcetmErE=
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/datatrails/go-datatrails-common v0.9.0 h1:kdDTPfi+GOP6Q0C2XRET33MfNxPNYw3QXcIstFnxqeE=
github.com/datatrails/go-datatrails-common v0.9.0/go.mod h1:LsPfbYoTEEdPnANm0+seLRX2OO7c5yF7tZw8499prAM=
github.com/datatrails/go-datatrails-common-api-gen v0.2.0 h1:2pVpQSpStPsaUCPM3vNqEtNJu4VZeXYrZTvZUlPeDwk=
github.com/datatrails/go-datatrails-common-api-gen v0.2.0/go.mod h1:SiE9nDEhmD+B7tAbniH0bTl0vI4cTfnFPa5aUPq3/kE=
github.com/datatrails/go-datatrails-common v0.10.2 h1:OnvayMGpeia3wHMrj26njuQlpsOvGjYOsHChvj6ErtQ=
github.com/datatrails/go-datatrails-common v0.10.2/go.mod h1:LsPfbYoTEEdPnANm0+seLRX2OO7c5yF7tZw8499prAM=
github.com/datatrails/go-datatrails-common-api-gen v0.3.7 h1:TEbf6HwjXsiKUZrYgStKroJz+O9QA1wIRZPIKPd1Crc=
github.com/datatrails/go-datatrails-common-api-gen v0.3.7/go.mod h1:qGRrvhR3DCw90EYOQLtWvvNtUZbcmAKYAkgr2/yfOKI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -68,17 +68,17 @@ golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUU
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c h1:3kC/TjQ+xzIblQv39bCOyRk8fbEeJcDHwbyxPUU2BpA=
golang.org/x/sys v0.14.1-0.20231108175955-e4099bfacb8c/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 h1:wpZ8pe2x1Q3f2KyT5f8oP/fa9rHAKgFPr/HZdNuS+PQ=
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:J7XzRzVy1+IPwWHZUzoD0IccYZIrXILAQpc+Qy9CMhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 h1:JpwMPBpFN3uKhdaekDpiNlImDdkUAyiJ6ez/uxGaUSo=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:0xJLfVdJqpAPl8tDg1ujOCGzx6LFLttXT5NhllGOXY4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4 h1:W12Pwm4urIbRdGhMEg2NM9O3TWKjNcxQhs46V0ypf/k=
google.golang.org/genproto v0.0.0-20231127180814-3a041ad873d4/go.mod h1:5RBcpGRxr25RbDzY5w+dmaqpSEvl8Gwl1x2CICf60ic=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 h1:ZcOkrmX74HbKFYnpPY8Qsw93fC29TbJXspYKaBkSXDQ=
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4/go.mod h1:k2dtGpRrbsSyKcNPKKI5sstZkrNCZwpU/ns96JoHbGg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f h1:ultW7fxlIvee4HYrtnaRPon9HpEgFk5zYpmfMgtKB5I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f/go.mod h1:L9KNLi232K1/xB6f7AlSX692koaRnKaWSR0stBki0Yc=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
69 changes: 69 additions & 0 deletions simplehash/hasher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package simplehash

import (
"crypto/sha256"
"hash"

v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/datatrails/go-datatrails-common-api-gen/marshalers/simpleoneof"
)

type Hasher struct {
hasher hash.Hash
marshaler *simpleoneof.Marshaler
}

func NewHasher() Hasher {
h := Hasher{
hasher: sha256.New(),
marshaler: NewEventMarshaler(),
}
return h
}

func (h *Hasher) Sum(b []byte) []byte { return h.hasher.Sum(b) }

// Reset resets the hasher state
// This is only useful in combination with WithAccumulate
func (h *Hasher) Reset() { h.hasher.Reset() }

// NewEventMarshaler creates a flat marshaler to transform events to api format.
//
// otherwise attributes look like this: {"foo":{"str_val": "bar"}} instead of {"foo": "bar"}
// this mimics the public list events api response, so minimises changes to the
// public api response, to reproduce the anchor
func NewEventMarshaler() *simpleoneof.Marshaler {
return v2assets.NewFlatMarshalerForEvents()
}

func (h *Hasher) applyEventOptions(o HashOptions, event *v2assets.EventResponse) {
if o.publicFromPermissioned {
PublicFromPermissionedEvent(event)
}

// force the commited time in the hash. only useful to the service that is
// actually doing the committing. public consumers only ever see confirmed
// events with the timestamp already in place.
if o.committed != nil {
event.TimestampCommitted = o.committed
}
}

func (h *Hasher) applyHashingOptions(o HashOptions) {

// By default, one hash at at time with a reset.
if !o.accumulateHash {
h.hasher.Reset()
}

// If the prefix is provided it must be first.
if len(o.prefix) != 0 {
h.hasher.Write(o.prefix)
}

// If the idcommitted is provided, add it to the hash immediately before the
// event data.
if o.idcommitted != nil {
h.hasher.Write(o.idcommitted)
}
}
12 changes: 5 additions & 7 deletions simplehash/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simplehash

import (
"encoding/binary"
"errors"

"google.golang.org/protobuf/types/known/timestamppb"
)
Expand All @@ -12,14 +13,17 @@ import (
type HashOptions struct {
accumulateHash bool
publicFromPermissioned bool
asConfirmed bool
prefix []byte
committed *timestamppb.Timestamp
idcommitted []byte
}

type HashOption func(*HashOptions)

var (
ErrInvalidOption = errors.New("option not supported by this method")
)

// WithIDCommitted includes the snowflakeid unique commitment timestamp in the hash
// idcommitted is never (legitimately) zero
func WithIDCommitted(idcommitted uint64) HashOption {
Expand Down Expand Up @@ -56,9 +60,3 @@ func WithPublicFromPermissioned() HashOption {
o.publicFromPermissioned = true
}
}

func WithAsConfirmed() HashOption {
return func(o *HashOptions) {
o.asConfirmed = true
}
}
10 changes: 10 additions & 0 deletions simplehash/publicevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package simplehash

import v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"

// PublicFromPermissionedEvent translates the permissioned event and asset identities to
// their public counter parts.
func PublicFromPermissionedEvent(event *v2assets.EventResponse) {
event.Identity = v2assets.PublicIdentityFromPermissioned(event.Identity)
event.AssetIdentity = v2assets.PublicIdentityFromPermissioned(event.AssetIdentity)
}
Loading

0 comments on commit 4a38691

Please sign in to comment.