Skip to content
Merged
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
1 change: 1 addition & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ The dumpgenesis command dumps the genesis block configuration in JSON format to
utils.HeimdallgRPCAddressFlag,
utils.RunHeimdallFlag,
utils.RunHeimdallArgsFlag,
utils.UseHeimdallAppFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
Expand Down
8 changes: 4 additions & 4 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,6 @@ func geth(ctx *cli.Context) error {
return fmt.Errorf("invalid command: %q", args[0])
}

prepare(ctx)
stack, backend := makeFullNode(ctx)
defer stack.Close()

if ctx.GlobalBool(utils.RunHeimdallFlag.Name) {
shutdownCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer stop()
Expand All @@ -360,6 +356,10 @@ func geth(ctx *cli.Context) error {
}()
}

prepare(ctx)
stack, backend := makeFullNode(ctx)
defer stack.Close()

startNode(ctx, stack, backend, false)
stack.Wait()
return nil
Expand Down
8 changes: 8 additions & 0 deletions cmd/utils/bor_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ var (
Value: "",
}

// UseHeimdallApp flag for using internall heimdall app to fetch data
UseHeimdallAppFlag = cli.BoolFlag{
Name: "bor.useheimdallapp",
Usage: "Use child heimdall process to fetch data, Only works when bor.runheimdall is true",
}

// BorFlags all bor related flags
BorFlags = []cli.Flag{
HeimdallURLFlag,
WithoutHeimdallFlag,
HeimdallgRPCAddressFlag,
RunHeimdallFlag,
RunHeimdallArgsFlag,
UseHeimdallAppFlag,
}
)

Expand All @@ -82,6 +89,7 @@ func SetBorConfig(ctx *cli.Context, cfg *eth.Config) {
cfg.HeimdallgRPCAddress = ctx.GlobalString(HeimdallgRPCAddressFlag.Name)
cfg.RunHeimdall = ctx.GlobalBool(RunHeimdallFlag.Name)
cfg.RunHeimdallArgs = ctx.GlobalString(RunHeimdallArgsFlag.Name)
cfg.UseHeimdallApp = ctx.GlobalBool(UseHeimdallAppFlag.Name)
}

// CreateBorEthereum Creates bor ethereum object from eth.Config
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
HeimdallgRPCAddress: ctx.GlobalString(HeimdallgRPCAddressFlag.Name),
RunHeimdall: ctx.GlobalBool(RunHeimdallFlag.Name),
RunHeimdallArgs: ctx.GlobalString(RunHeimdallArgsFlag.Name),
UseHeimdallApp: ctx.GlobalBool(UseHeimdallAppFlag.Name),
})
engine = ethereum.Engine()
} else {
Expand Down
52 changes: 52 additions & 0 deletions consensus/bor/heimdallapp/checkpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package heimdallapp

import (
"context"
"math/big"

"github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/consensus/bor/heimdall/checkpoint"
"github.com/ethereum/go-ethereum/log"

hmTypes "github.com/maticnetwork/heimdall/types"
abci "github.com/tendermint/tendermint/abci/types"
)

func (h *HeimdallAppClient) FetchCheckpointCount(_ context.Context) (int64, error) {
log.Info("Fetching checkpoint count")

res := h.hApp.CheckpointKeeper.GetACKCount(h.NewContext())

log.Info("Fetched checkpoint count")

return int64(res), nil
}

func (h *HeimdallAppClient) FetchCheckpoint(_ context.Context, number int64) (*checkpoint.Checkpoint, error) {
log.Info("Fetching checkpoint", "number", number)

res, err := h.hApp.CheckpointKeeper.GetCheckpointByNumber(h.NewContext(), uint64(number))
if err != nil {
return nil, err
}

log.Info("Fetched checkpoint", "number", number)

return toBorCheckpoint(res), nil
}

func (h *HeimdallAppClient) NewContext() types.Context {
return h.hApp.NewContext(true, abci.Header{Height: h.hApp.LastBlockHeight()})
}

func toBorCheckpoint(hdCheckpoint hmTypes.Checkpoint) *checkpoint.Checkpoint {
return &checkpoint.Checkpoint{
Proposer: hdCheckpoint.Proposer.EthAddress(),
StartBlock: big.NewInt(int64(hdCheckpoint.StartBlock)),
EndBlock: big.NewInt(int64(hdCheckpoint.EndBlock)),
RootHash: hdCheckpoint.RootHash.EthHash(),
BorChainID: hdCheckpoint.BorChainID,
Timestamp: hdCheckpoint.TimeStamp,
}
}
27 changes: 27 additions & 0 deletions consensus/bor/heimdallapp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package heimdallapp

import (
"github.com/ethereum/go-ethereum/log"

"github.com/maticnetwork/heimdall/app"
"github.com/maticnetwork/heimdall/cmd/heimdalld/service"
)

const (
stateFetchLimit = 50
)

type HeimdallAppClient struct {
hApp *app.HeimdallApp
}

func NewHeimdallAppClient() *HeimdallAppClient {
return &HeimdallAppClient{
hApp: service.GetHeimdallApp(),
}
}

func (h *HeimdallAppClient) Close() {
// Nothing to close as of now
log.Warn("Shutdown detected, Closing Heimdall App conn")
}
86 changes: 86 additions & 0 deletions consensus/bor/heimdallapp/span.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package heimdallapp

import (
"context"

hmTypes "github.com/maticnetwork/heimdall/types"

"github.com/ethereum/go-ethereum/consensus/bor/heimdall/span"
"github.com/ethereum/go-ethereum/consensus/bor/valset"
"github.com/ethereum/go-ethereum/log"
)

func (h *HeimdallAppClient) Span(ctx context.Context, spanID uint64) (*span.HeimdallSpan, error) {
log.Info("Fetching span", "spanID", spanID)

res, err := h.hApp.BorKeeper.GetSpan(h.NewContext(), spanID)
if err != nil {
return nil, err
}

log.Info("Fetched span", "spanID", spanID)

return toSpan(res), nil
}

func toSpan(hdSpan *hmTypes.Span) *span.HeimdallSpan {
return &span.HeimdallSpan{
Span: span.Span{
ID: hdSpan.ID,
StartBlock: hdSpan.StartBlock,
EndBlock: hdSpan.EndBlock,
},
ValidatorSet: toValidatorSet(hdSpan.ValidatorSet),
SelectedProducers: toValidators(hdSpan.SelectedProducers),
ChainID: hdSpan.ChainID,
}
}

func toValidatorSet(vs hmTypes.ValidatorSet) valset.ValidatorSet {
return valset.ValidatorSet{
Validators: toValidatorsRef(vs.Validators),
Proposer: toValidatorRef(vs.Proposer),
}
}

func toValidators(vs []hmTypes.Validator) []valset.Validator {
newVS := make([]valset.Validator, len(vs))

for i, v := range vs {
newVS[i] = toValidator(v)
}

return newVS
}

func toValidatorsRef(vs []*hmTypes.Validator) []*valset.Validator {
newVS := make([]*valset.Validator, len(vs))

for i, v := range vs {
if v == nil {
continue
}

newVS[i] = toValidatorRef(v)
}

return newVS
}

func toValidatorRef(v *hmTypes.Validator) *valset.Validator {
return &valset.Validator{
ID: v.ID.Uint64(),
Address: v.Signer.EthAddress(),
VotingPower: v.VotingPower,
ProposerPriority: v.ProposerPriority,
}
}

func toValidator(v hmTypes.Validator) valset.Validator {
return valset.Validator{
ID: v.ID.Uint64(),
Address: v.Signer.EthAddress(),
VotingPower: v.VotingPower,
ProposerPriority: v.ProposerPriority,
}
}
64 changes: 64 additions & 0 deletions consensus/bor/heimdallapp/state_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package heimdallapp

import (
"context"
"time"

"github.com/maticnetwork/heimdall/clerk/types"

"github.com/ethereum/go-ethereum/consensus/bor/clerk"

abci "github.com/tendermint/tendermint/abci/types"
)

func (h *HeimdallAppClient) StateSyncEvents(ctx context.Context, fromID uint64, to int64) ([]*clerk.EventRecordWithTime, error) {
totalRecords := make([]*clerk.EventRecordWithTime, 0)

hCtx := h.hApp.NewContext(true, abci.Header{Height: h.hApp.LastBlockHeight()})

for {
fromRecord, err := h.hApp.ClerkKeeper.GetEventRecord(hCtx, fromID)
if err != nil {
return nil, err
}

events, err := h.hApp.ClerkKeeper.GetEventRecordListWithTime(hCtx, fromRecord.RecordTime, time.Unix(to, 0), 1, stateFetchLimit)
if err != nil {
return nil, err
}

totalRecords = append(totalRecords, toEvents(events)...)

if len(events) < stateFetchLimit {
break
}

fromID += uint64(stateFetchLimit)
}

return totalRecords, nil
}

func toEvents(hdEvents []types.EventRecord) []*clerk.EventRecordWithTime {
events := make([]*clerk.EventRecordWithTime, len(hdEvents))

for i, ev := range hdEvents {
events[i] = toEvent(ev)
}

return events
}

func toEvent(hdEvent types.EventRecord) *clerk.EventRecordWithTime {
return &clerk.EventRecordWithTime{
EventRecord: clerk.EventRecord{
ID: hdEvent.ID,
Contract: hdEvent.Contract.EthAddress(),
Data: hdEvent.Data.Bytes(),
TxHash: hdEvent.TxHash.EthHash(),
LogIndex: hdEvent.LogIndex,
ChainID: hdEvent.ChainID,
},
Time: hdEvent.RecordTime,
}
}
8 changes: 8 additions & 0 deletions consensus/bor/valset/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ func (vals *ValidatorSet) StringIndented(indent string) string {
indent)
}

func (vals *ValidatorSet) SetTotalVotingPower(totalVotingPower int64) {
vals.totalVotingPower = totalVotingPower
}

func (vals *ValidatorSet) SetMap(validatorsMap map[common.Address]int) {
vals.validatorsMap = validatorsMap
}

//-------------------------------------
// Implements sort for sorting validators by address.

Expand Down
8 changes: 7 additions & 1 deletion eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/consensus/bor/contract"
"github.com/ethereum/go-ethereum/consensus/bor/heimdall" //nolint:typecheck
"github.com/ethereum/go-ethereum/consensus/bor/heimdall/span"
"github.com/ethereum/go-ethereum/consensus/bor/heimdallapp"
"github.com/ethereum/go-ethereum/consensus/bor/heimdallgrpc"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
Expand Down Expand Up @@ -227,6 +228,9 @@ type Config struct {
// Arguments to pass to heimdall service
RunHeimdallArgs string

// Use child heimdall process to fetch data, Only works when RunHeimdall is true
UseHeimdallApp bool

// Bor logs flag
BorLogs bool

Expand Down Expand Up @@ -264,7 +268,9 @@ func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, et
log.Warn("Sanitizing DevFakeAuthor", "Use DevFakeAuthor with", "--bor.withoutheimdall")
}
var heimdallClient bor.IHeimdallClient
if ethConfig.HeimdallgRPCAddress != "" {
if ethConfig.RunHeimdall && ethConfig.UseHeimdallApp {
heimdallClient = heimdallapp.NewHeimdallAppClient()
} else if ethConfig.HeimdallgRPCAddress != "" {
heimdallClient = heimdallgrpc.NewHeimdallGRPCClient(ethConfig.HeimdallgRPCAddress)
} else {
heimdallClient = heimdall.NewHeimdallClient(ethConfig.HeimdallURL)
Expand Down
Loading