Skip to content

Commit

Permalink
api: make trees.packed not null (#60)
Browse files Browse the repository at this point in the history
Booleans with 3 possible values are needlessly confusing.

Unpacked data is padded to 32 bytes. Most of the time, callers will
provide a list of [20]byte values (ETH addresses) --which is packed.
  • Loading branch information
ryandotsmith authored Aug 30, 2022
1 parent b23545d commit 0c1066d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
13 changes: 12 additions & 1 deletion api/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ var Migrations = []migrate.Migration{
{
Name: "2022-08-22.1.add-inserted-at.sql",
SQL: `
ALTER TABLE trees
ALTER TABLE trees
ADD COLUMN "inserted_at" timestamptz NOT NULL DEFAULT now();
`,
},
{
Name: "2022-08-30.0.packed-bool.sql",
SQL: `
UPDATE trees SET packed = true
WHERE packed IS NULL;
ALTER TABLE trees
ALTER COLUMN packed
SET NOT NULL;
`,
},
}
35 changes: 4 additions & 31 deletions api/tree.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"database/sql"
"encoding/json"
"errors"
"net/http"
Expand Down Expand Up @@ -74,32 +73,6 @@ func addrPacked(leaf []byte, ltd []string) common.Address {
return common.Address{}
}

type jsonNullBool struct {
sql.NullBool
}

func (jnb *jsonNullBool) UnmarshalJSON(d []byte) error {
var b *bool
if err := json.Unmarshal(d, &b); err != nil {
return err
}
if b == nil {
jnb.Valid = false
return nil
}

jnb.Valid = true
jnb.Bool = *b
return nil
}

func (jnb jsonNullBool) MarshalJSON() ([]byte, error) {
if jnb.Valid {
return json.Marshal(jnb.Bool)
}
return json.Marshal(nil)
}

func encodeProof(p [][]byte) []string {
var res []string
for i := range p {
Expand All @@ -111,7 +84,7 @@ func encodeProof(p [][]byte) []string {
type createTreeReq struct {
Leaves []hexutil.Bytes `json:"unhashedLeaves"`
Ltd []string `json:"leafTypeDescriptor"`
Packed jsonNullBool `json:"packedEncoding"`
Packed bool `json:"packedEncoding"`
}

type createTreeResp struct {
Expand Down Expand Up @@ -158,7 +131,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
}
proofs = append(proofs, proofItem{
Leaf: hexutil.Encode(l),
Addr: leaf2Addr(l, req.Ltd, req.Packed.Bool).Hex(),
Addr: leaf2Addr(l, req.Ltd, req.Packed).Hex(),
Proof: encodeProof(pf),
})
}
Expand All @@ -177,7 +150,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
tree.Root(),
req.Leaves,
req.Ltd,
req.Packed.NullBool,
req.Packed,
proofs,
)
if err != nil {
Expand All @@ -192,7 +165,7 @@ type getTreeResp struct {
UnhashedLeaves []hexutil.Bytes `json:"unhashedLeaves"`
LeafCount int `json:"leafCount"`
Ltd []string `json:"leafTypeDescriptor"`
Packed jsonNullBool `json:"packedEncoding"`
Packed bool `json:"packedEncoding"`
}

func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
Expand Down

1 comment on commit 0c1066d

@vercel
Copy link

@vercel vercel bot commented on 0c1066d Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lanyard – ./

allowlist.context.wtf
lanyard-git-main.context.wtf
lanyard.context.wtf

Please sign in to comment.