Skip to content

Commit

Permalink
Merge branch 'base'
Browse files Browse the repository at this point in the history
  • Loading branch information
sink772 committed Oct 18, 2023
2 parents a27af6f + 58ad477 commit b732a46
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 54 deletions.
9 changes: 8 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/icon-project/goloop/server/metric"
"github.com/icon-project/goloop/service"
"github.com/icon-project/goloop/service/eeproxy"
"github.com/icon-project/goloop/service/state"
)

type State int
Expand Down Expand Up @@ -428,7 +429,13 @@ func (c *singleChain) prepareDatabase(chainDir string) error {
return errors.Wrapf(err, "UnknownCacheStrategy(%s)", c.cfg.NodeCache)
}
cacheDir := path.Join(chainDir, DefaultCacheDir)
c.database = cache.AttachManager(cdb, cacheDir, mLevel, fLevel, stores)
cdb = cache.AttachManager(cdb, cacheDir, mLevel, fLevel, stores)
cdb, err = state.AttachAPIInfoCache(cdb, ConfigDefaultAPIInfoCacheSize)
if err != nil {
_ = cdb.Close()
return errors.Wrap(err, "FailToAttachAPIInfoCache")
}
c.database = cdb
return nil
}

Expand Down
1 change: 1 addition & 0 deletions chain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
ConfigDefaultTxTimeout = 5000 * time.Millisecond
ConfigDefaultChildrenLimit = 10
ConfigDefaultNephewLimit = 10
ConfigDefaultAPIInfoCacheSize = 2048
)

const (
Expand Down
18 changes: 13 additions & 5 deletions cmd/cli/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman
MarkAnnotationRequired(callFlags, "to", "method")

deployCmd := &cobra.Command{
Use: "deploy SCORE_ZIP_FILE",
Use: "deploy SCORE_FILE",
Short: "Deploy Transaction",
Args: ArgsWithDefaultErrorFunc(cobra.ExactArgs(1)),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -1043,21 +1043,31 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman
param.ToAddress = jsonrpc.Address(to)
}
dataM := make(map[string]interface{})
dataM["contentType"] = cmd.Flag("content_type").Value.String()
isDir, err := IsDirectory(args[0])
if err != nil {
return err
}
var contentType string
var b []byte
if isDir {
if b, err = ZipDirectory(args[0], "__pycache__"); err != nil {
return fmt.Errorf("fail to zip with directory %s err:%+v", args[0], err)
}
contentType = "application/zip"
} else {
contentType = cmd.Flag("content_type").Value.String()
if contentType == "" {
if strings.HasSuffix(strings.ToLower(args[0]), ".jar") {
contentType = "application/java"
} else {
contentType = "application/zip"
}
}
if b, err = readFile(args[0]); err != nil {
return fmt.Errorf("fail to read %s err:%+v", args[0], err)
}
}
dataM["contentType"] = contentType
dataM["content"] = "0x" + hex.EncodeToString(b)
if dataParams, err := getParamsFromFlags(cmd.Flags()); err != nil {
return err
Expand All @@ -1078,13 +1088,11 @@ func NewSendTxCmd(parentCmd *cobra.Command, parentVc *viper.Viper) *cobra.Comman
rootCmd.AddCommand(deployCmd)
deployFlags := deployCmd.Flags()
deployFlags.String("to", "cx0000000000000000000000000000000000000000", "ToAddress")
deployFlags.String("content_type", "application/zip",
"Mime-type of the content")
deployFlags.String("content_type", "", "Mime-type of the content")
deployFlags.String("params", "",
"raw json string or '@<json file>' or '-' for stdin for parameter JSON")
deployFlags.StringToString("param", nil,
"key=value, Function parameters will be delivered to on_install() or on_update()")
MarkAnnotationHidden(deployFlags, "content-type")
return rootCmd
}

Expand Down
9 changes: 8 additions & 1 deletion cmd/gochain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var genesisStorage, genesisPath string
var keyStoreFile, keyStoreSecret string
var saveFile, saveKeyStore string
var cfg GoChainConfig
var cpuProfile, memProfile string
var cpuProfile, memProfile, blockProfile string
var chainDir string
var eeSocket string
var modLevels map[string]string
Expand Down Expand Up @@ -130,6 +130,7 @@ func main() {
flag.StringVar(&cfg.KeyStorePass, "key_password", "", "Password for the KeyStore file")
flag.StringVar(&cpuProfile, "cpuprofile", "", "CPU Profiling data file")
flag.StringVar(&memProfile, "memprofile", "", "Memory Profiling data file")
flag.StringVar(&blockProfile, "blockprofile", "", "Memory Profiling data file")
flag.StringVar(&chainDir, "chain_dir", "", "Chain data directory (default: .chain/<address>/<nid>)")
flag.IntVar(&cfg.EEInstances, "ee_instances", 1, "Number of execution engines")
flag.IntVar(&cfg.ConcurrencyLevel, "concurrency", 1, "Maximum number of executors to be used for concurrency")
Expand Down Expand Up @@ -448,6 +449,12 @@ func Execute(cmd *cobra.Command, args []string) {
}
}

if blockProfile != "" {
if err := cli.StartBlockProfile(blockProfile, 0); err != nil {
log.Panicf("Fail to start block profiling err=%+v", err)
}
}

logoLines := []string{
" ____ ___ ____ _ _ _ ___ _ _ ",
" / ___|/ _ \\ / ___| | | | / \\ |_ _| \\ | |",
Expand Down
2 changes: 1 addition & 1 deletion cmd/txgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
}

var maker TransactionMaker
if len(scorePath) > 0 && len(methodName) > 0 {
if len(scorePath) > 0 && len(params) > 0 {
maker = &CallMaker{
NID: nid,
SourcePath: scorePath,
Expand Down
8 changes: 4 additions & 4 deletions common/cache/lrucache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/icon-project/goloop/common"
)

type Create func(string) (interface{}, error)
type Create func([]byte) (interface{}, error)

type LRUCache struct {
lock sync.Mutex
Expand All @@ -34,11 +34,11 @@ func (c *LRUCache) putInLock(key string, value interface{}) {
}
}

func (c *LRUCache) Get(key string) (interface{}, error) {
func (c *LRUCache) Get(key []byte) (interface{}, error) {
c.lock.Lock()
defer c.lock.Unlock()

e, ok := c.items[key]
e, ok := c.items[string(key)]
if ok {
c.lru.MoveToBack(e)
i := e.Value.(item)
Expand All @@ -51,7 +51,7 @@ func (c *LRUCache) Get(key string) (interface{}, error) {
if err != nil {
return nil, err
}
c.putInLock(key, value)
c.putInLock(string(key), value)
return value, nil
}

Expand Down
2 changes: 1 addition & 1 deletion javaee/rt/src/java/a/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Array(Void ignore, int readIndex) {
* @param perElementFee energy to be charged per element depending on type.
*/
static protected void chargeEnergyInitArray(int length, int perElementFee) {
long cost = EnergyCalculator.multiply(length, perElementFee);
long cost = EnergyCalculator.multiply(Math.max(length, 0), perElementFee);
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(cost);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ private void peekRLPString(int b) {
((arr[p + 2] & 0xff) << 16) |
((arr[p + 3] & 0xff) << 8) |
(arr[p + 4] & 0xff);
if (l < 0) {
throw new UnsupportedOperationException();
}
} else {
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -116,6 +119,9 @@ private void peekRLPListHeader(int b) {
((arr[p + 2] & 0xff) << 16) |
((arr[p + 3] & 0xff) << 8) |
(arr[p + 4] & 0xff);
if (l < 0) {
throw new UnsupportedOperationException();
}
} else {
throw new UnsupportedOperationException();
}
Expand Down
6 changes: 3 additions & 3 deletions javaee/rt/src/java/foundation/icon/ee/score/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static ValidationException fail(String fmt, Object... args) throws
throw new ValidationException(String.format(fmt, args));
}

private static ValidationException fail(Exception cause, String fmt, Object... args) throws
private static ValidationException fail(Throwable cause, String fmt, Object... args) throws
ValidationException {
throw new ValidationException(String.format(fmt, args), cause);
}
Expand Down Expand Up @@ -75,8 +75,8 @@ public static Method[] validate(byte[] codeBytes) throws ValidationException, Zi
structDB = new StructDB(classMap);
eeMethods = MethodUnpacker.readFrom(apisBytes);
} catch (IOException e) {
throw fail ("bad APIS format");
} catch (RuntimeException e) {
throw fail("bad APIS format");
} catch (Throwable e) {
throw fail(e, "malformed class file");
}
String cur = jar.mainClassName;
Expand Down
12 changes: 4 additions & 8 deletions javaee/rt/src/java/org/aion/avm/EnergyCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class EnergyCalculator {
* @return base + linearValue * RT_METHOD_FEE_FACTOR_LEVEL_2
*/
public static long multiplyLinearValueByMethodFeeLevel2AndAddBase(int base, int linearValue) {
return addAndCheckForOverflow(base, multiplyAndCheckForOverflow(linearValue, RuntimeMethodFeeSchedule.RT_METHOD_FEE_FACTOR_LEVEL_2));
return add(base, multiply(Math.max(linearValue, 0), RuntimeMethodFeeSchedule.RT_METHOD_FEE_FACTOR_LEVEL_2));
}

/**
Expand All @@ -20,18 +20,14 @@ public static long multiplyLinearValueByMethodFeeLevel2AndAddBase(int base, int
* @return base + linearValue * RT_METHOD_FEE_FACTOR_LEVEL_1
*/
public static long multiplyLinearValueByMethodFeeLevel1AndAddBase(int base, int linearValue) {
return addAndCheckForOverflow(base, multiplyAndCheckForOverflow(linearValue, RuntimeMethodFeeSchedule.RT_METHOD_FEE_FACTOR_LEVEL_1));
return add(base, multiply(Math.max(linearValue, 0), RuntimeMethodFeeSchedule.RT_METHOD_FEE_FACTOR_LEVEL_1));
}

public static long multiply(int value1, int value2) {
return multiplyAndCheckForOverflow(value1, value2);
return (long) value1 * (long) value2;
}

private static long addAndCheckForOverflow(int value1, long value2) {
private static long add(int value1, long value2) {
return (long) value1 + value2;
}

private static long multiplyAndCheckForOverflow(int value1, int value2) {
return (long) value1 * (long) value2;
}
}
6 changes: 4 additions & 2 deletions javaee/rt/src/java/pi/ObjectReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ public void avm_skip() {

public void avm_skip(int count) {
wrapVoidRead(() -> {
reader.skip(count);
chargeSkip();
for (var i=0; i<count; i++) {
reader.skip(1);
chargeSkip();
}
});
}

Expand Down
33 changes: 9 additions & 24 deletions service/state/apiinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"bytes"

"github.com/icon-project/goloop/common/codec"
"github.com/icon-project/goloop/common/crypto"
"github.com/icon-project/goloop/common/db"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/common/merkle"
"github.com/icon-project/goloop/service/scoreapi"
)

type apiInfoStore struct {
bk db.Bucket
bk APIInfoBucket
dirty bool
info *scoreapi.Info
hash []byte
Expand All @@ -22,29 +20,19 @@ type apiInfoStore struct {

func (s *apiInfoStore) getHash() []byte {
if s.hash == nil && s.info != nil {
bs, err := codec.BC.MarshalToBytes(s.info)
if err != nil {
log.Panicf("Fail to encode api info info=%+v err=%+v",
s.info, err)
}
s.bytes = bs
s.hash = crypto.SHA3Sum256(bs)
s.hash, s.bytes = MustEncodeAPIInfo(s.info)
}
return s.hash
}

func (s *apiInfoStore) Get() (*scoreapi.Info, error) {
if s.bk != nil {
if len(s.hash) > 0 && s.info == nil {
bs, err := s.bk.Get(s.hash)
if err != nil {
return nil, errors.CriticalIOError.Wrapf(err, "FailToGetAPIInfo(hash=%x)", s.hash)
}
_, err = codec.UnmarshalFromBytes(bs, &s.info)
if err != nil {
return nil, errors.CriticalFormatError.Wrapf(err, "InvalidAPIInfo(hash=%x)", s.hash)
if info, err := s.bk.Get(s.hash); err != nil {
return nil, err
} else {
s.info = info
}
s.bytes = bs
}
}
return s.info, nil
Expand All @@ -70,7 +58,7 @@ func (s *apiInfoStore) Equal(s2 *apiInfoStore) bool {
func (s *apiInfoStore) Flush() error {
if s.dirty && s.bk != nil {
h := s.getHash()
err := s.bk.Set(h, s.bytes)
err := s.bk.Set(h, s.bytes, s.info)
if err != nil {
return errors.CriticalIOError.Wrap(err, "FailToSetAPIInfo")
}
Expand All @@ -79,7 +67,7 @@ func (s *apiInfoStore) Flush() error {
}

func (s *apiInfoStore) ResetDB(b db.Database) error {
if bk, err := b.GetBucket(db.BytesByHash); err != nil {
if bk, err := GetAPIInfoBucket(b); err != nil {
return err
} else {
s.bk = bk
Expand Down Expand Up @@ -117,10 +105,7 @@ func (s *apiInfoStore) Resolve(bd merkle.Builder) error {
bd.RequestData(db.BytesByHash, s.hash, s)
return nil
}
if _, err = codec.UnmarshalFromBytes(value, &s.info); err != nil {
return err
}
s.bytes = value
s.info = value
}
return nil
}
Expand Down
Loading

0 comments on commit b732a46

Please sign in to comment.