This commit is contained in:
pavelkrolevets 2018-08-22 15:34:37 +08:00
commit 197fcd5445

View file

@ -5,12 +5,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"encoding/binary"
"github.com/pavelkrolevets/go-ethereum/common" "github.com/pavelkrolevets/go-ethereum/common"
"github.com/pavelkrolevets/go-ethereum/crypto/sha3" "github.com/pavelkrolevets/go-ethereum/crypto/sha3"
"github.com/pavelkrolevets/go-ethereum/ethdb"
"github.com/pavelkrolevets/go-ethereum/rlp" "github.com/pavelkrolevets/go-ethereum/rlp"
"github.com/pavelkrolevets/go-ethereum/trie" "github.com/pavelkrolevets/go-ethereum/trie"
"encoding/binary"
"github.com/pavelkrolevets/go-ethereum/ethdb"
) )
type LCPContext struct { type LCPContext struct {
@ -27,12 +27,12 @@ type LCPContext struct {
} }
var ( var (
epochPrefix = []byte("epoch-") epochPrefix = []byte("epoch-")
delegatePrefix = []byte("delegate-") delegatePrefix = []byte("delegate-")
votePrefix = []byte("vote-") votePrefix = []byte("vote-")
candidatePrefix = []byte("candidate-") candidatePrefix = []byte("candidate-")
mintCntPrefix = []byte("mintCnt-") mintCntPrefix = []byte("mintCnt-")
periodBlockPrefix = []byte("Period-") periodBlockPrefix = []byte("Period-")
maxValidatorsPrefix = []byte("MaxValidator-") maxValidatorsPrefix = []byte("MaxValidator-")
epochIntervalPrefix = []byte("EpochInterval-") epochIntervalPrefix = []byte("EpochInterval-")
) )
@ -115,7 +115,7 @@ func NewLCPContext(db ethdb.Database) (*LCPContext, error) {
voteTrie: voteTrie, voteTrie: voteTrie,
candidateTrie: candidateTrie, candidateTrie: candidateTrie,
mintCntTrie: mintCntTrie, mintCntTrie: mintCntTrie,
periodBlock: periodTrie, periodBlock: periodTrie,
maxValidators: maxValidatorTrie, maxValidators: maxValidatorTrie,
epochInterval: epochIntervalTrie, epochInterval: epochIntervalTrie,
db: db, db: db,
@ -161,7 +161,7 @@ func NewLCPContextFromProto(db ethdb.Database, ctxProto *LCPContextProto) (*LCPC
voteTrie: voteTrie, voteTrie: voteTrie,
candidateTrie: candidateTrie, candidateTrie: candidateTrie,
mintCntTrie: mintCntTrie, mintCntTrie: mintCntTrie,
periodBlock: periodTrie, periodBlock: periodTrie,
maxValidators: maxValidatorTrie, maxValidators: maxValidatorTrie,
epochInterval: epochIntervalTrie, epochInterval: epochIntervalTrie,
db: db, db: db,
@ -174,9 +174,9 @@ func (d *LCPContext) Copy() *LCPContext {
voteTrie := *d.voteTrie voteTrie := *d.voteTrie
candidateTrie := *d.candidateTrie candidateTrie := *d.candidateTrie
mintCntTrie := *d.mintCntTrie mintCntTrie := *d.mintCntTrie
periodTrie:= *d.periodBlock periodTrie := *d.periodBlock
maxValidatorTrie:= *d.maxValidators maxValidatorTrie := *d.maxValidators
epochIntervalTrie:= *d.epochInterval epochIntervalTrie := *d.epochInterval
return &LCPContext{ return &LCPContext{
epochTrie: &epochTrie, epochTrie: &epochTrie,
delegateTrie: &delegateTrie, delegateTrie: &delegateTrie,
@ -256,25 +256,23 @@ func (d *LCPContext) FromProto(dcp *LCPContextProto) error {
} }
type LCPContextProto struct { type LCPContextProto struct {
EpochHash common.Hash `json:"epochRoot" gencodec:"required"` EpochHash common.Hash `json:"epochRoot" gencodec:"required"`
DelegateHash common.Hash `json:"delegateRoot" gencodec:"required"` DelegateHash common.Hash `json:"delegateRoot" gencodec:"required"`
CandidateHash common.Hash `json:"candidateRoot" gencodec:"required"` CandidateHash common.Hash `json:"candidateRoot" gencodec:"required"`
VoteHash common.Hash `json:"voteRoot" gencodec:"required"` VoteHash common.Hash `json:"voteRoot" gencodec:"required"`
MintCntHash common.Hash `json:"mintCntRoot" gencodec:"required"` MintCntHash common.Hash `json:"mintCntRoot" gencodec:"required"`
periodBlockHash common.Hash `json:"periodBlockRoot" gencodec:"required"` periodBlockHash common.Hash `json:"periodBlockRoot" gencodec:"required"`
maxValidatorsHash common.Hash `json:"maxValidatorRoot" gencodec:"required"` maxValidatorsHash common.Hash `json:"maxValidatorRoot" gencodec:"required"`
epochIntervalHash common.Hash `json:"epochIntervalRoot" gencodec:"required"` epochIntervalHash common.Hash `json:"epochIntervalRoot" gencodec:"required"`
} }
func (d *LCPContext) ToProto() *LCPContextProto { func (d *LCPContext) ToProto() *LCPContextProto {
return &LCPContextProto{ return &LCPContextProto{
EpochHash: d.epochTrie.Hash(), EpochHash: d.epochTrie.Hash(),
DelegateHash: d.delegateTrie.Hash(), DelegateHash: d.delegateTrie.Hash(),
CandidateHash: d.candidateTrie.Hash(), CandidateHash: d.candidateTrie.Hash(),
VoteHash: d.voteTrie.Hash(), VoteHash: d.voteTrie.Hash(),
MintCntHash: d.mintCntTrie.Hash(), MintCntHash: d.mintCntTrie.Hash(),
periodBlockHash: d.periodBlock.Hash(), periodBlockHash: d.periodBlock.Hash(),
maxValidatorsHash: d.maxValidators.Hash(), maxValidatorsHash: d.maxValidators.Hash(),
epochIntervalHash: d.epochInterval.Hash(), epochIntervalHash: d.epochInterval.Hash(),
@ -389,6 +387,7 @@ func (d *LCPContext) UnDelegate(delegatorAddr, candidateAddr common.Address) err
} }
return d.voteTrie.TryDelete(delegator) return d.voteTrie.TryDelete(delegator)
} }
//function to write tries to memory //function to write tries to memory
func (d *LCPContext) CommitTo(db ethdb.Database) (*LCPContextProto, error) { func (d *LCPContext) CommitTo(db ethdb.Database) (*LCPContextProto, error) {
epochRoot, err := d.epochTrie.Commit(nil) epochRoot, err := d.epochTrie.Commit(nil)
@ -425,12 +424,12 @@ func (d *LCPContext) CommitTo(db ethdb.Database) (*LCPContextProto, error) {
return nil, err return nil, err
} }
return &LCPContextProto{ return &LCPContextProto{
EpochHash: epochRoot, EpochHash: epochRoot,
DelegateHash: delegateRoot, DelegateHash: delegateRoot,
VoteHash: voteRoot, VoteHash: voteRoot,
CandidateHash: candidateRoot, CandidateHash: candidateRoot,
MintCntHash: mintCntRoot, MintCntHash: mintCntRoot,
periodBlockHash: blockPeriod, periodBlockHash: blockPeriod,
maxValidatorsHash: maxVal, maxValidatorsHash: maxVal,
epochIntervalHash: epochInterv, epochIntervalHash: epochInterv,
}, nil }, nil
@ -451,7 +450,6 @@ func (dc *LCPContext) SetVote(vote *trie.Trie) { dc.voteTrie = vote }
func (dc *LCPContext) SetCandidate(candidate *trie.Trie) { dc.candidateTrie = candidate } func (dc *LCPContext) SetCandidate(candidate *trie.Trie) { dc.candidateTrie = candidate }
func (dc *LCPContext) SetMintCnt(mintCnt *trie.Trie) { dc.mintCntTrie = mintCnt } func (dc *LCPContext) SetMintCnt(mintCnt *trie.Trie) { dc.mintCntTrie = mintCnt }
func (dc *LCPContext) GetValidators() ([]common.Address, error) { func (dc *LCPContext) GetValidators() ([]common.Address, error) {
var validators []common.Address var validators []common.Address
key := []byte("validator") key := []byte("validator")
@ -473,48 +471,48 @@ func (dc *LCPContext) SetValidators(validators []common.Address) error {
} }
// Set and Get block creation speed // Set and Get block creation speed
func (dc *LCPContext) SetPeriodBlock(period int64) error { func (dc *LCPContext) SetPeriodBlock(period int64) error {
key:= []byte("BlockPeriod") key := []byte("BlockPeriod")
periodByte :=make([]byte, 8) periodByte := make([]byte, 8)
binary.LittleEndian.PutUint64(periodByte,uint64(period)) binary.LittleEndian.PutUint64(periodByte, uint64(period))
dc.epochTrie.Update(key, periodByte) dc.epochTrie.Update(key, periodByte)
return nil return nil
} }
func (dc *LCPContext) GetPeriodBlock() (int64) { func (dc *LCPContext) GetPeriodBlock() int64 {
key:= []byte("BlockPeriod") key := []byte("BlockPeriod")
periodRLP := dc.epochTrie.Get(key) periodRLP := dc.epochTrie.Get(key)
period := int64(binary.LittleEndian.Uint64(periodRLP)) period := int64(binary.LittleEndian.Uint64(periodRLP))
return period return period
} }
//Set and Get maximim validators //Set and Get maximim validators
func (dc *LCPContext) SetMaxValidators(maxVal int64) error { func (dc *LCPContext) SetMaxValidators(maxVal int64) error {
key:= []byte("MaxValidators") key := []byte("MaxValidators")
maxValByte :=make([]byte, 8) maxValByte := make([]byte, 8)
binary.LittleEndian.PutUint64(maxValByte,uint64(maxVal)) binary.LittleEndian.PutUint64(maxValByte, uint64(maxVal))
dc.epochTrie.Update(key, maxValByte) dc.epochTrie.Update(key, maxValByte)
return nil return nil
} }
func (dc *LCPContext) GetMaxValidators() (int64) { func (dc *LCPContext) GetMaxValidators() int64 {
key:= []byte("MaxValidators") key := []byte("MaxValidators")
maxValRLP := dc.epochTrie.Get(key) maxValRLP := dc.epochTrie.Get(key)
maxVal := int64(binary.LittleEndian.Uint64(maxValRLP)) maxVal := int64(binary.LittleEndian.Uint64(maxValRLP))
return maxVal return maxVal
} }
//Set and Get epoch interval //Set and Get epoch interval
func (dc *LCPContext) SetEpochInterval(epochInt int64) error { func (dc *LCPContext) SetEpochInterval(epochInt int64) error {
key:= []byte("EpochInterval") key := []byte("EpochInterval")
epochIntByte :=make([]byte, 8) epochIntByte := make([]byte, 8)
binary.LittleEndian.PutUint64(epochIntByte,uint64(epochInt)) binary.LittleEndian.PutUint64(epochIntByte, uint64(epochInt))
dc.epochTrie.Update(key, epochIntByte) dc.epochTrie.Update(key, epochIntByte)
return nil return nil
} }
func (dc *LCPContext) GetEpochInterval() (int64) { func (dc *LCPContext) GetEpochInterval() int64 {
key:= []byte("EpochInterval") key := []byte("EpochInterval")
epochIntRLP := dc.epochTrie.Get(key) epochIntRLP := dc.epochTrie.Get(key)
period := int64(binary.LittleEndian.Uint64(epochIntRLP)) period := int64(binary.LittleEndian.Uint64(epochIntRLP))
return period return period
} }