Skip to content

Commit

Permalink
make transaction attributes contains more bytes; try to fix LevelDBEx…
Browse files Browse the repository at this point in the history
…ception "Corruption: unknown WriteBatch tag"; fix bug of changing password;
  • Loading branch information
Erik Zhang committed Nov 7, 2016
1 parent e4c606e commit 66999b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/AntShares/Core/TransactionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public int Size
{
if (Usage == TransactionAttributeUsage.ContractHash || Usage == TransactionAttributeUsage.ECDH02 || Usage == TransactionAttributeUsage.ECDH03 || Usage == TransactionAttributeUsage.Vote || (Usage >= TransactionAttributeUsage.Hash1 && Usage <= TransactionAttributeUsage.Hash15))
return sizeof(TransactionAttributeUsage) + 32;
else if (Usage == TransactionAttributeUsage.CertUrl || Usage == TransactionAttributeUsage.DescriptionUrl)
return sizeof(TransactionAttributeUsage) + sizeof(byte) + Data.Length;
else
return sizeof(TransactionAttributeUsage) + Data.Length.GetVarSize() + Data.Length;
}
Expand All @@ -42,9 +44,9 @@ void ISerializable.Deserialize(BinaryReader reader)
else if (Usage == TransactionAttributeUsage.Script)
Data = reader.ReadVarBytes(ushort.MaxValue);
else if (Usage == TransactionAttributeUsage.CertUrl || Usage == TransactionAttributeUsage.DescriptionUrl)
Data = reader.ReadVarBytes(byte.MaxValue);
Data = reader.ReadBytes(reader.ReadByte());
else if (Usage == TransactionAttributeUsage.Description || Usage >= TransactionAttributeUsage.Remark)
Data = reader.ReadVarBytes(byte.MaxValue);
Data = reader.ReadVarBytes(ushort.MaxValue);
else
throw new FormatException();
}
Expand All @@ -57,7 +59,7 @@ void ISerializable.Serialize(BinaryWriter writer)
else if (Usage == TransactionAttributeUsage.CertUrl || Usage == TransactionAttributeUsage.DescriptionUrl)
writer.Write((byte)Data.Length);
else if (Usage == TransactionAttributeUsage.Description || Usage >= TransactionAttributeUsage.Remark)
writer.Write((byte)Data.Length);
writer.WriteVarInt(Data.Length);
if (Usage == TransactionAttributeUsage.ECDH02 || Usage == TransactionAttributeUsage.ECDH03)
writer.Write(Data, 1, 32);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,24 @@ private void Persist(Block block)
batch.Put(SliceBuilder.Begin(DataEntryPrefix.ST_QuantityIssued).Add(quantity.Key), (GetQuantityIssued(quantity.Key) + quantity.Value).GetData());
}
batch.Put(SliceBuilder.Begin(DataEntryPrefix.SYS_CurrentBlock), SliceBuilder.Begin().Add(block.Hash).Add(block.Height));
db.Write(WriteOptions.Default, batch);
// There's a bug in .Net Core.
// When calling DB.Write(), it will throw LevelDBException sometimes.
// But when you try to catch the exception, the bug disappears.
// We shall remove the "try...catch" clause when Microsoft fix the bug.
byte retry = 0;
while (true)
{
try
{
db.Write(WriteOptions.Default, batch);
break;
}
catch (LevelDBException ex)
{
if (++retry >= 4) throw;
File.AppendAllText("leveldb.log", ex.Message + "\r\n");
}
}
current_block_height = block.Height;
}

Expand Down
1 change: 1 addition & 0 deletions src/AntShares/Wallets/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public bool ChangePassword(string password_old, string password_new)
{
try
{
SaveStoredData("PasswordHash", passwordKey.Sha256());
SaveStoredData("MasterKey", masterKey.AesEncrypt(passwordKey, iv));
return true;
}
Expand Down

0 comments on commit 66999b5

Please sign in to comment.