feat: add 3 estimation actions (block time, supply, height)
blockchain.estimate.block_time — average from genesis to tip blockchain.estimate.supply_at_height — premine + height*reward blockchain.estimate.height_at_time — timestamp → estimated height 42 total Core actions. Pure calculations, no banned imports. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
fabb1d7b8f
commit
fd7ce4fd8e
1 changed files with 53 additions and 0 deletions
53
actions.go
53
actions.go
|
|
@ -614,3 +614,56 @@ func hexVal(c byte) int {
|
|||
default: return -1
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterEstimateActions registers estimation/calculation actions.
|
||||
func RegisterEstimateActions(c *core.Core, ch *chain.Chain) {
|
||||
c.Action("blockchain.estimate.block_time", makeEstBlockTime(ch))
|
||||
c.Action("blockchain.estimate.supply_at_height", makeEstSupplyAtHeight())
|
||||
c.Action("blockchain.estimate.height_at_time", makeEstHeightAtTime(ch))
|
||||
}
|
||||
|
||||
func makeEstBlockTime(ch *chain.Chain) core.ActionHandler {
|
||||
return func(ctx context.Context, opts core.Options) core.Result {
|
||||
h, _ := ch.Height()
|
||||
if h < 2 {
|
||||
return core.Result{Value: uint64(120), OK: true}
|
||||
}
|
||||
genesis, _, _ := ch.GetBlockByHeight(0)
|
||||
_, top := ch.Snapshot()
|
||||
if genesis == nil || top == nil || top.Height == 0 {
|
||||
return core.Result{Value: uint64(120), OK: true}
|
||||
}
|
||||
avg := (top.Timestamp - genesis.Timestamp) / top.Height
|
||||
return core.Result{Value: avg, OK: true}
|
||||
}
|
||||
}
|
||||
|
||||
func makeEstSupplyAtHeight() core.ActionHandler {
|
||||
return func(ctx context.Context, opts core.Options) core.Result {
|
||||
height := uint64(opts.Int("height"))
|
||||
supply := PremineAmount + height*DefaultBlockReward
|
||||
return core.Result{Value: map[string]interface{}{
|
||||
"height": height, "supply_lthn": supply,
|
||||
"supply_atomic": supply * AtomicUnit,
|
||||
}, OK: true}
|
||||
}
|
||||
}
|
||||
|
||||
func makeEstHeightAtTime(ch *chain.Chain) core.ActionHandler {
|
||||
return func(ctx context.Context, opts core.Options) core.Result {
|
||||
targetTime := uint64(opts.Int("timestamp"))
|
||||
if targetTime == 0 {
|
||||
return core.Result{OK: false}
|
||||
}
|
||||
genesis, _, _ := ch.GetBlockByHeight(0)
|
||||
if genesis == nil {
|
||||
return core.Result{OK: false}
|
||||
}
|
||||
if targetTime <= genesis.Timestamp {
|
||||
return core.Result{Value: uint64(0), OK: true}
|
||||
}
|
||||
elapsed := targetTime - genesis.Timestamp
|
||||
est := elapsed / 120 // avg block time
|
||||
return core.Result{Value: est, OK: true}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue