Speedup p2pool archive API by hinting existing template id on /archive/block_by_main_id
This commit is contained in:
parent
2ddb4b0373
commit
00f5956897
|
@ -1377,7 +1377,7 @@ func main() {
|
|||
buf, _ := utils.MarshalJSON(raw)
|
||||
_, _ = writer.Write(buf)
|
||||
case "/raw":
|
||||
raw := p2api.ByMainId(block.MainId)
|
||||
raw := p2api.ByMainIdWithHint(block.MainId, block.TemplateId)
|
||||
|
||||
if raw == nil {
|
||||
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
|
|
@ -790,9 +790,31 @@ func getServerMux(instance *p2pool.P2Pool) *mux.Router {
|
|||
})
|
||||
|
||||
serveMux.HandleFunc("/archive/block_by_main_id/{id:[0-9a-f]+}", func(writer http.ResponseWriter, request *http.Request) {
|
||||
params := request.URL.Query()
|
||||
|
||||
var templateIdHint types.Hash
|
||||
if params.Has("templateIdHint") {
|
||||
if h, err := types.HashFromString(params.Get("templateIdHint")); err == nil {
|
||||
templateIdHint = h
|
||||
}
|
||||
}
|
||||
|
||||
if mainId, err := types.HashFromString(mux.Vars(request)["id"]); err == nil {
|
||||
var result p2pooltypes.P2PoolBinaryBlockResult
|
||||
if b := archiveCache.LoadByMainId(mainId); b != nil {
|
||||
var b *sidechain.PoolBlock
|
||||
if templateIdHint != types.ZeroHash {
|
||||
// Fast lookup on sidechain
|
||||
if b = instance.SideChain().GetPoolBlockByTemplateId(templateIdHint); b == nil || b.MainId() != mainId {
|
||||
b = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback
|
||||
if b == nil {
|
||||
b = archiveCache.LoadByMainId(mainId)
|
||||
}
|
||||
|
||||
if b != nil {
|
||||
result.Version = int(b.ShareVersion())
|
||||
if err := archiveCache.ProcessBlock(b); err != nil {
|
||||
result.Error = err.Error()
|
||||
|
|
|
@ -254,7 +254,14 @@ func FindAndInsertMainHeaderOutputs(mb *index.MainBlock, indexDb *index.Index, c
|
|||
if len(indexDb.GetMainCoinbaseOutputs(mb.CoinbaseId)) == 0 {
|
||||
//fill information
|
||||
log.Printf("inserting coinbase outputs for %s, template id %s, coinbase id %s", mb.Id, mb.SideTemplateId, mb.CoinbaseId)
|
||||
if t := getByMainId(mb.Id); t != nil {
|
||||
var t *sidechain.PoolBlock
|
||||
if t = getByTemplateId(mb.SideTemplateId); t == nil || t.MainId() != mb.Id {
|
||||
t = nil
|
||||
}
|
||||
if t == nil {
|
||||
t = getByMainId(mb.Id)
|
||||
}
|
||||
if t != nil {
|
||||
if err := processBlock(t); err != nil {
|
||||
return fmt.Errorf("could not process block: %s", err)
|
||||
}
|
||||
|
|
|
@ -140,6 +140,30 @@ func (p *P2PoolApi) ByMainId(id types.Hash) *sidechain.PoolBlock {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *P2PoolApi) ByMainIdWithHint(id, templateIdHint types.Hash) *sidechain.PoolBlock {
|
||||
if response, err := p.Client.Get(p.Host + "/archive/block_by_main_id/" + id.String() + "?templateIdHint=" + templateIdHint.String()); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
defer response.Body.Close()
|
||||
|
||||
if buf, err := io.ReadAll(response.Body); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
var result p2pooltypes.P2PoolBinaryBlockResult
|
||||
|
||||
if err = utils.UnmarshalJSON(buf, &result); err != nil || result.Version == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
b := &sidechain.PoolBlock{}
|
||||
if err = b.UnmarshalBinary(p.Consensus(), p.derivationCache, result.Blob); err != nil || int(b.ShareVersion()) != result.Version {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *P2PoolApi) LightByTemplateId(id types.Hash) sidechain.UniquePoolBlockSlice {
|
||||
if response, err := p.Client.Get(p.Host + "/archive/light_blocks_by_template_id/" + id.String()); err != nil {
|
||||
return nil
|
||||
|
|
Loading…
Reference in a new issue