-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feat>(test): client,connection,account unit test
- Loading branch information
yangfang2
committed
Aug 16, 2024
1 parent
058dd57
commit 15791cc
Showing
24 changed files
with
1,227 additions
and
14 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
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
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
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
92 changes: 92 additions & 0 deletions
92
src/main/java/com/webank/wecross/stub/web3/event/Web3ChainEventManager.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,92 @@ | ||
package com.webank.wecross.stub.web3.event; | ||
|
||
import com.webank.wecross.stub.web3.client.ClientWrapper; | ||
import com.webank.wecross.stub.web3.config.Web3StubConfig; | ||
import java.util.Arrays; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.web3j.abi.EventEncoder; | ||
import org.web3j.abi.TypeReference; | ||
import org.web3j.abi.datatypes.Address; | ||
import org.web3j.abi.datatypes.DynamicArray; | ||
import org.web3j.abi.datatypes.Event; | ||
import org.web3j.abi.datatypes.Utf8String; | ||
import org.web3j.abi.datatypes.generated.Uint256; | ||
import org.web3j.protocol.core.DefaultBlockParameterName; | ||
import org.web3j.protocol.core.methods.request.EthFilter; | ||
import org.web3j.protocol.core.methods.response.Log; | ||
|
||
public class Web3ChainEventManager { | ||
private static final Logger logger = LoggerFactory.getLogger(Web3ChainEventManager.class); | ||
|
||
private final ClientWrapper clientWrapper; | ||
|
||
public static final String LUYU_CALL = "LuyuCall"; | ||
public static final String LUYU_SEND_TRANSACTION = "LuyuSendTransaction"; | ||
|
||
public static final Event LUYUCALL_EVENT = | ||
new Event( | ||
LUYU_CALL, | ||
Arrays.asList( | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<DynamicArray<Utf8String>>() {}, | ||
new TypeReference<Uint256>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Address>() {})); | ||
|
||
public static final Event LUYUSENDTRANSACTION_EVENT = | ||
new Event( | ||
LUYU_SEND_TRANSACTION, | ||
Arrays.asList( | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<DynamicArray<Utf8String>>() {}, | ||
new TypeReference<Uint256>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Utf8String>() {}, | ||
new TypeReference<Address>() {})); | ||
|
||
public Web3ChainEventManager(ClientWrapper clientWrapper) { | ||
this.clientWrapper = clientWrapper; | ||
} | ||
|
||
public void registerEvent(Web3StubConfig.Resource resource) { | ||
registerEvent(resource.getName(), resource.getAddress(), LUYUCALL_EVENT); | ||
registerEvent(resource.getName(), resource.getAddress(), LUYUSENDTRANSACTION_EVENT); | ||
} | ||
|
||
private void registerEvent(String resourceName, String address, Event event) { | ||
EthFilter ethFilter = | ||
new EthFilter( | ||
DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, address); | ||
String encodedEventSignature = EventEncoder.encode(event); | ||
ethFilter.addSingleTopic(encodedEventSignature); | ||
|
||
clientWrapper.subscribe( | ||
ethFilter, | ||
new Subscriber<Log>() { | ||
@Override | ||
public void onSubscribe(Subscription subscription) { | ||
subscription.request(Long.MAX_VALUE); | ||
} | ||
|
||
@Override | ||
public void onNext(Log log) { | ||
// TODO handle events based on event name | ||
String eventName = event.getName(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) {} | ||
|
||
@Override | ||
public void onComplete() { | ||
registerEvent(resourceName, address, event); | ||
} | ||
}); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/com/webank/wecross/stub/web3/Web3AccountFactoryTest.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,27 @@ | ||
package com.webank.wecross.stub.web3; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static junit.framework.TestCase.assertTrue; | ||
|
||
import com.webank.wecross.stub.web3.account.Web3Account; | ||
import com.webank.wecross.stub.web3.account.Web3AccountFactory; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
import org.junit.Test; | ||
import org.web3j.crypto.CipherException; | ||
|
||
public class Web3AccountFactoryTest { | ||
|
||
@Test | ||
public void buildAccountTest() throws CipherException, IOException { | ||
Web3Account web3Account = Web3AccountFactory.build("wallet", "./account"); | ||
|
||
assertTrue(Objects.nonNull(web3Account)); | ||
assertEquals(web3Account.getName(), "wallet"); | ||
assertEquals(web3Account.getType(), "WEB3"); | ||
assertEquals(web3Account.getIdentity(), "0x698d83382f9ffb72271cd0826479e8ab02077842"); | ||
assertEquals( | ||
web3Account.getPublicKey(), | ||
"ab1c9d486b269adbb604766daf7d209440663299dd78b229ffcf728568b6b8a93c8a096ca4328d93040a0c8b4bd448e0e2da2566bf252845426f806a053d2cc6"); | ||
} | ||
} |
Oops, something went wrong.