Skip to content

Commit

Permalink
consortium-v2: add missing weight field to our customized json marshal (
Browse files Browse the repository at this point in the history
#449)

* consortium-v2: define MarshalJSON on a non-pointer ValidatorWithBlsPub

We want json.Marshal to invoke our custom MarshalJSON when be called with
non-pointer ValidatorWithBlsPub. In order to do that, we need to define
MarshalJSON on a non-pointer receiver.

* consortium-v2: add missing weight field to our customized json marshal
  • Loading branch information
minh-bq committed May 16, 2024
1 parent 0f9799c commit 4e37c72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion consensus/consortium/v2/finality/consortium_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ func (validator *ValidatorWithBlsPub) UnmarshalJSON(input []byte) error {
if err != nil {
return err
}
validator.Weight = savedValidator.Weight
return nil
}

func (validator *ValidatorWithBlsPub) MarshalJSON() ([]byte, error) {
func (validator ValidatorWithBlsPub) MarshalJSON() ([]byte, error) {
savedValidator := savedValidatorWithBlsPub{
Address: validator.Address,
Weight: validator.Weight,
}

if validator.BlsPublicKey != nil {
Expand Down
40 changes: 39 additions & 1 deletion consensus/consortium/v2/finality/consortium_header_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package finality

import "testing"
import (
"encoding/json"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/bls/blst"
)

func TestFinalityVoteBitSet(t *testing.T) {
var bitSet FinalityVoteBitSet
Expand Down Expand Up @@ -37,3 +44,34 @@ func TestFinalityVoteBitSet(t *testing.T) {
t.Fatalf("Wrong bit, exp %d got %d", 0, bitSet.GetBit(70))
}
}

func TestMarshallJsonValidatorWithPub(t *testing.T) {
blsPubkey, err := blst.PublicKeyFromBytes(common.Hex2Bytes("affe116dad1eb59bda4dc6a6442a891c1b19502c07acff7304a070a5d53b7bf89c01d6985a1ad0d86b009b39e5cd7a9e"))
if err != nil {
t.Fatal(err)
}
validator := ValidatorWithBlsPub{
Address: common.BigToAddress(big.NewInt(1)),
BlsPublicKey: blsPubkey,
Weight: 100,
}
marshalled, err := json.Marshal(validator)
if err != nil {
t.Fatal(err)
}

var unmarshalledValidator ValidatorWithBlsPub
err = json.Unmarshal(marshalled, &unmarshalledValidator)
if err != nil {
t.Fatal(err)
}
if validator.Address != unmarshalledValidator.Address {
t.Fatalf("Address mismatches, got %v expect %v", unmarshalledValidator.Address, validator.Address)
}
if !validator.BlsPublicKey.Equals(unmarshalledValidator.BlsPublicKey) {
t.Fatalf("BLS public key mismatches, got %v expect %v", unmarshalledValidator.BlsPublicKey, validator.BlsPublicKey)
}
if validator.Weight != unmarshalledValidator.Weight {
t.Fatalf("Weight mismatches, got %d expect %d", unmarshalledValidator.Weight, validator.Weight)
}
}

0 comments on commit 4e37c72

Please sign in to comment.