Skip to content

Commit

Permalink
Merge pull request #2221 from elastos/feature_bpos
Browse files Browse the repository at this point in the history
Merge branch feature_bpos into release_v0.10.0
  • Loading branch information
RainFallsSilent committed Jul 3, 2023
2 parents 794fdd0 + 897307d commit b38f29d
Show file tree
Hide file tree
Showing 104 changed files with 2,890 additions and 1,284 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.16"]
go: ["1.20"]
steps:

- name: Set up Go ${{ matrix.go }}
Expand Down
6 changes: 3 additions & 3 deletions account/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"encoding/hex"

"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/core/contract"
"github.com/elastos/Elastos.ELA/crypto"
"github.com/elastos/Elastos.ELA/dpos/state"
)

const (
Expand All @@ -33,7 +33,7 @@ func GetSigners(code []byte) ([]*common.Uint160, error) {

var signers []*common.Uint160
for _, publicKey := range publicKeys {
hash, err := contract.PublicKeyToStandardCodeHash(publicKey[1:])
hash, err := state.GetOwnerKeyCodeHash(publicKey[1:])
if err != nil {
return nil, err
}
Expand All @@ -51,7 +51,7 @@ func GetCorssChainSigners(code []byte) ([]*common.Uint160, error) {

var signers []*common.Uint160
for _, publicKey := range publicKeys {
hash, err := contract.PublicKeyToStandardCodeHash(publicKey[1:])
hash, err := state.GetOwnerKeyCodeHash(publicKey[1:])
if err != nil {
return nil, err
}
Expand Down
26 changes: 0 additions & 26 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ func (b *BlockChain) InitCheckpoint(interrupt <-chan struct{},
barStart(bestHeight - startHeight)
}

var lastBlock Block
for i := startHeight; i <= bestHeight; i++ {
hash, e := b.GetBlockHash(i)
if e != nil {
Expand All @@ -315,17 +314,6 @@ func (b *BlockChain) InitCheckpoint(interrupt <-chan struct{},
break
}

if b.AncestorBlock.Height == 0 && block.Height > b.chainParams.DPoSV2StartHeight {
// tod check block
err := b.CheckTransactions(block.Block)
if err != nil {
b.AncestorBlock = lastBlock
log.Info("transaction check error:", err)
} else {
lastBlock = *block.Block
}
}

if block.Height >= b.chainParams.DPoSV2StartHeight {
CalculateTxsFee(block.Block)
} else {
Expand All @@ -349,20 +337,6 @@ func (b *BlockChain) InitCheckpoint(interrupt <-chan struct{},
}
}

log.Info("ancestor block height:", b.AncestorBlock.Height)
if b.AncestorBlock.Height != 0 {
log.Info("ReorganizeChain2 ancestor block height:", b.AncestorBlock.Height)
err := b.ReorganizeChain2(&b.AncestorBlock)
if err != nil {
log.Info("ReorganizeChain2 error:", err)
panic(err)
}

b.db.Close()
log.Info("###################### need to restart again ######################")
os.Exit(0)
}

done <- struct{}{}
}()
select {
Expand Down
6 changes: 3 additions & 3 deletions blockchain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func CheckDuplicateTx(block *Block) error {
return errors.New("[PowCheckBlockSanity] invalid register producer payload")
}

producer := BytesToHexString(producerPayload.OwnerPublicKey)
producer := BytesToHexString(producerPayload.OwnerKey)
// Check for duplicate producer in a block
if _, exists := existingProducer[producer]; exists {
return errors.New("[PowCheckBlockSanity] block contains duplicate producer")
Expand All @@ -178,7 +178,7 @@ func CheckDuplicateTx(block *Block) error {
return errors.New("[PowCheckBlockSanity] invalid update producer payload")
}

producer := BytesToHexString(producerPayload.OwnerPublicKey)
producer := BytesToHexString(producerPayload.OwnerKey)
// Check for duplicate producer in a block
if _, exists := existingProducer[producer]; exists {
return errors.New("[PowCheckBlockSanity] block contains duplicate producer")
Expand All @@ -197,7 +197,7 @@ func CheckDuplicateTx(block *Block) error {
return errors.New("[PowCheckBlockSanity] invalid cancel producer payload")
}

producer := BytesToHexString(processProducerPayload.OwnerPublicKey)
producer := BytesToHexString(processProducerPayload.OwnerKey)
// Check for duplicate producer in a block
if _, exists := existingProducer[producer]; exists {
return errors.New("[PowCheckBlockSanity] block contains duplicate producer")
Expand Down
4 changes: 4 additions & 0 deletions blockchain/indexers/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (c *Checkpoint) OnInit() {

}

func (c *Checkpoint) SaveStartHeight() uint32 {
return 500000
}

func (c *Checkpoint) StartHeight() uint32 {
//return math.MaxInt32
return 500000
Expand Down
74 changes: 9 additions & 65 deletions blockchain/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
package blockchain

import (
"crypto/sha256"
"errors"

"sort"

"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/common/log"
"github.com/elastos/Elastos.ELA/core/contract"
. "github.com/elastos/Elastos.ELA/core/contract/program"
common2 "github.com/elastos/Elastos.ELA/core/types/common"
Expand Down Expand Up @@ -48,19 +48,23 @@ func RunPrograms(data []byte, programHashes []common.Uint168, programs []*Progra
if !ownerHash.IsEqual(*codeHash) {
return errors.New("the data hashes is different with corresponding program code")
}

if prefixType == contract.PrefixStandard || prefixType == contract.PrefixDeposit {
if contract.IsSchnorr(program.Code) {
if ok, err := checkSchnorrSignatures(*program, common.Sha256D(data[:])); !ok {
return errors.New("check schnorr signature failed:" + err.Error())
}
} else {
} else if contract.IsStandard(program.Code) {
if err := CheckStandardSignature(*program, data); err != nil {
return err
}
} else if contract.IsMultiSig(program.Code) {
log.Info("mulitisign deposite")
if err := crypto.CheckMultiSigSignatures(*program, data); err != nil {
return err
}
}
} else if prefixType == contract.PrefixMultiSig {
if err := CheckMultiSigSignatures(*program, data); err != nil {
if err := crypto.CheckMultiSigSignatures(*program, data); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -116,23 +120,6 @@ func CheckStandardSignature(program Program, data []byte) error {
return crypto.Verify(*publicKey, data, program.Parameter[1:])
}

func CheckMultiSigSignatures(program Program, data []byte) error {
code := program.Code
// Get N parameter
n := int(code[len(code)-2]) - crypto.PUSH1 + 1
// Get M parameter
m := int(code[0]) - crypto.PUSH1 + 1
if m < 1 || m > n {
return errors.New("invalid multi sign script code")
}
publicKeys, err := crypto.ParseMultisigScript(code)
if err != nil {
return err
}

return verifyMultisigSignatures(m, n, publicKeys, program.Parameter, data)
}

func checkSchnorrSignatures(program Program, data [32]byte) (bool, error) {
publicKey := [33]byte{}
copy(publicKey[:], program.Code[2:])
Expand All @@ -154,50 +141,7 @@ func checkCrossChainSignatures(program Program, data []byte) error {
return err
}

return verifyMultisigSignatures(m, n, publicKeys, program.Parameter, data)
}

func verifyMultisigSignatures(m, n int, publicKeys [][]byte, signatures, data []byte) error {
if len(publicKeys) != n {
return errors.New("invalid multi sign public key script count")
}
if len(signatures)%crypto.SignatureScriptLength != 0 {
return errors.New("invalid multi sign signatures, length not match")
}
if len(signatures)/crypto.SignatureScriptLength < m {
return errors.New("invalid signatures, not enough signatures")
}
if len(signatures)/crypto.SignatureScriptLength > n {
return errors.New("invalid signatures, too many signatures")
}

var verified = make(map[common.Uint256]struct{})
for i := 0; i < len(signatures); i += crypto.SignatureScriptLength {
// Remove length byte
sign := signatures[i : i+crypto.SignatureScriptLength][1:]
// Match public key with signature
for _, publicKey := range publicKeys {
pubKey, err := crypto.DecodePoint(publicKey[1:])
if err != nil {
return err
}
err = crypto.Verify(*pubKey, data, sign)
if err == nil {
hash := sha256.Sum256(publicKey)
if _, ok := verified[hash]; ok {
return errors.New("duplicated signatures")
}
verified[hash] = struct{}{}
break // back to public keys loop
}
}
}
// Check signatures count
if len(verified) < m {
return errors.New("matched signatures not enough")
}

return nil
return crypto.VerifyMultisigSignatures(m, n, publicKeys, program.Parameter, data)
}

func SortPrograms(programs []*Program) {
Expand Down

0 comments on commit b38f29d

Please sign in to comment.