-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
275 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
package com.oarmour; | ||
|
||
import com.oarmour.datasource.EthereumSource; | ||
import com.oarmour.datasource.WrappedTransaction; | ||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.apache.flink.streaming.api.functions.KeyedProcessFunction; | ||
import org.apache.flink.streaming.api.functions.ProcessFunction; | ||
import org.apache.flink.util.Collector; | ||
import org.web3j.protocol.core.methods.response.Transaction; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws Exception { | ||
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
// Create an instance of EthereumSource | ||
EthereumSource source = new EthereumSource(); | ||
|
||
// Create a DataStream using the EthereumSource | ||
DataStream<Transaction> stream = env.addSource(source); | ||
stream.process(new ProcessFunction<Transaction, Object>() { | ||
DataStream keyedStream = stream.process(new ProcessFunction<Transaction, WrappedTransaction>() { | ||
@Override | ||
public void processElement(Transaction value, ProcessFunction<Transaction, Object>.Context ctx, Collector<Object> out) throws Exception { | ||
System.out.println(value.getFrom() + " -> " + value.getTo() + " : " + value.getValue()); | ||
public void processElement(Transaction value, ProcessFunction<Transaction, WrappedTransaction>.Context ctx, Collector<WrappedTransaction> out) throws Exception { | ||
out.collect(WrappedTransaction.FromKey(value, value.getFrom())); | ||
out.collect(WrappedTransaction.FromKey(value, value.getTo())); | ||
} | ||
}).print(); | ||
}).keyBy(tx -> tx.Key).process(new KeyedProcessFunction<String, WrappedTransaction, Object>() { | ||
@Override | ||
public void processElement(WrappedTransaction value, Context ctx, Collector<Object> out) throws Exception { | ||
out.collect(value); | ||
System.out.println(value); | ||
} | ||
}); | ||
env.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.oarmour.datasource; | ||
|
||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.web3j.protocol.core.methods.response.Transaction; | ||
|
||
public class MockSource { | ||
public static DataStream<Transaction> MockTransactions(StreamExecutionEnvironment env) { | ||
return env.fromData( | ||
SimpleTransaction("0x123", "0x456", "1000", "0x", "0x"), | ||
SimpleTransaction("0x456", "0x123", "1000", "0x","0x") | ||
); | ||
} | ||
|
||
public static Transaction SimpleTransaction(String from, String to, String value, String input, String creates) { | ||
Transaction tx = new Transaction(); | ||
tx.setFrom(from); | ||
tx.setTo(to); | ||
tx.setValue(value); | ||
tx.setInput(input); | ||
tx.setCreates(creates); | ||
return tx; | ||
} | ||
|
||
public static String StringOfTX(Transaction tx) { | ||
return String.format("from: %s, to: %s, value: %s, input: %s", tx.getFrom(), tx.getTo(), tx.getValue(), tx.getInput()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/oarmour/datasource/WrappedTransaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.oarmour.datasource; | ||
|
||
import org.web3j.protocol.core.methods.response.Transaction; | ||
|
||
public class WrappedTransaction { | ||
public Transaction transaction; | ||
public String Key; | ||
|
||
public WrappedTransaction() { | ||
} | ||
|
||
public Transaction getTransaction() { | ||
return transaction; | ||
} | ||
|
||
public WrappedTransaction(Transaction transaction, String Key) { | ||
this.transaction = transaction; | ||
this.Key = Key; | ||
} | ||
public static WrappedTransaction FromKey(Transaction transaction, String key) { | ||
return new WrappedTransaction(transaction, key); | ||
} | ||
|
||
public String toString() { | ||
return String.format("from: %s, to: %s, value: %s, input: %s createAt: %s", this.getTransaction().getFrom(), this.getTransaction().getTo(), this.getTransaction().getValue(), this.getTransaction().getInput(), this.getTransaction().getCreates()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.oarmour.pattern; | ||
|
||
import com.oarmour.datasource.WrappedTransaction; | ||
import org.apache.flink.cep.pattern.Pattern; | ||
import org.apache.flink.cep.pattern.conditions.IterativeCondition; | ||
|
||
public class PFishing { | ||
public static Pattern getPattern() { | ||
return Pattern.<WrappedTransaction>begin("token-send") | ||
.where(new IterativeCondition<WrappedTransaction>() { | ||
@Override | ||
public boolean filter(WrappedTransaction value, Context<WrappedTransaction> ctx) throws Exception { | ||
if (value.getTransaction().getValue().longValue() > 1000L) | ||
return true; | ||
return false; | ||
} | ||
}).followedBy("poisoning-address").where(new IterativeCondition<WrappedTransaction>() { | ||
@Override | ||
public boolean filter(WrappedTransaction value, Context<WrappedTransaction> ctx) throws Exception { | ||
if (value.transaction.getTo().equals("0x123") && value.getTransaction().getValue().longValue() == 0) | ||
return true; | ||
return false; | ||
} | ||
}).followedBy("fishing-success").where(new IterativeCondition<WrappedTransaction>() { | ||
@Override | ||
public boolean filter(WrappedTransaction value, Context<WrappedTransaction> ctx) throws Exception { | ||
return false; | ||
} | ||
}); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/com/oarmour/datasource/EthereumSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.oarmour.datasource; | ||
|
||
import com.oarmour.pattern.PFishing; | ||
import org.apache.flink.cep.CEP; | ||
import org.apache.flink.cep.PatternSelectFunction; | ||
import org.apache.flink.streaming.api.datastream.DataStream; | ||
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; | ||
import org.apache.flink.streaming.api.functions.KeyedProcessFunction; | ||
import org.apache.flink.streaming.api.functions.ProcessFunction; | ||
import org.apache.flink.util.Collector; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.web3j.protocol.core.methods.response.Transaction; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.apache.flink.cep.CEP.pattern; | ||
|
||
public class EthereumSourceTest { | ||
|
||
StreamExecutionEnvironment env; | ||
DataStream<Transaction> mockStream; | ||
@Before | ||
public void setUpClass() throws Exception { | ||
env = StreamExecutionEnvironment.getExecutionEnvironment(); | ||
mockStream = env.fromData( | ||
MockSource.SimpleTransaction("0x123", "0x456", "1000", "0x", "0x"), | ||
MockSource.SimpleTransaction("0x456", "0x123", "1000", "0x", "0x" ) | ||
); | ||
} | ||
|
||
@Test | ||
public void testConnectStream() throws Exception { | ||
DataStream keyedStream = mockStream.process(new ProcessFunction<Transaction, WrappedTransaction>() { | ||
@Override | ||
public void processElement(Transaction value, ProcessFunction<Transaction, WrappedTransaction>.Context ctx, Collector<WrappedTransaction> out) throws Exception { | ||
out.collect(WrappedTransaction.FromKey(value, value.getFrom())); | ||
out.collect(WrappedTransaction.FromKey(value, value.getTo())); | ||
} | ||
}).keyBy(tx -> tx.Key).process(new KeyedProcessFunction<String, WrappedTransaction, WrappedTransaction>() { | ||
@Override | ||
public void processElement(WrappedTransaction value, Context ctx, Collector<WrappedTransaction> out) throws Exception { | ||
out.collect(value); | ||
} | ||
}); | ||
|
||
|
||
CEP.pattern(keyedStream, PFishing.getPattern()).select(new PatternSelectFunction<WrappedTransaction, String>() { | ||
@Override | ||
public String select(Map<String, List<WrappedTransaction>> pattern) throws Exception { | ||
return "Fishing Alert"; | ||
} | ||
}).print(); | ||
env.execute(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.oarmour.datasource; | ||
|
||
import io.reactivex.disposables.Disposable; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.http.HttpService; | ||
|
||
public class ProviderTest { | ||
Object waitLock; | ||
Disposable disposable; | ||
Web3j web3j; | ||
|
||
@Before | ||
public void setUpClass() throws Exception { | ||
web3j = Web3j.build(new HttpService("https://little-few-paper.quiknode.pro/b5d1d2678912de9078cba3c29d6180a685732418/")); | ||
} | ||
|
||
@Test | ||
public void testBlock() throws InterruptedException { | ||
waitLock = new Object(); | ||
disposable = web3j.blockFlowable(true).subscribe( | ||
blk -> { | ||
System.out.println(blk.getBlock().getNumber()); | ||
}, | ||
error -> { | ||
System.out.println(error.getMessage()); | ||
}, | ||
() -> { | ||
System.out.println("complete"); | ||
} | ||
); | ||
|
||
while (!disposable.isDisposed()) { | ||
synchronized (waitLock) { | ||
waitLock.wait(100L); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testTransaction() throws InterruptedException { | ||
waitLock = new Object(); | ||
disposable = web3j.transactionFlowable().subscribe( | ||
tx -> { | ||
System.out.println(tx.getFrom() + " -> " + tx.getTo() + " : " + tx.getValue()); | ||
}, | ||
error -> { | ||
System.out.println(error.getMessage()); | ||
}, | ||
() -> { | ||
System.out.println("complete"); | ||
} | ||
); | ||
|
||
while (!disposable.isDisposed()) { | ||
synchronized (waitLock) { | ||
waitLock.wait(100L); | ||
} | ||
} | ||
} | ||
} |