3
0
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:
Mike Hearn 2012-08-20 23:37:46 +02:00
parent fedfe9d0e6
commit 83bb66cc25
4 changed files with 874 additions and 57 deletions

View File

@ -26,6 +26,12 @@ package wallet;
option java_package = "org.bitcoinj.wallet"; option java_package = "org.bitcoinj.wallet";
option java_outer_classname = "Protos"; 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 * 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 * Parsing should be lenient, since this could change for different applications yet we should
* maintain backward compatibility. * maintain backward compatibility.
*/ */
message TransactionConfidence { message TransactionConfidence {
enum Type { enum Type {
UNKNOWN = 0; 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 // If type == BUILDING then this is the cumulative workDone for the block the transaction appears in, together with
// all blocks that bury it. // all blocks that bury it.
optional int64 work_done = 5; optional int64 work_done = 5;
repeated PeerAddress broadcast_by = 6;
} }
/** A bitcoin transaction */ /** A bitcoin transaction */

View File

@ -29,6 +29,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -59,7 +61,7 @@ public class WalletProtobufSerializer {
// Used for de-serialization // Used for de-serialization
private Map<ByteString, Transaction> txMap; private Map<ByteString, Transaction> txMap;
private WalletExtensionSerializer helper; private WalletExtensionSerializer helper;
public WalletProtobufSerializer() { public WalletProtobufSerializer() {
txMap = new HashMap<ByteString, Transaction>(); txMap = new HashMap<ByteString, Transaction>();
helper = new WalletExtensionSerializer(); helper = new WalletExtensionSerializer();
@ -210,6 +212,14 @@ public class WalletProtobufSerializer {
Sha256Hash overridingHash = confidence.getOverridingTransaction().getHash(); Sha256Hash overridingHash = confidence.getOverridingTransaction().getHash();
confidenceBuilder.setOverridingTransaction(hashToByteString(overridingHash)); 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); txBuilder.setConfidence(confidenceBuilder);
} }
@ -400,5 +410,17 @@ public class WalletProtobufSerializer {
} }
confidence.setOverridingTransaction(overridingTransaction); 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

View File

@ -13,13 +13,8 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Collection; import java.net.InetAddress;
import java.util.LinkedList; import java.util.*;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import static com.google.bitcoin.core.TestUtils.createFakeTx; import static com.google.bitcoin.core.TestUtils.createFakeTx;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -59,13 +54,15 @@ public class WalletProtobufSerializerTest {
// Check basic tx serialization. // Check basic tx serialization.
BigInteger v1 = Utils.toNanoCoins(1, 0); BigInteger v1 = Utils.toNanoCoins(1, 0);
Transaction t1 = createFakeTx(params, v1, myAddress); 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); myWallet.receiveFromBlock(t1, null, BlockChain.NewBlockType.BEST_CHAIN);
Wallet wallet1 = roundTrip(myWallet); Wallet wallet1 = roundTrip(myWallet);
assertEquals(1, wallet1.getTransactions(true, true).size()); assertEquals(1, wallet1.getTransactions(true, true).size());
assertEquals(v1, wallet1.getBalance()); assertEquals(v1, wallet1.getBalance());
assertArrayEquals(t1.bitcoinSerialize(), assertArrayEquals(t1.bitcoinSerialize(),
wallet1.getTransaction(t1.getHash()).bitcoinSerialize()); wallet1.getTransaction(t1.getHash()).bitcoinSerialize());
assertEquals(2, wallet1.getTransaction(t1.getHash()).getConfidence().numBroadcastPeers());
Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet); Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet);
assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType()); assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType());