Skip to content

Commit

Permalink
Refactor: played with hive.go framework structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Nov 2, 2023
1 parent a0bde5c commit 0c33362
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 305 deletions.
28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ require (
github.com/google/uuid v1.3.1
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42
github.com/iotaledger/hive.go/ads v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/app v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/constraints v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/crypto v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/ds v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/ierrors v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/kvstore v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/lo v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/log v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/logger v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/runtime v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231102135205-0f3d1e75042a
github.com/iotaledger/hive.go/stringify v0.0.0-20231102135205-0f3d1e75042a
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231020152103-b6ea7ff7a4af
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231020151337-569450d5bf7d
github.com/iotaledger/iota.go/v4 v4.0.0-20231019174124-aa2290512bcd
Expand Down
56 changes: 28 additions & 28 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,34 @@ github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJ
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7 h1:dTrD7X2PTNgli6EbS4tV9qu3QAm/kBU3XaYZV2xdzys=
github.com/iotaledger/grocksdb v1.7.5-0.20230220105546-5162e18885c7/go.mod h1:ZRdPu684P0fQ1z8sXz4dj9H5LWHhz4a9oCtvjunkSrw=
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42 h1:EOfxTuAiBmED1VHuVh7/UIeB27cCRe13gdSzyioNMBw=
github.com/iotaledger/hive.go/ads v0.0.0-20231027195901-620bd7470e42/go.mod h1:IFh0gDfeMgZtfCo+5afK59IDR4xXh+cTR9YtLnZPcbY=
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42 h1:xAER9M9Uoz2EOWT43E9wmXRe+RmAk8OBSUoboH4Su8M=
github.com/iotaledger/hive.go/app v0.0.0-20231027195901-620bd7470e42/go.mod h1:8ZbIKR84oQd/3iQ5eeT7xpudO9/ytzXP7veIYnk7Orc=
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42 h1:drmpgLlJy7kZ09Dt1qKSnbILU+27Qu2jp4VdPDNwbFk=
github.com/iotaledger/hive.go/constraints v0.0.0-20231027195901-620bd7470e42/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42 h1:BC5GkIHyXdoJGdw6Tu5ds2kjw9grFLtwQiuMaKfdLU8=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231027195901-620bd7470e42/go.mod h1:Mc+ACqBGPxrPMIPUBOm6/HL0J6m0iVMwjtIEKW3uow8=
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42 h1:r8TkdQJB7/bJd8cF8z5GQ+rX/7JpbPdPoN7wMoV1OCM=
github.com/iotaledger/hive.go/crypto v0.0.0-20231027195901-620bd7470e42/go.mod h1:h3o6okvMSEK3KOX6pOp3yq1h9ohTkTfo6X8MzEadeb0=
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42 h1:ytzZZPtclAzLfjxv26frbinCGx3Z6ouUENbx5U7lFGg=
github.com/iotaledger/hive.go/ds v0.0.0-20231027195901-620bd7470e42/go.mod h1:3XkUSKfHaVxGbT0XAvjNlVYqPzhfLTGhDtdNA5UBPco=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42 h1:QMxd32Y/veVhTDPCiOFgetjUbG7sr9MryF29/rSPkMA=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231027195901-620bd7470e42/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42 h1:/xPwStUckZ2V0XPoY496cXU+c5elpHyvYoT6JAmuvRY=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231027195901-620bd7470e42/go.mod h1:O/U3jtiUDeqqM0MZQFu2UPqS9fUm0C5hNISxlmg/thE=
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42 h1:AvNLzONVMspwx7nD/NyYUgb5Hi7/zgzIOegr1uRD/M8=
github.com/iotaledger/hive.go/lo v0.0.0-20231027195901-620bd7470e42/go.mod h1:s4kzx9QY1MVWHJralj+3q5kI0eARtrJhphYD/iBbPfo=
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42 h1:e1uJAlXE3zeXpa+c4uFOG+/AMFbUlLt2mcrSK5NMxVs=
github.com/iotaledger/hive.go/log v0.0.0-20231027195901-620bd7470e42/go.mod h1:JvokzmpmFZPDskMlUqqjgHtD8usVJU4nAY/TNMGge8M=
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42 h1:7wjs4t1snBDJ8LOTl+tZhr2ORywSOTgJMppxiIAMA0A=
github.com/iotaledger/hive.go/logger v0.0.0-20231027195901-620bd7470e42/go.mod h1:aBfAfIB2GO/IblhYt5ipCbyeL9bXSNeAwtYVA3hZaHg=
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42 h1:1QMJ39qXIx/IZVzus3+97IV7Pa++e+d340TvbMjhiBU=
github.com/iotaledger/hive.go/runtime v0.0.0-20231027195901-620bd7470e42/go.mod h1:jRw8yFipiPaqmTPHh7hTcxAP9u6pjRGpByS3REJKkbY=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42 h1:hZli4E9kJUAEQ7gzZR1XbPcpgqvqMPYq8YBPMbrBuos=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231027195901-620bd7470e42/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42 h1:OlDhgvJ48bZxcvTeebJ1b96xtNnJAddejd2Q4rlH1mU=
github.com/iotaledger/hive.go/stringify v0.0.0-20231027195901-620bd7470e42/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/hive.go/ads v0.0.0-20231102135205-0f3d1e75042a h1:ORysxrc6QCG/uX0eHPmrl00isL+K37Gi7CPXWkWvKpA=
github.com/iotaledger/hive.go/ads v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:IFh0gDfeMgZtfCo+5afK59IDR4xXh+cTR9YtLnZPcbY=
github.com/iotaledger/hive.go/app v0.0.0-20231102135205-0f3d1e75042a h1:sNID0417k8UPZAHZjzx9cfPdDLD1J3EV89nWwRXkKA4=
github.com/iotaledger/hive.go/app v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:8ZbIKR84oQd/3iQ5eeT7xpudO9/ytzXP7veIYnk7Orc=
github.com/iotaledger/hive.go/constraints v0.0.0-20231102135205-0f3d1e75042a h1:kB5JFbOsZj/pBIcqnIMEnwk9XRBXZC6bEo08M6kFPaY=
github.com/iotaledger/hive.go/constraints v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231102135205-0f3d1e75042a h1:shooidIGLQm8wt7hzcx2hWXuC3ce4XLjIqB920R1V4U=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231102135205-0f3d1e75042a/go.mod h1:Mc+ACqBGPxrPMIPUBOm6/HL0J6m0iVMwjtIEKW3uow8=
github.com/iotaledger/hive.go/crypto v0.0.0-20231102135205-0f3d1e75042a h1:B1/zDZJDnBgCh+hwxzHyjGrUM1MTLumHwKZei+5Aehg=
github.com/iotaledger/hive.go/crypto v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:h3o6okvMSEK3KOX6pOp3yq1h9ohTkTfo6X8MzEadeb0=
github.com/iotaledger/hive.go/ds v0.0.0-20231102135205-0f3d1e75042a h1:hAsljqcUaL2ywSnztVBoaNh0cJwqaqNfPfTrUSLT9xo=
github.com/iotaledger/hive.go/ds v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:3XkUSKfHaVxGbT0XAvjNlVYqPzhfLTGhDtdNA5UBPco=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231102135205-0f3d1e75042a h1:c3XbjBDMiqo7jilZzZd+kmWrYvzufKmbHUOcOE3NpdI=
github.com/iotaledger/hive.go/ierrors v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:HcE8B5lP96enc/OALTb2/rIIi+yOLouRoHOKRclKmC8=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231102135205-0f3d1e75042a h1:2RZenapa2UI/Vj4vLNIlUgFUN1jJICV2XsZiIo9jQT4=
github.com/iotaledger/hive.go/kvstore v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:O/U3jtiUDeqqM0MZQFu2UPqS9fUm0C5hNISxlmg/thE=
github.com/iotaledger/hive.go/lo v0.0.0-20231102135205-0f3d1e75042a h1:cHjCgpSdnrOCc69YPyntVVO+dFmVQh9kDj+rVpoJNlE=
github.com/iotaledger/hive.go/lo v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:s4kzx9QY1MVWHJralj+3q5kI0eARtrJhphYD/iBbPfo=
github.com/iotaledger/hive.go/log v0.0.0-20231102135205-0f3d1e75042a h1:PU+z5uVPUEVzmQf0JRLxJjnfMzdtz6mlnToECVzdVFQ=
github.com/iotaledger/hive.go/log v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:JvokzmpmFZPDskMlUqqjgHtD8usVJU4nAY/TNMGge8M=
github.com/iotaledger/hive.go/logger v0.0.0-20231102135205-0f3d1e75042a h1:8RigRlTTTBsT5Ik/Jz6VcLWgIzbx9HynzgnRq9/VhJE=
github.com/iotaledger/hive.go/logger v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:aBfAfIB2GO/IblhYt5ipCbyeL9bXSNeAwtYVA3hZaHg=
github.com/iotaledger/hive.go/runtime v0.0.0-20231102135205-0f3d1e75042a h1:vYyd5E4j02kwbAhx+nivcc7DPpmI46S72GBGGGpalSs=
github.com/iotaledger/hive.go/runtime v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:jRw8yFipiPaqmTPHh7hTcxAP9u6pjRGpByS3REJKkbY=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231102135205-0f3d1e75042a h1:/xwSejpRNjjv20gX9kQZ1Pgy+m6kFHcUMt1u/f+kc1Y=
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20231102135205-0f3d1e75042a/go.mod h1:SdK26z8/VhWtxaqCuQrufm80SELgowQPmu9T/8eUQ8g=
github.com/iotaledger/hive.go/stringify v0.0.0-20231102135205-0f3d1e75042a h1:DNcRsEdvfnrfwh9Mb19lHfsz5Izf9BiMqJILYYW0SvU=
github.com/iotaledger/hive.go/stringify v0.0.0-20231102135205-0f3d1e75042a/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231020152103-b6ea7ff7a4af h1:3+heabXNGjv3Sx1sg0qG+QdqeEWIpf7jM3T5cPiVmSA=
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231020152103-b6ea7ff7a4af/go.mod h1:3BZ5r/MhswUZwXHDzMZacOhm6DWpUdGjefNRNshfwiM=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231020151337-569450d5bf7d h1:PeJNFBkR28hdQCxUyC/cbOlDjFm3D1U7O5bxi+qsOvQ=
Expand Down
105 changes: 0 additions & 105 deletions pkg/core/rx/define.go

This file was deleted.

60 changes: 25 additions & 35 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/iotaledger/hive.go/ds/reactive"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/iota-core/pkg/core/rx"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
)
Expand Down Expand Up @@ -81,51 +80,47 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
c.IsAttested.InheritFrom(c.IsRoot),
c.IsVerified.InheritFrom(c.IsRoot),

rx.WithNonEmptyValue(c.Parent, func(parent *Commitment) func() {
reactive.WithNonEmptyValue(func(parent *Commitment) func() {
c.Weight.Set(c.CumulativeWeight() - parent.CumulativeWeight())

parent.registerChild(c)

return lo.Batch(
rx.AssignValue[*Chain](c.Chain, reactive.NewDerivedVariable2(func(parentChain, spawnedChain *Chain) *Chain {
if spawnedChain != nil {
return spawnedChain
}
c.inheritChainFrom(parent),

return parentChain
c.Chain.DeriveValueFrom(reactive.NewDerivedVariable2(func(parentChain, spawnedChain *Chain) *Chain {
return lo.Cond(spawnedChain != nil, spawnedChain, parentChain)
}, parent.Chain, c.SpawnedChain)),

rx.AssignValue[bool](c.IsSolid, reactive.NewDerivedVariable(func(parentIsSolid bool) bool {
c.IsSolid.DeriveValueFrom(reactive.NewDerivedVariable(func(parentIsSolid bool) bool {
return parentIsSolid
}, parent.IsSolid)),

rx.AssignValue[uint64](c.CumulativeAttestedWeight, reactive.NewDerivedVariable2(func(parentCumulativeAttestedWeight, attestedWeight uint64) uint64 {
c.CumulativeAttestedWeight.DeriveValueFrom(reactive.NewDerivedVariable2(func(parentCumulativeAttestedWeight, attestedWeight uint64) uint64 {
return parentCumulativeAttestedWeight + attestedWeight
}, parent.CumulativeAttestedWeight, c.AttestedWeight)),

rx.AssignValue[bool](c.IsAboveLatestVerifiedCommitment, reactive.NewDerivedVariable3(func(parentAboveLatestVerifiedCommitment, parentIsVerified, isVerified bool) bool {
c.IsAboveLatestVerifiedCommitment.DeriveValueFrom(reactive.NewDerivedVariable3(func(parentAboveLatestVerifiedCommitment, parentIsVerified, isVerified bool) bool {
return parentAboveLatestVerifiedCommitment || (parentIsVerified && !isVerified)
}, parent.IsAboveLatestVerifiedCommitment, parent.IsVerified, c.IsVerified)),

rx.WithNonEmptyValue(c.Chain, func(chain *Chain) func() {
reactive.WithNonEmptyValue(func(chain *Chain) func() {
return lo.Batch(
rx.AssignValue[bool](c.InSyncRange, reactive.NewDerivedVariable3(func(spawnedEngine *engine.Engine, warpSyncing, isAboveLatestVerifiedCommitment bool) bool {
c.InSyncRange.DeriveValueFrom(reactive.NewDerivedVariable3(func(spawnedEngine *engine.Engine, warpSyncing, isAboveLatestVerifiedCommitment bool) bool {
return spawnedEngine != nil && !warpSyncing && isAboveLatestVerifiedCommitment
}, chain.SpawnedEngine, chain.WarpSync, c.IsAboveLatestVerifiedCommitment)),

rx.AssignValue[bool](c.WarpSync, reactive.NewDerivedVariable4(func(spawnedEngine *engine.Engine, warpSync, parentIsVerified, isVerified bool) bool {
c.WarpSync.DeriveValueFrom(reactive.NewDerivedVariable4(func(spawnedEngine *engine.Engine, warpSync, parentIsVerified, isVerified bool) bool {
return spawnedEngine != nil && warpSync && parentIsVerified && !isVerified
}, chain.SpawnedEngine, chain.WarpSync, parent.IsVerified, c.IsVerified)),

rx.AssignValue[bool](c.RequestAttestations, reactive.NewDerivedVariable3(func(verifyAttestations, parentIsAttested, isAttested bool) bool {
c.RequestAttestations.DeriveValueFrom(reactive.NewDerivedVariable3(func(verifyAttestations, parentIsAttested, isAttested bool) bool {
return verifyAttestations && parentIsAttested && !isAttested
}, chain.VerifyAttestations, parent.IsAttested, c.IsAttested)),
)
}),
}, c.Chain),
)
}),
}, c.Parent),

rx.WithNonEmptyValue(c.Chain, func(chain *Chain) func() { return chain.registerCommitment(c) }),
reactive.WithNonEmptyValue(func(chain *Chain) func() { return chain.registerCommitment(c) }, c.Chain),
)

c.IsEvicted.OnTrigger(unsubscribe)
Expand All @@ -134,28 +129,23 @@ func NewCommitment(commitment *model.Commitment, protocol *Protocol) *Commitment
}

func (c *Commitment) Engine() *engine.Engine {
chain := c.Chain.Get()
if chain == nil {
return nil
if chain := c.Chain.Get(); chain != nil {
return chain.Engine.Get()
}

return chain.Engine.Get()
return nil
}

func (c *Commitment) registerChild(child *Commitment) {
if c.Children.Add(child) {
c.MainChild.Compute(func(currentMainChild *Commitment) *Commitment {
if currentMainChild != nil {
return currentMainChild
}

return child
})
func (c *Commitment) inheritChainFrom(parent *Commitment) (unregister func()) {
if !parent.Children.Add(c) {
return func() {}
}

unsubscribeChild := c.MainChild.OnUpdate(child.inheritChain(c))
parent.MainChild.Compute(func(mainChild *Commitment) *Commitment {
return lo.Cond(mainChild != nil, mainChild, c)
})

c.IsEvicted.OnTrigger(unsubscribeChild)
}
return parent.MainChild.OnUpdate(c.inheritChain(parent))
}

func (c *Commitment) inheritChain(parent *Commitment) func(*Commitment, *Commitment) {
Expand Down
Loading

0 comments on commit 0c33362

Please sign in to comment.