Skip to content

Commit

Permalink
Merge branch 'netty-server' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 20, 2024
2 parents fa037b5 + a1b92a2 commit 7d4d4d5
Show file tree
Hide file tree
Showing 101 changed files with 2,496 additions and 1,292 deletions.
5 changes: 5 additions & 0 deletions convex-cli/GENESIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ alias convex="java -jar ~/convex.jar"
### Upload keystore


### Critical public keys:

Genesis/Admin Key: `0xc1d3b0104d55ddf7680181a46e93422e49e2ea9298e37794860f1ef1128427f7`
Governance key: `0xaE9C747a9730D63Fc16BcccEBd12B5dD4c8fBe1328e9a953025e8C02164Ed5E6`
mikera key: `0x89b5142678bfef7a2245af5ae5b9ab1e10c282b375fa297c5aaeccc48ac97cac`


### Managing with screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public Convex connect() {
throw new CLIError("Timeout while attempting to connect to peer: "+hostname,e);
} catch (IOException e) {
throw new CLIError("IO Error: "+e.getMessage(),e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CLIError("Connection interrupted",e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/main/cvx/convex/asset/multi-token.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
(assoc rec 1 (assoc os receiver quantity)))]
(set-holding *caller* (assoc hs id nrec)))
(do ;; create a new record with given offer
(or (get tokens id) (fail "token does not exist"))
(or (get tokens id) (fail :STATE "token does not exist"))
(set-holding *caller* {id [0 {receiver quantity}]})))
quantity ;; return value is quantity offered
))
Expand Down Expand Up @@ -203,7 +203,7 @@
[a b]
(let [a (cond a (int a) 0)
b (cond b (int b) 0)]
(if (>= a b) (- a b) 0)))
(cond (>= a b) (- a b) 0)))

(defn quantity-subset?
^{:callable true}
Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/main/cvx/convex/torus/exchange.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

;;;;;;;;;; Imports

(set-controller #2)

(import convex.asset :as asset)
(import convex.fungible :as fungible)
Expand Down Expand Up @@ -45,6 +46,8 @@
(def torus ~torus)
(def token-balance 0)

(set-controller torus)

(defn -qc
[q]
(cond (int? q) q ;; base case, quantity should always be an integer
Expand Down
12 changes: 6 additions & 6 deletions convex-core/src/main/java/convex/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Constants {
/**
* Initial timestamp for new States
*/
public static final long INITIAL_TIMESTAMP = Instant.parse("2020-12-06T05:08:13.0864Z").toEpochMilli();
public static final long INITIAL_TIMESTAMP = Instant.parse("2024-12-20T02:21:42.0200Z").toEpochMilli();
// public static final long INITIAL_TIMESTAMP = Instant.parse("2024-12-06T05:08:13.0864Z").toEpochMilli();

/**
Expand All @@ -38,15 +38,15 @@ public class Constants {
public static final long INITIAL_MEMORY_PRICE = 1000000L;

/**
* Memory Pool of growth increment 1mb
* Memory Pool of growth increment 40kb / hour i.e. approx. 1mb per day
*/
public static final long MEMORY_POOL_GROWTH = 1000000L;
public static final long MEMORY_POOL_GROWTH = 40000L;

/**
* Memory Pool of growth interval (once per day). This means regular price drops
* Memory Pool of growth interval (once per hour). This means regular price drops
* in memory pool
*/
public static final long MEMORY_POOL_GROWTH_INTERVAL = 1000L * 24 * 3600;
public static final long MEMORY_POOL_GROWTH_INTERVAL = 1000L * 3600;

/**
* Max juice allowable during execution of a single transaction.
Expand Down Expand Up @@ -161,7 +161,7 @@ public class Constants {
* Flag to omit filling in stack traces on validation exceptions. This helps
* performance against DoS attacks
*/
public static final boolean OMIT_VALIDATION_STACKTRACES = true;
public static final boolean OMIT_VALIDATION_STACKTRACES = false;

public static final int PBE_ITERATIONS = 100000;

Expand Down
25 changes: 23 additions & 2 deletions convex-core/src/main/java/convex/core/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public static Result error(Keyword errorCode, AString message) {
return error(errorCode,message,null);
}

public static Result value(ACell value) {
return create(null,value);
}

public static Result error(Keyword errorCode, String message) {
return error(errorCode,Strings.create(message),null);
}
Expand Down Expand Up @@ -320,7 +324,7 @@ public boolean isError() {
* @return New Result instance
*/

public static Result fromContext(CVMLong id,ResultContext rc) {
public static Result fromContext(ACell id,ResultContext rc) {
Context ctx=rc.context;
Object result=ctx.getValue();
ACell errorCode=null;
Expand Down Expand Up @@ -377,6 +381,7 @@ public static Result fromContext(Context ctx) {
* @return Updated Result
*/
public Result withID(ACell id) {
if (Cells.equals(id, getID())) return this;
return withValues(values.assoc(ID_POS, id));
}

Expand All @@ -386,7 +391,9 @@ public Result withID(ACell id) {
* @return Result instance representing the exception (will be an error)
*/
public static Result fromException(Throwable e) {
if (e==null) return Result.error(ErrorCodes.EXCEPTION,Strings.NIL);
if (e==null) {
return Result.error(ErrorCodes.EXCEPTION,Strings.NIL);
}
if (e instanceof TimeoutException) {
String msg=e.getMessage();
return Result.error(ErrorCodes.TIMEOUT,Strings.create(msg));
Expand Down Expand Up @@ -426,6 +433,12 @@ public static Result fromException(Throwable e) {
// Note interrupts are always caused by CLIENT from a local perspective
private static final Result INTERRUPTED_RESULT=Result.error(ErrorCodes.INTERRUPTED,Strings.create("Interrupted!")).withSource(SourceCodes.CLIENT);
private static final Result MISSING_RESULT=Result.error(ErrorCodes.MISSING,Strings.create("Missing Data!")).withSource(SourceCodes.CLIENT);
public static final Result CLOSED_CONNECTION = Result.error(ErrorCodes.CONNECT,Strings.create("Connection Closed")).withSource(SourceCodes.COMM);
public static final Result SENT_MESSAGE = Result.value(Strings.intern("Sent"));
public static final Result FULL_CLIENT_BUFFER = Result.error(ErrorCodes.LOAD, Strings.FULL_BUFFER).withSource(SourceCodes.COMM);
public static final Result BAD_FORMAT = Result.error(ErrorCodes.FORMAT, "Bad format");



/**
* Returns a Result representing a thread interrupt, AND sets the interrupt status on the current thread
Expand All @@ -436,6 +449,8 @@ private static Result interruptThread() {
return INTERRUPTED_RESULT;
}



/**
* Converts this result to a JSON representation. WARNING: some information may be lost because JSON is a terrible format.
*
Expand All @@ -461,6 +476,7 @@ public HashMap<String, Object> toJSON() {

private static final StringShort RESULT_TAG=StringShort.create("#Result");


@Override
public boolean print(BlobBuilder sb, long limit) {
sb.append(RESULT_TAG);
Expand Down Expand Up @@ -532,6 +548,11 @@ public static Result fromJSON(Object json) {
return Result.create(id, value, errorCode);
}

public static ACell peekResultID(Blob messageData, int i) throws BadFormatException {
Result r=Result.read(messageData, i);
return r.getID();
}



}
13 changes: 9 additions & 4 deletions convex-core/src/main/java/convex/core/cpos/Belief.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public static Collection<SignedData<Order>> extractOrders(ACell payload) {
@Override
public void validateStructure() throws InvalidDataException {
super.validateStructure();
if (!(values.get(IX_ORDERS) instanceof Index)) {
if (!(values.get(IX_ORDERS) instanceof Index)) {
throw new InvalidDataException("Orders should be an Index",this);
}
}
Expand All @@ -248,20 +248,25 @@ public void validateStructure() throws InvalidDataException {
* @param signedBlock Signed Block of transactions
* @return Updated Belief with new Order
*/
public Belief proposeBlock(AKeyPair kp, SignedData<Block> signedBlock) {
@SuppressWarnings("unchecked")
public Belief proposeBlock(AKeyPair kp, SignedData<Block>... signedBlocks) {
AccountKey peerKey=kp.getAccountKey();
Index<AccountKey, SignedData<Order>> orders = getOrders();

SignedData<Order> mySO=orders.get(peerKey);
Order myOrder;
if (mySO==null) {
myOrder=Order.create();
throw new IllegalStateException("Trying to propose block without a current ordering for peer "+peerKey);
} else {
myOrder=mySO.getValue();
}

// Create new order with signed Block
Order newOrder = myOrder.append(signedBlock);
Order newOrder = myOrder;
int n=signedBlocks.length;
for (int i=0; i<n; i++) {
newOrder=newOrder.append(signedBlocks[i]);
}
SignedData<Order> newSignedOrder = kp.signData(newOrder);

Index<AccountKey, SignedData<Order>> newOrders = orders.assoc(peerKey, newSignedOrder);
Expand Down
20 changes: 8 additions & 12 deletions convex-core/src/main/java/convex/core/cpos/BeliefMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,7 @@ private final AVector<SignedData<Block>> appendNewBlocks(AVector<SignedData<Bloc
HashSet<SignedData<Block>> newBlocks = new HashSet<>();
newBlocks.addAll(newBlocksOrdered);

// exclude new blocks already in the base Order
// TODO: what about blocks already in consensus?
// Probably need to check last block time from Peer
// exclude new blocks already in the base consensus Order
long scanStart=Math.min(blocks.count(), consensusPoint);
Iterator<SignedData<Block>> it = blocks.listIterator(scanStart);
while (it.hasNext()) {
Expand Down Expand Up @@ -483,7 +481,8 @@ public Long apply(Order c) {
// in order to sort by length of matched proposals
long blockMatch = proposedBlocks.commonPrefixLength(c.getBlocks());

long minPrevious = Math.min(winnningOrder.getConsensusPoint(level-1), c.getConsensusPoint(level-1));
int prevLevel=level-1;
long minPrevious = Math.min(winnningOrder.getConsensusPoint(prevLevel), c.getConsensusPoint(prevLevel));

// Match length is how many blocks agree with winning order at previous consensus level
long match = Math.min(blockMatch, minPrevious);
Expand Down Expand Up @@ -561,22 +560,19 @@ public static boolean compareOrders(Order oldOrder, Order newOrder) {
// new Order is more recent, so switch to this
return true;
} else {
// timestamps are the same

// Don't replace if equal
if (oldOrder.equals(newOrder)) return false;

// This probably shouldn't happen if peers are sticking to timestamps
// But we compare anyway
// Prefer advanced consensus
for (int level=CPoSConstants.CONSENSUS_LEVELS-1; level>=1; level--) {
// Prefer more blocks / advanced consensus
for (int level=0; level<CPoSConstants.CONSENSUS_LEVELS; level++) {
if (newOrder.getConsensusPoint(level)>oldOrder.getConsensusPoint(level)) return true;
}

// Finally prefer more blocks
AVector<SignedData<Block>> abs=oldOrder.getBlocks();
AVector<SignedData<Block>> bbs=newOrder.getBlocks();
if(abs.count()<bbs.count()) return true;
return false;
}
return false;
}

/**
Expand Down
35 changes: 28 additions & 7 deletions convex-core/src/main/java/convex/core/cpos/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Comparator;
import java.util.List;

import convex.core.Constants;
import convex.core.cvm.ARecordGeneric;
import convex.core.cvm.CVMTag;
import convex.core.cvm.Keywords;
Expand Down Expand Up @@ -41,14 +42,22 @@ public final class Block extends ARecordGeneric {

private final long timestamp;
private AVector<SignedData<ATransaction>> transactions;

private static final int IX_TIMESTAMP= 0;
private static final int IX_TRANSACTIONS = 1;

private static final long NUM_FIELDS = FORMAT.count();

/**
* Comparator to sort blocks by timestamp
*/
static final Comparator<SignedData<Block>> TIMESTAMP_COMPARATOR = new Comparator<>() {
@Override
public int compare(SignedData<Block> a, SignedData<Block> b) {
int sig = Long.compare(a.getValue().getTimeStamp(), b.getValue().getTimeStamp());
Block ba=a.getValue();
Block bb=b.getValue();

int sig = Long.compare(ba.getTimeStamp(), bb.getTimeStamp());
return sig;
}
};
Expand All @@ -59,15 +68,16 @@ private Block(long timestamp, AVector<SignedData<ATransaction>> transactions) {
this.transactions = transactions;
}

public Block(AVector<ACell> values) {
private Block(AVector<ACell> values) {
super(CVMTag.BLOCK,FORMAT,values);
this.timestamp=RT.ensureLong(values.get(0)).longValue();
this.timestamp=RT.ensureLong(values.get(IX_TIMESTAMP)).longValue();

}

@Override
public ACell get(Keyword k) {
if (Keywords.TIMESTAMP.equals(k)) return CVMLong.create(timestamp);
if (Keywords.TRANSACTIONS.equals(k)) return transactions;
if (Keywords.TIMESTAMP.equals(k)) return values.get(IX_TIMESTAMP);
if (Keywords.TRANSACTIONS.equals(k)) return getTransactions();
return null;
}

Expand Down Expand Up @@ -151,7 +161,7 @@ public static Block read(Blob b, int pos) throws BadFormatException {
* @return Vector of transactions
*/
public AVector<SignedData<ATransaction>> getTransactions() {
if (transactions==null) transactions=RT.ensureVector(values.get(1));
if (transactions==null) transactions=RT.ensureVector(values.get(IX_TRANSACTIONS));
return transactions;
}

Expand All @@ -162,7 +172,18 @@ public boolean isCanonical() {

@Override
public void validateCell() throws InvalidDataException {
// nothing to do
if (values.count()!=NUM_FIELDS) throw new InvalidDataException("Wrong field count",this);
}

@Override
public void validateStructure() throws InvalidDataException {
AVector<SignedData<ATransaction>> txs=getTransactions();
if (txs==null) throw new InvalidDataException("No transactions",this);
if (txs.count()>Constants.MAX_TRANSACTIONS_PER_BLOCK) {
throw new InvalidDataException("Too many transactions: "+txs.count(),this);
}
// We don't validate individual transactions here
// This gets enforced latter when transactions are applied
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions convex-core/src/main/java/convex/core/cpos/CPoSConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class CPoSConstants {
public static final long INITIAL_PEER_TIMESTAMP = -1L;

/**
* Minimum stake for a Peer to be considered by other Peers in consensus
* Minimum stake balance for a Peer to be considered by other Peers in consensus
*/
public static final long MINIMUM_EFFECTIVE_STAKE = Coin.GOLD * 1000;
/**
Expand All @@ -69,8 +69,15 @@ public class CPoSConstants {

/**
* Maximum allowed number of missing hashes in missing data request
*
* (2 header values short of 256, so that request vector is 2 levels at max size)
*/
public static final long MISSING_LIMIT = 256;
public static final int MISSING_LIMIT = 254;

/**
* Milliseconds time between blocks for a peer to collect maximum rewards (10 mins)
*/
public static final long MAX_REWARD_TIME = 10*60*1000;


}
Loading

0 comments on commit 7d4d4d5

Please sign in to comment.