Skip to content

Commit

Permalink
Merge branch 'base'
Browse files Browse the repository at this point in the history
  • Loading branch information
sink772 committed Jul 11, 2022
2 parents adfbf9f + 6aa96fb commit 6b036e9
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 48 deletions.
46 changes: 46 additions & 0 deletions common/codec/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package codec

import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"reflect"
Expand Down Expand Up @@ -448,3 +450,47 @@ func (c *rlpCodec) NewEncoder(w io.Writer) EncodeAndCloser {
writer: w,
})
}

func DumpRLP(indent string, data []byte) string {
p := 0
var res string
for p < len(data) {
switch q := data[p]; {
case q < 0x80:
res += fmt.Sprintf("%sbytes(0x%x:%d) : %x\n", indent, 1, 1, data[p:p+1])
p = p + 1
case q <= 0xb7:
l := int(q - 0x80)
res += fmt.Sprintf("%sbytes(0x%x:%d) : %x\n", indent, l, l, data[p+1:p+1+l])
p = p + 1 + l
case q <= 0xbf:
ll := int(q - 0xb7)
buf := make([]byte, 8)
lBytes := data[p+1 : p+1+ll]
copy(buf[8-ll:], lBytes)
l := int(binary.BigEndian.Uint64(buf))
res += fmt.Sprintf("%sbytes(0x%x:%d) : %x\n", indent, l, l, data[p+1+ll:p+1+ll+l])
p = p + 1 + ll + l
case q <= 0xf7:
l := int(q - 0xc0)
res += fmt.Sprintf("%slist(0x%x:%d) [\n", indent, l, l)
res += DumpRLP(indent+" ", data[p+1:p+1+l])
res += fmt.Sprintf("%s]\n", indent)
p = p + 1 + l
case q == 0xf8 && data[p+1] == 0:
res += fmt.Sprintf("%slist(0x0:0) [] nil?\n", indent)
p = p + 2
default:
ll := int(q - 0xf7)
buf := make([]byte, 8)
lBytes := data[p+1 : p+1+ll]
copy(buf[8-ll:], lBytes)
l := int(binary.BigEndian.Uint64(buf))
res += fmt.Sprintf("%slist(0x%x:%d) [\n", indent, l, l)
res += DumpRLP(indent+" ", data[p+1+ll:p+1+ll+l])
res += fmt.Sprintf("%s]\n", indent)
p = p + 1 + ll + l
}
}
return res
}
2 changes: 1 addition & 1 deletion consensus/fastsync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (cl *client) fetchBlocks(
fr.cb = cb
fr.maxActive = configMaxActive

peerIDs := cl.nm.GetPeers()
peerIDs := cl.ph.GetPeers()
fr.validPeers = make([]*peer, len(peerIDs))
for i, id := range peerIDs {
fr.validPeers[i] = &peer{id, 0, nil}
Expand Down
2 changes: 1 addition & 1 deletion consensus/fastsync/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *server) start() {

if !s.running {
s.running = true
pids := s.nm.GetPeers()
pids := s.ph.GetPeers()
for _, id := range pids {
s._addPeer(id)
}
Expand Down
4 changes: 4 additions & 0 deletions consensus/internal/test/networkmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (ph *tProtocolHandler) Unicast(pi module.ProtocolInfo, b []byte, id module.
return errors.Errorf("Unknown peer")
}

func (ph *tProtocolHandler) GetPeers() []module.PeerID {
return ph.nm.GetPeers()
}

func createAPeerID() module.PeerID {
return network.NewPeerIDFromAddress(wallet.New().Address())
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (s *syncer) Start() error {
return err
}

peerIDs := s.nm.GetPeers()
peerIDs := s.ph.GetPeers()
s.peers = make([]*peer, len(peerIDs))
for i, peerID := range peerIDs {
s.log.Debugf("Start: starting peer list %v\n", common.HexPre(peerID.Bytes()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2022 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package foundation.icon.ee.tooling;

import foundation.icon.ee.test.SimpleTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import score.Context;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ClassRejectionTest extends SimpleTest {

public static class ClassNotInJCL {
public ClassNotInJCL() {
byte[] b = null;
Objects.requireNonNull(b);
}
}

public static class ClassNotInJCL2 {
public ClassNotInJCL2() {
System.out.println("test");
}
}

public static class ClassNotInJCL3 {
public ClassNotInJCL3() {
List<String> list = new ArrayList<>();
list.add("a");
Context.println(list.toString());
}
}

public static class ClassNotInJCL4 {
public ClassNotInJCL4() {
Map<String, String> map = new HashMap<>();
map.put("a", "test");
Context.println(map.toString());
}
}

public static class MethodNotSupported {
public MethodNotSupported() {
Context.println("2^10=" + BigInteger.TWO.pow(10));
}
}

public static class MethodNotSupported2 {
public MethodNotSupported2() {
String a = "test";
Context.println(String.format("%s", a));
}
}

public static class MethodNotSupported3 {
public MethodNotSupported3() {
Arrays.asList("a", "b");
}
}

@Test
public void testOptimize() {
final Class<?>[] cases = new Class[]{
ClassNotInJCL.class,
ClassNotInJCL2.class,
ClassNotInJCL3.class,
ClassNotInJCL4.class,
MethodNotSupported.class,
MethodNotSupported2.class,
MethodNotSupported3.class,
};
for (var c : cases) {
Exception e = Assertions.assertThrows(
UnsupportedOperationException.class,
() -> makeRelJar(c));
System.out.println(e.getMessage());
}
}

public static class LambdaPredicate {
public LambdaPredicate() {
var list = List.of("a", "b");
list.removeIf(e -> e.equals("a"));
}
}

@Test
public void testLambda() {
Assertions.assertThrows(
UnsupportedOperationException.class,
() -> makeRelJar(LambdaPredicate.class));
}
}
2 changes: 1 addition & 1 deletion javaee/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GROUP=foundation.icon
VERSION=0.9.1
VERSION=0.9.2
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private static List<Class<?>> getCallableShadowClasses() throws ClassNotFoundExc
jclClassNames.removeAll(MethodDescriptorCollector.getOmittedClassNames());
jclClassNames.removeAll(Arrays.asList(
"score/RevertedException",
"score/UserRevertedException"
"score/UserRevertedException",
"score/UserRevertException"
));
jclClassNames.replaceAll(s -> PackageConstants.kShadowSlashPrefix + s);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object.
methodsCalled.add(new MethodInvocation(ownerHandle.getOwner(),
ownerHandle.getName() + ownerHandle.getDesc(), Opcodes.INVOKEDYNAMIC));
} else if (!name.equals("makeConcatWithConstants")) {
throw new RuntimeException("Unsure how to handle this Invoke Dynamic instruction");
throw new UnsupportedOperationException("Unsupported invokedynamic instruction: " + name);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aion.avm.tooling.deploy.eliminator;

import foundation.icon.ee.struct.Member;
import i.PackageConstants;
import org.objectweb.asm.Opcodes;

import java.util.LinkedList;
Expand Down Expand Up @@ -91,11 +92,19 @@ private void traverse() throws Exception {
default:
throw new Exception("This is not an invoke method opcode");
}
} else if (!canAccessClass(invocation.className)) {
throw new UnsupportedOperationException(
"Unsupported JCL class detected: " + invocation.className);
}
}
}
}

private boolean canAccessClass(String className) {
return className.startsWith(PackageConstants.kPublicApiSlashPrefix)
|| className.startsWith("[");
}

/* The logic about what should be marked reachable when we've tried to invoke method M in structure S (which can be a class or an interface)
is a two-step process.
Expand Down
1 change: 1 addition & 0 deletions module/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ProtocolHandler interface {
Broadcast(pi ProtocolInfo, b []byte, bt BroadcastType) error
Multicast(pi ProtocolInfo, b []byte, role Role) error
Unicast(pi ProtocolInfo, b []byte, id PeerID) error
GetPeers() []PeerID
}

type BroadcastType byte
Expand Down
17 changes: 12 additions & 5 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,22 @@ func (m *manager) PeerID() module.PeerID {
return m.p2p.ID()
}

func (m *manager) GetPeers() []module.PeerID {
arr := m.p2p.getPeers(true)
l := make([]module.PeerID, len(arr))
for i, p := range arr {
func toPeerIDs(ps []*Peer) []module.PeerID {
l := make([]module.PeerID, len(ps))
for i, p := range ps {
l[i] = p.ID()
}
return l
}

func (m *manager) GetPeers() []module.PeerID {
return toPeerIDs(m.p2p.getPeers(true))
}

func (m *manager) getPeersByProtocol(pi module.ProtocolInfo) []module.PeerID {
return toPeerIDs(m.p2p.getPeersByProtocol(pi, true))
}

func (m *manager) Term() {
defer m.mtx.Unlock()
m.mtx.Lock()
Expand Down Expand Up @@ -236,7 +243,7 @@ func (m *manager) unicast(pi module.ProtocolInfo, spi module.ProtocolInfo, bytes
pkt.priority = ph.getPriority()
pkt.src = m.PeerID()
pkt.forceSend = true
p := m.p2p.getPeer(id, true)
p := m.p2p.getPeerByProtocol(id, pkt.protocol, true)
return p.sendPacket(pkt)
}

Expand Down
Loading

0 comments on commit 6b036e9

Please sign in to comment.