Skip to content

Commit

Permalink
feat(state): merkleschedule (#104)
Browse files Browse the repository at this point in the history
Signed-off-by: failfmi <oscurocalma@gmail.com>
  • Loading branch information
failfmi authored Dec 9, 2020
1 parent 070bdec commit 9008c88
Show file tree
Hide file tree
Showing 4 changed files with 764 additions and 8 deletions.
35 changes: 28 additions & 7 deletions hedera-node/src/main/java/com/hedera/services/ServicesState.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.hedera.services.state.merkle.MerkleEntityId;
import com.hedera.services.state.merkle.MerkleNetworkContext;
import com.hedera.services.state.merkle.MerkleOptionalBlob;
import com.hedera.services.state.merkle.MerkleSchedule;
import com.hedera.services.state.merkle.MerkleToken;
import com.hedera.services.state.merkle.MerkleTokenRelStatus;
import com.hedera.services.state.merkle.MerkleTopic;
Expand Down Expand Up @@ -76,7 +77,8 @@ public class ServicesState extends AbstractMerkleInternal implements SwirldState
static final int RELEASE_070_VERSION = 1;
static final int RELEASE_080_VERSION = 2;
static final int RELEASE_090_VERSION = 3;
static final int MERKLE_VERSION = RELEASE_090_VERSION;
static final int RELEASE_100_VERSION = 4;
static final int MERKLE_VERSION = RELEASE_100_VERSION;
static final long RUNTIME_CONSTRUCTABLE_ID = 0x8e300b0dfdafbb1aL;

static Consumer<MerkleNode> merkleDigest = CryptoFactory.getInstance()::digestTreeSync;
Expand All @@ -99,6 +101,8 @@ static class ChildIndices {
static final int TOKEN_ASSOCIATIONS = 6;
static final int DISK_FS = 7;
static final int NUM_090_CHILDREN = 8;
static final int SCHEDULE_TXS = 8;
static final int NUM_100_CHILDREN = 9;
}

ServicesContext ctx;
Expand All @@ -107,7 +111,7 @@ public ServicesState() {
}

public ServicesState(List<MerkleNode> children) {
super(ChildIndices.NUM_090_CHILDREN);
super(ChildIndices.NUM_100_CHILDREN);
addDeserializedChildren(children, MERKLE_VERSION);
}

Expand Down Expand Up @@ -135,10 +139,12 @@ public int getVersion() {
public int getMinimumChildCount(int version) {
if (version == RELEASE_070_VERSION) {
return ChildIndices.NUM_070_CHILDREN;
} else if (version == RELEASE_080_VERSION) {
return ChildIndices.NUM_080_CHILDREN;
} else if (version == RELEASE_090_VERSION) {
return ChildIndices.NUM_090_CHILDREN;
} else {
return (version == RELEASE_080_VERSION)
? ChildIndices.NUM_080_CHILDREN
: ChildIndices.NUM_090_CHILDREN;
return ChildIndices.NUM_100_CHILDREN;
}
}

Expand All @@ -159,6 +165,11 @@ public void initialize(MerkleInternal previous) {
log.info("Created disk file system after <=0.9.0 state restoration");
skipDiskFsHashCheck = true;
}
if (scheduleTxs() == null) {
setChild(ChildIndices.SCHEDULE_TXS,
new FCMap<>(new MerkleEntityId.Provider(), MerkleSchedule.LEGACY_PROVIDER));
log.info("Created scheduled txs FCMap after <= 1.0.0 state restoration");
}
}

@Override
Expand All @@ -183,7 +194,7 @@ public void init(Platform platform, AddressBook addressBook) {
} catch (ContextNotFoundException ignoreToInstantiateNewContext) {
ctx = new ServicesContext(nodeId, platform, this, properties);
}
if (getNumberOfChildren() < ChildIndices.NUM_090_CHILDREN) {
if (getNumberOfChildren() < ChildIndices.NUM_100_CHILDREN) {
log.info("Init called on Services node {} WITHOUT Merkle saved state", nodeId);
long seqStart = bootstrapProps.getLongProperty("hedera.numReservedSystemEntities") + 1;
setChild(ChildIndices.NETWORK_CTX,
Expand All @@ -201,6 +212,8 @@ public void init(Platform platform, AddressBook addressBook) {
new FCMap<>(MerkleEntityAssociation.LEGACY_PROVIDER, MerkleTokenRelStatus.LEGACY_PROVIDER));
setChild(ChildIndices.DISK_FS,
new MerkleDiskFs(diskFsBaseDirPath, asLiteralString(ctx.nodeAccount())));
setChild(ChildIndices.SCHEDULE_TXS,
new FCMap<>(new MerkleEntityId.Provider(), MerkleSchedule.LEGACY_PROVIDER));
} else {
log.info("Init called on Services node {} WITH Merkle saved state", nodeId);

Expand Down Expand Up @@ -290,7 +303,9 @@ public synchronized ServicesState copy() {
accounts().copy(),
tokens().copy(),
tokenAssociations().copy(),
diskFs().copy()));
diskFs().copy(),
scheduleTxs().copy()
));
}

@Override
Expand Down Expand Up @@ -340,6 +355,7 @@ public void printHashes() {
" Tokens :: %s\n" +
" TokenAssociations :: %s\n" +
" DiskFs :: %s\n" +
" ScheduledTxs :: %s\n" +
" NetworkContext :: %s\n" +
" AddressBook :: %s",
getHash(),
Expand All @@ -349,6 +365,7 @@ public void printHashes() {
tokens().getHash(),
tokenAssociations().getHash(),
diskFs().getHash(),
scheduleTxs().getHash(),
networkCtx().getHash(),
addressBook().getHash()));
}
Expand All @@ -373,6 +390,10 @@ public FCMap<MerkleEntityAssociation, MerkleTokenRelStatus> tokenAssociations()
return getChild(ChildIndices.TOKEN_ASSOCIATIONS);
}

public FCMap<MerkleEntityId, MerkleSchedule> scheduleTxs() {
return getChild(ChildIndices.SCHEDULE_TXS);
}

public MerkleNetworkContext networkCtx() {
return getChild(ChildIndices.NETWORK_CTX);
}
Expand Down
Loading

0 comments on commit 9008c88

Please sign in to comment.