Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
FAB-11081 check marshaled json for eth RPC fields
Browse files Browse the repository at this point in the history
 - explicitly list the fields and check for existence of exactly that list

Change-Id: I2ef4ed0c9ea97b669aff2c7440e13e5a3701326c
Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>
  • Loading branch information
MHBauer committed Dec 19, 2018
1 parent 6bb8493 commit 719a4f6
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions fabproxy/ethservice_types_roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package fabproxy_test

import (
"encoding/json"
"reflect"

"github.com/hyperledger/fabric-chaincode-evm/fabproxy"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ethereum json rpc struct fields", func() {
By("that match case exactly, in camelCase starting with a lowercase letter")
assertTypeMarshalsJSONFields := func(fieldNames []string, itype interface{}) {
b, err := json.Marshal(itype)
Expect(err).ToNot(HaveOccurred())
m := make(map[string]interface{})
err = json.Unmarshal(b, &m)
Expect(err).ToNot(HaveOccurred())
Expect(m).To(HaveLen(len(fieldNames)), "did you add a field to this type? %q", reflect.TypeOf(itype))
for _, fieldName := range fieldNames {
_, ok := m[fieldName]
Expect(ok).To(BeTrue(), "couldn't find an expected field %q in this type", fieldName)
}
}

It("for TxReceipt with the proper cases", func() {
fieldNames := []string{"transactionHash", "transactionIndex",
"blockHash", "blockNumber", "contractAddress", "gasUsed",
"cumulativeGasUsed", "to", "status", "logs"}
assertTypeMarshalsJSONFields(fieldNames, fabproxy.TxReceipt{})
})
It("for Log subobjects in TxReceipt with the proper cases", func() {
fieldNames := []string{"address", "topics", "data", "blockNumber",
"transactionHash", "transactionIndex", "blockHash", "logIndex"}
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Log{})
})
It("for Transaction with the proper cases", func() {
fieldNames := []string{"blockHash", "blockNumber", "to", "input", "transactionIndex", "hash"}
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Transaction{})
})
It("for Block with the proper cases", func() {
fieldNames := []string{"number", "hash", "parentHash", "transactions"}
assertTypeMarshalsJSONFields(fieldNames, fabproxy.Block{})
})
})

0 comments on commit 719a4f6

Please # to comment.