This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom marshalling and unmarshalling for block Header (#642)
* Add custom marshalling and unmarshalling for block Header * Change header uint64 and []byte to string * Fix linter * Add test for encoding decoding uint64 * Add additional test * Use trimPrefix * Remove mustDecodeUint64 method * Inline return value * Implement RemoveHexPrefixFromByteArray method * Modify test for encodingUint64 * Fix lint * Make HeaderJSON non exportable * Add UnmarshallFromJSON test * Remove testName * Simplify uint64 encoding test * Simplify header JSON test * Simplify decodeuint64 test * Add more sleek maxuint64 Co-authored-by: Milos Zivkovic <milos.zivkovic@mvpworkshop.co>
- Loading branch information
1 parent
3ec0ea3
commit f786241
Showing
5 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package hex | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// TestDecodeUint64 verifies that uint64 values | ||
// are properly decoded from hex | ||
func TestDecodeUint64(t *testing.T) { | ||
t.Parallel() | ||
|
||
uint64Array := []uint64{ | ||
0, | ||
1, | ||
11, | ||
67312, | ||
80604, | ||
^uint64(0), // max uint64 | ||
} | ||
|
||
toHexArr := func(nums []uint64) []string { | ||
numbers := make([]string, len(nums)) | ||
|
||
for index, num := range nums { | ||
numbers[index] = fmt.Sprintf("0x%x", num) | ||
} | ||
|
||
return numbers | ||
} | ||
|
||
for index, value := range toHexArr(uint64Array) { | ||
decodedValue, err := DecodeUint64(value) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, uint64Array[index], decodedValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
// TestHeader_JSON makes sure the Header is properly | ||
// marshalled and unmarshalled from JSON | ||
func TestHeader_JSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
// nolint:lll | ||
headerJSON = `{ | ||
"parentHash": "0x0100000000000000000000000000000000000000000000000000000000000000", | ||
"sha3Uncles" : "0x0200000000000000000000000000000000000000000000000000000000000000", | ||
"miner": "0x0100000000000000000000000000000000000000", | ||
"stateRoot" : "0x0400000000000000000000000000000000000000000000000000000000000000", | ||
"transactionsRoot" : "0x0500000000000000000000000000000000000000000000000000000000000000", | ||
"receiptsRoot" : "0x0600000000000000000000000000000000000000000000000000000000000000", | ||
"logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
"difficulty" : "0xa", | ||
"number" : "0xb", | ||
"gasLimit" : "0xc", | ||
"gasUsed" : "0xd", | ||
"timestamp" : "0xe", | ||
"extraData":"0x616263646566", | ||
"mixHash" : "0x0700000000000000000000000000000000000000000000000000000000000000", | ||
"nonce" : "0x0a00000000000000", | ||
"hash" : "0x0800000000000000000000000000000000000000000000000000000000000000" | ||
}` | ||
header = Header{ | ||
ParentHash: Hash{0x1}, | ||
Sha3Uncles: Hash{0x2}, | ||
Miner: Address{0x1}, | ||
StateRoot: Hash{0x4}, | ||
TxRoot: Hash{0x5}, | ||
ReceiptsRoot: Hash{0x6}, | ||
LogsBloom: Bloom{0x0}, | ||
Difficulty: 10, | ||
Number: 11, | ||
GasLimit: 12, | ||
GasUsed: 13, | ||
Timestamp: 14, | ||
ExtraData: []byte{97, 98, 99, 100, 101, 102}, | ||
MixHash: Hash{0x7}, | ||
Nonce: Nonce{10}, | ||
Hash: Hash{0x8}, | ||
} | ||
rg = regexp.MustCompile(`(\t|\n| )+`) | ||
) | ||
|
||
t.Run("Header marshalled to JSON", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
marshalledHeader, err := json.Marshal(&header) | ||
if err != nil { | ||
t.Fatalf("Unable to marshal header, %v", err) | ||
} | ||
|
||
assert.Equal(t, rg.ReplaceAllString(headerJSON, ""), string(marshalledHeader)) | ||
}) | ||
|
||
t.Run("Header unmarshalled from JSON", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
unmarshalledHeader := Header{} | ||
if err := json.Unmarshal([]byte(headerJSON), &unmarshalledHeader); err != nil { | ||
t.Fatalf("unable to unmarshall JSON, %v", err) | ||
} | ||
|
||
assert.Equal(t, header, unmarshalledHeader) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters