aboutsummaryrefslogtreecommitdiffstats
path: root/core/vm/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/vm/utils.go')
-rw-r--r--core/vm/utils.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/core/vm/utils.go b/core/vm/utils.go
new file mode 100644
index 000000000..0cc6343cc
--- /dev/null
+++ b/core/vm/utils.go
@@ -0,0 +1,92 @@
+package vm
+
+import (
+ "errors"
+ "math/big"
+
+ "github.com/tangerine-network/go-tangerine/common"
+ "github.com/tangerine-network/go-tangerine/core/state"
+ "github.com/tangerine-network/go-tangerine/crypto"
+ "github.com/tangerine-network/go-tangerine/log"
+ dexCore "github.com/tangerine-network/tangerine-consensus/core"
+)
+
+type GovUtilInterface interface {
+ GetHeadGovState() (*GovernanceState, error)
+ StateAt(height uint64) (*state.StateDB, error)
+}
+
+type GovUtil struct {
+ Intf GovUtilInterface
+}
+
+func (g GovUtil) GetRoundHeight(round uint64) uint64 {
+ gs, err := g.Intf.GetHeadGovState()
+ if err != nil {
+ return 0
+ }
+ return gs.RoundHeight(big.NewInt(int64(round))).Uint64()
+}
+
+func (g GovUtil) GetStateAtRound(round uint64) (*GovernanceState, error) {
+ height := g.GetRoundHeight(round)
+
+ if round != 0 && height == 0 {
+ log.Error("Governance state incorrect", "round", round, "got height", height)
+ return nil, errors.New("incorrect governance state")
+ }
+
+ s, err := g.Intf.StateAt(height)
+ if err != nil {
+ return nil, err
+ }
+ return &GovernanceState{StateDB: s}, nil
+}
+
+func (g GovUtil) GetConfigState(round uint64) (*GovernanceState, error) {
+ if round < dexCore.ConfigRoundShift {
+ round = 0
+ } else {
+ round -= dexCore.ConfigRoundShift
+ }
+ return g.GetStateAtRound(round)
+}
+
+func (g *GovUtil) CRSRound() uint64 {
+ gs, err := g.Intf.GetHeadGovState()
+ if err != nil {
+ return 0
+ }
+ return gs.CRSRound().Uint64()
+}
+
+func (g GovUtil) CRS(round uint64) common.Hash {
+ if round <= dexCore.DKGDelayRound {
+ s, err := g.GetStateAtRound(0)
+ if err != nil {
+ return common.Hash{}
+ }
+ crs := s.CRS()
+ for i := uint64(0); i < round; i++ {
+ crs = crypto.Keccak256Hash(crs[:])
+ }
+ return crs
+ }
+ if round > g.CRSRound() {
+ return common.Hash{}
+ }
+ var s *GovernanceState
+ var err error
+ if round == g.CRSRound() {
+ s, err = g.Intf.GetHeadGovState()
+ if err != nil {
+ return common.Hash{}
+ }
+ } else {
+ s, err = g.GetStateAtRound(round)
+ if err != nil {
+ return common.Hash{}
+ }
+ }
+ return s.CRS()
+}