Skip to content

Commit

Permalink
Add test comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kourin1996 committed Jan 6, 2025
1 parent a9bc96f commit c925ccc
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 59 deletions.
51 changes: 0 additions & 51 deletions core/rawdb/accessors_celo.go

This file was deleted.

54 changes: 54 additions & 0 deletions core/rawdb/celo_accessors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rawdb

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
)

var (
CeloPreGingerbreadFieldsPrefix = []byte("celoPgbFields-") // CeloPreGingerbreadFieldsPrefix + block hash -> PreGingerbreadAdditionalFields
)

type PreGingerbreadFields struct {
BaseFee *big.Int
GasLimit *big.Int
}

// preGingerbreadAdditionalFieldsKey calculates a database key for PreGingerbreadAdditionalFields for the given block hash
func preGingerbreadAdditionalFieldsKey(hash common.Hash) []byte {
return append(CeloPreGingerbreadFieldsPrefix, hash[:]...)
}

// ReadPreGingerbreadAdditionalFields reads PreGingerbreadAdditionalFields from the given database for the given block hash
func ReadPreGingerbreadAdditionalFields(db ethdb.KeyValueReader, blockHash common.Hash) (*PreGingerbreadFields, error) {
data, _ := db.Get(preGingerbreadAdditionalFieldsKey(blockHash))
if len(data) == 0 {
return nil, nil
}

fields := &PreGingerbreadFields{}

err := rlp.DecodeBytes(data, fields)
if err != nil {
return nil, err
}

return fields, nil
}

// WritePreGingerbreadAdditionalFields writes PreGingerbreadAdditionalFields to the given database for the given block hash
func WritePreGingerbreadAdditionalFields(db ethdb.KeyValueWriter, blockHash common.Hash, data *PreGingerbreadFields) error {
rawData, err := rlp.EncodeToBytes(data)
if err != nil {
return err
}

if err := db.Put(preGingerbreadAdditionalFieldsKey(blockHash), rawData); err != nil {
return err
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/stretchr/testify/assert"
)

// TestPreGingerbreadAdditionalFields tests ReadPreGingerbreadAdditionalFields function
func TestPreGingerbreadAdditionalFields(t *testing.T) {
db := NewMemoryDatabase()

mockData, err := rlp.EncodeToBytes(&PreGingerbreadAdditionalFields{
mockData, err := rlp.EncodeToBytes(&PreGingerbreadFields{
BaseFee: big.NewInt(1000),
GasLimit: big.NewInt(2000),
})
Expand All @@ -27,7 +28,7 @@ func TestPreGingerbreadAdditionalFields(t *testing.T) {
name string
seedData []SeedData
hash common.Hash
expectedRes *PreGingerbreadAdditionalFields
expectedRes *PreGingerbreadFields
returnsError bool
}{
{
Expand All @@ -46,7 +47,7 @@ func TestPreGingerbreadAdditionalFields(t *testing.T) {
},
},
hash: common.HexToHash("0x2"),
expectedRes: &PreGingerbreadAdditionalFields{
expectedRes: &PreGingerbreadFields{
BaseFee: big.NewInt(1000),
GasLimit: big.NewInt(2000),
},
Expand Down Expand Up @@ -91,3 +92,40 @@ func TestPreGingerbreadAdditionalFields(t *testing.T) {
})
}
}

// TestWritePreGingerbreadAdditionalFields tests WritePreGingerbreadAdditionalFields function
func TestWritePreGingerbreadAdditionalFields(t *testing.T) {
db := NewMemoryDatabase()

hash := common.HexToHash("0x1")
data := []*PreGingerbreadFields{
{
BaseFee: big.NewInt(0),
GasLimit: big.NewInt(2000),
},
{
BaseFee: big.NewInt(3000),
GasLimit: big.NewInt(0),
},
{
BaseFee: big.NewInt(5000),
GasLimit: big.NewInt(6000),
},
}

// Make sure that the data is not found
record0, err := ReadPreGingerbreadAdditionalFields(db, hash)
assert.NoError(t, err)
assert.Nil(t, record0)

for _, d := range data {
// Write data
err := WritePreGingerbreadAdditionalFields(db, hash, d)
assert.NoError(t, err)

// Read data
record, err := ReadPreGingerbreadAdditionalFields(db, hash)
assert.NoError(t, err)
assert.Equal(t, d, record)
}
}
11 changes: 7 additions & 4 deletions internal/ethapi/celo_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ func PopulatePreGingerbreadHeaderFields(ctx context.Context, backend CeloBackend

fields, err := rawdb.ReadPreGingerbreadAdditionalFields(backend.ChainDb(), header.Hash())
if fields != nil {
// If the record is found, use the values from the record
baseFee = fields.BaseFee
gasLimit = fields.GasLimit
if fields.BaseFee != nil && fields.BaseFee.BitLen() != 0 {
baseFee = fields.BaseFee
}
if fields.GasLimit != nil && fields.GasLimit.BitLen() != 0 {
gasLimit = fields.GasLimit
}
} else {
if err != nil {
log.Debug("failed to read pre-gingerbread fields", "err", err)
Expand All @@ -63,7 +66,7 @@ func PopulatePreGingerbreadHeaderFields(ctx context.Context, backend CeloBackend
gasLimit = retrievePreGingerbreadGasLimit(backend, header.Number)

if baseFee != nil || gasLimit != nil {
err = rawdb.WritePreGingerbreadAdditionalFields(backend.ChainDb(), header.Hash(), &rawdb.PreGingerbreadAdditionalFields{
err = rawdb.WritePreGingerbreadAdditionalFields(backend.ChainDb(), header.Hash(), &rawdb.PreGingerbreadFields{
BaseFee: baseFee,
GasLimit: gasLimit,
})
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/celo_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

// Test_parseGasPriceMinimumUpdated checks the gas price minimum updated event parsing
func Test_parseGasPriceMinimumUpdated(t *testing.T) {
tests := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion params/celo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const (
CeloBaklavaChainID = 62320
)

// GasLimits holds the gas limit changes for a given chain
type GasLimits struct {
// changes holds all gas limit changes, it is assumed that the first change ocurrs at block 0.
changes []LimitChange
}

Expand All @@ -18,6 +18,7 @@ type LimitChange struct {
gasLimit uint64
}

// Limit returns the gas limit at a given block number
func (g *GasLimits) Limit(block *big.Int) uint64 {
// Grab the gas limit at block 0
curr := g.changes[0].gasLimit
Expand Down
1 change: 1 addition & 0 deletions params/celo_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

// TestGasLimits_Limit checks the gas limits at the beginning and end of each limit change of each chain
func TestGasLimits_Limit(t *testing.T) {
subTest := func(t *testing.T, name string, chainId uint64, limits []LimitChange) {
t.Run(name, func(t *testing.T) {
Expand Down

0 comments on commit c925ccc

Please sign in to comment.