Skip to content

Commit

Permalink
[protocol] pre-commit at every block
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Oct 13, 2022
1 parent cad56e6 commit 7d34006
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
46 changes: 46 additions & 0 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package staking
import (
"context"
"encoding/hex"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
"sort"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -81,6 +83,9 @@ type (
candBucketsIndexer *CandidatesBucketsIndexer
voteReviser *VoteReviser
patch *PatchStore
_nameHash string
_opHash string
_ownerHash string
}

// Configuration is the staking protocol configuration.
Expand Down Expand Up @@ -635,6 +640,47 @@ func readCandCenterStateFromStateDB(sr protocol.StateReader, height uint64) (Can
}

func (p *Protocol) writeCandCenterStateToStateDB(sm protocol.StateManager, name, op, owners CandidateList) error {
{
sort.Sort(name)
ser, err := name.Serialize()
if err != nil {
return err
}
h := crypto.Keccak256Hash(ser)
if h.String() != p._nameHash {
p._nameHash = h.String()
} else {
println("===================== same name list", p._nameHash)
}

sort.Sort(op)
ser, err = op.Serialize()
if err != nil {
return err
}
h = crypto.Keccak256Hash(ser)
if h.String() != p._opHash {
p._opHash = h.String()
} else {
println("===================== same operator list", p._opHash)
}

ser, err = owners.Serialize()
if err != nil {
return err
}
h = crypto.Keccak256Hash(ser)
if h.String() != p._ownerHash {
if len(p._ownerHash) > 0 {
println("======= old owner", p._ownerHash)
println("======= new owner", h.String())
}
p._ownerHash = h.String()
} else {
println("======= same owner list", p._ownerHash)
}
}

if _, err := sm.PutState(name, protocol.NamespaceOption(CandsMapNS), protocol.KeyOption(_nameKey)); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions state/factory/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ func generateWorkingSetCacheKey(blkHeader block.Header, producerAddr string) has
func protocolPreCommit(ctx context.Context, sr protocol.StateManager) error {
if reg, ok := protocol.GetRegistry(ctx); ok {
for _, p := range reg.All() {
post, ok := p.(protocol.PreCommitter)
if ok && sr.ProtocolDirty(p.Name()) {
if post, ok := p.(protocol.PreCommitter); ok {
if err := post.PreCommit(ctx, sr); err != nil {
return err
}
Expand Down

0 comments on commit 7d34006

Please sign in to comment.