Using a long data type for the authorities weight
This commit is contained in:
parent
d21fea7cb6
commit
ee78d48947
4 changed files with 63 additions and 36 deletions
|
@ -1,15 +1,25 @@
|
||||||
package de.bitsharesmunich.graphenej;
|
package de.bitsharesmunich.graphenej;
|
||||||
|
|
||||||
import com.google.common.primitives.Bytes;
|
import com.google.common.primitives.Bytes;
|
||||||
import com.google.gson.*;
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonDeserializationContext;
|
||||||
|
import com.google.gson.JsonDeserializer;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import de.bitsharesmunich.graphenej.errors.MalformedAddressException;
|
import de.bitsharesmunich.graphenej.errors.MalformedAddressException;
|
||||||
import de.bitsharesmunich.graphenej.interfaces.GrapheneSerializable;
|
import de.bitsharesmunich.graphenej.interfaces.GrapheneSerializable;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by nelson on 11/30/16.
|
* Class used to represent the weighted set of keys and accounts that must approve operations.
|
||||||
|
*
|
||||||
|
* {@see <a href="https://bitshares.org/doxygen/structgraphene_1_1chain_1_1authority.html">Authority</a>}
|
||||||
*/
|
*/
|
||||||
public class Authority implements GrapheneSerializable {
|
public class Authority implements GrapheneSerializable {
|
||||||
public static final String KEY_ACCOUNT_AUTHS = "account_auths";
|
public static final String KEY_ACCOUNT_AUTHS = "account_auths";
|
||||||
|
@ -18,14 +28,14 @@ public class Authority implements GrapheneSerializable {
|
||||||
public static final String KEY_EXTENSIONS = "extensions";
|
public static final String KEY_EXTENSIONS = "extensions";
|
||||||
|
|
||||||
private long weight_threshold;
|
private long weight_threshold;
|
||||||
private HashMap<UserAccount, Integer> account_auths;
|
private HashMap<UserAccount, Long> account_auths;
|
||||||
private HashMap<PublicKey, Integer> key_auths;
|
private HashMap<PublicKey, Long> key_auths;
|
||||||
private Extensions extensions;
|
private Extensions extensions;
|
||||||
|
|
||||||
public Authority(){
|
public Authority(){
|
||||||
this.weight_threshold = 1;
|
this.weight_threshold = 1;
|
||||||
this.account_auths = new HashMap<UserAccount, Integer>();
|
this.account_auths = new HashMap<UserAccount, Long>();
|
||||||
this.key_auths = new HashMap<PublicKey, Integer>();
|
this.key_auths = new HashMap<PublicKey, Long>();
|
||||||
extensions = new Extensions();
|
extensions = new Extensions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +46,7 @@ public class Authority implements GrapheneSerializable {
|
||||||
* @param accountAuths: Map of account to weights relationships. Can be null.
|
* @param accountAuths: Map of account to weights relationships. Can be null.
|
||||||
* @throws MalformedAddressException
|
* @throws MalformedAddressException
|
||||||
*/
|
*/
|
||||||
public Authority(long weight_threshold, HashMap<PublicKey, Integer> keyAuths, HashMap<UserAccount, Integer> accountAuths) {
|
public Authority(long weight_threshold, HashMap<PublicKey, Long> keyAuths, HashMap<UserAccount, Long> accountAuths) {
|
||||||
this();
|
this();
|
||||||
this.weight_threshold = weight_threshold;
|
this.weight_threshold = weight_threshold;
|
||||||
if(keyAuths != null)
|
if(keyAuths != null)
|
||||||
|
@ -49,7 +59,7 @@ public class Authority implements GrapheneSerializable {
|
||||||
this.account_auths = new HashMap<>();
|
this.account_auths = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKeyAuthorities(HashMap<Address, Integer> keyAuths){
|
public void setKeyAuthorities(HashMap<Address, Long> keyAuths){
|
||||||
if(keyAuths != null){
|
if(keyAuths != null){
|
||||||
for(Address address : keyAuths.keySet()){
|
for(Address address : keyAuths.keySet()){
|
||||||
key_auths.put(address.getPublicKey(), keyAuths.get(address));
|
key_auths.put(address.getPublicKey(), keyAuths.get(address));
|
||||||
|
@ -57,10 +67,13 @@ public class Authority implements GrapheneSerializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountAuthorities(HashMap<UserAccount, Integer> accountAuthorities){
|
public void setAccountAuthorities(HashMap<UserAccount, Long> accountAuthorities){
|
||||||
this.account_auths = accountAuthorities;
|
this.account_auths = accountAuthorities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return: Returns a list of public keys linked to this authority
|
||||||
|
*/
|
||||||
public List<PublicKey> getKeyAuthList(){
|
public List<PublicKey> getKeyAuthList(){
|
||||||
ArrayList<PublicKey> keys = new ArrayList<>();
|
ArrayList<PublicKey> keys = new ArrayList<>();
|
||||||
for(PublicKey pk : key_auths.keySet()){
|
for(PublicKey pk : key_auths.keySet()){
|
||||||
|
@ -69,11 +82,22 @@ public class Authority implements GrapheneSerializable {
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<PublicKey, Integer> getKeyAuths(){
|
/**
|
||||||
|
* @return: Returns a list of accounts linked to this authority
|
||||||
|
*/
|
||||||
|
public List<UserAccount> getAccountAuthList(){
|
||||||
|
ArrayList<UserAccount> accounts = new ArrayList<>();
|
||||||
|
for(UserAccount account : account_auths.keySet()){
|
||||||
|
accounts.add(account);
|
||||||
|
}
|
||||||
|
return accounts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap<PublicKey, Long> getKeyAuths(){
|
||||||
return this.key_auths;
|
return this.key_auths;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<UserAccount, Integer> getAccountAuths(){
|
public HashMap<UserAccount, Long> getAccountAuths(){
|
||||||
return this.account_auths;
|
return this.account_auths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +173,8 @@ public class Authority implements GrapheneSerializable {
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
Authority authority = (Authority) obj;
|
Authority authority = (Authority) obj;
|
||||||
HashMap<PublicKey, Integer> keyAuths = authority.getKeyAuths();
|
HashMap<PublicKey, Long> keyAuths = authority.getKeyAuths();
|
||||||
HashMap<UserAccount, Integer> accountAuths = authority.getAccountAuths();
|
HashMap<UserAccount, Long> accountAuths = authority.getAccountAuths();
|
||||||
System.out.println("key auths match: "+this.key_auths.equals(keyAuths));
|
System.out.println("key auths match: "+this.key_auths.equals(keyAuths));
|
||||||
System.out.println("account auths match: "+this.account_auths.equals(accountAuths));
|
System.out.println("account auths match: "+this.account_auths.equals(accountAuths));
|
||||||
System.out.println("weight threshold matches: "+(this.weight_threshold == authority.weight_threshold));
|
System.out.println("weight threshold matches: "+(this.weight_threshold == authority.weight_threshold));
|
||||||
|
@ -179,12 +203,12 @@ public class Authority implements GrapheneSerializable {
|
||||||
long weightThreshold = baseObject.get(KEY_WEIGHT_THRESHOLD).getAsLong();
|
long weightThreshold = baseObject.get(KEY_WEIGHT_THRESHOLD).getAsLong();
|
||||||
JsonArray keyAuthArray = baseObject.getAsJsonArray(KEY_KEY_AUTHS);
|
JsonArray keyAuthArray = baseObject.getAsJsonArray(KEY_KEY_AUTHS);
|
||||||
JsonArray accountAuthArray = baseObject.getAsJsonArray(KEY_ACCOUNT_AUTHS);
|
JsonArray accountAuthArray = baseObject.getAsJsonArray(KEY_ACCOUNT_AUTHS);
|
||||||
HashMap<PublicKey, Integer> keyAuthMap = new HashMap<>();
|
HashMap<PublicKey, Long> keyAuthMap = new HashMap<>();
|
||||||
HashMap<UserAccount, Integer> accountAuthMap = new HashMap<>();
|
HashMap<UserAccount, Long> accountAuthMap = new HashMap<>();
|
||||||
for(int i = 0; i < keyAuthArray.size(); i++){
|
for(int i = 0; i < keyAuthArray.size(); i++){
|
||||||
JsonArray subArray = keyAuthArray.get(i).getAsJsonArray();
|
JsonArray subArray = keyAuthArray.get(i).getAsJsonArray();
|
||||||
String addr = subArray.get(0).getAsString();
|
String addr = subArray.get(0).getAsString();
|
||||||
int weight = subArray.get(1).getAsInt();
|
long weight = subArray.get(1).getAsLong();
|
||||||
try {
|
try {
|
||||||
keyAuthMap.put(new Address(addr).getPublicKey(), weight);
|
keyAuthMap.put(new Address(addr).getPublicKey(), weight);
|
||||||
} catch (MalformedAddressException e) {
|
} catch (MalformedAddressException e) {
|
||||||
|
@ -192,7 +216,11 @@ public class Authority implements GrapheneSerializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int i = 0; i < accountAuthArray.size(); i++){
|
for(int i = 0; i < accountAuthArray.size(); i++){
|
||||||
//TODO: Implement this
|
JsonArray subArray = accountAuthArray.get(i).getAsJsonArray();
|
||||||
|
String userId = subArray.get(0).getAsString();
|
||||||
|
long weight = subArray.get(1).getAsLong();
|
||||||
|
UserAccount userAccount = new UserAccount(userId);
|
||||||
|
accountAuthMap.put(userAccount, weight);
|
||||||
}
|
}
|
||||||
return new Authority(weightThreshold, keyAuthMap, accountAuthMap);
|
return new Authority(weightThreshold, keyAuthMap, accountAuthMap);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,12 @@
|
||||||
package de.bitsharesmunich.graphenej;
|
package de.bitsharesmunich.graphenej;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.InputMismatchException;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by nelson on 12/16/16.
|
* Created by nelson on 12/16/16.
|
||||||
|
@ -25,19 +22,19 @@ public class AuthorityTest {
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
authority = new Authority();
|
authority = new Authority();
|
||||||
sameAuthority = new Authority();
|
sameAuthority = new Authority();
|
||||||
HashMap<UserAccount, Integer> accountAuthorityMap = new HashMap<>();
|
HashMap<UserAccount, Long> accountAuthorityMap = new HashMap<>();
|
||||||
UserAccount userAccount = new UserAccount("1.2.20000");
|
UserAccount userAccount = new UserAccount("1.2.20000");
|
||||||
accountAuthorityMap.put(userAccount, 1);
|
accountAuthorityMap.put(userAccount, 1l);
|
||||||
differentAuthority = new Authority(1, null, accountAuthorityMap);
|
differentAuthority = new Authority(1, null, accountAuthorityMap);
|
||||||
|
|
||||||
Address address1 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
Address address1 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
||||||
Address address2 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
Address address2 = new Address("BTS8RiFgs8HkcVPVobHLKEv6yL3iXcC9SWjbPVS15dDAXLG9GYhnY");
|
||||||
PublicKey publicKey = address1.getPublicKey();
|
PublicKey publicKey = address1.getPublicKey();
|
||||||
PublicKey samePublicKey = address2.getPublicKey();
|
PublicKey samePublicKey = address2.getPublicKey();
|
||||||
HashMap<PublicKey, Integer> keyMap1 = new HashMap<>();
|
HashMap<PublicKey, Long> keyMap1 = new HashMap<>();
|
||||||
HashMap<PublicKey, Integer> keyMap2 = new HashMap<>();
|
HashMap<PublicKey, Long> keyMap2 = new HashMap<>();
|
||||||
keyMap1.put(publicKey, 1);
|
keyMap1.put(publicKey, 1l);
|
||||||
keyMap2.put(samePublicKey, 1);
|
keyMap2.put(samePublicKey, 1l);
|
||||||
keyAuthority1 = new Authority(1, keyMap1, null);
|
keyAuthority1 = new Authority(1, keyMap1, null);
|
||||||
keyAuthority2 = new Authority(1, keyMap2, null);
|
keyAuthority2 = new Authority(1, keyMap2, null);
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ import de.bitsharesmunich.graphenej.models.WitnessResponse;
|
||||||
public class GetObjectsTest extends BaseApiTest{
|
public class GetObjectsTest extends BaseApiTest{
|
||||||
private final Asset asset = new Asset("1.3.0", "BTS", 5);
|
private final Asset asset = new Asset("1.3.0", "BTS", 5);
|
||||||
private final UserAccount account = new UserAccount("1.2.116354");
|
private final UserAccount account = new UserAccount("1.2.116354");
|
||||||
|
private final UserAccount bilthon_25 = new UserAccount("1.2.151069");
|
||||||
private final String bitAssetId = "2.4.13";
|
private final String bitAssetId = "2.4.13";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -68,7 +69,7 @@ public class GetObjectsTest extends BaseApiTest{
|
||||||
public void testGetAccount(){
|
public void testGetAccount(){
|
||||||
try{
|
try{
|
||||||
ArrayList<String> ids = new ArrayList<>();
|
ArrayList<String> ids = new ArrayList<>();
|
||||||
ids.add(account.getObjectId());
|
ids.add(bilthon_25.getObjectId());
|
||||||
mWebSocket.addListener(new GetObjects(ids, new WitnessResponseListener() {
|
mWebSocket.addListener(new GetObjects(ids, new WitnessResponseListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,10 +77,11 @@ public class GetObjectsTest extends BaseApiTest{
|
||||||
System.out.println("onSuccess");
|
System.out.println("onSuccess");
|
||||||
List<GrapheneObject> result = (List<GrapheneObject>) response.result;
|
List<GrapheneObject> result = (List<GrapheneObject>) response.result;
|
||||||
UserAccount userAccount = (UserAccount) result.get(0);
|
UserAccount userAccount = (UserAccount) result.get(0);
|
||||||
System.out.println("Account name: "+userAccount.getName());
|
System.out.println("Account name.....: "+userAccount.getName());
|
||||||
System.out.println("json string: "+userAccount.toJsonString());
|
System.out.println("json string......: "+userAccount.toJsonString());
|
||||||
System.out.println("owner: "+userAccount.getOwner().getKeyAuthList().get(0).getAddress());
|
System.out.println("owner............: "+userAccount.getOwner().getKeyAuthList().get(0).getAddress());
|
||||||
System.out.println("active: "+userAccount.getActive().getKeyAuthList().get(0).getAddress());
|
System.out.println("active key.......: "+userAccount.getActive().getKeyAuthList().get(0).getAddress());
|
||||||
|
System.out.println("active account...: "+userAccount.getActive().getAccountAuthList().get(0).getObjectId());
|
||||||
System.out.println("memo: "+userAccount.getOptions().getMemoKey().getAddress());
|
System.out.println("memo: "+userAccount.getOptions().getMemoKey().getAddress());
|
||||||
synchronized (GetObjectsTest.this){
|
synchronized (GetObjectsTest.this){
|
||||||
GetObjectsTest.this.notifyAll();
|
GetObjectsTest.this.notifyAll();
|
||||||
|
|
|
@ -39,8 +39,8 @@ public class AccountUpdateOperationTest {
|
||||||
@Before
|
@Before
|
||||||
public void setup(){
|
public void setup(){
|
||||||
try{
|
try{
|
||||||
HashMap<Address, Integer> keyAuth = new HashMap<>();
|
HashMap<Address, Long> keyAuth = new HashMap<>();
|
||||||
keyAuth.put(new Address(ADDRESS), 1);
|
keyAuth.put(new Address(ADDRESS), 1l);
|
||||||
active = new Authority();
|
active = new Authority();
|
||||||
active.setKeyAuthorities(keyAuth);
|
active.setKeyAuthorities(keyAuth);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue