Skip to content

Commit

Permalink
Make unfunded Commits and Commit Chain wiat on an EC address in the n…
Browse files Browse the repository at this point in the history
…ew holding
  • Loading branch information
factom-clay committed Feb 13, 2019
1 parent 5f787fc commit 912036d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 7 deletions.
11 changes: 10 additions & 1 deletion common/interfaces/state.go
Expand Up @@ -344,6 +344,15 @@ type IState interface {
CheckFileName(string) bool
AddToReplayFilter(mask int, hash [32]byte, timestamp Timestamp, systemtime Timestamp) bool

// Activations
// Activations -------------------------------------------------------
IsActive(id activations.ActivationType) bool

// Holding of dependent messages -------------------------------------
// Add a messsage to a dependent holding list
Add(h [32]byte, msg IMsg)
// get and remove the list of dependent message for a hash
Get(h [32]byte) []IMsg
// expire any dependent messages that are in holding but are older than limit
Review(limit Timestamp)
ExecuteFromHolding(h [32]byte)
}
5 changes: 4 additions & 1 deletion common/messages/commitChain.go
Expand Up @@ -137,7 +137,10 @@ func (m *CommitChainMsg) Validate(state interfaces.IState) int {
ebal := state.GetFactoidState().GetECBalance(*m.CommitChain.ECPubKey)
v := int(ebal) - int(m.CommitChain.Credits)
if v < 0 {
return 0
// return 0 // old way add to scanned holding queue
// new holding mechanism added it to a list of messages dependent on the EC address
state.Add(m.CommitChain.ECPubKey.Fixed(), m)
return -2 // claim it's invalid so it doesnt get into the old holding queue
}

return 1
Expand Down
5 changes: 4 additions & 1 deletion common/messages/commitEntry.go
Expand Up @@ -253,7 +253,10 @@ func (m *CommitEntryMsg) Validate(state interfaces.IState) int {

ebal := state.GetFactoidState().GetECBalance(*m.CommitEntry.ECPubKey)
if int(m.CommitEntry.Credits) > int(ebal) {
return 0
// return 0 // old way add to scanned holding queue
// new holding mechanism added it to a list of messages dependent on the EC address
state.Add(m.CommitEntry.ECPubKey.Fixed(), m)
return -2 // claim it's invalid so it doesn't get into the old holding queue
}
return 1
}
Expand Down
84 changes: 84 additions & 0 deletions state/HoldingList.go
@@ -0,0 +1,84 @@
package state

import (
"fmt"
"github.com/FactomProject/factomd/common/interfaces"
)

// This hold a slice of messages dependent on a hash
type HoldingList struct {
holding map[[32]byte][]interfaces.IMsg
s *State // for debug logging
}

func (l *HoldingList) Init(s *State) {
l.holding = make(map[[32]byte][]interfaces.IMsg)
l.s = s
}

// Add a messsage to a dependent holding list
func (l *HoldingList) Add(h [32]byte, msg interfaces.IMsg) {

if l.holding[h] == nil {
l.holding[h] = []interfaces.IMsg{msg}
} else {
l.holding[h] = append(l.holding[h], msg)
}
}

// get and remove the list of dependent message for a hash
func (l *HoldingList) Get(h [32]byte) []interfaces.IMsg {
rval := l.holding[h]
l.holding[h] = nil
return rval
}

// expire any dependent messages that are in holding but are older than limit
func (l *HoldingList) Review(limit interfaces.Timestamp) {
for h := range l.holding {
for m := range l.holding[h] {
fmt.Print(m)
}
}
}

// Add a messsage to a dependent holding list
func (s *State) Add(h [32]byte, msg interfaces.IMsg) {
s.Hold.Add(h, msg)
}

// get and remove the list of dependent message for a hash
func (s *State) Get(h [32]byte) []interfaces.IMsg {
return s.Hold.Get(h)
}

// expire any dependent messages that are in holding but are older than limit
func (s *State) Review(limit interfaces.Timestamp) {
s.Hold.Review(limit)
}

// Execute a list of messages from holding that are dependant on a hash
// the hash may be a EC address or a CainID or a height (ok heights are not really hashes but we cheat on that)
func (s *State) ExecuteFromHolding(h [32]byte) {
// get the list of messages waiting on this hash
l := s.Get(h)
if l != nil {
// add the messages to the msgQueue so they get executed as space is available
func() {
for _, m := range l {
s.LogMessage("msgQueue", "enqueue_from_holding", m)
s.msgQueue <- m
}
}()
}
}

// put a height in the first 4 bytes of a hash so we can use it to look up dependent message in holding
func HeightToHash(height uint32) [32]byte {
var h [32]byte
h[0] = byte((height >> 24) & 0xFF)
h[1] = byte((height >> 16) & 0xFF)
h[2] = byte((height >> 8) & 0xFF)
h[3] = byte((height >> 0) & 0xFF)
return h
}
12 changes: 9 additions & 3 deletions state/factoidstate.go
Expand Up @@ -268,7 +268,7 @@ func (fs *FactoidState) UpdateECTransaction(rt bool, trans interfaces.IECBlockEn
fs.State.GetE(rt, t.ECPubKey.Fixed()),
t.Credits)
}
fs.State.PutE(rt, t.ECPubKey.Fixed(), v)
fs.State.PutE(rt, t.ECPubKey.Fixed(), v) // deduct Chain Commit
fs.State.NumTransactions++
fs.State.Replay.IsTSValid(constants.INTERNAL_REPLAY, t.GetSigHash(), t.GetTimestamp())
fs.State.Replay.IsTSValid(constants.NETWORK_REPLAY, t.GetSigHash(), t.GetTimestamp())
Expand All @@ -282,10 +282,11 @@ func (fs *FactoidState) UpdateECTransaction(rt bool, trans interfaces.IECBlockEn
fs.State.GetE(rt, t.ECPubKey.Fixed()),
t.Credits)
}
fs.State.PutE(rt, t.ECPubKey.Fixed(), v)
fs.State.PutE(rt, t.ECPubKey.Fixed(), v) // deduct EntryCommit
fs.State.NumTransactions++
fs.State.Replay.IsTSValid(constants.INTERNAL_REPLAY, t.GetSigHash(), t.GetTimestamp())
fs.State.Replay.IsTSValid(constants.NETWORK_REPLAY, t.GetSigHash(), t.GetTimestamp())

default:
return fmt.Errorf("Unknown EC Transaction")
}
Expand Down Expand Up @@ -331,7 +332,12 @@ func (fs *FactoidState) UpdateTransaction(rt bool, trans interfaces.ITransaction
}
for _, ecOut := range trans.GetECOutputs() {
ecbal := int64(ecOut.GetAmount()) / int64(fs.State.FactoshisPerEC)
fs.State.PutE(rt, ecOut.GetAddress().Fixed(), fs.State.GetE(rt, ecOut.GetAddress().Fixed())+ecbal)
fs.State.PutE(rt, ecOut.GetAddress().Fixed(), fs.State.GetE(rt, ecOut.GetAddress().Fixed())+ecbal) // Add EC's from FCT

// execute any messages that were waiting on this EC address
if rt == true {
fs.State.ExecuteFromHolding(ecOut.GetAddress().Fixed())
}
}
fs.State.NumTransactions++
return nil
Expand Down
3 changes: 3 additions & 0 deletions state/state.go
Expand Up @@ -411,6 +411,7 @@ type State struct {

reportedActivations [activations.ACTIVATION_TYPE_COUNT + 1]bool // flags about which activations we have reported (+1 because we don't use 0)
validatorLoopThreadID string
Hold HoldingList
}

var _ interfaces.IState = (*State)(nil)
Expand Down Expand Up @@ -908,6 +909,8 @@ func (s *State) Init() {
//s.Logger = log.NewLogFromConfig(s.LogPath, s.LogLevel, "State")
}

s.Hold.Init(s) // setup the dependant holding map

s.ControlPanelChannel = make(chan DisplayState, 20)
s.tickerQueue = make(chan int, 100) //ticks from a clock
s.timerMsgQueue = make(chan interfaces.IMsg, 100) //incoming eom notifications, used by leaders
Expand Down
5 changes: 4 additions & 1 deletion state/stateConsensus.go
Expand Up @@ -217,6 +217,9 @@ func (s *State) executeMsg(vm *VM, msg interfaces.IMsg) (ret bool) {
s.LogMessage("executeMsg", "drop, IReplay", msg)
}

case -2:
s.LogMessage("executeMsg", "new holding", msg)

default:
s.LogMessage("executeMsg", "drop, InvalidMsg", msg)
if !msg.SentInvalid() {
Expand Down Expand Up @@ -2566,7 +2569,7 @@ func (s *State) GetE(rt bool, adr [32]byte) (v int64) {
}

// PutE()
// If rt == true, update the Temp balances. Otherwise update the Permenent balances.
// If rt == true, update the Temp balances. Otherwise update the Permanent balances.
// concurrency safe to call
func (s *State) PutE(rt bool, adr [32]byte, v int64) {
if rt {
Expand Down

0 comments on commit 912036d

Please sign in to comment.