Skip to content

Commit

Permalink
Make certain fields in the Transaction object nullable (umbracle#119)
Browse files Browse the repository at this point in the history
* Make certain fields of the txn object nullable

* Update gitignore

* Remove leftover files

* Remove the dependency on pointers, use default values

* Swap invalid values for test cases
  • Loading branch information
zivkovicmilos authored Dec 6, 2021
1 parent 2291ba9 commit e590eac
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bin/
pkg/
pkg/

.idea
4 changes: 2 additions & 2 deletions structs_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestJSONEncoding(t *testing.T) {
"v":"0x25",
"r":"{{.Hash1}}",
"s":"{{.Hash1}}",
"blockHash": "{{.Hash0}}",
"blockHash": "{{.Hash1}}",
"blockNumber": "0x0",
"transactionIndex": "0x0"
}`,
Expand All @@ -92,7 +92,7 @@ func TestJSONEncoding(t *testing.T) {
"v":"0x25",
"r":"{{.Hash1}}",
"s":"{{.Hash1}}",
"blockHash": "{{.Hash0}}",
"blockHash": "{{.Hash1}}",
"blockNumber": "0x0",
"transactionIndex": "0x0"
}`,
Expand Down
14 changes: 11 additions & 3 deletions structs_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,17 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
o.Set("r", a.NewString("0x"+hex.EncodeToString(t.R)))
o.Set("s", a.NewString("0x"+hex.EncodeToString(t.R)))

o.Set("blockHash", a.NewString(t.BlockHash.String()))
o.Set("blockNumber", a.NewString(fmt.Sprintf("0x%x", t.BlockNumber)))
o.Set("transactionIndex", a.NewString(fmt.Sprintf("0x%x", t.TxnIndex)))
if t.BlockHash == ZeroHash {
// The transaction is a pending transaction
o.Set("blockHash", a.NewNull())
o.Set("blockNumber", a.NewNull())
o.Set("transactionIndex", a.NewNull())
} else {
// The transaction has valid metadata fields, fill them
o.Set("blockHash", a.NewString(t.BlockHash.String()))
o.Set("blockNumber", a.NewString(fmt.Sprintf("0x%x", t.BlockNumber)))
o.Set("transactionIndex", a.NewString(fmt.Sprintf("0x%x", t.TxnIndex)))
}

res := o.MarshalTo(nil)
defaultArena.Put(a)
Expand Down
42 changes: 34 additions & 8 deletions structs_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ func (t *Transaction) UnmarshalJSON(buf []byte) error {
return t.unmarshalJSON(v)
}

// isKeySet is a helper function for checking if a key has any value != nil,
// or if it's been set at all
func isKeySet(v *fastjson.Value, key string) bool {
value := v.Get(key)
return value != nil && value.Type() != fastjson.TypeNull
}

func (t *Transaction) unmarshalJSON(v *fastjson.Value) error {
var err error
if err := decodeHash(&t.Hash, v, "hash"); err != nil {
Expand Down Expand Up @@ -158,15 +165,28 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error {
return err
}

if err = decodeHash(&t.BlockHash, v, "blockHash"); err != nil {
return err
}
if t.BlockNumber, err = decodeUint(v, "blockNumber"); err != nil {
return err
}
if t.TxnIndex, err = decodeUint(v, "transactionIndex"); err != nil {
return err
// Check if the block hash field is set
// If it's not -> the transaction is a pending txn, so these fields should be omitted
// If it is -> the transaction is a sealed txn, so these fields should be included
if isKeySet(v, "blockHash") {
// The transaction is not a pending transaction, read data

// Grab the block hash
if err = decodeHash(&t.BlockHash, v, "blockHash"); err != nil {
return err
}

// Grab the block number
if t.BlockNumber, err = decodeUint(v, "blockNumber"); err != nil {
return err
}

// Grab the transaction index
if t.TxnIndex, err = decodeUint(v, "transactionIndex"); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -356,6 +376,12 @@ func decodeHash(h *Hash, v *fastjson.Value, key string) error {
if len(b) == 0 {
return fmt.Errorf("field '%s' not found", key)
}

// Make sure the memory location is initialized
if h == nil {
h = &Hash{}
}

h.UnmarshalText(b)
return nil
}
Expand Down

0 comments on commit e590eac

Please sign in to comment.