mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-07 06:44:16 +00:00
Serialize the broadcastBy set for each transaction. Resolves issue 237.
This commit is contained in:
parent
fedfe9d0e6
commit
83bb66cc25
@ -26,6 +26,12 @@ package wallet;
|
||||
option java_package = "org.bitcoinj.wallet";
|
||||
option java_outer_classname = "Protos";
|
||||
|
||||
message PeerAddress {
|
||||
required bytes ip_address = 1;
|
||||
required uint32 port = 2;
|
||||
required uint64 services = 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* A key use to control Bitcoin spending
|
||||
*
|
||||
@ -80,7 +86,6 @@ message TransactionOutput {
|
||||
* Parsing should be lenient, since this could change for different applications yet we should
|
||||
* maintain backward compatibility.
|
||||
*/
|
||||
|
||||
message TransactionConfidence {
|
||||
enum Type {
|
||||
UNKNOWN = 0;
|
||||
@ -108,6 +113,8 @@ message TransactionConfidence {
|
||||
// If type == BUILDING then this is the cumulative workDone for the block the transaction appears in, together with
|
||||
// all blocks that bury it.
|
||||
optional int64 work_done = 5;
|
||||
|
||||
repeated PeerAddress broadcast_by = 6;
|
||||
}
|
||||
|
||||
/** A bitcoin transaction */
|
||||
|
@ -29,6 +29,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
@ -59,7 +61,7 @@ public class WalletProtobufSerializer {
|
||||
// Used for de-serialization
|
||||
private Map<ByteString, Transaction> txMap;
|
||||
private WalletExtensionSerializer helper;
|
||||
|
||||
|
||||
public WalletProtobufSerializer() {
|
||||
txMap = new HashMap<ByteString, Transaction>();
|
||||
helper = new WalletExtensionSerializer();
|
||||
@ -210,6 +212,14 @@ public class WalletProtobufSerializer {
|
||||
Sha256Hash overridingHash = confidence.getOverridingTransaction().getHash();
|
||||
confidenceBuilder.setOverridingTransaction(hashToByteString(overridingHash));
|
||||
}
|
||||
for (PeerAddress address : confidence.getBroadcastBy()) {
|
||||
Protos.PeerAddress proto = Protos.PeerAddress.newBuilder()
|
||||
.setIpAddress(ByteString.copyFrom(address.getAddr().getAddress()))
|
||||
.setPort(address.getPort())
|
||||
.setServices(address.getServices().longValue())
|
||||
.build();
|
||||
confidenceBuilder.addBroadcastBy(proto);
|
||||
}
|
||||
txBuilder.setConfidence(confidenceBuilder);
|
||||
}
|
||||
|
||||
@ -400,5 +410,17 @@ public class WalletProtobufSerializer {
|
||||
}
|
||||
confidence.setOverridingTransaction(overridingTransaction);
|
||||
}
|
||||
for (Protos.PeerAddress proto : confidenceProto.getBroadcastByList()) {
|
||||
InetAddress ip = null;
|
||||
try {
|
||||
ip = InetAddress.getByAddress(proto.getIpAddress().toByteArray());
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e); // IP address is of invalid length.
|
||||
}
|
||||
int port = proto.getPort();
|
||||
PeerAddress address = new PeerAddress(ip, port);
|
||||
address.setServices(BigInteger.valueOf(proto.getServices()));
|
||||
confidence.markBroadcastBy(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -13,13 +13,8 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.net.InetAddress;
|
||||
import java.util.*;
|
||||
|
||||
import static com.google.bitcoin.core.TestUtils.createFakeTx;
|
||||
import static org.junit.Assert.*;
|
||||
@ -59,13 +54,15 @@ public class WalletProtobufSerializerTest {
|
||||
// Check basic tx serialization.
|
||||
BigInteger v1 = Utils.toNanoCoins(1, 0);
|
||||
Transaction t1 = createFakeTx(params, v1, myAddress);
|
||||
|
||||
t1.getConfidence().markBroadcastBy(new PeerAddress(InetAddress.getByName("1.2.3.4")));
|
||||
t1.getConfidence().markBroadcastBy(new PeerAddress(InetAddress.getByName("5.6.7.8")));
|
||||
myWallet.receiveFromBlock(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
|
||||
Wallet wallet1 = roundTrip(myWallet);
|
||||
assertEquals(1, wallet1.getTransactions(true, true).size());
|
||||
assertEquals(v1, wallet1.getBalance());
|
||||
assertArrayEquals(t1.bitcoinSerialize(),
|
||||
wallet1.getTransaction(t1.getHash()).bitcoinSerialize());
|
||||
assertEquals(2, wallet1.getTransaction(t1.getHash()).getConfidence().numBroadcastPeers());
|
||||
|
||||
Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet);
|
||||
assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType());
|
||||
|
Loading…
Reference in New Issue
Block a user