Skip to content

Commit

Permalink
fix polygon snap (#65)
Browse files Browse the repository at this point in the history
* fix

* upgrade with chainid instead of height
  • Loading branch information
zhiqiangxu committed Jul 27, 2021
1 parent 1df5961 commit b76268c
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 1 deletion.
13 changes: 12 additions & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ var ETH1559_HEIGHT = map[uint32]uint64{
NETWORK_ID_TEST_NET: constants.ETH1559_HEIGHT_TESTNET,
}

var EXTRA_INFO_HEIGHT_FORK_CHECK bool
var POLYGON_SNAP_CHAINID = map[uint32]uint32{
NETWORK_ID_MAIN_NET: constants.POLYGON_SNAP_CHAINID_MAINNET,
}

var (
EXTRA_INFO_HEIGHT_FORK_CHECK bool
)

func GetNetworkMagic(id uint32) uint32 {
nid, ok := NETWORK_MAGIC[id]
Expand All @@ -119,6 +125,11 @@ func GetNetworkMagic(id uint32) uint32 {
return id
}

func GetPolygonSnapChainID(id uint32) uint32 {
height := POLYGON_SNAP_CHAINID[id]
return height
}

func GetEth1559Height(id uint32) uint64 {
height := ETH1559_HEIGHT[id]
if height == 0 {
Expand Down
2 changes: 2 additions & 0 deletions common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ const EXTRA_INFO_HEIGHT_TESTNET = 1664798
// eth 1559 heigh
const ETH1559_HEIGHT_MAINNET = 12965000
const ETH1559_HEIGHT_TESTNET = 10499401

const POLYGON_SNAP_CHAINID_MAINNET = 16
18 changes: 18 additions & 0 deletions native/service/header_sync/polygon/bor_header_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/polynetwork/poly/common"
"github.com/polynetwork/poly/common/config"
"github.com/polynetwork/poly/common/log"
cstates "github.com/polynetwork/poly/core/states"
"github.com/polynetwork/poly/native"
Expand Down Expand Up @@ -608,7 +609,13 @@ func validateHeaderExtraFieldWithSpan(native *native.NativeService, headerWOP *H
return nil
}

// only used for test
var skipVerifySpan bool

func validateHeaderExtraField(native *native.NativeService, headerWOP *HeaderWithOptionalProof, ctx *Context) (err error) {
if skipVerifySpan {
return
}
if headerWOP.Proof == nil {
var span *Span
span, err = getSpan(native, ctx)
Expand Down Expand Up @@ -715,9 +722,17 @@ func validatorContains(a []*Validator, x *Validator) (*Validator, bool) {
return nil, false
}

func shouldApplyFix(currentChainID uint64) bool {
chainID := config.GetPolygonSnapChainID(config.DefConfig.P2PNode.NetworkId)
return uint32(currentChainID) != chainID
}

func getSnapshot(native *native.NativeService, parent *HeaderWithDifficultySum, ctx *Context) (s *Snapshot, err error) {
if parent.HeaderWithOptionalSnap.Snapshot != nil {
s = parent.HeaderWithOptionalSnap.Snapshot
if shouldApplyFix(ctx.ChainID) {
err = s.ValidatorSet.updateTotalVotingPower()
}
return
}

Expand All @@ -736,6 +751,9 @@ func getSnapshot(native *native.NativeService, parent *HeaderWithDifficultySum,
}

s = snapHeader.HeaderWithOptionalSnap.Snapshot
if shouldApplyFix(ctx.ChainID) {
err = s.ValidatorSet.updateTotalVotingPower()
}
return
}

Expand Down
149 changes: 149 additions & 0 deletions native/service/header_sync/polygon/header_sync_test.go

Large diffs are not rendered by default.

0 comments on commit b76268c

Please sign in to comment.