Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Added a Hard Fork event which will enable transaction metadata in Bor #1064

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
if: matrix.os == 'ubuntu-20.04'
uses: golangci/golangci-lint-action@v3
with:
version: v1.53
version: v1.54

- name: Test win
if: matrix.os == 'windows-2022'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ lintci-deps-clean: golangci-lint-clean

# download and build golangci-lint (https://golangci-lint.run)
$(GOBINREL)/golangci-lint: | $(GOBINREL)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(GOBIN)" v1.53.3
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(GOBIN)" v1.54.0

golangci-lint-clean:
rm -f "$(GOBIN)/golangci-lint"
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# erigon-lib
Dependencies of Erigon project, rewritten from scratch and licensed under Apache 2.0

## Dev workflow

In erigon folder create go.work file (it’s already in .gitignore)
```
go 1.20

use (
.

./../erigon-lib
)
```

Create PR in erigon-lib, don’t merge PR, refer from erigon to non-merged erigon-lib branch (commit) by:
go get github.com/ledgerwatch/erigon-lib/kv@<commit_hash>

Create Erigon PR

When both CI are green - merge 2 PR. That’s it.
13 changes: 13 additions & 0 deletions chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ type BorConfig struct {
StateSyncConfirmationDelay map[string]uint64 `json:"stateSyncConfirmationDelay"` // StateSync Confirmation Delay, in seconds, to calculate `to`

sprints sprints

ParallelUniverseBlock *big.Int `json:"parallelUniverseBlock"` // TODO: update all occurrence, change name and finalize number (hardfork for block-stm related changes)
}

// String implements the stringer interface, returning the consensus engine details.
Expand Down Expand Up @@ -520,6 +522,17 @@ func (c *BorConfig) CalculateStateSyncDelay(number uint64) uint64 {
return borKeyValueConfigHelper(c.StateSyncConfirmationDelay, number)
}

// TODO: modify this function once the block number is finalized
func (c *BorConfig) IsParallelUniverse(number uint64) bool {
if c.ParallelUniverseBlock != nil {
if c.ParallelUniverseBlock.Cmp(big.NewInt(0)) == 0 {
return false
}
}

return isForked(c.ParallelUniverseBlock, number)
}

func (c *BorConfig) calcConfig(field map[string]uint64, number uint64) uint64 {
keys := sortMapKeys(field)
for i := 0; i < len(keys)-1; i++ {
Expand Down
2 changes: 2 additions & 0 deletions common/dbg/leak_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/ledgerwatch/log/v3"
)

const FileCloseLogLevel = log.LvlTrace

// LeakDetector - use it to find which resource was created but not closed (leaked)
// periodically does print in logs resources which living longer than 1min with their creation stack trace
// For example db transactions can call Add/Del from Begin/Commit/Rollback methods
Expand Down
16 changes: 9 additions & 7 deletions compress/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,16 @@ func (d *Decompressor) ModTime() time.Time {
return d.modTime
}

func (d *Decompressor) Close() error {
if err := mmap.Munmap(d.mmapHandle1, d.mmapHandle2); err != nil {
log.Trace("unmap", "err", err, "file", d.FileName())
}
if err := d.f.Close(); err != nil {
return err
func (d *Decompressor) Close() {
if d.f != nil {
if err := mmap.Munmap(d.mmapHandle1, d.mmapHandle2); err != nil {
log.Log(dbg.FileCloseLogLevel, "unmap", "err", err, "file", d.FileName(), "stack", dbg.Stack())
}
if err := d.f.Close(); err != nil {
log.Log(dbg.FileCloseLogLevel, "close", "err", err, "file", d.FileName(), "stack", dbg.Stack())
}
d.f = nil
}
return nil
}

func (d *Decompressor) FilePath() string { return d.filePath }
Expand Down
103 changes: 90 additions & 13 deletions crypto/kzg/kzg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,80 @@ package kzg

import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"sync"

gokzg4844 "github.com/crate-crypto/go-kzg-4844"
)

const (
BlobCommitmentVersionKZG uint8 = 0x01
PrecompileInputLength int = 192
)

type VersionedHash [32]byte

var gCryptoCtx *gokzg4844.Context
var initCryptoCtx sync.Once
var (
errInvalidInputLength = errors.New("invalid input length")

// InitializeCryptoCtx initializes the global context object returned via CryptoCtx
func InitializeCryptoCtx() {
// The value that gets returned when the `verify_kzg_proof“ precompile is called
precompileReturnValue [64]byte

trustedSetupFile string

gokzgCtx *gokzg4844.Context
initCryptoCtx sync.Once
)

func init() {
new(big.Int).SetUint64(gokzg4844.ScalarsPerBlob).FillBytes(precompileReturnValue[:32])
copy(precompileReturnValue[32:], gokzg4844.BlsModulus[:])
}

func SetTrustedSetupFilePath(path string) {
trustedSetupFile = path
}

// InitKZGCtx initializes the global context object returned via CryptoCtx
func InitKZGCtx() {
initCryptoCtx.Do(func() {
var err error
// Initialize context to match the configurations that the
// specs are using.
gCryptoCtx, err = gokzg4844.NewContext4096Insecure1337()
if err != nil {
panic(fmt.Sprintf("could not create context, err : %v", err))
if trustedSetupFile != "" {
file, err := os.ReadFile(trustedSetupFile)
if err != nil {
panic(fmt.Sprintf("could not read file, err: %v", err))
}

setup := new(gokzg4844.JSONTrustedSetup)
if err = json.Unmarshal(file, setup); err != nil {
panic(fmt.Sprintf("could not unmarshal, err: %v", err))
}

gokzgCtx, err = gokzg4844.NewContext4096(setup)
if err != nil {
panic(fmt.Sprintf("could not create KZG context, err: %v", err))
}
} else {
var err error
// Initialize context to match the configurations that the
// specs are using.
gokzgCtx, err = gokzg4844.NewContext4096Insecure1337()
if err != nil {
panic(fmt.Sprintf("could not create context, err : %v", err))
}
}
})
}

// Ctx returns a context object that stores all of the necessary configurations to allow one to
// create and verify blob proofs. This function is expensive to run if the crypto context isn't
// initialized, so production services should pre-initialize by calling InitializeCryptoCtx.
// initialized, so production services should pre-initialize by calling InitKZGCtx.
func Ctx() *gokzg4844.Context {
InitializeCryptoCtx()
return gCryptoCtx
InitKZGCtx()
return gokzgCtx
}

// KZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844
Expand All @@ -45,3 +85,40 @@ func KZGToVersionedHash(kzg gokzg4844.KZGCommitment) VersionedHash {

return VersionedHash(h)
}

// PointEvaluationPrecompile implements point_evaluation_precompile from EIP-4844
func PointEvaluationPrecompile(input []byte) ([]byte, error) {
if len(input) != PrecompileInputLength {
return nil, errInvalidInputLength
}
// versioned hash: first 32 bytes
var versionedHash [32]byte
copy(versionedHash[:], input[:32])

var x, y [32]byte
// Evaluation point: next 32 bytes
copy(x[:], input[32:64])
// Expected output: next 32 bytes
copy(y[:], input[64:96])

// input kzg point: next 48 bytes
var dataKZG [48]byte
copy(dataKZG[:], input[96:144])
if KZGToVersionedHash(dataKZG) != versionedHash {
return nil, errors.New("mismatched versioned hash")
}

// Quotient kzg: next 48 bytes
var quotientKZG [48]byte
copy(quotientKZG[:], input[144:PrecompileInputLength])

cryptoCtx := Ctx()
err := cryptoCtx.VerifyKZGProof(dataKZG, x, y, quotientKZG)
if err != nil {
return nil, fmt.Errorf("verify_kzg_proof error: %w", err)
}

result := precompileReturnValue // copy the value

return result[:], nil
}
12 changes: 6 additions & 6 deletions direct/execution_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,8 @@ func (s *ExecutionClientDirect) GetAssembledBlock(ctx context.Context, in *execu
}

// Chain Putters.
func (s *ExecutionClientDirect) InsertHeaders(ctx context.Context, in *execution.InsertHeadersRequest, opts ...grpc.CallOption) (*execution.InsertionResult, error) {
return s.server.InsertHeaders(ctx, in)
}

func (s *ExecutionClientDirect) InsertBodies(ctx context.Context, in *execution.InsertBodiesRequest, opts ...grpc.CallOption) (*execution.InsertionResult, error) {
return s.server.InsertBodies(ctx, in)
func (s *ExecutionClientDirect) InsertBlocks(ctx context.Context, in *execution.InsertBlocksRequest, opts ...grpc.CallOption) (*execution.InsertionResult, error) {
return s.server.InsertBlocks(ctx, in)
}

// Chain Validation and ForkChoice.
Expand Down Expand Up @@ -88,3 +84,7 @@ func (s *ExecutionClientDirect) GetHeaderHashNumber(ctx context.Context, in *typ
func (s *ExecutionClientDirect) GetForkChoice(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*execution.ForkChoice, error) {
return s.server.GetForkChoice(ctx, in)
}

func (s *ExecutionClientDirect) Ready(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*execution.ReadyResponse, error) {
return s.server.Ready(ctx, in)
}
103 changes: 103 additions & 0 deletions direct/sentinel_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2021 Erigon contributors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package direct

import (
"context"
"io"

"github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
"google.golang.org/grpc"
)

type SentinelClientDirect struct {
server sentinel.SentinelServer
}

func NewSentinelClientDirect(sentinel sentinel.SentinelServer) sentinel.SentinelClient {
return &SentinelClientDirect{server: sentinel}
}

func (s *SentinelClientDirect) SendRequest(ctx context.Context, in *sentinel.RequestData, opts ...grpc.CallOption) (*sentinel.ResponseData, error) {
return s.server.SendRequest(ctx, in)
}

func (s *SentinelClientDirect) SetStatus(ctx context.Context, in *sentinel.Status, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
return s.server.SetStatus(ctx, in)
}

func (s *SentinelClientDirect) GetPeers(ctx context.Context, in *sentinel.EmptyMessage, opts ...grpc.CallOption) (*sentinel.PeerCount, error) {
return s.server.GetPeers(ctx, in)
}

func (s *SentinelClientDirect) BanPeer(ctx context.Context, p *sentinel.Peer, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
return s.server.BanPeer(ctx, p)
}

func (s *SentinelClientDirect) PublishGossip(ctx context.Context, in *sentinel.GossipData, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
return s.server.PublishGossip(ctx, in)
}

// Subscribe gossip part. the only complex section of this bullshit

func (s *SentinelClientDirect) SubscribeGossip(ctx context.Context, in *sentinel.EmptyMessage, opts ...grpc.CallOption) (sentinel.Sentinel_SubscribeGossipClient, error) {
ch := make(chan *gossipReply, 16384)
streamServer := &SentinelSubscribeGossipS{ch: ch, ctx: ctx}
go func() {
defer close(ch)
streamServer.Err(s.server.SubscribeGossip(in, streamServer))
}()
return &SentinelSubscribeGossipC{ch: ch, ctx: ctx}, nil
}

type SentinelSubscribeGossipC struct {
ch chan *gossipReply
ctx context.Context
grpc.ClientStream
}

func (c *SentinelSubscribeGossipC) Recv() (*sentinel.GossipData, error) {
m, ok := <-c.ch
if !ok || m == nil {
return nil, io.EOF
}
return m.r, m.err
}
func (c *SentinelSubscribeGossipC) Context() context.Context { return c.ctx }

type SentinelSubscribeGossipS struct {
ch chan *gossipReply
ctx context.Context
grpc.ServerStream
}

type gossipReply struct {
r *sentinel.GossipData
err error
}

func (s *SentinelSubscribeGossipS) Send(m *sentinel.GossipData) error {
s.ch <- &gossipReply{r: m}
return nil
}
func (s *SentinelSubscribeGossipS) Context() context.Context { return s.ctx }
func (s *SentinelSubscribeGossipS) Err(err error) {
if err == nil {
return
}
s.ch <- &gossipReply{err: err}
}
Loading