Skip to content

Commit

Permalink
Cache script bytes during indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 23, 2023
1 parent 4b579d4 commit fbe2485
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
16 changes: 16 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ func (cache *Cache) Script(ctx context.Context, address, symLink string) (contra
}
return item.Value().(contract.Script), nil
}

func (cache *Cache) ScriptBytes(ctx context.Context, address, symLink string) ([]byte, error) {
key := fmt.Sprintf("script_bytes:%s:%s", address, symLink)
item, err := cache.Fetch(key, time.Hour, func() (interface{}, error) {
script, err := cache.contracts.Script(ctx, address, symLink)
if err != nil {
return script, err
}
return script.Full()
})
if err != nil {
cache.Delete(key)
return nil, err
}
return item.Value().([]byte), nil
}
7 changes: 2 additions & 5 deletions internal/parsers/operations/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (p Transaction) parseContractParams(ctx context.Context, data noderpc.Opera
}
}

scriptEntity, err := p.ctx.Cache.Script(ctx, tx.Destination.Address, p.protocol.SymLink)
scriptBytes, err := p.ctx.Cache.ScriptBytes(ctx, tx.Destination.Address, p.protocol.SymLink)
if err != nil {
if !tx.Internal {
return nil
Expand Down Expand Up @@ -146,10 +146,7 @@ func (p Transaction) parseContractParams(ctx context.Context, data noderpc.Opera
return err
}
} else {
tx.Script, err = scriptEntity.Full()
if err != nil {
return err
}
tx.Script = scriptBytes
}

tx.AST, err = ast.NewScriptWithoutCode(tx.Script)
Expand Down
2 changes: 1 addition & 1 deletion internal/postgres/contract/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (storage *Storage) Storage(ctx context.Context, id int64) ([]byte, error) {
return data, err
}

// Storage -
// Views -
func (storage *Storage) Views(ctx context.Context, id int64) ([]byte, error) {
var data []byte
err := storage.DB.NewSelect().
Expand Down

0 comments on commit fbe2485

Please sign in to comment.