Skip to content

Commit

Permalink
Fix epoch block marshaling and add test for it
Browse files Browse the repository at this point in the history
Epoch blocks previously were not json unmarshalable by the geth client,
they now are.
  • Loading branch information
piersy committed Sep 17, 2021
1 parent b848c70 commit 5393345
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package types

import (
"encoding/json"
"fmt"
"io"
"math/big"
Expand Down Expand Up @@ -183,6 +184,37 @@ func (r *EpochSnarkData) IsEmpty() bool {
return len(r.Signature) == 0
}

// MarshalJSON marshals as JSON.
func (r EpochSnarkData) MarshalJSON() ([]byte, error) {
type EpochSnarkData struct {
Bitmap hexutil.Bytes
Signature hexutil.Bytes
}
var enc EpochSnarkData
enc.Bitmap = r.Bitmap.Bytes()
enc.Signature = r.Signature
return json.Marshal(&enc)
}

// UnmarshalJSON unmarshals from JSON.
func (r *EpochSnarkData) UnmarshalJSON(input []byte) error {
type EpochSnarkData struct {
Bitmap hexutil.Bytes
Signature hexutil.Bytes
}
var dec EpochSnarkData
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Bitmap != nil {
r.Bitmap = new(big.Int).SetBytes(dec.Bitmap)
}
if dec.Signature != nil {
r.Signature = dec.Signature
}
return nil
}

// Body is a simple (mutable, non-safe) data container for storing and moving
// a block's data contents (transactions and uncles) together.
type Body struct {
Expand Down
29 changes: 29 additions & 0 deletions e2e_test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/celo-org/celo-blockchain/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -37,3 +38,31 @@ func TestSendCelo(t *testing.T) {
err = network.AwaitTransactions(ctx, tx)
require.NoError(t, err)
}

// This test is intended to ensure that epoch blocks can be correctly marshalled.
// We previously had an open bug for this https://github.com/celo-org/celo-blockchain/issues/1574
func TestEpochBlockMarshaling(t *testing.T) {
accounts := test.Accounts(1)
gc, ec, err := test.BuildConfig(accounts)
require.NoError(t, err)

// Configure the shortest possible epoch, uptimeLookbackWindow minimum is 3
// and it needs to be < (epoch -2).
ec.Istanbul.Epoch = 6
ec.Istanbul.DefaultLookbackWindow = 3
network, err := test.NewNetwork(accounts, gc, ec)
require.NoError(t, err)
defer network.Shutdown()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

// Wait for the whole network to process the transaction.
err = network.AwaitBlock(ctx, 6)
require.NoError(t, err)
b := network[0].Tracker.GetProcessedBlock(6)

// Check that epoch snark data was actually unmarshalled, I.E there was
// something there.
assert.True(t, len(b.EpochSnarkData().Signature) > 0)
assert.True(t, b.EpochSnarkData().Bitmap.Uint64() > 0)
}

0 comments on commit 5393345

Please sign in to comment.