Skip to content

Commit

Permalink
Merge remote-tracking branch 'elastos/release_v0.9.8' into tx_history…
Browse files Browse the repository at this point in the history
…_v0.9.8
  • Loading branch information
RainFallsSilent committed Sep 21, 2024
2 parents 0baa18c + 5089b4f commit bbf93f7
Show file tree
Hide file tree
Showing 22 changed files with 288,783 additions and 85 deletions.
26 changes: 13 additions & 13 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,19 @@ 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)
}
//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{}{}
}()
Expand Down
26 changes: 26 additions & 0 deletions blockchain/blockvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ func CheckDuplicateTx(block *Block) error {
existingProducer := make(map[string]struct{})
existingProducerNode := make(map[string]struct{})
existingCR := make(map[Uint168]struct{})
recordSponsorCount := 0
for _, txn := range block.Transactions {
switch txn.TxType() {
case common.RecordSponsor:
recordSponsorCount++
if recordSponsorCount > 1 {
return errors.New("[PowCheckBlockSanity] block contains duplicate record sponsor Tx")
}

case common.WithdrawFromSideChain:
witPayload := txn.Payload().(*payload.WithdrawFromSideChain)

Expand Down Expand Up @@ -318,10 +325,29 @@ func (b *BlockChain) CheckBlockContext(block *Block, prevNode *BlockNode) error
return errors.New("block timestamp is not after expected")
}

var recordSponsorExist bool
for _, tx := range block.Transactions[1:] {
if !IsFinalizedTransaction(tx, block.Height) {
return errors.New("block contains unfinalized transaction")
}
if tx.IsRecordSponorTx() {
recordSponsorExist = true
}
}

// check if need to record sponsor
if block.Height >= b.chainParams.DPoSConfiguration.RecordSponsorStartHeight {
lastBlock, err := b.GetDposBlockByHash(*prevNode.Hash)
if err != nil {
return errors.New("get last block failed")
}

if lastBlock.Confirm == nil && recordSponsorExist {
return errors.New("record sponsor transaction must be confirmed")
}
if lastBlock.Confirm != nil && !recordSponsorExist {
return errors.New("confirmed block must have record sponsor transaction")
}
}

if err := DefaultLedger.Arbitrators.CheckDPOSIllegalTx(block); err != nil {
Expand Down
12 changes: 11 additions & 1 deletion common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func GetDefaultParams() *Configuration {
CandidatesCount: 72,
DPoSV2RewardAccumulateProgramHash: StakeRewardProgramHash,
NFTStartHeight: 1405000,
SponsorsFilePath: "sponsors",
RecordSponsorStartHeight: 1801550,
OriginArbiters: []string{
"0248df6705a909432be041e0baa25b8f648741018f70d1911f2ed28778db4b8fe4",
"02771faf0f4d4235744b30972d5f2c470993920846c761e4d08889ecfdc061cddf",
Expand Down Expand Up @@ -245,7 +247,7 @@ func GetDefaultParams() *Configuration {
ProhibitTransferToDIDHeight: 1032840,
DIDSideChainAddress: "XKUh4GLhFJiqAMTF6HyWQrV9pK9HcGUdfJ",
DPoSV2EffectiveVotes: 80000 * 100000000,
DPoSV2StartHeight: 1405000,
DPoSV2StartHeight: 1405000, //1405000+262800=1667800
StakePoolProgramHash: StakePoolProgramHash,
SchnorrStartHeight: math.MaxUint32,
NormalSchnorrStartHeight: 1405000,
Expand Down Expand Up @@ -380,6 +382,8 @@ func (p *Configuration) TestNet() *Configuration {
p.CrossChainMonitorInterval = 100
p.CRConfiguration.CRClaimPeriod = 10080
p.DPoSConfiguration.NFTStartHeight = 1098000
p.DPoSConfiguration.SponsorsFilePath = "sponsors"
p.DPoSConfiguration.RecordSponsorStartHeight = 1174500

p.HttpInfoPort = 21333
p.HttpRestPort = 21334
Expand Down Expand Up @@ -500,6 +504,8 @@ func (p *Configuration) RegNet() *Configuration {
p.CrossChainMonitorInterval = 100
p.CRConfiguration.CRClaimPeriod = 10080
p.DPoSConfiguration.NFTStartHeight = 968000
p.DPoSConfiguration.SponsorsFilePath = "sponsors"
p.DPoSConfiguration.RecordSponsorStartHeight = math.MaxUint32
p.HttpInfoPort = 22333
p.HttpRestPort = 22334
p.HttpWsPort = 22335
Expand Down Expand Up @@ -720,6 +726,10 @@ type DPoSConfiguration struct {
CRDPoSNodeHotFixHeight uint32 `screw:"--crdposnodehotfixheight" usage:"CRDPoSNodeHotFixHeight indicates the hot fix start height of CR DPoS node"`
// NFTStartHeight defines the height of NFT started.
NFTStartHeight uint32 `screw:"--nftstartheight" usage:"the start height of NFT transaction"`
// SponsorsFilePath defines the sponsors file path.
SponsorsFilePath string `screw:"--sponsorsfilepath" usage:"defines the sponsors file path"`
// RecordSponsorStartHeight defines the start height to record sponsor.
RecordSponsorStartHeight uint32 `screw:"--recordsponsorstartheight" usage:"defines the start height to record sponsor"`
}

type CRConfiguration struct {
Expand Down
3 changes: 3 additions & 0 deletions core/transaction/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func GetTransaction(txType common2.TxType) (txn interfaces.Transaction, err erro
case common2.VotesRealWithdraw:
txn = new(VotesRealWithdrawTransaction)

case common2.RecordSponsor:
txn = new(RecordSponsorTransaction)

case common2.DposV2ClaimReward:
txn = new(DPoSV2ClaimRewardTransaction)

Expand Down
106 changes: 106 additions & 0 deletions core/transaction/recordsponsortransaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2017-2021 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//

package transaction

import (
"bytes"
"errors"
"fmt"
"github.com/elastos/Elastos.ELA/blockchain"
"github.com/elastos/Elastos.ELA/core/types/payload"
elaerr "github.com/elastos/Elastos.ELA/errors"
)

type RecordSponsorTransaction struct {
BaseTransaction
}

func (t *RecordSponsorTransaction) HeightVersionCheck() error {
blockHeight := t.parameters.BlockHeight
chainParams := t.parameters.Config

if t.payloadVersion != 0 {
return errors.New("invalid payload version, need to be zero")
}

if blockHeight < chainParams.DPoSConfiguration.RecordSponsorStartHeight {
return fmt.Errorf("not support %s transaction before RecordSponsorStartHeight", t.TxType().Name())
}

return nil
}

func (t *RecordSponsorTransaction) CheckTransactionInput() error {

if len(t.Inputs()) != 0 {
return errors.New("no cost transactions must has no input")
}

return nil
}

func (t *RecordSponsorTransaction) CheckTransactionOutput() error {

if len(t.Outputs()) != 0 {
return errors.New("no need to have output in sponsor transaction")
}

return nil
}

func (t *RecordSponsorTransaction) CheckAttributeProgram() error {

if len(t.Programs()) != 0 {
return errors.New("no need to have program in sponsor transaction")

}
if len(t.Attributes()) != 1 {
return errors.New("need to have one attribute in sponsor transaction")
}

return nil
}

func (t *RecordSponsorTransaction) CheckTransactionPayload() error {
switch t.Payload().(type) {
case *payload.RecordSponsor:
return nil
}

return errors.New("invalid payload type")
}

func (t *RecordSponsorTransaction) IsAllowedInPOWConsensus() bool {
return true
}

func (t *RecordSponsorTransaction) SpecialContextCheck() (elaerr.ELAError, bool) {
payloadRecordSponsor, ok := t.Payload().(*payload.RecordSponsor)
if !ok {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("record sponsor transaction has invalid payload")), true
}

// check sponsor is in current or last arbitrators
current, last := blockchain.DefaultLedger.Arbitrators.GetCurrentAndLastArbitrators()
exist := false
for _, currentArbiter := range current {
if bytes.Equal(currentArbiter.NodePublicKey, payloadRecordSponsor.Sponsor) {
exist = true
break
}
}
for _, lastArbiter := range last {
if bytes.Equal(lastArbiter.NodePublicKey, payloadRecordSponsor.Sponsor) {
exist = true
break
}
}
if !exist {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("sponsor is not in current or last arbitrators")), true
}

return nil, true
}
21 changes: 21 additions & 0 deletions core/transaction/temp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package transaction

import (
"fmt"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/utils"
"testing"
)

func Test_ReverTxID(t *testing.T) {
str := "65dd4737e9a85030d341653ea21005bb132200a97ad8cc8555f4c28ae2e16d71"
txHash, _ := common.Uint256FromHexString(str)
fmt.Println(common.ToReversedString(*txHash))

code, _ := common.HexStringToBytes("2103997349de5629299fd2b8d255c99d6b2047c6fcfa0237e9d1b07e5ac8db45f310ac")
addr, _ := utils.GetAddressByCode(code)
fmt.Println("addr:", addr)

saddr, _ := utils.GetStakeAddressByCode(code)
fmt.Println("saddr:", saddr)
}
6 changes: 5 additions & 1 deletion core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (tx *BaseTransaction) IsIllegalTypeTx() bool {
return tx.IsIllegalProposalTx() || tx.IsIllegalVoteTx() || tx.IsIllegalBlockTx() || tx.IsSidechainIllegalDataTx()
}

//special tx is this kind of tx who have no input and output
// special tx is this kind of tx who have no input and output
func (tx *BaseTransaction) IsSpecialTx() bool {
if tx.IsIllegalTypeTx() || tx.IsInactiveArbitrators() || tx.IsNextTurnDPOSInfoTx() {
return true
Expand Down Expand Up @@ -538,6 +538,10 @@ func (tx *BaseTransaction) IsCreateNFTTX() bool {
return tx.txType == common2.CreateNFT
}

func (tx *BaseTransaction) IsRecordSponorTx() bool {
return tx.txType == common2.RecordSponsor
}

// SerializeSizeStripped returns the number of bytes it would take to serialize
// the block, excluding any witness data (if any).
func (tx *BaseTransaction) SerializeSizeStripped() int {
Expand Down
3 changes: 3 additions & 0 deletions core/types/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const (
Voting TxType = 0x63
ReturnVotes TxType = 0x64
VotesRealWithdraw TxType = 0x65
RecordSponsor TxType = 0x66

// NFT
CreateNFT TxType = 0x71
Expand Down Expand Up @@ -161,6 +162,8 @@ func (self TxType) Name() string {
return "ReturnVotes"
case VotesRealWithdraw:
return "VotesRealWithdraw"
case RecordSponsor:
return "RecordSponsor"
case DposV2ClaimReward:
return "DposV2ClaimReward"
case DposV2ClaimRewardRealWithdraw:
Expand Down
1 change: 1 addition & 0 deletions core/types/interfaces/basetransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ type Transaction interface {
IsDposV2ClaimRewardRealWithdraw() bool
IsVotesRealWithdrawTX() bool
IsCreateNFTTX() bool
IsRecordSponorTx() bool
}
2 changes: 2 additions & 0 deletions core/types/interfaces/initpayload.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func GetPayload(txType common.TxType, payloadVersion byte) (Payload, error) {
p = new(payload.ReturnVotes)
case common.VotesRealWithdraw:
p = new(payload.VotesRealWithdrawPayload)
case common.RecordSponsor:
p = new(payload.RecordSponsor)
case common.DposV2ClaimReward:
p = new(payload.DPoSV2ClaimReward)
case common.DposV2ClaimRewardRealWithdraw:
Expand Down
46 changes: 46 additions & 0 deletions core/types/payload/recordsponsor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2017-2020 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//

package payload

import (
"errors"
"io"

"github.com/elastos/Elastos.ELA/common"
)

const RecordSponsorVersion byte = 0x00

const SponsorMaxLength = 33

type RecordSponsor struct {
Sponsor []byte
}

func (a *RecordSponsor) Data(version byte) []byte {
//TODO: implement RegisterRecord.Data()
return []byte{0}
}

// Serialize is the implement of SignableData interface.
func (a *RecordSponsor) Serialize(w io.Writer, version byte) error {
err := common.WriteVarBytes(w, a.Sponsor)
if err != nil {
return errors.New("[RecordSponsor], Sponsor serialize failed.")
}
return nil
}

// Deserialize is the implement of SignableData interface.
func (a *RecordSponsor) Deserialize(r io.Reader, version byte) error {
var err error
a.Sponsor, err = common.ReadVarBytes(r, SponsorMaxLength,
"payload record data")
if err != nil {
return errors.New("[RecordSponsor], Sponsor deserialize failed.")
}
return nil
}
Loading

0 comments on commit bbf93f7

Please sign in to comment.