Skip to content

Commit 0c1066d

Browse files
authored
api: make trees.packed not null (#60)
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.
1 parent b23545d commit 0c1066d

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

api/migrations/migrations.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,19 @@ var Migrations = []migrate.Migration{
7070
{
7171
Name: "2022-08-22.1.add-inserted-at.sql",
7272
SQL: `
73-
ALTER TABLE trees
73+
ALTER TABLE trees
7474
ADD COLUMN "inserted_at" timestamptz NOT NULL DEFAULT now();
7575
`,
7676
},
77+
{
78+
Name: "2022-08-30.0.packed-bool.sql",
79+
SQL: `
80+
UPDATE trees SET packed = true
81+
WHERE packed IS NULL;
82+
83+
ALTER TABLE trees
84+
ALTER COLUMN packed
85+
SET NOT NULL;
86+
`,
87+
},
7788
}

api/tree.go

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"database/sql"
54
"encoding/json"
65
"errors"
76
"net/http"
@@ -74,32 +73,6 @@ func addrPacked(leaf []byte, ltd []string) common.Address {
7473
return common.Address{}
7574
}
7675

77-
type jsonNullBool struct {
78-
sql.NullBool
79-
}
80-
81-
func (jnb *jsonNullBool) UnmarshalJSON(d []byte) error {
82-
var b *bool
83-
if err := json.Unmarshal(d, &b); err != nil {
84-
return err
85-
}
86-
if b == nil {
87-
jnb.Valid = false
88-
return nil
89-
}
90-
91-
jnb.Valid = true
92-
jnb.Bool = *b
93-
return nil
94-
}
95-
96-
func (jnb jsonNullBool) MarshalJSON() ([]byte, error) {
97-
if jnb.Valid {
98-
return json.Marshal(jnb.Bool)
99-
}
100-
return json.Marshal(nil)
101-
}
102-
10376
func encodeProof(p [][]byte) []string {
10477
var res []string
10578
for i := range p {
@@ -111,7 +84,7 @@ func encodeProof(p [][]byte) []string {
11184
type createTreeReq struct {
11285
Leaves []hexutil.Bytes `json:"unhashedLeaves"`
11386
Ltd []string `json:"leafTypeDescriptor"`
114-
Packed jsonNullBool `json:"packedEncoding"`
87+
Packed bool `json:"packedEncoding"`
11588
}
11689

11790
type createTreeResp struct {
@@ -158,7 +131,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
158131
}
159132
proofs = append(proofs, proofItem{
160133
Leaf: hexutil.Encode(l),
161-
Addr: leaf2Addr(l, req.Ltd, req.Packed.Bool).Hex(),
134+
Addr: leaf2Addr(l, req.Ltd, req.Packed).Hex(),
162135
Proof: encodeProof(pf),
163136
})
164137
}
@@ -177,7 +150,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
177150
tree.Root(),
178151
req.Leaves,
179152
req.Ltd,
180-
req.Packed.NullBool,
153+
req.Packed,
181154
proofs,
182155
)
183156
if err != nil {
@@ -192,7 +165,7 @@ type getTreeResp struct {
192165
UnhashedLeaves []hexutil.Bytes `json:"unhashedLeaves"`
193166
LeafCount int `json:"leafCount"`
194167
Ltd []string `json:"leafTypeDescriptor"`
195-
Packed jsonNullBool `json:"packedEncoding"`
168+
Packed bool `json:"packedEncoding"`
196169
}
197170

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

0 commit comments

Comments
 (0)